Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul CategPlus (codi · ús · discussió · proves · tests · casos prova | subpàgines · enllaços)

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Diverses funcions que faciliten l'assignació de pàgines de la nostra Viquipèdia en categories, prenent de model la Viquipèdia anglesa, o una altra.

Mireu Ajuda:Endreçar una categoria, així aquest mòdul s'utilitza per a les següents plantilles:

Funció Resum explicació
{{CategPlus PL en}} Presenta, en una llista, els enllaços dels articles existents en català que corresponen als enllaços d'articles mostrats en una pàgina (tipus categoria) anglesa.
{{CategPlus ScL en}} Presenta, en una llista, els enllaços de les subcategories existents en català que corresponen als enllaços a subcategories mostrades en una pàgina (tipus categoria) anglesa; de no existir la corresponent categoria en català, la mostra en anglès.
{{CategPlus CL en}} Crea la llista de categories d'una pàgina d'una article, prenent de model la mateixa llista del corresponent article en anglès.
{{CategPlus PL}} Presenta, en una llista, els enllaços dels articles existents en català que corresponen als enllaços d'articles mostrats en una pàgina (tipus categoria) d'una Viquipèdia rellevant.
{{CategPlus ScL}} Presenta, en una llista, els enllaços de les subcategories existents en català que corresponen als enllaços a subcategories mostrades en una pàgina (tipus categoria) d'una Viquipèdia rellevant; de no existir la corresponent categoria en català, la mostra en l'idioma de la Viquipèdia referent.
{{CategPlus CL}} Crea la llista de categories d'una pàgina d'un article, prenent de model la mateixa llista del corresponent article d'una Viquipèdia rellevant.

local p = {}

local SD = require "Module:SimpleDebug"
local SA = require "Module:SimpleArgs"
local enwiki = 'enwiki'
local SRet = "\r\n\r\n"
local SRet2 = '<br />'

function p.getEntityFromTitle(frame)
	local args = SA.GetArgs (frame)
	local title = mw.text.trim(SA.RStr_Par (args, 1))
	local site = args.site or ""
	local qid = mw.wikibase.getEntityIdForTitle(title, site)
	if qid then
		return qid
	else	
		return nil
	end
end

function from_lang(title, lang, nowiki, sCatEnd)
	local qid = mw.wikibase.getEntityIdForTitle(title, lang)
	local sBegin = '[['
	local sEnd = ']]'
	if qid then
		local s = mw.wikibase.getSitelink(qid)
		if s then
			if (nowiki == false) and (string.sub(s,1,10) == 'Categoria:') then
				s = ':'..s
			end	
			return sBegin..s..sCatEnd..sEnd, qid
		else
			return nil
		end	
	else	
		return nil
	end
end

function ExistInTable(tab, val)
	for index, value in ipairs(tab) do
        if value == val then
            return true
        end
	end	
	return false
end

function p.PageList_from_lang(lang, text, text2)
	local list = ''
	local list2 = {}
	local list3 = {}
	for idx,line in ipairs(mw.text.split(text, "\n", true)) do
		line = mw.text.trim(line)
		if string.len (line) > 1 then
			line, qid = from_lang (line, lang, false, '')
			if line then
				if text2 ~= nil then
					table.insert (list2, qid)
				end
				if list == '' then
					list = line
				else	
					list = list..'&nbsp; '..line 
				end	
			end
		end
	end
	if (text2 ~= nil) and (mw.text.trim(text2) ~= '') then
		local list_ca = ''
		for idx,line in ipairs(mw.text.split(text2, "\n", true)) do
			line = mw.text.trim(line)
			if string.len (line) > 1 then
				line, qid = from_lang (line, 'cawiki', false, '')
				table.insert (list3, qid)
				if line and (not ExistInTable(list2, qid)) then
					list_ca = list_ca..'&nbsp; '..line
				end
			end
		end
		if list_ca ~= '' then
			list = list..SRet2..'No trobat a la Viquipèdia '..lang..':'..list_ca
		end	
		list_ca = ''
		for idx,line in ipairs(mw.text.split(text, "\n", true)) do
			line = mw.text.trim(line)
			if string.len (line) > 1 then
				line, qid = from_lang (line, lang, false, '')
				if line and (not ExistInTable(list3, qid)) then
					list_ca = list_ca..'&nbsp; '..line
				end
			end
		end
		if list_ca ~= '' then
			list = list..SRet2..'No trobat a la pàgina de la Viquipedia:'..list_ca
		end	
	end	
	return list
end

function p.PageList(frame)
	local args = SA.GetArgs (frame)
	local lang = SA.RStr_Par (args, 1)
	local text = SA.RStr_Par (args, 2)
	local text2 = SA.Str_Par (args, 3)
	return p.PageList_from_lang(lang, text, text2)
end	

function p.PageList_from_en(frame)
	local args = SA.GetArgs (frame)
	local text = SA.RStr_Par (args, 1)
	local text2 = SA.Str_Par (args, 2)
	return p.PageList_from_lang(enwiki, text, text2)
end	

function p.CatList_from_lang(lang, text)
	local list = ''
	for idx,line in ipairs(mw.text.split(text, "\n", true)) do
		line = mw.text.trim(line)
		local sEnd = ''
		if string.len (line) > 14 then
			local pos = string.find (line, '|')
			if pos then
				sEnd = string.sub(line, pos, string.len(line)-2)
				line = string.sub(line, 3, pos-1)
			else	
				line = string.sub(line, 3, string.len(line)-2)
			end	
			line = from_lang (line, lang, true, sEnd)
			if line then
				line =  mw.text.nowiki (line)
				if list == '' then
					list = line
				else	
					list = list..SRet2..line 
				end	
			end
		end
	end
	return list
end

function p.CatList(frame)
	local args = SA.GetArgs (frame)
	local lang = SA.RStr_Par (args, 1)
	local text = SA.RStr_Par (args, 2)
	return p.CatList_from_lang(lang, text)
end

function p.CatList_from_en(frame)
	local args = SA.GetArgs (frame)
	local text = SA.RStr_Par (args, 1)
	return p.CatList_from_lang(enwiki, text)
end

function p.SubcatList_from_lang(lang, addtemp, text, default)
	local list = ''
	local temp = ''
	if addtemp == true then
		if default == true then
			temp = 'CategPlus PL en|'
		else
			temp = 'CategPlus PL|'..lang..'|'
		end	
		temp = SRet..mw.text.nowiki ('{{')..temp..SRet..mw.text.nowiki ('}}')..SRet
	end	
	for idx,line in ipairs(mw.text.split(text, "\n", true)) do
		line = mw.text.trim(line)
		if string.len (line) > 8 then
			local pos = mw.ustring.find (line, '‎')
			if pos == nil then
				pos = mw.ustring.find (line, ' %(')
			end
			if pos ~= nil then
				line = string.sub(line, 1, pos-1)
				local line2 = from_lang ('Category:'..line, lang, false, '')
				if line2 == nil then
					line2 = line
				end	
				if list == '' then
					list = line2
				else	
					list = list..SRet..line2 
				end
				if addtemp == true then
					list = list..temp
				end	
			end	
		end
	end
	return list
end

function p.SubcatList(frame)
	local args = SA.GetArgs (frame)
	local lang = SA.RStr_Par (args, 1)
	local addtemp = SA.RBool_Par (args, 2)
	local text = SA.RStr_Par (args, 3)
	return p.SubcatList_from_lang(lang, addtemp, text, false)
end

function p.SubcatList_from_en(frame)
	local args = SA.GetArgs (frame)
	local addtemp = SA.RBool_Par (args, 1)
	local text = SA.RStr_Par (args, 2)
	return p.SubcatList_from_lang(enwiki, addtemp, text, true)
end	

return p