Module:Article list: Difference between revisions
Appearance
Content deleted Content added
better parameter handling |
add warnings on template if qid is invalid or sitelink does not exist |
||
Line 13: | Line 13: | ||
local removelist = mw.text.split(removes,"%s*,%s*") |
local removelist = mw.text.split(removes,"%s*,%s*") |
||
local links = {} |
local links = {} |
||
⚫ | |||
⚫ | |||
if qids then |
if qids then |
||
for qid in qids:gmatch("Q%d+") do |
for qid in qids:gmatch("Q%d+") do |
||
local |
local target = mw.wikibase.sitelink(qid) |
||
⚫ | |||
if |
if target then |
||
⚫ | |||
local newlink = {} |
|||
newlink.target = target |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
notes = notes..p.warning("No sitelink for "..label) |
|||
else |
|||
if mw.wikibase.entityExists(qid) then |
|||
notes = notes..p.warning("No sitelink for "..qid) |
|||
else |
|||
notes = notes..p.warning("Invalid identifier "..qid) |
|||
end |
|||
⚫ | |||
end |
end |
||
end |
end |
||
Line 25: | Line 45: | ||
if #links>0 then |
if #links>0 then |
||
if sort then |
if sort then |
||
table.sort(links,function (link1,link2) return link1 |
table.sort(links,function (link1,link2) return link1.label<link2.label end) |
||
end |
end |
||
⚫ | |||
for i,link in ipairs(links) do |
for i,link in ipairs(links) do |
||
links2[i]=p.makelink(link) |
links2[i]=p.makelink(link) |
||
end |
end |
||
return table.concat(links2) |
|||
end |
end |
||
local output = '<span class="hlist">'..table.concat(links2) |
|||
⚫ | |||
if mw.title.getCurrentTitle():inNamespace(10) then |
|||
output = output..notes |
|||
function p.getlink(qid,removelist) |
|||
local slink = mw.wikibase.sitelink(qid) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return {slink,pipe} |
|||
⚫ | |||
⚫ | |||
end |
end |
||
output = output..'</span>' |
|||
⚫ | |||
end |
end |
||
Line 62: | Line 70: | ||
function p.makelink(link) |
function p.makelink(link) |
||
return "<li>[[" .. link |
return "<li>[[" .. link.target .. "|" .. link.label .. "]]</li>" |
||
end |
|||
function p.warning(note) |
|||
return "<li>[[File:Achtung-orange.svg|20px]] "..note.."</li>" |
|||
end |
end |
||
Revision as of 15:28, 28 February 2022
Please see Template:Article list.
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 "..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