Jump to content

Module:Article list

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MSGJ (talk | contribs) at 10:48, 24 February 2022 (inside template). 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 qids = frame.args[1]
	local sort=true
	if frame.args.sort=='no' then
		sort=false
	end
	local removes = frame.args.remove or ""
	local removelist = mw.text.split(removes,"%s*,%s*")
	local links = {}
	if qids then
		for qid in qids:gmatch("Q%d+") do
			local link = p.getlink(qid,removelist)
			if link ~= nil then
				table.insert(links,link)
			end
		end
	else
		return "Error: no parameter"
	end
	if #links>0 then
		if sort then
			table.sort(links,function (link1,link2) return link1[2]<link2[2] end)
		end
		local links2 = {}
		for i,link in ipairs(links) do
			links2[i]=p.makelink(link)
		end
		return table.concat(links2,"\n")
	end
end

function p.getlink(qid,removelist)
	local slink = mw.wikibase.sitelink(qid)
	local label = mw.wikibase.getLabel(qid)
	local pipe = ""
	if slink then
		if label then
			pipe = p.removeword(label,removelist)
		else
			pipe = p.removeword(slink,removelist)
		end
		return {slink,pipe}
	else
		return nil
	end
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 "* [[" .. link[1] .. "|" .. link[2] .. "]]"
end

function p.convert(frame)
	local input = frame.args[1]
	if input == nil then
		return nil
	end
	local resolveEntity = require( "Module:ResolveEntityId" )
	local articlelist = mw.text.split(frame.args[1],"%*%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,",\r").."}}"
end

return p