Jump to content

Module:Article list/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MSGJ (talk | contribs) at 15:41, 28 February 2022 (+link). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {};
require('Module:No globals')

function p.getlinks(frame)
	local args = frame.args
	local pargs = frame:getParent().args
	local qids = args[1] or pargs[1]
	local sort=true
	if args.sort=='no' or pargs.sort=='no' then
		sort=false
	end
	local removes = args.remove or pargs.remove or ""
	local removelist = mw.text.split(removes,"%s*,%s*")
	local links = {}
	local links2 = {}
	local notes = ""
	if qids then
		for qid in qids:gmatch("Q%d+") do
			local target = mw.wikibase.sitelink(qid)
			local label = mw.wikibase.getLabel(qid)
			if target then
				local newlink = {}
				newlink.target = target
				if label then
					newlink.label = p.removeword(label,removelist)
				else
					newlink.label = p.removeword(target,removelist)
				end
				table.insert(links,newlink)
			else
				if label then
					notes = notes..p.warning("No sitelink for "..label)
				else
					if mw.wikibase.entityExists(qid) then
						notes = notes..p.warning("No sitelink for [[d:Special:EntityPage/"..qid.."]]")
					else
						notes = notes..p.warning("Invalid identifier "..qid)
					end
				end
			end
		end
	else
		return "Error: no parameter"
	end
	if #links>0 then
		if sort then
			table.sort(links,function (link1,link2) return link1.label<link2.label end)
		end
		for i,link in ipairs(links) do
			links2[i]=p.makelink(link)
		end
	end
	local output = '<span class="hlist">'..table.concat(links2)
	if mw.title.getCurrentTitle():inNamespace(10) then
		output = output..notes
	end
	output = output..'</span>'
	return output
end

function p.removeword(link,removelist)
	for i,remove in ipairs(removelist) do
		local char1=string.sub(remove,1,1)
		local regex="%f[%w][" .. string.upper(char1) .. string.lower(char1) .. "]" .. string.sub(remove,2) .. "*%f[%W]"
		link=link:gsub(regex,"")
	end
	link = link:gsub("^(%l)", mw.ustring.upper)
	return link
end

function p.makelink(link)
	return "<li>[[" .. link.target .. "|" .. link.label .. "]]</li>"
end

function p.warning(note)
	return "<li>[[File:Achtung-orange.svg|20px]] "..note.."</li>"
end

function p.convert(frame)
	local args = frame.args
	local pargs = frame:getParent().args
	local input = args[1] or pargs[1]
	if input == nil then
		return nil
	end
	local resolveEntity = require( "Module:ResolveEntityId" )
	local articlelist = mw.text.split(input,"%*%s*")
	local qidlist = {}
	for i,article in ipairs(articlelist) do
		local rawarticle=string.match(article,'%[%[(.+)%|') or string.match(article,'%[%[(.+)%]%]')
		if rawarticle then
			local qid = resolveEntity._id(rawarticle)
	 		if qid then
				qidlist[#qidlist+1] = qid.."<!-- "..rawarticle.." -->"
	 		else
	 			qidlist[#qidlist+1] = "<!-- No QID for "..rawarticle.." -->"
	 		end
		end
	end
	return "{{Article list|"..table.concat(qidlist,", ").."}}"
end

return p