Module:Article list

require('strict')local p = {};local function makelink(link)return "<li>[[" .. link.target .. "|" .. link.label .. "]]</li>"endlocal function warning(note)return "<li>[[File:Achtung-orange.svg|20px]] "..note.."</li>"endlocal function removeword(link,removelist)for i,remove in ipairs(removelist) dolocal 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,"")endlink = link:gsub("^%s*","") -- strip spaces from startlink = link:gsub("%s*$","") -- strip spaces from endlink = link:gsub("^(%l)", mw.ustring.upper) -- capitalise first letterreturn linkendfunction p.getlinks(frame)local args = frame.argslocal pargs = frame:getParent().argslocal qids = args[1] or pargs[1]local sort = true -- sort entries unless sort=noif args.sort=='no' or pargs.sort=='no' thensort = falseendlocal redlinks = false -- do not show redlinks unless redlinks=yesif args.redlinks=='yes' or pargs.redlinks=='yes' thenredlinks = trueendlocal removes = args.remove or pargs.remove or ""local removelist = mw.text.split(removes,"%s*,%s*") -- split string into table at commaslocal links = {} -- for constructing the linkslocal notes = "" -- for warning messages on the templateif qids thenfor qid in qids:gmatch("Q%d+") dolocal target = mw.wikibase.sitelink(qid)local label = mw.wikibase.getLabel(qid)if target then -- sitelink to enwiki existslocal newlink = {}newlink.target = targetif label then -- make piped link using English label to avoid unnecessary disambiguation termsnewlink.label = removeword(label,removelist) -- remove common words from labelelse -- there is no label so we are using target as the labelnewlink.label = removeword(target,removelist) -- remove common words from targetendtable.insert(links,newlink)else -- no sitelink to enwiki exists yetif label then -- English label existsif redlinks == true thenif mw.title.new(label).exists then -- [[label]] is already a page linked to a different itemnotes = notes..warning("Cannot show link to [["..label.."]] because it is not linked to [[d:Special:EntityPage/"..qid.."|"..qid.."]]")else -- we can create a redlink to [[label]]local newlink = {}newlink.target = labelnewlink.label = removeword(label,removelist)table.insert(links,newlink)endelse -- add warning on template that there is no sitelinknotes = notes..warning("No sitelink for [["..label.."]]")endelse -- no target and no English labelif mw.wikibase.entityExists(qid) thenif redlinks == true then -- add warning on template that no redlink can be generated without labelnotes = notes..warning("Cannot show link to [[d:Special:EntityPage/"..qid.."|"..qid.."]] because no label is defined")else -- add warning on template that there is no sitelink availablenotes = notes..warning("No sitelink for [[d:Special:EntityPage/"..qid.."|"..qid.."]]")endelse -- add warning on template that qid is invalidnotes = notes..warning("Invalid identifier "..qid)endendendendelsereturn "Error: no parameter"endlocal links2 = {} -- will contain wikilinks sorted alphabeticallyif #links>0 thenif sort thentable.sort(links,function (link1,link2) return link1.label<link2.label end)endfor i,link in ipairs(links) dolinks2[i]=makelink(link)endendlocal output = '<ul>'..table.concat(links2)if mw.title.getCurrentTitle():inNamespace(10) thenoutput = output..notesendreturn output ..'</ul>'endfunction p.convert(frame)local args = frame.argslocal pargs = frame:getParent().argslocal input = args[1] or pargs[1]local prefix = args.prefix or pargs.prefixif prefix == nil thenprefix = "Article list|"endif input == nil thenreturn nilendlocal resolveEntity = require( "Module:ResolveEntityId" )local articlelist = mw.text.split(input,"%*%s*")local qidlist = {}for i,article in ipairs(articlelist) dolocal rawarticle=string.match(article,'%[%[(.+)%|') or string.match(article,'%[%[(.+)%]%]')if rawarticle thenlocal qid = resolveEntity._id(rawarticle) if qid thenqidlist[#qidlist+1] = qid.."<!-- "..rawarticle.." -->" else qidlist[#qidlist+1] = "<!-- No QID for "..rawarticle.." -->" endendendreturn "{{" .. prefix .. table.concat(qidlist,", ").."}}"endreturn p