پودمان:IrelandByCountyCatNav

توضیحات پودمان[ایجاد] [پاکسازی]
--[[ v02

 each title consists of 3 parts
    * prefix
    * county name
    * suffix
 e.g. "Foo in County Mayo"
    * prefix = "Foo in "
    * county name = "County Mayo"
    * suffix = ""
 e.g. "County Sligo-related lists"
    * prefix = ""
    * county name = "County Sligo"
    * suffix = "-related lists"
]]

-- config
local textSize = '90%'
local tableClass="toc"
local tableFallbackMaxWidth="auto"
local tableMaxWidth="calc(100% - 25em)" -- Template:GeoGroup has width: 23em<
-- local tableStyle="margin-right:0; margin-left:auto; clear:right !important; margin-top:0 !important; float:right; width:auto;"
local tableStyle="margin-right:0; margin-left:auto; clear:right !important; margin-top:0 !important; width:auto;"
local evenRowStyle = "vertical-align:top; background-color:#f3f3f3;"
local oddRowStyle = "vertical-align:top;"
local labelStyle = "text-align:left; font-weight: bold; padding: 0.25em 0.5em 0.25em 0.5em;"
local listStyle = "text-align:right; font-weight: normal; padding: 0.25em 0.5em 0.25em 0.5em;"
local greyLinkColor = "#888"

local callingTemplates = {
	'الگو:AllIrelandByCountyCatNav',
	'الگو:ناوبری همه رده‌های ایرلند بر پایه شهرستان',
	'الگو:RepublicOfIrelandOnlyByCountyCatNav',
	'الگو:ناوبری رده فقط شهرستان‌های جمهوری ایرلند',
	'الگو:NorthernIrelandOnlyByCountyCatNav',
	'الگو:ناوبری رده فقط شهرستان‌های ایرلند شمالی',
	'الگو:PrePartitionIrelandByCountyCatNav'
}

-- globals for this module
local debugging = false
local debugmsg = ""
local tableRowNum = 0
local includeNewCounties = true
local useCountyWord = true
local title_prefix = ""
local title_suffix = ""
local countySet = nil
local title_prefix
local title_suffix
local thisPageCounty
local isNorniron = false
local greyLinkCount = 0
local blueLinkCount = 0
local parentname = ""
local templateName

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local p = {}

local TwentySixCounties = {
	'کارلو',
	'کاوان',
	'کلر',
	'کورک',
	'دانیگول',
	'دوبلین',
	'گالوی',
	'کری',
	'کیلدر',
	'کیلکنی',
	'لیش',
	'لیتریم',
	'لیمریک',
	'لانگفورد',
	'لوث',
	'مایو',
	'میث',
	'مانهن',
	'افلی',
	'رسکومون',
	'اسلیگو',
	'تیپراری',
	'واترفورد',
	'وست میث',
	'وکسفورد',
	'ویکلو'
}

local SixCounties = {
	'انتریم',
	'آرما',
	'داون',
	'فرمانا',
	'لندندری',
	'تایرون'
}

local newCounties = {
	'دانلیری-راتفارنهام',
	'فینگل',
	'دوبلین جنوبی',
	'دوبلین (شهر)'
}

local Traditional32Counties = {
	'شهرستان انتریم',
	'شهرستان آرما',
	'شهرستان کارلو',
	'شهرستان کاوان',
	'شهرستان کلر',
	'شهرستان کورک',
	'شهرستان دانیگول',
	'شهرستان داون',
	'شهرستان دوبلین',
	'شهرستان فرمانا',
	'شهرستان گالوی',
	'شهرستان کری',
	'شهرستان کیلدر',
	'شهرستان کیلکنی',
	'شهرستان لیتریم',
	'شهرستان لیمریک',
	'شهرستان لندندری',
	'شهرستان لانگفورد',
	'شهرستان لوث',
	'شهرستان مایو',
	'شهرستان میث',
	'شهرستان مانهن',
	'شهرستان رسکومون',
	'شهرستان اسلیگو',
	'شهرستان تیپراری',
	'شهرستان تایرون',
	'شهرستان واترفورد',
	'شهرستان وست میث',
	'شهرستان وکسفورد',
	'شهرستان ویکلو',
	"شهرستان پادشاه",
	"شهرستان ملکه"
}


function trackingCategory()
	-- discount this page, which will always be coded as a blue link, but rendered as bold un-navigable
	blueLinkCount = blueLinkCount - 1
	if greyLinkCount == 0 then
		return "[[رده:" .. templateName .. " فاقد پیوند خاکستری]]"
	end
	if blueLinkCount == 0 then
		return "[[رده:" .. templateName .. " فقط دارای پیوند خاکستری]]"
	end
	if greyLinkCount > 25 then
		return "[[رده:" .. templateName .. " با بیش از ۲۵ پیوند خاکستری]]"
	elseif greyLinkCount > 15 then
		return "[[رده:" .. templateName .. " با بیش از ۱۵ پیوند خاکستری]]"
	elseif greyLinkCount > 5 then
		return "[[رده:" .. templateName .. " با بیش از ۵ پیوند خاکستری]]"
	end
	return ""
end

function makeTableRow(rowLabel, useCountyWord, countyList)
	debugLog(2, "makeTableRow, برچسب: ")
	if (rowLabel == nil) then
		rowLabel = "بر&nbsp;پایه&nbsp;شهرستان"
		debugLog(nil, rowLabel)
	else
		rowLabel = mw.text.trim(rowLabel)
		debugLog(nil, " [" .. rowLabel .. "]")
	end
	tableRowNum = tableRowNum + 1
	local thisRow
	if (tableRowNum % 2) == 0 then
		debugLog(3, "شماره‌گذاری زوج")
		thisRow = '<tr style="' .. evenRowStyle .. '">\n'
	else
		debugLog(3, "شماره‌گذاری فرد")
		thisRow = '<tr style="' .. oddRowStyle .. '">\n'
	end
	if not ((rowLabel == nil) or (rowLabel =="")) then
		thisRow = thisRow .. '<td style="' .. labelStyle .. '">' .. rowLabel .. '</td>\n'
	end
	-- now begin making the row contents
	local countyWord = ""
	if useCountyWord then
		debugLog(3, "استفاده از واژه شهرستان")
		countyWord = "شهرستان "
	else
		debugLog(3, "عدم استفاده از واژه شهرستان")
	end
	thisRow = thisRow .. '<td style="' .. listStyle .. ';"><div class="hlist">\n'
	local i, aCounty
		debugLog(3, "پردازش فهرست شهرستان")
	for i, aCounty in ipairs(countyList) do
		debugLog(4, "شماره [" .. tostring(i) .. ": [" .. aCounty .. "]")
		myCatName = makeCatName(countyWord .. aCounty, title_prefix, title_suffix)
		thisRow = thisRow .. "* " .. makeCatLink(myCatName, aCounty) .. "\n"
		local j, nuCounty
		if (includeNewCounties and (aCounty == "دوبلین")) then
			-- make a sub-list for the newCounties
			local subCatName
			for j, nuCounty in ipairs(newCounties) do
				subCatName = makeCatName(nuCounty, title_prefix, title_suffix)
				displayname = nuCounty
				if displayname == "دوبلین (شهر)" then
					displayname = "دوبلین"
				end
				thisRow = thisRow .. "** " .. makeCatLink(subCatName, displayname) .. "\n"
			end
		end
	end
	thisRow = thisRow .. '</div></td>\n</tr>'
	return thisRow
end


function makeTable()
	debugLog(1, "ساخت جدول")
	tableRowNum = 0
	local myTable = '<table class="' .. tableClass .. '"'
	myTable = myTable .. ' style="' .. tableStyle .. '; font-size:' .. textSize .. '; max-width:' .. tableFallbackMaxWidth .. '; max-width:' .. tableMaxWidth ..'">\n'

	if (countySet == "thirtytwo") then
		useCountyWord = false
		myTable = myTable .. makeTableRow(nil, useCountyWord, Traditional32Counties)
	elseif (countySet == "twentysix") then
		myTable = myTable .. makeTableRow(nil, useCountyWord, TwentySixCounties)
	elseif (countySet == "six") then
		myTable = myTable .. makeTableRow(nil, useCountyWord, SixCounties)
	else -- default to 26 plus 6
		if isNorniron then
			myTable = myTable .. makeTableRow("ایرلند&nbsp;شمالی", useCountyWord, SixCounties)
			myTable = myTable .. makeTableRow("جمهوری ایرلند", useCountyWord, TwentySixCounties)
		else
			myTable = myTable .. makeTableRow("جمهوری ایرلند", useCountyWord, TwentySixCounties)
			myTable = myTable .. makeTableRow("ایرلند&nbsp;شمالی", useCountyWord, SixCounties)
		end
	end
	myTable = myTable .. "</table>\n"
	return myTable
end


-- Make a piped link to a category, if it exists
-- If it doesn't exist, just display the greyed the link title without linking
function makeCatLink(catname, disp)
	local displaytext
	if (disp ~= "") and (disp ~= nil) then
		-- use 'disp' parameter, but strip any trailing disambiguator
		displaytext = mw.ustring.gsub(disp, "%s+%(.+$", "");
	else
		displaytext = catname
	end
	local fmtlink
	local catPage = mw.title.new( catname, "رده" )
	if (catPage.exists) then
		fmtlink = "[[:رده:" .. catname .. "|" .. displaytext .. "]]"
		blueLinkCount = blueLinkCount + 1
	else
		fmtlink = '<span style="color:' .. greyLinkColor .. '">' .. displaytext .. "</span>"
		greyLinkCount = greyLinkCount + 1
	end

	return fmtlink
end


function makeCatName(countyName, prefix, suffix)
	local this_cat_name = '';
	this_cat_name = this_cat_name .. prefix
	this_cat_name = this_cat_name .. countyName
	this_cat_name = this_cat_name .. suffix
	return this_cat_name
end


function patternSearchEncode(s)
	return mw.ustring.gsub(s, "([%W])", "%%%1")
end

-- Does the pagename include a county name?
-- with or without the prefix "County", depending on value of useCountyWord
function findCountyNameInPagename(pn, countylist, description, prefixCountyWord)
	local i, aCounty, testCounty
	debugLog(2, "بررسی [" .. pn .."] برای نام شهرستان در مجموعه شهرستان: " .. description)
	for i, aCounty in ipairs(countylist) do
		if prefixCountyWord then
			testCounty = "شهرستان " .. aCounty
		else
			testCounty = aCounty
		end
		debugLog(3, "آزمودن: ["  .. testCounty .. "]")
		local testCountyEncoded = patternSearchEncode(testCounty)
		-- For efficiency, the first test is a simple match as a a screening test
		-- If the bare county name is nowhere in the pagename, then no need for
		-- more precise checks
		-- This check would be one line in regex, but Lua pattern matching is cruder,
		--so we need several passes to ensure that any match is of a complete word
		debugLog(4, "انطباق ساده؟ ")
		if (not mw.ustring.match(pn, testCountyEncoded)) then
			debugLog(nil, "ناموفق")
		else
			debugLog(nil, "موفق")

			debugLog(4, "انطباق نام کامل؟ ")
			if (pn == testCounty) then
				debugLog(nil, "بله")
				return testCounty
			end
			debugLog(nil, "خیر")

			debugLog(4, "انطباق در شروع، پیش از جداکننده؟ ")
			if mw.ustring.match(pn, "^" .. testCountyEncoded .. "[^%w]") then
				debugLog(nil, "بله")
				return testCounty
			end
			debugLog(nil, "خیر")
			
			debugLog(4, "انطباق در پایان، پس از جداکننده؟ ")
			if mw.ustring.match(pn, "[^%w]" .. testCountyEncoded .. "$") then
				debugLog(nil, "بله")
				return testCounty
			end
			debugLog(nil, "خیر")

			debugLog(4, "انطباق در هرجا، پیش و پس از جداکننده؟ ")
			if mw.ustring.match(pn, "[^%w]" .. testCountyEncoded .. "[^%w]") then
				debugLog(nil, "بله")
				return testCounty
			end
			debugLog(nil, "خیر")
		end
	end
	return nil
end

-- check whether a given county name is in a particular set
function isCountyInSet(s, aSet, description)
	local thisCounty = mw.ustring.gsub(s, "^شهرستان +", "")
	debugLog(4, "بررسی [" .. thisCounty .. "] در مجموعه: " .. description)
	local aValidCounty
	for i, aValidCounty in ipairs(aSet) do
		debugLog(4, "مقایسه با [" .. aValidCounty .. "]: ")
		if mw.ustring.match(thisCounty, "^" .. aValidCounty .. "$") then
			debugLog(nil, " منطبق")
			return true
		end
		debugLog(nil, " نامنطبق")
	end
	return false
end


-- parse the pagename to find 3 parts: prefix, county name, suffix
function parsePagename(pn)
	debugLog(1, "تجزیه نام صفحه: [" .. pn .. "]")
	isNorniron = false
	local validCountyName
	if (countySet == "twentysix") then
		validCountyName = findCountyNameInPagename(pn, TwentySixCounties, "بیست و شش", useCountyWord)
		if (validCountyName == nil and includeNewCounties) then 
			validCountyName = findCountyNameInPagename(pn, newCounties, "شهرستان‌های نو", false)
		end
	elseif (countySet == "thirtytwo")  then
		useCountyWord = false
		validCountyName = findCountyNameInPagename(pn, Traditional32Counties, "سی و دو", useCountyWord)
		if (validCountyName == nil and includeNewCounties) then 
			validCountyName = findCountyNameInPagename(pn, newCounties, "شهرستان‌های نو", false)
		end
	elseif (countySet == "six") then
		validCountyName = findCountyNameInPagename(pn, SixCounties, "شش", useCountyWord, false)
		if validCountyName ~= nil then
			isNorniron = true
		end
	elseif (countySet == "gaa") then
		validCountyName = findCountyNameInPagename(pn, GAACounties, "gaa", useCountyWord, false)
	else -- default: treat as (countySet == "twentysixplussix")
		validCountyName = findCountyNameInPagename(pn, TwentySixCounties, "بیست و شش", useCountyWord)
		if validCountyName == nil then
			validCountyName = findCountyNameInPagename(pn, SixCounties, "شش", useCountyWord)
			if validCountyName ~= nil then
				isNorniron = true
			end
		end
		if (validCountyName == nil and includeNewCounties) then 
			validCountyName = findCountyNameInPagename(pn, newCounties, "شهرستان‌های نو", false)
		end

	end
	if validCountyName == nil then
		return false
	end
	
	-- if we get here, the page name "pn" includes a validCountyName
	-- so now we need to split the string
	
	debugLog(2, "تفکیک نام صفحه پیرامون [" .. validCountyName .. "]")
	local validCountyEncoded = mw.ustring.gsub(validCountyName, "([%W])", "%%%1")
	match_prefix, match_county, match_suffix = mw.ustring.match(pn, "^(.*)(" .. validCountyEncoded .. ")(.*)$")
	
	title_prefix = match_prefix
	title_suffix = match_suffix
	thisPageCounty = mw.ustring.gsub(match_county, "^شهرستان%s+", "")
	debugLog(2, "تجزیه موفق")
	debugLog(3, "پیشوند عنوان = [" .. title_prefix .. "]")
	debugLog(3, "شهرستان این صفحه = [" .. thisPageCounty .. "]")
	debugLog(3, "پسوند عنوان = [" .. title_suffix .. "]")
	return true
end


function publishDebugLog()
	if not debugging then
		return ""
	end
	return "== خرفسترزدایی ==\n\n" .. debugmsg .. "\n== خروجی ==\n"
end


-- debugLog builds a log which can be output if debuging is enabled
-- each log entry is given a level, so that the output is not simply a flat list
-- a debug msg may be appended to the previous msg by setting the level to nil
function debugLog(level, msg)

	if (debugmsg == nil) then
		debugmsg = ""
	end

	if (level ~= nil) then
		-- not appending, so make a new line
		debugmsg = debugmsg .. "\n"
		-- then add the level
		local i
		for i = 1, level do
			if (i % 2) == 1 then
				debugmsg = debugmsg .. "#"
			else
				debugmsg = debugmsg .. "*"
			end
		end 
	end
	debugmsg = debugmsg .. " " .. msg
	return true
end


function getYesNoParam(args, thisParamName, defaultVal)
	local paramVal = args[thisParamName]
	if paramVal == nil then
		paramVal = ""
	end

	debugLog(2, "ارزیابی پارامتر بله/خیر: [" .. thisParamName .. "] = [" .. paramVal .. "]")
	debugLog(3, "پیش‌فرض = " .. ((defaultVal and "بله") or "خیر"))
	debugLog(3, "ارزیابی در قالب: ")
	local returnValue
	if paramVal == "" then
		returnValue = defaultVal
	else
		returnValue = yesno(args[thisParamName], defaultVal)
	end
	if (returnValue) then
		debugLog(nil, "بله")
	else
		debugLog(nil, "خیر")
	end
	return returnValue
end

function makeErrorMsg(s)
	return '<p class="error">خطای [[' .. parentname .. ']]: ' .. s .. '</p>\n'
end

function isValidParent(p)
	local i, aParent
	for i, aParent in ipairs(callingTemplates) do
		if p == aParent then
			return true
		end
	end
	return false
end

function p.main(frame)
	local parent = frame:getParent()
	if parent then
		 parentname = parent:getTitle():gsub('/تمرین$', '')
	end

	if (parentname == nil) or not isValidParent(parentname) then
		local errormsg = '<p class="error"> خطا: ' .. parentname .. ' بسته‌بندی‌کنندهٔ معتبری برای [[' .. frame:getTitle() .. ']] نیست\n'
		errormsg = errormsg .. '<br><br>بسته‌بندی‌کننده‌های معتبر: '
		local i, aParent
		for i, aParent in ipairs(callingTemplates) do
			errormsg = errormsg .. '[[' .. aParent  .. ']]'
		end
		errormsg = errormsg .. '</p>'
		return errormsg
	end
	templateName = mw.ustring.gsub(parentname, "^الگو:", "")
	
	debugLog(1, "بررسی پارامترها")
	debugging = getYesNoParam(frame.args, "debug", false)
	useCountyWord = getYesNoParam(frame.args, "usecountyword", true)
	includeNewCounties = getYesNoParam(frame.args, "newcounties", true)

	countySetParam = "twentysixplussix" -- default
	debugLog(2, "مجموعه شهرستان")
	if ((frame.args['countyset'] == nil) or (frame.args['countyset'] == "")) then
		debugLog(3, "not set")
		countySet = nil
	else
		debugLog(3, "مجموعه شهرستان=[" .. frame.args['countyset'] .. "]")
		local countySetParam = mw.text.trim(mw.ustring.lower(frame.args['countyset']))
		debugLog(4, "ارزیابی در قالب: ")
		if (countySetParam == "twentysix") or
			(countySetParam == "six") or
			(countySetParam == "thirtytwo") or
			(countySetParam == "twentysixplussix")
		then
			countySet = countySetParam
			debugLog(nil, "[" .. countySetParam .. "]")
			if (countySetParam == "gaa") then
				useCountyWord = false
				includeNewCounties = false
				debugLog(5, "Yes/no parameter [newcounties] reset to [no]")
				debugLog(5, "Yes/no parameter [usecountyword] reset to [no]")
			end
		else
			countySet = nil
			debugLog(nil, "مجموعهٔ نامعتبر")
		end
	end

	-- get the page title
	thispage = mw.title.getCurrentTitle()
	thispagename = thispage.text;
	
	debugLog(1, "mw.title.getCurrentTitle()")
	debugLog(2, "thispage.text = [" .. thispage.text .."]")
	debugLog(2, "thispage.namespace = [" .. thispage.namespace .."]")
	debugLog(2, "thispage.nsText = [" .. thispage.nsText .."]")
	debugLog(2, "این صفحه یک رده است؟ با استفاده از (thispage:inNamespace(14)): ")
	if not (thispage:inNamespace(14)) then
		debugLog(nil, "خیر، رده نیست")
		debugLog(1, "رده نیست، پس خروجی نداریم")
		return makeErrorMsg("تنها برای استفاده در صفحه‌های فضای نام رده") .. publishDebugLog()
	end
	debugLog(nil, "بله، رده است")

	if not parsePagename(thispagename) then
		-- some error parsing the title, so don't proceed to output
		local trackingCatInvalid = "[[رده:" .. templateName .. " در رده نامعتبر]]"
		return makeErrorMsg('نام این رده دربردارندهٔ نام معتبر یکی از شهرستان‌های ایرلند نیست') .. publishDebugLog() .. trackingCatInvalid
	end
	
	debugLog(1, "تجزیه کامل شد")
	debugLog(2, "پیشوند عنوان = [" .. title_prefix .. "]")
	debugLog(2, "پسوند عنوان = [" .. title_suffix .. "]")

	return publishDebugLog() .. makeTable() .. trackingCategory()

end

return p