پودمان:Category described in year

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
require('Module:No globals')
local commonsLink = require('Module:Commons link')

local num_con = require('Module:Numeral converter').convert

local conf = require( 'Module:Category described in year/conf' ) --configuration module

--[[==========================================================================]]
--[[                             Local functions                              ]]
--[[==========================================================================]]

--[[ local function addOrd( i ) --12 -> 12th, etc.
	if tonumber(i) then
		local s = tostring(i)
		local  tens = mw.ustring.match(mw.getContentLanguage():parseFormattedNumber(s), '۱%d$')
		local  ones = mw.ustring.match(mw.getContentLanguage():parseFormattedNumber(s),  '%d$')
		if     tens        then return s..''
		elseif ones == '۱' then return s..''
		elseif ones == '۲' then return s..''
		elseif ones == '۳' then return s..''
		elseif ones ~= nil then return s..''
		end
	end
	return ''
end ]]-- not needed in fawiki

local function isNilOrEmpty( thing )
	return (thing == nil or thing == '')
end

local p = {}

--[[==========================================================================]]
--[[                            External function                             ]]
--[[==========================================================================]]

function p.autodetect( frame )
	local currentTitle = mw.title.getCurrentTitle()
	local parentArg = frame:getParent().args[1] --accept 1 unnamed category parameter if not in category namespace; required for testing/doc/etc. purposes
	local header = ' ' --header template(s), nav bar, and category description text; whitespace-initialized for convenience
	local nav = nil
	local portal = nil --برای {{درگاه|...}}
	local commons = nil --برای {{انبار-رده|...}}
	local wikispecies = nil --برای {{ویکی‌گونه|...}}
	local description = nil
	local toc = nil
	local categories = {}
	local trackingCategories = {
		[1] = '', --placeholder for [[Category:Described in year unknown category]]
		[2] = '', --placeholder for [[Category:Described in year error]]
	}
	local outString = nil
	local bConfError = false
	
	--prelim namespace/title determination
	local currCat = nil
	local currQID = nil
	if currentTitle.namespace == 14 then --category namespace
		currCat = currentTitle.text --without namespace nor interwiki prefixes
		currQID = mw.wikibase.getEntityIdForCurrentPage()
	else
		if parentArg then
			currCat = mw.ustring.gsub(parentArg, 'رده:', '')
			currQID = mw.wikibase.getEntityIdForTitle('رده:' .. currCat)
		else --currQID & currCat both nil
			if currentTitle.fullText ~= 'الگو:رده توصیف‌شده‌ها در سال' then --از خودت چشم‌پوشی کن...
				trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|پ]]' --فاقد پارامتر رده خارج از فضای نام رده
			end
		end
	end
	
	--find commons & wikispecies link(s); produce {{Commons and category}} and/or {{Wikispecies}} template(s)
	if currQID then
		if commonsLink._hasGallery(currQID) or commonsLink._hasCategory(currQID) then
			commons = frame:expandTemplate{ title = 'انبار و رده', args = { qid=currQID }}
		end
		local currEntity = mw.wikibase.getEntity(currQID)
		if currEntity then
			--check "Other sites" sitelinks for Wikispecies
			local currSiteLinks = currEntity.sitelinks
			if currSiteLinks then
				local currSpeciesWiki = currEntity.sitelinks.specieswiki
				if currSpeciesWiki then
					local currSpeciesWikiTitle = currSpeciesWiki.title
					if currSpeciesWikiTitle then
						wikispecies = frame:expandTemplate{ title = 'ویکی‌گونه', args = { currSpeciesWikiTitle } }
					end
				end
			end
		end
	end --if currQID then
	
	--[[======================================================================]]
	--[[                                 Main                                 ]]
	--[[======================================================================]]
	if currCat then
		
		--determine current/related/adjacent cats' properties/vars/etc.
		local currGroup = mw.ustring.match(currCat, '^(.+) توصیف‌شده در') --Bacteria/Plants/etc.
		if mw.ustring.match(currGroup, 'های') then currGroup = mw.ustring.gsub(currGroup, 'های', 'ها') end
		if isNilOrEmpty(currGroup) then currGroup = mw.ustring.match(currCat, '^([%w ]+) بر پایه سال توصیف رسمی') end
		if conf[currGroup] == nil then conf[currGroup] = conf['Default'] end --default to Default
		local currYDCF = nil --مقدارهای محتمل در آینده: سال/دهه/سده/رسمی
		local currYear = mw.ustring.match(currCat, 'توصیف‌شده در (%d%d%d%d) %(میلادی%)$')
		local currDeca = mw.ustring.match(currCat, 'توصیف‌شده در دهه (%d%d%d%d) %(میلادی%)$') --deprecated
		local currCent = mw.ustring.match(currCat, 'توصیف‌شده در سده (%d?%d) %(میلادی%)$')
		local currFrml = mw.ustring.match(currCat, 'بر پایه سال توصیف (رسمی)$')
		local parentCent = nil --used with currYear
		local minYear = tonumber(conf[currGroup].minyear)
		if minYear == nil or 
		  (minYear and (minYear <= 1700 or minYear >= 2000)) then
			minYear = 1758 --default to 1758 per ICZN Art. 5
		end
		if currYear then
			currYDCF = 'سال'
			if mw.ustring.match(num_con("en", currYear), '^%d%d00') then --1900 in 19th century
				parentCent = mw.ustring.match(currYear, '^%d%d')
			else --1901 in 20th century
				parentCent = 1 + num_con("en", mw.ustring.match(currYear, '^%d%d'))
			end
		elseif currDeca then
			currYDCF = 'دهه'
			bConfError = true
			trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|م]]' --ردهٔ والد دهه نامعتبر (منسوخ)
		elseif currCent then
			currYDCF = 'سده'
		elseif currFrml then
			currYDCF = 'رسمی'
		else
			bConfError = true
			trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|ع]]' --عنوان رده نامعتبر
		end
		
		--conf error checkng (missing keys)
		--Numeric sortkeys are unfortunately grouped together under "0-9".
		--Check phab T203355 (Magic word to force category number headings instead of 0-9).
		if bConfError == false then
			if conf[currGroup] == nil then
				bConfError = true
				trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|۱]]' --group (Bacteria/Plants/etc.) key missing from conf
			elseif conf[currGroup][currYDCF] == nil then
				bConfError = true
				trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|۲]]' --year/century/formal key missing
			else
				if conf[currGroup][currYDCF].description == nil then
					bConfError = true
					trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|۳]]' --description key missing
				end
				if conf[currGroup][currYDCF].parent1 == nil then
					bConfError = true
					trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|۴]]' --parent key missing
				end
			end
		end
		
		if bConfError == false then
			--produce portal
			--[[ if currGroup == 'Fossil taxa' or currGroup == 'Fossil parataxa' then
				portal = frame:expandTemplate{ title = 'Portal', args = { 'Paleontology' } }
			end ]]-- درگاه ناموجود
			
			--produce description, evaluate %variables%
			description = conf[currGroup][currYDCF].description
			if mw.ustring.match(description, '%%سال%%') then
				if currYear then description = mw.ustring.gsub(description, '%%سال%%', currYear) --"2011"
				else description = mw.ustring.gsub(description, '%%سال%%', 'این سال') end
			end
			if mw.ustring.match(description, '%%سده%%') then
				if currCent then description = mw.ustring.gsub(description, '%%سده%%', currCent) --"21st"
				else description = mw.ustring.gsub(description, '%%سده%%', 'این سده') end
			end
			
			--produce toc
-- {{CatAutoTOC}} now provided via [[Template:Category described in year]]
--[[
			if mw.site.stats.pagesInCategory(currCat, 'pages') >= conf['tocmin'] then --expensive
				local args = { numerals = 'no' }
				toc = frame:expandTemplate{ title = 'Category TOC', args = args }
			end
--]]

			--produce cats & navs
			local iparent = 1
			local parenti = 'parent' .. iparent
			local sortkeyi = 'ترتیب' .. iparent
			while conf[currGroup][currYDCF][parenti] do
				local parent = conf[currGroup][currYDCF][parenti]
				local sortkey = conf[currGroup][currYDCF][sortkeyi]
				
				--[[========================== Year ==========================]]
				if currYDCF == 'سال' then
					if nav == nil then
						local args = { min = minYear }
						if parentArg and currentTitle.namespace ~= 14 then
							args['testcase'] = parentArg
						end
						nav = frame:expandTemplate{ title = 'Navseasoncats', args = args }
					end
					if parent == 'سده' then
						if isNilOrEmpty(sortkey) then sortkey = currYear end --default to currYear
						local ye = ''
						if (mw.ustring.match(currGroup, 'ها') and currGroup ~= 'گیاهان') then ye = 'ی' end -- Worst local ever defined; but it works! 
						categories[iparent] = '[[رده:'..currGroup..ye..' توصیف‌شده در سده '..num_con("fa", parentCent)..' (میلادی)|'..sortkey..']]'
					elseif parent == 'زیست‌شناسی' then
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						if tonumber(num_con("en", currYear)) < 1865 then
							categories[iparent] = '[[رده:علم در '..currYear..' (میلادی)'..sortkey..']]' --biology cat structure doesn't exist pre-1865, as of 10/2018
						else
							categories[iparent] = '[[رده:زیست‌شناسی در '..currYear..' (میلادی)'..sortkey..']]' --if/when all biology cats exists, merge this elseif with 'paleontology'
						end
					elseif parent == 'دیرینه‌شناسی' then
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						categories[iparent] = '[[رده:'..parent..' در ' ..currYear..' (میلادی)'..sortkey..']]'
					elseif parent == 'محیط زیست' then
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						categories[iparent] = '[[رده:محیط زیست در '..currYear..' (میلادی)'..sortkey..']]'
					elseif mw.ustring.match(parent, 'جانوران') or mw.ustring.match(parent, '^گونه‌های') or mw.ustring.match(parent, '^حشرات') or mw.ustring.match(parent, '^نرم‌تنان') or mw.ustring.match(parent, '^آرایه %(زیست‌شناسی%)') or mw.ustring.match(parent, '^سنگواره‌ها') then --e.g. Animals/Insects/Fossil taxa
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						--if parent == 'گونه‌ها' or 'سنگواره‌ها' then parent = parent..'ی' end
						categories[iparent] = '[[رده:'..parent..' توصیف‌شده در '..currYear..' (میلادی)'..sortkey..']]'
					else
						trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|س]]' --سال والد نامعتبر
					end
					
				--[[======================== سده =========================]]
				elseif currYDCF == 'سده' then
					if nav == nil then
						local args = {}
						if parentArg and currentTitle.namespace ~= 14 then
							args['testcase'] = parentArg
						end
						nav = frame:expandTemplate{ title = 'رده نگهدارنده' } .. 
							  frame:expandTemplate{ title = 'Navseasoncats', args = args }
					end
					if parent == 'رسمی' then
						if isNilOrEmpty(sortkey) then sortkey = currCent end --default to currCent
						categories[iparent] = '[[رده:'..currGroup..' بر پایه سال توصیف رسمی|'..sortkey..']]'
					elseif parent == 'زیست‌شناسی' then
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						if tonumber(num_con("en", currCent)) < 19 then
							categories[iparent] = '[[رده:علم در سده '..currCent..' (میلادی)'..sortkey..']]' --biology cat structure doesn't exist pre-1865, as of 10/2018
						else
							categories[iparent] = '[[رده:زیست‌شناسی در سده '..currCent..' (میلادی)'..sortkey..']]' --if/when all biology cats exists, merge this elseif with 'paleontology'
						end
					elseif parent == 'دیرینه‌شناسی' then
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						categories[iparent] = '[[رده:'..parent..' در سده '..currCent..' (میلادی)'..sortkey..']]'
					elseif parent == 'محیط زیست' then
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						categories[iparent] = '[[رده:محیط زیست در سده '..currCent..' (میلادی)'..sortkey..']]'
					elseif mw.ustring.match(parent, 'جانوران') or mw.ustring.match(parent, '^گونه‌های') or mw.ustring.match(parent, '^حشرات') or mw.ustring.match(parent, '^نرم‌تنان') or mw.ustring.match(parent, '^آرایه %(زیست‌شناسی%)') or mw.ustring.match(parent, '^سنگواره‌ها') then --e.g. Animals/Insects/Fossil taxa
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						--if parent == 'گونه‌ها' or 'سنگواره‌ها' then parent = parent..'ی' end
						categories[iparent] = '[[رده:'..parent..' توصیف‌شده در سده '..currCent..' (میلادی)'..sortkey..']]'
					else
						trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|ق]]' --سده والد نامعتبر (قرن)
					end
					
				--[[======================== رسمی ==========================]]
				elseif currYDCF == 'رسمی' then
					if nav == nil then
						nav = frame:expandTemplate{ title = 'رده نگهدارنده' }
					end
					if parent == 'گروه' then
						if isNilOrEmpty(sortkey) then sortkey = ' سال' end --default to " سال"
						categories[iparent] = '[[رده:'..currGroup..'|'..sortkey..']]'
					elseif parent == 'جانوران' or parent == 'حشرات' or parent == 'نرم‌تنان' then
						if isNilOrEmpty(sortkey) then sortkey = ' ' end --default to " "
						categories[iparent] = '[[رده:'..parent..' بر پایه سال توصیف رسمی|'..sortkey..']]'
					elseif parent == 'گونه‌های' or parent == 'آرایه (زیست‌شناسی)' or parent == 'سنگواره‌ها' then
						if isNilOrEmpty(sortkey) then sortkey = '' --default to none
						else sortkey = '|'..sortkey end
						--if parent == 'گونه‌ها' or 'سنگواره‌ها' then parent = parent..'ی' end
						categories[iparent] = '[[رده:'..parent..' بر پایه سال توصیف رسمی'..sortkey..']]'
					elseif parent == 'دیرینه‌شناسی' then
						if isNilOrEmpty(sortkey) then sortkey = ' ' end --default to " "
						categories[iparent] = '[[رده:دیرینه‌شناسی بر پایه سال|'..sortkey..']]'
					else
						trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|ر]]' --رسمی والد نامعتبر
					end
					
				--[[========================= خطا ==========================]]
				else
					trackingCategories[2] = '[[رده:خطای توصیف‌شده‌ها در سال|ن]]' --پیکربندی ناشناخته
				end
				
				iparent = iparent + 1
				parenti = 'parent' .. iparent
				sortkeyi = 'ترتیب' .. iparent
			end --while conf[currGroup][currYDCF][parenti] do
		end --if bConfError == false then
		
		--check for non-existent cats
		for _, category in pairs(categories) do
			local cat = mw.ustring.match(category, '%[%[رده:([%w%s]+)')
			if mw.title.new(cat, 14).exists == false then
				trackingCategories[1] = '[[رده:رده ناشناخته توصیف‌شده‌ها در سال]]'
				break
			end
		end
		
	end --if currCat then
	
	--build header & rem surrounding whitespace
	if nav then header = nav end
	if portal then header = header .. portal end
	if commons then header = header .. commons end
	if wikispecies then header = header .. wikispecies end
	if description and description ~= '' then
		header = header .. description
	elseif portal or commons or wikispecies then 
		header = mw.ustring.gsub(header, '<br ?/?>', '')
	end
	if toc then header = header .. '<br />' .. toc end
	header = mw.text.trim(header)
	header = mw.ustring.gsub(header, '^<br />', '')
	header = mw.ustring.gsub(header, '<br />$', '')
	
	--append header to outString
	if outString then outString = outString .. header
	else outString = header end
	
	--append cats to outString
	if currentTitle.namespace == 14 then --category namespace
		if table.maxn(categories) > 0 then outString = outString .. table.concat(categories) end
		outString = outString .. table.concat(trackingCategories)
	else
		if table.maxn(categories) > 0 then --might be 0 if there's an error before setting cats
			outString = outString .. '<br />' .. mw.ustring.gsub(table.concat(categories, '<br />'), '%[%[', '[[:')
		end
		outString = outString .. '<br />' .. mw.ustring.gsub(table.concat(trackingCategories, '<br />'), '%[%[', '[[:')
		outString = mw.ustring.gsub(outString, '<br /><br />', '<br />') --produced by empty ('') first/consecutive tracking cat/s
		outString = mw.ustring.gsub(outString, '<br /><br />', '<br />') --jic (use while loop if #trackingCategories >= 3 or 4)
	end
	
	return outString
end

return p