Jump to content

Module:Article list: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Protected "Module:Article list": High-risk template or module: 305 transclusions (more info) ([Edit=Require autoconfirmed or confirmed access] (indefinite))
better parameter handling
Line 3: Line 3:


function p.getlinks(frame)
function p.getlinks(frame)
local pframe = frame:getParent()
local args = frame.args
local qids = frame.args[1] or pframe.args[1]
local pargs = frame:getParent().args
local qids = args[1] or pargs[1]
local sort=true
local sort=true
if frame.args.sort=='no' then
if args.sort=='no' or pargs.sort=='no' then
sort=false
sort=false
end
end
local removes = frame.args.remove or ""
local removes = args.remove or pargs.remove or ""
local removelist = mw.text.split(removes,"%s*,%s*")
local removelist = mw.text.split(removes,"%s*,%s*")
local links = {}
local links = {}
Line 30: Line 31:
links2[i]=p.makelink(link)
links2[i]=p.makelink(link)
end
end
return table.concat(links2,"\n")
return table.concat(links2)
end
end
end
end
Line 61: Line 62:


function p.makelink(link)
function p.makelink(link)
return "* [[" .. link[1] .. "|" .. link[2] .. "]]"
return "<li>[[" .. link[1] .. "|" .. link[2] .. "]]</li>"
end
end


function p.convert(frame)
function p.convert(frame)
local pframe = frame:getParent()
local args = frame.args
local input = frame.args[1] or pframe.args[1]
local pargs = frame:getParent().args
local input = args[1] or pargs[1]
if input == nil then
if input == nil then
return nil
return nil
end
end
local resolveEntity = require( "Module:ResolveEntityId" )
local resolveEntity = require( "Module:ResolveEntityId" )
local articlelist = mw.text.split(frame.args[1],"%*%s*")
local articlelist = mw.text.split(input,"%*%s*")
local qidlist = {}
local qidlist = {}
for i,article in ipairs(articlelist) do
for i,article in ipairs(articlelist) do

Revision as of 11:10, 26 February 2022

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 = {}
	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)
	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 "<li>[[" .. link[1] .. "|" .. link[2] .. "]]</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