-- This module counts the number of times that various reference tags and cs1|2 templates appear.
-- {{#invoke:ref_count|ref_count}}
-- {{ref info}}
-- {{ref info|aristotle}}

local p = {}

-- these are the cannonical names.  What to do about redirects? Lua patterns?

local cs1_template_list = {'ar[Xx]iv', '[Aa][Vv] media', '[Aa][Vv] media notes', 'book', 'conference', '[Dd][Vv][Dd] notes', 'encyclopedia',
	'episode', 'interview', 'journal', 'magazine', 'mailing list', 'map', 'news', 'newsgroup', 'podcast', 'press release',
	'report', 'sign', 'speech', 'serial', 'techreport', 'thesis', 'web',
	'بحلقة' , 'بخبر' , 'بخريطة' , 'بموسوعة' , 'كتاب' , 'ويب' , 'ويكي مصدر'};

local cs1_1 = {'[Cc]ite', 'استشهاد', 'مرجع' }

local cleanup_template_list = {'[Cc]itation needed', '[Cc]itation-needed'	
		, '[Dd]isputed inline'
		, '[Dd]ubious'
		, 'يحتاج لمصدر'
		, 'يحتاج لمصدر'
		, 'بحاجة لمصدر'
		, 'مصدر ناقص'
		, 'تأكيد رأي'
		, 'تأكيد مصدر'
		, 'محل شك'
		, 'مصدر'
		, 'مصادر أكثر'
		, 'غير موثق'
		, 'حقيقة'
		, 'غير مؤكد'
		, '[Ff]ailed verification'
		};

local dead_link_template_list = {'وصلة مكسورة' , '[Dd]ea?d[%- ]?[Ll]inks?', '[Dd]ead ?cite', '[Dd]ead page', '[Dd]ead ?url', '[Dd]ead%-inline',
	'404', '[Bb]ad ?link', '[Bb]roken ?link', '[Dd][Ll]','مرجع مكسور' , '[Ll]ink ?broken', '[Dd]ead' , 'مصدر مكسور'}

--[[--------------------------< C O U N T _ P A T T E R N >----------------------------------------------------

this is a general purpose function used to count occurrences of patterns in the unparsed article text

]]

local function count_pattern (text, pattern)
	local _;
	local count;
	_, count = mw.ustring.gsub (text, pattern, '%1');
	--mw.log(pattern .. count)
	return count;
end


--[[--------------------------< C O U N T _ C S 1 >------------------------------------------------------------

Using the list of cs1 templates, make a count of just those references or templates as dictated by base_pattern.

]]

local function count_cs1 (text, base_pattern)
	local _;
	local count, total = 0, 0;
	
	for o, tee in ipairs (cs1_1) do
		for i, template in ipairs (cs1_template_list) do
			--mw.log(base_pattern)
			pattern = string.format	(base_pattern, tee , template);						-- make a pattern for the selected cs1 template
			_, count = mw.ustring.gsub (text, pattern, '%1');						-- count occurences of that pattern
			--mw.log(pattern .. count)
			total = total + count;													-- accumulate a total
		end
	end
	return total;
end


--[[--------------------------< C O U N T _ C L E A N U P >----------------------------------------------------

Using the list of cleanup templates, make a count of those templates as dictated by base_pattern.

]]

local function count_cleanup (text, base_pattern)
	local _;
	local count, total = 0, 0;
	
	for i, template in ipairs (cleanup_template_list) do
		pattern = string.format	(base_pattern, template);						-- make a pattern for the selected cleanup template
		_, count = mw.ustring.gsub (text, pattern, '%1');						-- count occurences of that pattern
		total = total + count;													-- accumulate a total
	end
	return total;
end


--[[--------------------------< C O U N T _ D E A D _ L I N K S >----------------------------------------------

Using the list of dead link templates, make a count of those templates as dictated by base_pattern.

]]

local function count_dead_links (text, base_pattern)
	local _;
	local count, total = 0, 0;
	
	for i, template in ipairs (dead_link_template_list) do
		pattern = string.format	(base_pattern, template);						-- make a pattern for the selected cleanup template
		_, count = mw.ustring.gsub (text, pattern, '%1');						-- count occurences of that pattern
		total = total + count;													-- accumulate a total
	end
	return total;
end


--[[--------------------------< H A S _ L D R >----------------------------------------------------------------

returns a string set to 'yes' if the article uses list defined references.  ldr uses {{reflist |refs=...}} or
<references>...</references>.  Here we do simple 'find's to make the determination.

It is also possible to do ldr with {{refbegin}} ... {{refend}} 

the pattern value is passed to this function but ignored
]]
local function has_ldr (text, pattern)
	c = false
	rr = { 'مراجع' , '[Rr]eflist', 'المراجع'  }
	for i, template in ipairs (rr) do
		if mw.ustring.find (text, '{{%s*' .. template .. '[^}]*|%s*refs%s*=%s*[^}|]+') then	-- does page use {{Reflist |refs=...}}?
			c = true
		elseif mw.ustring.find (text, '{{%s*' .. template .. '[^}]*|%s*مراجع%s*=%s*[^}|]+') then	
			c = true
		end
	end
	if c == true then
		return 'نعم'
	elseif mw.ustring.find (text, '<references>[^<]+') then						-- does page use <references>...</references>?
		return 'نعم'
	else
		return 'no'
	end
end

--[[--------------------------< O B J E C T S   T A B L E >----------------------------------------------------

Here we define various properties and values necessary to the counting of referencing objects

]]

local objects = {
	['unnamed_refs'] = {														-- count unnamed ref tags
		['func'] = count_pattern,												-- the function that does the work for this object
		['pattern'] = '(<ref>)',												-- a pattern that the function uses to find and count this object
		['count'] = 0,															-- the returned result (called count because that is the most common but can be 'yes' or 'no' etc
		['label'] = 'بدون اسم'										-- a label and marckup for displaying the result; used with string.format()
		},
	['named_refs'] = {															-- count named ref tags
		['func'] = count_pattern,
		['pattern'] = '(<ref%s+name%s*=%s*[%a%d%p ]+>)',
		['count'] = 0,
		['label'] = 'مراجع مسماة'
		},
	['self_closed_refs'] = {													-- count self closed ref tags
		['func'] = count_pattern,
		['pattern'] = '(<ref%s*name%s*=%s*["%a%d%p ]+/>)',
		['count'] = 0,
		['label'] = 'مغلقة ذاتياً'
		},
	['r_templates'] = {															-- count R templates (wrapper for self closed refs)
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Rr]%s*|)',
		['count'] = 0,
		['label'] = 'قوالب R'
		},
	['refn_templates'] = {															-- count R templates (wrapper for self closed refs)
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Rr]efn%s*|)',
		['count'] = 0,
		['label'] = 'قوالب Refn'
		},
	['bare_url_refs'] = {														-- count bare url refs
		['func'] = count_pattern,												-- TODO: separate function to detect protocol relative urls?
		['pattern'] = '(<ref[^>]*>%s*http[^<%s]+%s*</ref>)',
		['count'] = 0,
		['label'] = '<span style="font-size:inherit" class="error">وصلات عارية</span>'
		},
	['ext_link_refs'] = {														-- count unlabeled external link refs
		['func'] = count_pattern,												-- TODO: separate function to detect protocol relative urls?
		['pattern'] = '(<ref[^>]*>%[%s*http[^%]<%s]+%][^<]*</ref>)',
		['count'] = 0,
		['label'] = '<span style="font-size:inherit" class="error">وصلات خارجية عارية</span>'
		},
	['cs1_like_refs'] = {														-- count cs1 refs and refs that look like cs1 (cite something)
		['func'] = count_pattern,
		['pattern'] = '(<ref[^>]*>[^<{]*{{%s*[Cc]ite%s+[^|]+)',
		['count'] = 0,
		['label'] = 'مراجع تشبه cs1'
		},
	['cs1_refs'] = {															-- count cs1 refs only
		['func'] = count_cs1,
		['pattern'] = '(<ref[^>]*>[^<{]*{{%%s*%s%%s+%s%%s*|)',				-- will be modified in the func by string.format()
		['count'] = 0,
		['label'] = 'مراجع cs1'
		},
	['cs1_like_templates'] = {													-- count cs1 templates and templates that look like cs1
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Cc]ite%s+[^|]+)',
		['count'] = 0,
		['label'] = 'قوالب تشبه cs1'
		},
	['cs1_templates'] = {														-- count cs1 templates only
		['func'] = count_cs1,
		['pattern'] = '({{%%s*%s%%s+%s%%s*|)',								-- will be modified in the func by string.format()
		['count'] = 0,
		['label'] = 'قوالب cs1'
		},
	['cs2_refs'] = {															-- count cs2 refs
		['func'] = count_pattern,
		['pattern'] = '(<ref[^>]*>[^<{]*{{%s*[Cc]itation%s*|)',
		['count'] = 0,
		['label'] = 'مراجع cs2'
		},
	['cs2_templates'] = {														-- count cs2 templates
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Cc]itation%s*|)',
		['count'] = 0,
		['label'] = 'قوالب cs2'
		},
	['vcite_refs'] = {															-- count vancite, vcite, and vcite2 refs
		['func'] = count_pattern,
		['pattern'] = '(<ref[^>]*>[^<{]*{{%s*[Vv]a?n?cite2?%s+[^|]+)',
		['count'] = 0,
		['label'] = 'مراجع vcite'
		},
	['vcite_templates'] = {														-- count vancite, vcite, and vcite2 templates
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Vv]a?n?cite2?%s+[^|]+)',
		['count'] = 0,
		['label'] = 'قوالب vcite'
		},
	['wikicite_templates'] = {													-- count wikicite templates
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Ww]ikicite%s*|)',
		['count'] = 0,
		['label'] = 'قوالب wikicite'
		},
	['harv_refs'] = {															-- count harv refs
		['func'] = count_pattern,
		['pattern'] = '(<ref[^>]*>[^<{]*{{%s*[Hh]arv[nbcolptx]*%s*|)',
		['count'] = 0,
		['label'] = 'مراجع harv'
		},
	['harv_templates'] = {														-- count harv templates
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Hh]arv[nbcolptx]*%s*|)',
		['count'] = 0,
		['label'] = 'قوالب harv'
		},
	['sfn_templates'] = {														-- count sfn templates
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Ss]fn[mp]?%s*|)',
		['count'] = 0,
		['label'] = 'قوالب sfn'
		},
	['rp_templates'] = {														-- count rp templates
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Rr]p%s*|)',
		['count'] = 0,
		['label'] = 'قوالب rp'
		},
	['ldr'] = {																	-- does this article use list defined references?
		['func'] = has_ldr,
		['pattern'] = '',														-- uses multiple patterns which are defined in the function
		['count'] = 'no',
		['label'] = 'يستخدم ldr'
		},
	['refbegin_templates'] = {													-- count refbegin templates - bibliography lists
		['func'] = count_pattern,
		['pattern'] = '({{%s*[Rr]efbegin|{{%s*بداية المراجع)',
		['count'] = 0,
		['label'] = 'قالب بداية المراجع'
		},
	['cleanup_templates'] = {													-- count cleanup templates
		['func'] = count_cleanup,
		['pattern'] = '({{%%s*%s)',												-- will be modified in the func by string.format()
		['count'] = 0,
		['label'] = 'قوالب صيانة'
		},
	['dead_link_templates'] = {													-- count deadlink templates (includes redirects)
		['func'] = count_dead_links,
		['pattern'] = '({{%%s*%s%%s*|)',										-- will be modified in the func by string.format()
		['count'] = 0,
		['label'] = 'قوالب وصلة مكسورة'
		},
	}
																				-- here we set the order in which the objects are processed
local order = {'unnamed_refs', 'named_refs', 'self_closed_refs',				-- these three are always output
	'r_templates',																-- this and the others only produce output when ...
	'refn_templates',															-- ... their count is not 0 or not 'no'
	'bare_url_refs',
	'ext_link_refs',
	'cs1_refs',
	'cs1_templates',
	'cs1_like_refs',
	'cs1_like_templates',
	'cs2_refs', 'cs2_templates',
	'vcite_refs', 'vcite_templates',
	'wikicite_templates',
	'harv_refs', 'harv_templates',
	'sfn_templates',
	'rp_templates',
	'ldr',
	'refbegin_templates',
	'cleanup_templates',
	'dead_link_templates'
	};


--[[--------------------------< P . R E F _ I N F O >----------------------------------------------------------

the working part of Template:Ref info

]]


function p.ref_info(frame)
	local text;																	-- unparsed page content
	local title;																-- page title without namespace or interwiki references
	local nstitle;																-- page title with namespace and interwiki references
	local page_title_object;													-- 
	local output = {};
	local i=1;
	local style = frame.args.style or '';										-- styling css for output table
	
	if frame.args[1] then
		page_title_object = mw.title.new(frame.args[1]);						-- title object for the page specified in the template call
	else
		page_title_object = mw.title.getCurrentTitle();							-- title object for the current page
	end

	text = page_title_object:getContent();										-- the unparsed content of the selected page
	nstitle = page_title_object.prefixedText;									-- the title of the page (with namespace)
	title = page_title_object.text;												-- the title of the page (without namespace)

	if nil == text then
		return string.format ('<span style="font-size:100%%" class="error">{{ref info}} – page is empty or does not exist: %s</span>', frame.args[1] or 'no page');
	end

	while order[i] do															-- loop through order and search for the related objects
		object = order[i];														-- the selected object
		objects[object].count = objects[object].func (text, objects[object].pattern)	-- do the search and store the result
		i=i+1;																	-- bump to the next object
	end
																				-- for those that count duplicates remove the duplicates from the counts
	objects['named_refs'].count = objects['named_refs'].count - objects['self_closed_refs'].count;
	objects['cs1_like_refs'].count = objects['cs1_like_refs'].count - objects['cs1_refs'].count;
	objects['cs1_like_templates'].count = objects['cs1_like_templates'].count - objects['cs1_templates'].count ;

	table.insert (output, string.format ('{| class="wikitable" style="text-align:right; %s"\n|+ معلومات مراجع [[%s|%s]]', style, nstitle, title));	-- output table header

	i=1;																		-- reset the indexer
	while order[i] do															-- loop through order and output from the related objects
		object = order[i];														-- the selected object
		mw.log(object)
		if i<=3 or object == 'dead_link_templates' or object == 'cleanup_templates'  then															-- first three (reference tags) are always output
			table.insert (output, string.format ('%s\n|%s', objects[object].label, objects[object].count));
		elseif 'string' == type (objects[object].count) then					-- objects[object].count can be a string or a number
			if 'no' ~= objects[object].count then								-- if a string and not 'no' ...
				table.insert (output, string.format ('%s\n|%s', objects[object].label, objects[object].count));	-- output the result
			end
		elseif 'number' == type (objects[object].count) then					-- if a number ...
		 	if 0 < objects[object].count then									-- ... and count is greater than zero ...
				table.insert (output, string.format ('%s\n|%s', objects[object].label, objects[object].count));	-- ... output the result
		--- تجربة
		 	--elseif 0 == objects[object].count then									-- ... and count is greater than zero ...
				--table.insert (output, string.format ('%s\n|%s', objects[object].label, objects[object].count));	-- ... output the result
			end
		--- تجربة
		end
		i=i+1;																	-- bump the indexer
	end
	local result = table.concat (output,'\n|-\n! scope="row" | ');
	return result .. '\n|-\n|scope="row" colspan="2" style="text-align:center"|[[قالب:Ref_info#Output_meanings|شرح]]\n|-\n|}'
end

return p