Jump to content

Module:L

From Wikidata
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Usage

[edit]

This module has one method, main: {{#invoke:L|main| ... }}.

For more information, see Template:Lexeme.

This template helps inserting Wikibase lexemes and forms and senses

Template parameters[Edit template data]

This template prefers inline formatting of parameters.

ParameterDescriptionTypeStatus
ID1

The lexeme ID, with or without the “L” prefix. May also be a form ID.

Example
L1, 1, L3341, L3341-F2
Linerequired
add anchoranchor

If set to any non-empty value, the template creates an anchor with the lexeme ID (always prefixed with “L”)

Lineoptional
show IDshow_l

If set to “no”, the lexeme ID isn’t shown (only the lemma)

Lineoptional
shortshort

Display only the form or sense ID, for example (S1) instead of (L123-S1)

Suggested values
1
Stringoptional
ss

Display glosses for senses

Suggested values
1
Stringoptional
langlang

The language of the glosses you want to display

Example
ar
Stringoptional

Examples

[edit]

forms

[edit]

short

[edit]

Senses

[edit]

Ids

[edit]

Glosses

[edit]

Languages

[edit]

See also

[edit]
Template Shorthand Example
{{Autodescription}} {{A}} totality consisting of space, time, matter and energy
{{Data}}
{{EntitySchema}} {{E}}
{{Label}} Douglas Adams
{{LinkedLabel}} Douglas Adams
{{Label and description}} {{LD}} Douglas Adams (British science fiction writer and humorist (1952–2001))
{{Item Link with Tooltip}} {{LT}} D. Adams
{{Q}} Douglas Adams (Q42)
{{Q+}} Douglas Adams (Q42): British science fiction writer and humorist (1952–2001)
{{Q'}} Douglas Adams (Q42)  View with Reasonator View with SQID
{{Q''}} Douglas Adams (Q42)  View with Reasonator View with SQID
{{Q'''}} Douglas Adams (Q42)  View with Reasonator View with SQID View profile on Scholia
{{Q!}} human : any single member of Homo sapiens, unique extant species of the genus HomoDouglas Adams (Q42)
{{Q*}} human : any single member of Homo sapiens, unique extant species of the genus HomoDouglas Adams (Q42): British science fiction writer and humorist (1952–2001)
{{Q(}} Douglas Adams (Q42)
{{QB}} Douglas Adams (Q42); talk
{{QP}} instance of (P31) of Douglas Adams (Q42)
{{QT}} Douglas Adams (Q42)
{{Property}} {{P}} country (P17)
{{P'}} country (P17) View with SQID
{{PT'}} country View with SQID
{{P+}} country (P17): sovereign state that this item is in (not to be used for human beings)
{{Lexeme}} {{L}}
{{Claim}} {{C}}
{{Statement}} {{St}}
{{Statement+}}
{{EzStatement+}}
(Label in Venetian) {{Qvec}} Douglas Adams(42)
(Label in Taraškievica) {{Qbe-tarask}}

Дуглас Адамз(42)

(Label in English) {{Qen}} Douglas Adams(42)
(Label in French) {{Qfr}} Douglas Adams(42)
(Label in Italian) {{Qit}} Douglas Adams(42)
(Label in German) {{Qde}}

Douglas Adams(42)

(Label in Polish) {{Qpl}}

Douglas Adams(42)

(Label in Russian) {{Qru}}

Дуглас Адамс(42)

(Label in Ukrainian) {{Quk}}

Дуглас Адамс(42)

(Label in Spanish) {{Qes}}

Douglas Adams(42)

(Label in Chinese) {{Qzh}}

道格拉斯·亞當斯(42)

(Label in Shahmukhi) {{Qpnb}} ڈگلس ایڈمس(42)
(Label in Gurmukhi) {{Qpa}} ਡਗਲਸ ਐਡਮਸ(42)
(Label in Saraiki) {{Qskr}} ڈگلس ایڈمس(42)
(Label in Swedish) {{Qsv}} Douglas Adams(42)




Code

local p = {}

local i18nmessages = mw.loadData('Module:i18n/wikidata')
local fb = require('Module:Fallback')
local defaultlang = mw.getCurrentFrame():preprocess("{{int:lang}}")

local function i18n(str)
	local message = i18nmessages[str]
	if type(message) == 'string' then
		return message
	end
	return fb._langSwitch(message, defaultlang) .. ''
end

local function anchor(id)
    return string.format('<span class="anchor" id="%s"></span>', id)
end

-- Returns the lemmas of the entity concatenated with '/'.
local function getLemma(entity) -- simple for simple templates like {{Q|}}}
    if not entity then
        return i18n('invalid-id')
    end

    local lemmas = ''
    local function valuesSortedByLanguage(t)
        local values = {}
        for k, v in pairs(t) do
            values[#values + 1] = v
        end
        table.sort(values,
            function(a, b)
                return (a.language < b.language)
            end)
        local i = 0
        return function()
            i = i + 1
            if values[i] then
                return values[i]
            end
        end
    end
    if entity.lemmas then
        for v in valuesSortedByLanguage(entity.lemmas) do
            if lemmas ~= '' then
                lemmas = lemmas .. '/' .. '<span lang=' .. v.language .. ' dir="auto">' .. v.value .. '</span>'
            else
                lemmas = '<span lang=' .. v.language .. ' dir="auto">' .. v.value .. '</span>'
            end
        end
    end
    return lemmas
end

local function lemma_value(id)
    -- return mw.getCurrentFrame():expandTemplate { title = "lemma", args = { id } }
    return getLemma(mw.wikibase.getEntity(id))
end

local function getSenses(lexemeId, Sid, senLang)
    local gloss_text = ''
    local entity = mw.wikibase.getEntity(lexemeId)
    for _, sense in pairs(entity:getSenses()) do
        local lexemeId, subId = mw.wikibase.lexeme.splitLexemeId(sense.id)
        if lexemeId == lexemeId and subId == Sid then
            -- "glosses": { "en": { "language": "en", "value": "human between the stages of birth and puberty" } }
            -- glosses = sense:getGlosses()
            if senLang then
                gloss_text = sense.glosses[senLang].value
            else
                for j, gloss in pairs(sense.glosses) do
                    gloss_text = gloss.value
                    break
                end
            end
            break
        end
    end
    return gloss_text
end

local function getFormRepFromEntity(entity)
    local function valuesSortedByLanguage(t)
        local values = {}
        for k, v in pairs(t) do
            values[#values + 1] = v
        end
        table.sort(values,
            function(a, b)
                return (a[2] < b[2])
            end)
        local i = 0
        return function()
            i = i + 1
            if values[i] then
                return values[i]
            end
        end
    end

    local lemmas = ''
    for v in valuesSortedByLanguage(entity:getRepresentations()) do
        if lemmas ~= '' then
            lemmas = lemmas .. '/' .. v[1]
        else
            lemmas = v[1]
        end
    end
    return lemmas
end

local function getLinkAndDisplay(id, showSen, senLang)
    local lexemeId, subId = mw.wikibase.lexeme.splitLexemeId(id)
    if not subId then
        -- only Lexeme
        return "Lexeme:" .. id, lemma_value(id)
    end
    local link = "Lexeme:" .. lexemeId .. "#" .. subId
    local display
    if subId:sub(1, 1) == "F" then
        -- display = frame:callParserFunction { name = "#invoke", args = { "Lexeme", "getFormRep", id } }
        display = getFormRepFromEntity(mw.wikibase.getEntity(id))
    elseif subId:sub(1, 1) == "S" and showSen then
        display = getSenses(lexemeId, subId, senLang)
    else
        display = lemma_value(lexemeId)
    end
    return link, display
end

local function getDisplay2(id, show_l, short)
    if show_l == "no" then return nil end
    if short == "1" then
        local _, subId = mw.wikibase.lexeme.splitLexemeId(id)
        if subId then
            return subId --  part F or S
        end
    end
    return id
end

function p._main(args, frame)
    local id = mw.text.trim(args[1] or "")
    if id == "" then return "" end

    -- if id start with numbers
    if id:sub(1, 1):match("%d") then
        id = "L" .. id
    end
    local showSen = args["s"]
    local senLang = args["lang"]
    local link, display = getLinkAndDisplay(id, showSen, senLang)
    local display2 = getDisplay2(id, args["show_l"], args["short"])

    local anchorValue = (args["anchor"] and args["anchor"] ~= "") and anchor("Lexeme:" .. id) or ""
    local fullDisplay = display .. (display2 and "&#32;<small>(" .. display2 .. ")</small>" or "")

    return string.format('%s[[%s|%s]]', anchorValue, link, fullDisplay)
end

function p.main(frame)
    return p._main(frame:getParent().args, frame)
end

function p.test(frame)
    return p._main(frame.args, frame)
end

function p.getSenses(frame)
    return getSenses(frame.args[1], frame.args[2])
end

return p