Module:Sitelinks

From WikiProjectMed
Jump to navigation Jump to search

Sitelinks

{{#invoke:Sitelinks|main|qid=Q76}}

badlangs

{{#invoke:Sitelinks|badlangs}}

  1. [[en-simple:Main_Page]]en-simple:Main_Page
  2. [[ami:Main_Page]]ami:Main_Page
  3. [[anp:Main_Page]]anp:Main_Page
  4. [[bbc:Main_Page]]bbc:Main_Page
  5. [[bho:Main_Page]]bho:Main_Page
  6. [[blk:Main_Page]]blk:Main_Page
  7. [[cbk-x-zam:Main_Page]]cbk-x-zam:Main_Page
  8. [[crh-Latn:Main_Page]]crh-Latn:Main_Page
  9. [[dag:Main_Page]]dag:Main_Page
  10. [[dga:Main_Page]]dga:Main_Page
  11. [[fr-x-nrm:Main_Page]]fr-x-nrm:Main_Page
  12. [[guw:Main_Page]]guw:Main_Page
  13. [[it-x-tara:Main_Page]]it-x-tara:Main_Page
  14. [[jv-x-bms:Main_Page]]jv-x-bms:Main_Page
  15. [[mad:Main_Page]]mad:Main_Page
  16. [[nia:Main_Page]]nia:Main_Page
  17. [[pcm:Main_Page]]pcm:Main_Page
  18. [[shi:Main_Page]]shi:Main_Page
  19. [[smn:Main_Page]]smn:Main_Page
  20. [[tly:Main_Page]]tly:Main_Page
  21. [[zgh:Main_Page]]zgh:Main_Page

-- {{#invoke:Sitelinks|main|qid=Q535691}}

local p = {}
local qids = require('Module:Sitelinks/qids').qids
local bad_langs = {
	"en-simple",
	"ami",
	"anp",
	"bbc",
	"bho",
	"blk",
	"cbk-x-zam",
	"crh-Latn",
	"dag",
	"dga",
	"fr-x-nrm",
	"guw",
	"it-x-tara",
	"jv-x-bms",
	"mad",
	"nia",
	"pcm",
	"shi",
	"smn",
	"tly",
	"zgh"
}
function p.get_sitelinks(qid)
	local query = [[
		SELECT DISTINCT
		?lang ?name WHERE {
			VALUES ?item { wd:]] .. qid .. [[ }
			?sitelink schema:isPartOf [ wikibase:wikiGroup "wikipedia" ];
              schema:name ?name;
			  schema:inLanguage ?lang;
			  schema:about ?item;
			  FILTER (?lang NOT IN ("]] .. table.concat(bad_langs, '", "') .. [["))
		  }

	]]
	mw.log(query)
	local result = mw.ext.UnlinkedWikibase.query(query).results.bindings

	local site_links = {}
	for _, sa in pairs(result) do
		local lang = sa.lang.value
		local name = sa.name.value
		site_links[lang] = name
	end
	mw.log("lenth of site_links" .. #site_links)
	return site_links
end

function p.main(frame)
	local qid = frame.args.qid or ""

	if not qid or qid == "" then
		mw.log("qid is empty")
		title = mw.title.getCurrentTitle()
		qid = qids[title.text]
		if not qid then
			return nil
		end
	end

	local links = p.get_sitelinks(qid)

	local row_text = {}

	for lang, name in pairs(links) do
		-- replace _ with - in lang
		lang = lang:gsub("_", "-")
		table.insert(row_text, "[[" .. lang .. ":" .. name .. "]]")
	end

	return table.concat(row_text, "\n")
end

function p.badlangs(frame)
	local row_text = {}

	-- create table for bad_langs keys
	for _, lang in ipairs(bad_langs) do
		local link = "[[" .. lang .. ":Main_Page]]"
		local line = "# <code>" .. mw.text.nowiki(link) .. "</code> → " .. link
		table.insert(row_text, line)
		
	end

	return table.concat(row_text, "\n")
end

return p