Module:Sitelinks/sandbox

From WikiProjectMed
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sitelinks/sandbox/doc

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

local p = {}
local qids = require('Module:Sitelinks/qids').qids
local bad_langs = {
    ["cbk-x-zam"] = "",
    ["crh-Latn"] = "",
    ["en-simple"] = "",
    ["fr-x-nrm"] = "",
    ["it-x-tara"] = "",
    ["jv-x-bms"] = "",
    ami = "",
    anp = "",
    bbc = "",
    bho = "",
    blk = "",
    dag = "",
    dga = "",
    fon = "",
    guc = "",
    guw = "",
    mad = "",
    mni = "",
    nia = "",
    pcm = "",
    pwn = "",
    shi = "",
    smn = "",
    tay = "",
    tly = "",
    zgh = "",
}
local function make_site_links(result)
    local site_links = {}
    for _, sa in pairs(result) do
        local lang = sa.lang.value
        local name = sa.name.value
        -- filter lang in bad_langs
        if not bad_langs[lang] then
            site_links[lang] = name
        end
    end

    return site_links
end

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;
		  }

	]]
    local result = mw.ext.UnlinkedWikibase.query(query).results.bindings
    local site_links = make_site_links(result)
    mw.log("lenth of site_links" .. #site_links)
    return site_links
end

function p.main(frame)
    local qid = frame.args.qid or ""
    local title = frame.args.title or mw.title.getCurrentTitle().text

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

    mw.log("qid:", qid, "title:", title)
    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
    local result = table.concat(row_text, "\n")
    return "<div class='Module:Sitelinks'>" .. result .. "</div>"
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