مَوادَس کُن گٔژھِو

Module:AutoLink

وِکیٖپیٖڈیا پؠٹھٕ، اَکھ آزاد اِنسایکلوپیٖڈیا

"یَمہٕ ماڈیوٗلُک دَستاویز ییٚہِ Module:AutoLink/دَستاویز جاے بَناونہٕ"

-- Module:AutoLink
-- Automatically generates a wikilink from an English title by fetching data from Wikidata.
local p = {}

function p.link(frame)
    -- Get the English page title from the first argument and trim whitespace.
    local englishTitle = frame.args[1] and mw.text.trim(frame.args[1]) or ''

    -- If no input is provided, return an empty string.
    if englishTitle == '' then
        return ''
    end

    -- 1. Get the Wikidata QID for the English title from English Wikipedia ('enwiki').
    local qid = mw.wikibase.getEntityIdForTitle(englishTitle, 'enwiki')

    -- If no QID is found, we cannot proceed. Return a helpful error.
    if not qid then
        return '<span class="error">Error: Wikidata item not found for "' .. englishTitle .. '".</span>'
    end

    -- 2. Get the Kashmiri label ('ks') for the item from Wikidata.
    local kashmiriLabel = mw.wikibase.getLabelByLang(qid, 'ks')

    -- If a Kashmiri label doesn't exist on Wikidata, use the original English title as a fallback.
    if not kashmiriLabel then
        kashmiriLabel = englishTitle
    end

    -- 3. Get the local page title on Kashmiri Wikipedia that is linked to this QID.
    local localTitle = mw.wikibase.getSitelink(qid, 'kswiki')

    -- 4. Generate the wikilink based on whether the local page exists.
    if localTitle then
        -- The page exists. Return a piped link: [[Local Page Title|Kashmiri Label]]
        return string.format('[[%s|%s]]', localTitle, kashmiriLabel)
    else
        -- The page does not exist. Return a red link: [[Kashmiri Label]]
        return string.format('[[%s]]', kashmiriLabel)
    end
end

return p