وحدة:ملعب/Mr. Ibrahem/o

local p = {}
local Templates = {}
Templates['category'] = '{{غير مصنفة/نواة|تاريخ=%s}}'
Templates['link'] =  '{{وصلات قليلة|تاريخ=%s}}'
Templates['Deadend'] ='{{نهاية مسدودة|تاريخ=%s}}'
Templates['ref'] =  '{{مصادر أكثر|تاريخ=%s}}'

local function Find(text , object , Date)
	local pattern = object.pattern
	local result = object.result
	find = mw.ustring.find (text,  pattern )
	
	if Templates[object.key] then 
		result = string.format( Templates[object.key] , Date)
	end
	mw.log( "result: " .. result )
	if find then
		mw.log( object.label .. 'find')
		return 'find'
	else
		mw.log(object.label .. 'no find')
		return result
	end
end

local function count (text, object , Date)
	local patterns = object.pattern
	if type(patterns) ~= 'table' then  patterns = {patterns} end
	local result = object.result
	local mincount = object.mincount
	local total = 0
	for k, pattern in pairs(patterns) do
		local _;
		local counts;
		_, counts = mw.ustring.gsub (text, pattern, '%1');
		total = total + counts
	end
	mw.log(object.label .. total)
	if Templates[object.key] then 
		result = string.format( Templates[object.key] , Date)
	end
	mw.log( "result: " .. result )
	if total < mincount then
		return result
	end
end

local fasoo = { }
	fasoo['link'] = {												
			['pat'] = { '' },
			['count'] = 0,
			}
	fasoo['Deadend'] = {												
			['pat'] = { '' },
			['count'] = 0,
			}
	fasoo['all'] = {												
			['pat'] = { '' },
			['count'] = 0,
			}
	fasoo['category'] = {												
			['pat'] = { 'تصنيف:' , '[Cc]ategory:' },
			['count'] = 0,
			}
	fasoo['image'] = {												
			['pat'] = { '[Ii]mage:', 'صورة:' , '[Ff]ile:', 'ملف:' },
			['count'] = 0,
			}

local function CountFasoo (str)
	for k, key in pairs(fasoo) do
   		--mw.log(k .. 'key : '  )
   		for v, pattern in pairs(fasoo[k].pat) do
   			 _, lenth = mw.ustring.gsub (str, "%[%[".. pattern .."([^%]]*)%]%]", '%1');
			fasoo[k].count = fasoo[k].count + lenth
		end
	end
	fasoo['link'].count = fasoo['link'].count - fasoo['category'].count - fasoo['image'].count
end

local function findotherlinks (str , object , Date)
	local typ = object.key
	local result = object.result
	local mincount = object.mincount
	--local pattern = "%[%[([^%]]*)%]%]"
	--_, all_lenth = mw.ustring.gsub (str, pattern, '%1');
	--mw.log( 'findotherlinks : ' .. typ)
	--[[local fasoo_ = fasoo
	if typ ~= 'link' then
		fasoo_ = {[typ]  = fasoo[typ] }
	end
	for k, key in pairs(fasoo_) do
   		--mw.log(k .. 'key : '  )
   		for v, pattern in pairs(fasoo[k].pat) do
   			 _, lenth = mw.ustring.gsub (str, "%[%[".. pattern .."([^%]@]*)%]%]", '%1');
			fasoo[k].count = fasoo[k].count + lenth
		end
		--mw.log(k .. 'lenth : ' .. fasoo[k].count )
	end
	fasoo['link'].count = fasoo['link'].count - fasoo['category'].count - fasoo['image'].count
	]]
	local lenth = fasoo[typ].count
	mw.log( object.label .. lenth )
	if Templates[object.key] then 
		result = string.format( Templates[object.key] , Date)
	end
	mw.log( "result: " .. result )
	if lenth < mincount then
		return result
	end
end

local objects = {}
objects['nocategory'] = {												
		['func'] = findotherlinks,
		['key'] = 'category',
		['mincount'] = 1,
		['label'] = 'تصنيفات: ',
		['result'] = '{{غير مصنفة}}'
		}
objects['link'] = {												
		['func'] = findotherlinks,
		['key'] = 'link',
		['mincount'] = 3,
		['label'] = 'وصلات قليلة: ',
		['result'] = '{{وصلات قليلة}}'
		}
objects['Deadend'] = {												
		['func'] = findotherlinks,
		['key'] = 'Deadend',
		['mincount'] = 1,
		['label'] = 'نهاية مسدودة: ',
		['result'] = '{{نهاية مسدودة}}'
		}
objects['all'] = {												
		['func'] = findotherlinks,
		['key'] = 'all',
		['mincount'] = 0,
		['label'] = 'جميع الوصلات: ',
		['result'] = ''
		}
objects['portalpar'] = {
		['func'] = count,
		['key'] = 'portalpar',
		['pattern'] = '({{%s*شريط%s*بوابات%s*)',
		['mincount'] = 1,
		['label'] = 'شريط بوابات: ',
		['result'] = '[[تصنيف:مقالات بحاجة لشريط بوابات]]'
		}
objects['ref'] = {
		['func'] = count,
		['key'] = 'ref',
		['pattern'] = '(<\/ref>)',
		['mincount'] = 1,
		['label'] = 'مراجع: ',
		['result'] = '{{مصادر أكثر}}'
		}
local _objects = objects
local objects2 = { }
objects2['شريط بوابات'] = 'portalpar'
objects2['مراجع'] = 'ref'
objects2['تصنيفات'] = 'nocategory'
objects2['وصلات داخلية'] = 'link'
objects2['جميع الوصلات'] = 'all'

function p.m(frame)
	local output = {}
	local pframe = frame:getParent()
	local text;
	local key;
	local title;
	local nstitle;
	local page_title_object;
	if frame.args[1] and frame.args[1] ~='all' then
		arg_1 = frame.args[1]
		key = objects[arg_1] or objects[objects2[arg_1]]
		if not key then 
			mw.log( "Can't find the key: " .. arg_1 .. " in objects")
			return nil
		else
			_objects = {[arg_1]  = key }
		end
	end
	--if key then
	local page_title = frame.args[2] or frame.args["page"] or frame.args["صفحة"] or ""
	local Date = pframe.args["date"] or pframe.args["تاريخ"] or ""
	--if frame.args[2] and frame.args[2] ~= "" then
		--page_title = frame.args[2]
	--end
	if page_title and page_title ~= "" then
		page_title_object = mw.title.new(page_title);
	else
		page_title_object = mw.title.getCurrentTitle();	
	end
	--page_title_object = mw.title.new('باراك أوباما')
	text = page_title_object:getContent();	
	title = page_title_object.text;	
	nstitle = page_title_object.namespace;
	mw.log("وحدة:صيانة : ")
	mw.log("العنوان: " .. title
		.." النطاق:" .. nstitle .. ":")
	CountFasoo(text)
	for object, k in pairs(_objects) do
		local findop = _objects[object].func (text , _objects[object] , Date )
		table.insert (output, findop)
	end

	--[[local order = {
	'nocategory', 'link', 'all' , 'Portal'
	};
	local i=1;
	while order[i] do
		object = order[i];
		local findop = objects[object].func (text , objects[object] )
		table.insert (output, findop)
		i=i+1;
	end]]
	local final = table.concat (output)
	return  frame:preprocess(final)
	--end
end

p['صيانة'] = function ( frame )
	return p.m(frame)
end

return p