Module:Languages: Difference between revisions

From Imperivm Romanvm
Jump to navigation Jump to search
(split off rare error handling code paths)
m (1 revision imported)
Tag: Reverted
Line 1: Line 1:
local export = {}
--[=[
Not globally exposed. Internal function only.


local function do_entry_name_or_sort_key_replacements(text, replacements)
language_subpages( frame, transform, options )
if replacements.from then
Parameters
for i, from in ipairs(replacements.from) do
    frame:    The frame that was passed to the method invoked. The first argument or the page argument will be respected.
local to = replacements.to[i] or ""
    transform: A transform function. Example: function( basepagename, subpagename, code, langname ) end
text = mw.ustring.gsub(text, from, to)
    options:  An object with options. Example: { abort= { on=function() end, time=0.8 }  }
end
        Following options are available:
end
        abort: Aborts iterating over the subpages if one of the conditions is met. If the process is aborted, nil is returned!
            on: Function to be called if an abort-condition was met.
if replacements.remove_diacritics then
            cycles: The maximum number of subpages to run over.
text = mw.ustring.toNFD(text)
            time: Maximum time to spend running over the subpages.
text = mw.ustring.gsub(text,
'[' .. replacements.remove_diacritics .. ']',
'')
text = mw.ustring.toNFC(text)
end
return text
end


local Language = {}
]=]
function language_subpages( frame, transform, options )
    local args, pargs, options = frame.args, ( frame:getParent() or {} ).args or {}, options or {};
    local title = args.page or args[1] or pargs.page or pargs[1] or "";
    local abort = options.abort or {};
    local at, clock = type( abort.on ), os.clock();
    local ac = function()
        if  at == 'function' or ( at == 'table' and getmetatable(abort.on).__call ) then
            abort.on();
        end
    end
    local tt = type( transform );
    local page = require( 'Module:Page' );


function Language:getCode()
    title = page.clean(title);
return self._code
end


    if tt == 'function' or ( tt == 'table' and getmetatable(transform).__call ) then
        local fetch, pages, langcode, langname = mw.language.fetchLanguageName, {};
--[==[


function Language:getCanonicalName()
    / \
return self._rawData[1] or self._rawData.canonicalName
    / | \
  /  ·  \
  ¯¯¯¯¯¯¯
  Page.subpages() no longer works because it attempted to parse the HTML content generated by
  calling the parser function "Special:Prefixindex:" which is no longer expanded in Lua but
  converted to a "stripped tag" (containing a unique identifier surrounded by ASCII DEL characters)
  representing the tag name and its parameters.
  The actual expansion of stripped tags can no longer be performed in Lua.
  Now unstripping these tags just kills ALL these tags (except "wiki" tags) instead of performing
  their expansion by running the extension code. Only MediaWiki can unstrip these tags in texts after
  they have been returned by Lua.
  For this reason, page.subpages() is now completely empty (Module:Page no longer works).
  This cannot be bypassed, except by using a Scribunto extension library if lifting the limits set by mw.unstrip.
  Note that "Special:Prefixindex:" is also costly, even if it just requires a single database query to
  get all subpages, instead of one costly #ifexist or one costly mw.title() property reading per
  tested subpage to know if it exists.
  For now there's still no reliable way to get a list of subpages, or performing queries similar to
  the [[Special:Prefixindex]] page or list members of a category like when viewing a category page.
  Ideally, there should exist a method for such queries on Title objects returned by the mw.title library;
  but for now there's none.
  In Lua now, the only expansion possible with an immediate effect is the expansion of standard templates,
  all special tags or special pages, or parser function extensions do not work (Only the #expr parser
  function is supported by using an external Scribunto library).
--]==]
        for pg in page.subpages( title, { ignoreNS=true } ) do
            if abort.cycles then
                abort.cycles = abort.cycles - 1
                if 0 == abort.cycles then return ac()  end
            end
            if abort.time then
                if (os.clock() - clock) > abort.time then return ac()  end
            end
            if mw.ustring.len( pg ) <= 12 then
                langcode = string.lower( pg );
                langname = fetch( langcode );
                if langname ~= '' then
                    table.insert( pages, transform( title, pg, langcode, langname ) );
                end
            end
        end
        return pages;
    end
    return {};
end
end


 
function cloneArgs(frame)
function Language:getDisplayForm()
    local args, pargs = {}, {}
return self:getCanonicalName()
    for k,v in pairs( frame.args ) do args[k] = v end
    if frame:getParent() then
        for k,v in pairs( frame:getParent().args ) do pargs[k] = v end
    end
    return args, pargs
end
end




function Language:getOtherNames(onlyOtherNames)
self:loadInExtraData()
return require("Module:language-like").getOtherNames(self, onlyOtherNames)
end


local p = {};


function Language:getAliases()
--[=[
self:loadInExtraData()
Usage:
return self._extraData.aliases or {}
{{#invoke:languages|internal|Template:Adjective}}
end
]=]
 
function p.internal(frame)
 
    return table.concat(
function Language:getVarieties(flatten)
        language_subpages( frame,
self:loadInExtraData()
            function( title, page, code, name )
return require("Module:language-like").getVarieties(self, flatten)
                return mw.ustring.format(
end
                    '<bdi class="language lang-%s" lang="%s">[[%s/%s|%s]]</bdi>',
 
                    code, code,
 
                    title, page,
function Language:getType()
                    name
return self._rawData.type or "regular"
                );
end
            end
 
        ),
 
        '&nbsp;<b>·</b>&#32;'
function Language:getWikimediaLanguages()
    );
if not self._wikimediaLanguageObjects then
local m_wikimedia_languages = require("Module:wikimedia languages")
self._wikimediaLanguageObjects = {}
local wikimedia_codes = self._rawData.wikimedia_codes or { self._code }
for _, wlangcode in ipairs(wikimedia_codes) do
table.insert(self._wikimediaLanguageObjects, m_wikimedia_languages.getByCode(wlangcode))
end
end
return self._wikimediaLanguageObjects
end
 
function Language:getWikipediaArticle()
if self._rawData.wikipedia_article then
return self._rawData.wikipedia_article
elseif self._wikipedia_article then
return self._wikipedia_article
elseif self:getWikidataItem() and mw.wikibase then
self._wikipedia_article = mw.wikibase.sitelink(self:getWikidataItem(), 'enwiki')
end
if not self._wikipedia_article then
self._wikipedia_article = mw.ustring.gsub(self:getCategoryName(), "Creole language", "Creole")
end
return self._wikipedia_article
end
 
function Language:makeWikipediaLink()
return "[[w:" .. self:getWikipediaArticle() .. "|" .. self:getCanonicalName() .. "]]"
end
end


function Language:getWikidataItem()
--[=[
local item = self._rawData[2]
Usage:
{{#invoke:languages|external|Template:Adjective}}
if type(item) == "number" then
]=]
return "Q" .. item
function p.external(frame)
else
    return table.concat(
return item
        language_subpages( frame,
end
            function( title, page, code, name )
                return mw.ustring.format(
                    '<bdi class="language lang-%s" lang="%s">[%s/%s %s]</bdi>',
                    code, code,
                    tostring( mw.uri.fullUrl( title ) ), page:gsub( ' ', '_' ),
                    name
                );
            end
        ),
        '&nbsp;<b>·</b>&#32;'
    );
end
end


function Language:getScripts()
--[=[
if not self._scriptObjects then
forEachLanguage
local m_scripts = require("Module:scripts")
self._scriptObjects = {}
for _, sc in ipairs(self:getScriptCodes()) do
table.insert(self._scriptObjects, m_scripts.getByCode(sc))
end
end
return self._scriptObjects
end


function Language:getScriptCodes()
This function iterates over all language codes known to MediaWiki based on a maintained list
return self._rawData.scripts or self._rawData[4] or { "None" }
replacing patterns in a pattern-string for each language
end


function Language:getFamily()
Usage
if self._familyObject then
{{#invoke:Languages|forEachLanguage
return self._familyObject
  |pattern=patternstring
end
  |before=string to insert before iteration
  |after=string added after iteration
local family = self._rawData[3] or self._rawData.family
  |sep=separator string between iterations
if family then
  |inLang=langcode used for $lnTrP and $lnTrUC1
self._familyObject = require("Module:families").getByCode(family)
}}
end
return self._familyObject
end


Parameters
    pattern: A pattern string which is processed for each language and which is concatenated at the end and returned as one string
    before: A string that is inserted before the concatenated result
    after: A string that is inserted after the concatenated result
    sep: A string that is inserted between each line created from the pattern while iterating (like ProcessedPattern_sep_ProcessedPattern_sep_ProcessedPattern)
    inLang: Langcode to use for $lnTrP and $lnTrUC1
    preprocess: if set to a non-empty value, the output will be preprocessed before being returned.


function Language:getAncestors()
Warning
if not self._ancestorObjects then
    The output is still not prepreprocessed by default: so parser functions and magic keywords generated by the pattern are still not executed and replaced,
self._ancestorObjects = {}
    and template transclusions are still not expanded (see examples in other functions in this module).
    When using this function directly from a MediaWiki page or template, this means it is only possible to use patterns generating basic MediaWiki formatting
if self._rawData.ancestors then
    or HTML tags. It you want the output to be preprocessed (in the given frame), set the preprocess parameter to a non-empty string.
for _, ancestor in ipairs(self._rawData.ancestors) do
   
table.insert(self._ancestorObjects, export.getByCode(ancestor) or require("Module:etymology languages").getByCode(ancestor))
Patterns
end
    $lc - language code such as en or de
else
    $lnP - language name in own language (autonym)
local fam = self:getFamily()
    $lnUC1 - language name in own language (autonym), first letter upper case
local protoLang = fam and fam:getProtoLanguage() or nil
    $lnTrP - language name translated to the language requested by language code passed to inLang
    $lnTrUC1 - language name translated to the language requested by language code passed to inLang, first letter upper case
-- For the case where the current language is the proto-language
-- of its family, we need to step up a level higher right from the start.
if protoLang and protoLang:getCode() == self:getCode() then
fam = fam:getFamily()
protoLang = fam and fam:getProtoLanguage() or nil
end
while not protoLang and not (not fam or fam:getCode() == "qfa-not") do
fam = fam:getFamily()
protoLang = fam and fam:getProtoLanguage() or nil
end
table.insert(self._ancestorObjects, protoLang)
end
end
return self._ancestorObjects
end


local function iterateOverAncestorTree(node, func)
Example
for _, ancestor in ipairs(node:getAncestors()) do
    {{#invoke:Languages|forEachLanguage|pattern=<span lang="$lc" xml:lang="$lc" class="language lang-$lc">[[Page/$lc|$lnP]]</span>}}
if ancestor then
]=]
local ret = func(ancestor) or iterateOverAncestorTree(ancestor, func)
if ret then
return ret
end
end
end
end


function Language:getAncestorChain()
-- =p.forEachLanguage({ args= { pattern = "$lc - $lnTrP\n", inLang = "en" } })
if not self._ancestorChain then
function p.forEachLanguage(frame)
self._ancestorChain = {}
    local l = require("Module:Languages/List")
local step = #self:getAncestors() == 1 and self:getAncestors()[1] or nil
while step do
table.insert(self._ancestorChain, 1, step)
step = #step:getAncestors() == 1 and step:getAncestors()[1] or nil
end
end
return self._ancestorChain
end


    local ret = {}
    local lang    = mw.language
    local line
    local pattern = frame.args.pattern  or frame.args[1] or ""
    local prefix  = frame.args.before    or frame.args[2] or ""
    local postfix = frame.args.after    or frame.args[3] or ""
    local sep    = frame.args.sep      or frame.args.separator or frame.args[4] or ""
    local inLang  = frame.args.inLang    or frame.args[5] or nil
    local preprocess = frame.args.preprocess or frame.args[6] or ""


function Language:hasAncestor(otherlang)
    local langNameUCFirstReq          = not not pattern:find( "$lnUC1", 1, true )
local function compare(ancestor)
    local langNameReq                  = not not pattern:find( "$lnP", 1, true ) or langNameUCFirstReq
return ancestor:getCode() == otherlang:getCode()
    local langNameTranslatedUCFirstReq = not not pattern:find( "$lnTrUC1", 1, true )
end
    local langNameTranslatedReq        = not not pattern:find( "$lnTrP", 1, true ) or langNameTranslatedUCFirstReq
    local contentLangInstance = mw.language.getContentLanguage()
return iterateOverAncestorTree(self, compare) or false
    local inLangLangInstance
end
    local l = mw.language.fetchLanguageNames() -- autonyms
    local lTr
    local lcIdList = require('Module:Languages/List').getSortedList( l )


    if langNameTranslatedReq then
        inLangLangInstance = --[==[
            mw.getLanguage( inLang ) -- Quota hit in :ucfirst() if using too many langInstances
            --]==] contentLangInstance
        lTr = mw.language.fetchLanguageNames( inLang ) -- translated names
    end


function Language:getCategoryName(nocap)
    for _, lcId in pairs( lcIdList ) do
local name = self:getCanonicalName()
        local subst = lcId:gsub('%%', '%%%%')
        line = pattern:gsub( "%$lc", subst )
-- If the name already has "language" in it, don't add it.
        local langName, langInstance
if not name:find("[Ll]anguage$") then
        -- autonym (name of lcId in locale lcId)
name = name .. " language"
        if langNameReq then
end
            langName = l[lcId]
if not nocap then
            subst = langName:gsub('%%', '%%%%')
name = mw.getContentLanguage():ucfirst(name)
            line = line:gsub( "%$lnP", subst )
end
        end
return name
        if langNameUCFirstReq then
end
            langInstance = --[==[
                mw.getLanguage( lcId ) -- Quota hit in :ucfirst() if using too many langInstances
                --]==] contentLangInstance
            langName = langInstance:ucfirst( langName )
            subst = langName:gsub('%%', '%%%%')
            line = line:gsub( "%$lnUC1", subst )
        end


        -- translated name (name of lcId in locale inLang)
        if langNameTranslatedReq then
            langName = lTr[lcId]
            subst = langName:gsub('%%', '%%%%')
            line = line:gsub( "%$lnTrP", subst )
        end
        if langNameTranslatedUCFirstReq then
            langName = inLangLangInstance:ucfirst( langName )
            subst = langName:gsub('%%', '%%%%')
            line = line:gsub( "%$lnTrUC1", subst )
        end


function Language:makeCategoryLink()
        table.insert(ret, line)
return "[[:Category:" .. self:getCategoryName() .. "|" .. self:getDisplayForm() .. "]]"
    end
    ret = prefix .. table.concat( ret, sep ) .. postfix
    if preprocess ~= '' then
        ret = frame:preprocess(ret)
    end
    return ret
end
end


 
--[=[
function Language:getStandardCharacters()
Provide logic for [[Template:Lle]] (Language Links external, to be substituted, language names written exactly as #language would provide them)
return self._rawData.standardChars
Warning: may expands too many costly #ifexist without limitation (if not substituted into a separate "/lang" template)
]=]
function p.lle(frame)
    return frame:preprocess(
        p.forEachLanguage({
            args = {
                pattern = '{{subst:#ifexist:{{{1}}}/$lc|[{{subst:fullurl:{{{1}}}/$lc}} <bdi class="language lang-$lc" lang="$lc">$lnP</bdi>]&nbsp;<b>∙</b>&#32;<!--\n-->}}'
            }
        })
    )
end
end


 
--[=[
function Language:makeEntryName(text)
Provide logic for [[Template:Ll]] (Language Links internal, to be substituted, language names written exactly as #language would provide them)
text = mw.ustring.match(text, "^[¿¡]?(.-[^%s%p].-)%s*[؟?!;՛՜ ՞ ՟?!︖︕।॥။၊་།]?$") or text
Warning: may expands too many costly #ifexist without limitation (if not substituted into a separate "/lang" template)
]=]
if self:getCode() == "ar" then
function p.ll(frame)
local U = mw.ustring.char
    return frame:preprocess(
local taTwiil = U(0x640)
        p.forEachLanguage({
local waSla = U(0x671)
            args = {
-- diacritics ordinarily removed by entry_name replacements
                pattern = '{{subst:#ifexist:{{{1}}}/$lc|[[{{{1}}}/$lc|<bdi class="language lang-$lc" lang="$lc">$lnP</bdi>]]&nbsp;<b>∙</b>&#32;<!--\n-->}}'
local Arabic_diacritics = U(0x64B, 0x64C, 0x64D, 0x64E, 0x64F, 0x650, 0x651, 0x652, 0x670)
            }
        })
if text == waSla or mw.ustring.find(text, "^" .. taTwiil .. "?[" .. Arabic_diacritics .. "]" .. "$") then
    )
return text
end
end
if type(self._rawData.entry_name) == "table" then
text = do_entry_name_or_sort_key_replacements(text, self._rawData.entry_name)
end
return text
end
end




-- Return true if the language has display processing enabled, i.e. lang:makeDisplayText()
--------------------------------------------------------
-- does non-trivial processing.
--- Different approaches for [[Template:Lang links]] ---
function Language:hasDisplayProcessing()
--------------------------------------------------------
return not not self._rawData.display
end


--[=[
Provide logic for [[Template:Lang links]]
Using a cute Hybrid-Method:
    First check the subpages which is quite fast; if there are too many fall back to checking for each language page individually
]=]


-- Apply display-text replacements to `text`, if any.
-- =p.langLinksNonExpensive({ args= { page='Commons:Picture of the Year/2010' }, getParent=function() end })
function Language:makeDisplayText(text)
-- =p.langLinksNonExpensive({ args= { page='Main Page' }, getParent=function() end })
if type(self._rawData.display) == "table" then
-- =p.langLinksNonExpensive({ args= { page='Template:No_source_since' }, getParent=function() end })
text = do_entry_name_or_sort_key_replacements(text, self._rawData.display)
-- =p.langLinksNonExpensive({ args= { page='MediaWiki:Gadget-HotCat' }, getParent=function() end })
end
function p.langLinksNonExpensive(frame)
    local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
return text
    local title = args.page or args[1] or pargs.page or pargs[1] or "";
    local contentLangInstance = mw.language.getContentLanguage();
    local pages2
    if frame.preprocess == nil then
        frame = mw.getCurrentFrame()
    end
--[==[
    local options = {
        abort = {
            time = 3.5,
            on = function()
                pages2 = p.forEachLanguage({
                    args = {
                      pattern = '{{#ifexist:' .. title .. '/$lc|[[' .. title .. '/$lc|<bdi lang="$lc">$lnP</bdi>]]&nbsp;<b>∙</b>&#32;}}'
                    }
                })
            end
        }
    }
    local pages = language_subpages( frame,
        function( title, page, code, langname )
            return mw.ustring.format(
                '[[%s/%s|<bdi lang="%s">%s</bdi>]]</span>&nbsp;<b>∙</b>&#32;',
                title, page, code, langname
            )
        end, options );
    return pages2 and frame:preprocess(pages2) or table.concat(pages, '');
--]==]
    return frame:preprocess(
        p.forEachLanguage( {
            args = {
                pattern = '{{#ifexist:' .. title .. '/$lc|[[' .. title .. '/$lc|<bdi lang="$lc">$lnP</bdi>]]&nbsp;<b>∙</b>&#32;}}'
            }
        })
    )
end
end


---------------------------------------------------------
----------------- [[Template:Autolang]] -----------------
---------------------------------------------------------
--[[
  Works like {{autotranslate}} just allowing an unlimited number of arguments, even named arguments.
  It's doing Magic! No arguments should be passed to {{#invoke:}}
]]


-- Add to data tables?
function p.autolang(frame)
local has_dotted_undotted_i = {
    local args, pargs = cloneArgs( frame )
["az"] = true,
    if nil == args.useargs then
["crh"] = true,
        if not args.base then args = pargs end
["gag"] = true,
    elseif 'both' == args.useargs then
["kaa"] = true,
        for k,v in pairs(args) do pargs[k] = v end
["tt"] = true,
        args = pargs
["tr"] = true,
    elseif 'parent' == args.useargs then
["zza"] = true,
        args = pargs
}
        if pargs.base and not args.base then
 
            args.base = pargs.base
function Language:makeSortKey(name, sc)
        end
if has_dotted_undotted_i[self:getCode()] then
    end
name = name:gsub("I", "ı")
    local base = args.base
end
    local userlang = frame:preprocess( '{{Int:Lang}}' )
    local tl, tlns = 'Template:', 10
name = mw.ustring.lower(name)
    local tlb, fallback1, currenttemplate
    local fallback, contentlang = mw.text.split( userlang, '-', true )[1], mw.language.getContentLanguage():getCode()
-- Remove initial hyphens and *
local hyphens_regex = "^[-־ـ*]+(.)"
name = mw.ustring.gsub(name, hyphens_regex, "%1")
-- If there are language-specific rules to generate the key, use those
if type(self._rawData.sort_key) == "table" then
name = do_entry_name_or_sort_key_replacements(name, self._rawData.sort_key)
elseif type(self._rawData.sort_key) == "string" then
name = require("Module:" .. self._rawData.sort_key).makeSortKey(name, self:getCode(), sc and sc:getCode())
end
-- Remove parentheses, as long as they are either preceded or followed by something
name = mw.ustring.gsub(name, "(.)[()]+", "%1")
name = mw.ustring.gsub(name, "[()]+(.)", "%1")
if has_dotted_undotted_i[self:getCode()] then
name = name:gsub("i", "İ")
end
return mw.ustring.upper(name)
end


function Language:overrideManualTranslit()
    local createReturn = function(title)
if self._rawData.override_translit then
        local ret
return true
        local tlargs = {}
else
        -- When LUA is invoked, templates are already expanded. This must be respected.
return false
        return frame:expandTemplate{ title = title, args = args }
end
    end
end


    if not base then
        return ("'autolang' in [[Module:Languages]] was called but the 'base' parameter could not be found." ..
            "The base parameter specifies the template that's subpages will be sought for a suitable translation.")
    end
    tlb = tl .. base .. '/'


function Language:transliterate(text, sc, module_override)
    currenttemplate = tlb .. userlang
if not ((module_override or self._rawData.translit_module) and text) then
    local ok, exists = pcall( function()
return nil
        return mw.title.new( currenttemplate, tlns ).exists
end
    end )
    if ok and exists then
if module_override then
        return createReturn(currenttemplate)
require("Module:debug").track("module_override")
    end
end
return require("Module:" .. (module_override or self._rawData.translit_module)).tr(text, self:getCode(), sc and sc:getCode() or nil)
end


function Language:hasTranslit()
    fallback1 = frame:preprocess( '{{Fallback|1=' .. base .. '|2=' .. userlang .. '}}' )
return self._rawData.translit_module and true or false
    if fallback1 ~= contentlang then
end
        return createReturn(tlb .. fallback1)
    end


    currenttemplate = tlb .. fallback
    local ok, exists = pcall( function()
        return mw.title.new( currenttemplate, tlns ).exists
    end )
    if ok and exists then
        return createReturn(currenttemplate)
    end


function Language:link_tr()
    currenttemplate = tlb .. contentlang
return self._rawData.link_tr and true or false
    local ok, exists = pcall( function()
        return mw.title.new( currenttemplate, tlns ).exists
    end )
    if ok and exists then
        return createReturn(currenttemplate)
    end
    return createReturn(tl .. base)
end
end


 
--[=[
function Language:toJSON()
Usage:
local entryNamePatterns = nil
{{#invoke:languages|isKnownLanguageTag|gsw}} -> 1
local entryNameRemoveDiacritics = nil
{{#invoke:languages|isKnownLanguageTag|doesNotExist}} ->
]=]
if self._rawData.entry_name then
function p.isKnownLanguageTag(frame)
entryNameRemoveDiacritics = self._rawData.entry_name.remove_diacritics
    return mw.language.isKnownLanguageTag( frame.args[1] or frame.args.tag or frame.args.code or '' ) and '1' or ''
if self._rawData.entry_name.from then
entryNamePatterns = {}
for i, from in ipairs(self._rawData.entry_name.from) do
local to = self._rawData.entry_name.to[i] or ""
table.insert(entryNamePatterns, { from = from, to = to })
end
end
end
local ret = {
ancestors = self._rawData.ancestors,
canonicalName = self:getCanonicalName(),
categoryName = self:getCategoryName("nocap"),
code = self._code,
entryNamePatterns = entryNamePatterns,
entryNameRemoveDiacritics = entryNameRemoveDiacritics,
family = self._rawData[3] or self._rawData.family,
otherNames = self:getOtherNames(true),
aliases = self:getAliases(),
varieties = self:getVarieties(),
scripts = self._rawData.scripts or self._rawData[4],
type = self:getType(),
wikimediaLanguages = self._rawData.wikimedia_codes,
wikidataItem = self:getWikidataItem(),
}
return require("Module:JSON").toJSON(ret)
end
 
 
-- Do NOT use these methods!
-- All uses should be pre-approved on the talk page!
function Language:getRawData()
return self._rawData
end
 
function Language:getRawExtraData()
self:loadInExtraData()
return self._extraData
end
 
Language.__index = Language
 
 
function export.getDataModuleName(code)
if code:find("^%l%l$") then
return "languages/data2"
elseif code:find("^%l%l%l$") then
local prefix = code:sub(1, 1)
return "languages/data3/" .. prefix
elseif code:find("^[%l-]+$") then
return "languages/datax"
else
return nil
end
end
 
 
function export.getExtraDataModuleName(code)
if code:find("^%l%l$") then
return "languages/extradata2"
elseif code:find("^%l%l%l$") then
local prefix = code:sub(1, 1)
return "languages/extradata3/" .. prefix
elseif code:find("^[%l-]+$") then
return "languages/extradatax"
else
return nil
end
end
 
 
local function getRawLanguageData(code)
local modulename = export.getDataModuleName(code)
return modulename and mw.loadData("Module:" .. modulename)[code] or nil
end
 
 
local function getRawExtraLanguageData(code)
local modulename = export.getExtraDataModuleName(code)
return modulename and mw.loadData("Module:" .. modulename)[code] or nil
end
 
 
function Language:loadInExtraData()
if not self._extraData then
-- load extra data from module and assign to meta table
-- use empty table as a fallback if extra data is nil
local meta = getmetatable(self)
meta._extraData = getRawExtraLanguageData(self._code) or {}
setmetatable(self, meta)
end
end
 
 
function export.makeObject(code, data)
if data and data.deprecated then
require("Module:debug").track {
"languages/deprecated",
"languages/deprecated/" .. code
}
end
return data and setmetatable({ _rawData = data, _code = code }, Language) or nil
end
 
 
function export.getByCode(code, paramForError, allowEtymLang, allowFamily)
if type(code) ~= "string" then
error("The function getByCode expects a string as its first argument, but received " .. (code == nil and "nil" or "a " .. type(code)) .. ".")
end
local retval = export.makeObject(code, getRawLanguageData(code))
if not retval and allowEtymLang then
retval = require("Module:etymology languages").getByCode(code)
end
if not retval and allowFamily then
retval = require("Module:families").getByCode(code)
end
if not retval and paramForError then
require("Module:languages/errorGetBy").code(code, paramForError, allowEtymLang, allowFamily)
end
return retval
end
 
 
function export.getByName(name, errorIfInvalid)
local byName = mw.loadData("Module:languages/by name")
local code = byName.all and byName.all[name] or byName[name]
if not code then
if errorIfInvalid then
error("The language name \"" .. name .. "\" is not valid. See [[Wiktionary:List of languages]].")
else
return nil
end
end
return export.makeObject(code, getRawLanguageData(code))
end
 
function export.getByCanonicalName(name, errorIfInvalid, allowEtymLang, allowFamily)
local byName = mw.loadData("Module:languages/canonical names")
local code = byName and byName[name]
 
local retval = code and export.makeObject(code, getRawLanguageData(code)) or nil
if not retval and allowEtymLang then
retval = require("Module:etymology languages").getByCanonicalName(name)
end
if not retval and allowFamily then
local famname = name:match("^(.*) languages$")
famname = famname or name
retval = require("Module:families").getByCanonicalName(famname)
end
if not retval and errorIfInvalid then
require("Module:languages/errorGetBy").canonicalName(name, allowEtymLang, allowFamily)
end
return retval
end
end


--[[ If language is an etymology language, iterates through parent languages
function p.file_languages(frame)
until it finds a non-etymology language. ]]
    local M_link = require( 'Module:Link' )
function export.getNonEtymological(lang)
    local contentLangInstance = mw.language.getContentLanguage()
while lang:getType() == "etymology language" do
    local pattern = frame.args.pattern or '%s (%s)'
local parentCode = lang:getParentCode()
    local original = frame.args.original or mw.title.getCurrentTitle().text
lang = export.getByCode(parentCode)
    local ext_start, _ = string.find( original, '\.%w+$' )
or require("Module:etymology languages").getByCode(parentCode)
    local file_ext = string.sub( original, ext_start )
or require("Module:families").getByCode(parentCode)
    original = string.sub( original, 0, ext_start - 1 )
end
    return frame:preprocess(
        '<gallery>\n' ..
return lang
        (table.concat(
            M_link.forEachLink(
                p.forEachLanguage({
                    args = { pattern = '[[$lc]]' }
                }),
                function( linkInfo )
                    local filename = mw.ustring.format( pattern, original, linkInfo.text ) .. file_ext
                    local ok, exists = pcall( function()
                            return mw.title.new( filename, 6 ).exists
                        end )
                    if ok and exists then
                        return mw.ustring.format( '%s|%s',
                            filename,
                            mw.language.fetchLanguageName( linkInfo.text )
                        )
                    else
                        return nil
                    end
                end
            ), '\n'
        )) ..
        '\n</gallery>'
    )
end
end


-- for backwards compatibility only; modules should require the /error themselves
function p.runTests()
function export.err(lang_code, param, code_desc, template_tag, not_real_lang)
    return p.langLinksNonExpensive({
return require("Module:languages/error")(lang_code, param, code_desc, template_tag, not_real_lang)
        args = {
            page = 'Module:Languages/testcases/test'
        },
        getParent = function() end
    }) ==
        '[[Module:Languages/testcases/test/de|<bdi lang="de">Deutsch</bdi>]]&nbsp;<b>∙</b>&#32;' ..
        '[[Module:Languages/testcases/test/en|<bdi lang="en">English</bdi>]]&nbsp;<b>∙</b>&#32;'
end
end


return export
return p;

Revision as of 17:47, 3 March 2022

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

--[=[
Not globally exposed. Internal function only.

language_subpages( frame, transform, options )
Parameters
    frame:     The frame that was passed to the method invoked. The first argument or the page argument will be respected.
    transform: A transform function. Example: function( basepagename, subpagename, code, langname ) end
    options:   An object with options. Example: { abort= { on=function() end, time=0.8 }  }
        Following options are available:
        abort: Aborts iterating over the subpages if one of the conditions is met. If the process is aborted, nil is returned!
            on: Function to be called if an abort-condition was met.
            cycles: The maximum number of subpages to run over.
            time: Maximum time to spend running over the subpages.

]=]
function language_subpages( frame, transform, options )
    local args, pargs, options = frame.args, ( frame:getParent() or {} ).args or {}, options or {};
    local title = args.page or args[1] or pargs.page or pargs[1] or "";
    local abort = options.abort or {};
    local at, clock = type( abort.on ), os.clock();
    local ac = function()
        if  at == 'function' or ( at == 'table' and getmetatable(abort.on).__call ) then
            abort.on();
        end
    end
    local tt = type( transform );
    local page = require( 'Module:Page' );

    title = page.clean(title);

    if tt == 'function' or ( tt == 'table' and getmetatable(transform).__call ) then
        local fetch, pages, langcode, langname = mw.language.fetchLanguageName, {};
--[==[

     / \
    / | \
   /  ·  \
   ¯¯¯¯¯¯¯
   Page.subpages() no longer works because it attempted to parse the HTML content generated by
   calling the parser function "Special:Prefixindex:" which is no longer expanded in Lua but
   converted to a "stripped tag" (containing a unique identifier surrounded by ASCII DEL characters)
   representing the tag name and its parameters.
   The actual expansion of stripped tags can no longer be performed in Lua.
   Now unstripping these tags just kills ALL these tags (except "wiki" tags) instead of performing
   their expansion by running the extension code. Only MediaWiki can unstrip these tags in texts after
   they have been returned by Lua.
   For this reason, page.subpages() is now completely empty (Module:Page no longer works).
   This cannot be bypassed, except by using a Scribunto extension library if lifting the limits set by mw.unstrip.
   Note that "Special:Prefixindex:" is also costly, even if it just requires a single database query to
   get all subpages, instead of one costly #ifexist or one costly mw.title() property reading per
   tested subpage to know if it exists.
   For now there's still no reliable way to get a list of subpages, or performing queries similar to
   the [[Special:Prefixindex]] page or list members of a category like when viewing a category page.
   Ideally, there should exist a method for such queries on Title objects returned by the mw.title library;
   but for now there's none.
   In Lua now, the only expansion possible with an immediate effect is the expansion of standard templates,
   all special tags or special pages, or parser function extensions do not work (Only the #expr parser
   function is supported by using an external Scribunto library).
--]==]
        for pg in page.subpages( title, { ignoreNS=true } ) do
            if abort.cycles then
                abort.cycles = abort.cycles - 1
                if 0 == abort.cycles then return ac()  end
            end
            if abort.time then
                if (os.clock() - clock) > abort.time then return ac()  end
            end
            if mw.ustring.len( pg ) <= 12 then
                langcode = string.lower( pg );
                langname = fetch( langcode );
                if langname ~= '' then
                    table.insert( pages, transform( title, pg, langcode, langname ) );
                end
            end
        end
        return pages;
    end
    return {};
end

function cloneArgs(frame)
    local args, pargs = {}, {}
    for k,v in pairs( frame.args ) do args[k] = v end
    if frame:getParent() then
        for k,v in pairs( frame:getParent().args ) do pargs[k] = v end
    end
    return args, pargs
end



local p = {};

--[=[
Usage:
{{#invoke:languages|internal|Template:Adjective}}
]=]
function p.internal(frame)
    return table.concat(
        language_subpages( frame,
            function( title, page, code, name )
                return mw.ustring.format(
                    '<bdi class="language lang-%s" lang="%s">[[%s/%s|%s]]</bdi>',
                    code, code,
                    title, page,
                    name
                );
            end
        ),
        '&nbsp;<b>·</b>&#32;'
    );
end

--[=[
Usage:
{{#invoke:languages|external|Template:Adjective}}
]=]
function p.external(frame)
    return table.concat(
        language_subpages( frame,
            function( title, page, code, name )
                return mw.ustring.format(
                    '<bdi class="language lang-%s" lang="%s">[%s/%s %s]</bdi>',
                    code, code,
                    tostring( mw.uri.fullUrl( title ) ), page:gsub( ' ', '_' ),
                    name
                );
            end
        ),
        '&nbsp;<b>·</b>&#32;'
    );
end

--[=[
forEachLanguage

This function iterates over all language codes known to MediaWiki based on a maintained list
replacing patterns in a pattern-string for each language

Usage
{{#invoke:Languages|forEachLanguage
  |pattern=patternstring
  |before=string to insert before iteration
  |after=string added after iteration
  |sep=separator string between iterations
  |inLang=langcode used for $lnTrP and $lnTrUC1
}}

Parameters
    pattern: A pattern string which is processed for each language and which is concatenated at the end and returned as one string
    before: A string that is inserted before the concatenated result
    after: A string that is inserted after the concatenated result
    sep: A string that is inserted between each line created from the pattern while iterating (like ProcessedPattern_sep_ProcessedPattern_sep_ProcessedPattern)
    inLang: Langcode to use for $lnTrP and $lnTrUC1
    preprocess: if set to a non-empty value, the output will be preprocessed before being returned.

Warning
    The output is still not prepreprocessed by default: so parser functions and magic keywords generated by the pattern are still not executed and replaced,
    and template transclusions are still not expanded (see examples in other functions in this module).
    When using this function directly from a MediaWiki page or template, this means it is only possible to use patterns generating basic MediaWiki formatting
    or HTML tags. It you want the output to be preprocessed (in the given frame), set the preprocess parameter to a non-empty string.
    
Patterns
    $lc - language code such as en or de
    $lnP - language name in own language (autonym)
    $lnUC1 - language name in own language (autonym), first letter upper case
    $lnTrP - language name translated to the language requested by language code passed to inLang
    $lnTrUC1 - language name translated to the language requested by language code passed to inLang, first letter upper case

Example
    {{#invoke:Languages|forEachLanguage|pattern=<span lang="$lc" xml:lang="$lc" class="language lang-$lc">[[Page/$lc|$lnP]]</span>}}
]=]

-- =p.forEachLanguage({ args= { pattern = "$lc - $lnTrP\n", inLang = "en" } })
function p.forEachLanguage(frame)
    local l = require("Module:Languages/List")

    local ret = {}
    local lang    = mw.language
    local line
    local pattern = frame.args.pattern   or frame.args[1] or ""
    local prefix  = frame.args.before    or frame.args[2] or ""
    local postfix = frame.args.after     or frame.args[3] or ""
    local sep     = frame.args.sep       or frame.args.separator or frame.args[4] or ""
    local inLang  = frame.args.inLang    or frame.args[5] or nil
    local preprocess = frame.args.preprocess or frame.args[6] or ""

    local langNameUCFirstReq           = not not pattern:find( "$lnUC1", 1, true )
    local langNameReq                  = not not pattern:find( "$lnP", 1, true ) or langNameUCFirstReq
    local langNameTranslatedUCFirstReq = not not pattern:find( "$lnTrUC1", 1, true )
    local langNameTranslatedReq        = not not pattern:find( "$lnTrP", 1, true ) or langNameTranslatedUCFirstReq
    local contentLangInstance = mw.language.getContentLanguage()
    local inLangLangInstance
    local l = mw.language.fetchLanguageNames() -- autonyms
    local lTr
    local lcIdList = require('Module:Languages/List').getSortedList( l )

    if langNameTranslatedReq then
        inLangLangInstance = --[==[
            mw.getLanguage( inLang ) -- Quota hit in :ucfirst() if using too many langInstances
            --]==] contentLangInstance
        lTr = mw.language.fetchLanguageNames( inLang ) -- translated names
    end

    for _, lcId in pairs( lcIdList ) do
        local subst = lcId:gsub('%%', '%%%%')
        line = pattern:gsub( "%$lc", subst )
        local langName, langInstance
        -- autonym (name of lcId in locale lcId)
        if langNameReq then
            langName = l[lcId]
            subst = langName:gsub('%%', '%%%%')
            line = line:gsub( "%$lnP", subst )
        end
        if langNameUCFirstReq then
            langInstance = --[==[
                mw.getLanguage( lcId ) -- Quota hit in :ucfirst() if using too many langInstances
                --]==] contentLangInstance
            langName = langInstance:ucfirst( langName )
            subst = langName:gsub('%%', '%%%%')
            line = line:gsub( "%$lnUC1", subst )
        end

        -- translated name (name of lcId in locale inLang)
        if langNameTranslatedReq then
            langName = lTr[lcId]
            subst = langName:gsub('%%', '%%%%')
            line = line:gsub( "%$lnTrP", subst )
        end
        if langNameTranslatedUCFirstReq then
            langName = inLangLangInstance:ucfirst( langName )
            subst = langName:gsub('%%', '%%%%')
            line = line:gsub( "%$lnTrUC1", subst )
        end

        table.insert(ret, line)
    end
    ret = prefix .. table.concat( ret, sep ) .. postfix
    if preprocess ~= '' then
        ret = frame:preprocess(ret)
    end
    return ret
end

--[=[
 Provide logic for [[Template:Lle]] (Language Links external, to be substituted, language names written exactly as #language would provide them)
 Warning: may expands too many costly #ifexist without limitation (if not substituted into a separate "/lang" template)
]=]
function p.lle(frame)
    return frame:preprocess(
        p.forEachLanguage({
            args = {
                pattern = '{{subst:#ifexist:{{{1}}}/$lc|[{{subst:fullurl:{{{1}}}/$lc}} <bdi class="language lang-$lc" lang="$lc">$lnP</bdi>]&nbsp;<b>∙</b>&#32;<!--\n-->}}'
            }
        })
    )
end

--[=[
 Provide logic for [[Template:Ll]] (Language Links internal, to be substituted, language names written exactly as #language would provide them)
 Warning: may expands too many costly #ifexist without limitation (if not substituted into a separate "/lang" template)
]=]
function p.ll(frame)
    return frame:preprocess(
        p.forEachLanguage({
            args = {
                pattern = '{{subst:#ifexist:{{{1}}}/$lc|[[{{{1}}}/$lc|<bdi class="language lang-$lc" lang="$lc">$lnP</bdi>]]&nbsp;<b>∙</b>&#32;<!--\n-->}}'
            }
        })
    )
end


--------------------------------------------------------
--- Different approaches for [[Template:Lang links]] ---
--------------------------------------------------------

--[=[
 Provide logic for [[Template:Lang links]]
 Using a cute Hybrid-Method:
    First check the subpages which is quite fast; if there are too many fall back to checking for each language page individually
]=]

-- =p.langLinksNonExpensive({ args= { page='Commons:Picture of the Year/2010' }, getParent=function() end })
-- =p.langLinksNonExpensive({ args= { page='Main Page' }, getParent=function() end })
-- =p.langLinksNonExpensive({ args= { page='Template:No_source_since' }, getParent=function() end })
-- =p.langLinksNonExpensive({ args= { page='MediaWiki:Gadget-HotCat' }, getParent=function() end })
function p.langLinksNonExpensive(frame)
    local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
    local title = args.page or args[1] or pargs.page or pargs[1] or "";
    local contentLangInstance = mw.language.getContentLanguage();
    local pages2
    if frame.preprocess == nil then
        frame = mw.getCurrentFrame()
    end
--[==[
    local options = {
        abort = {
            time = 3.5,
            on = function()
                pages2 = p.forEachLanguage({
                    args = {
                       pattern = '{{#ifexist:' .. title .. '/$lc|[[' .. title .. '/$lc|<bdi lang="$lc">$lnP</bdi>]]&nbsp;<b>∙</b>&#32;}}'
                    }
                })
            end
        }
    }
    local pages = language_subpages( frame,
        function( title, page, code, langname )
            return mw.ustring.format(
                '[[%s/%s|<bdi lang="%s">%s</bdi>]]</span>&nbsp;<b>∙</b>&#32;',
                title, page, code, langname
            )
        end, options );
    return pages2 and frame:preprocess(pages2) or table.concat(pages, '');
--]==]
    return frame:preprocess(
        p.forEachLanguage( {
            args = {
                pattern = '{{#ifexist:' .. title .. '/$lc|[[' .. title .. '/$lc|<bdi lang="$lc">$lnP</bdi>]]&nbsp;<b>∙</b>&#32;}}'
            }
        })
    )
end

---------------------------------------------------------
----------------- [[Template:Autolang]] -----------------
---------------------------------------------------------
--[[
  Works like {{autotranslate}} just allowing an unlimited number of arguments, even named arguments.
  It's doing Magic! No arguments should be passed to {{#invoke:}}
]]

function p.autolang(frame)
    local args, pargs = cloneArgs( frame )
    if nil == args.useargs then
        if not args.base then args = pargs end
    elseif 'both' == args.useargs then
        for k,v in pairs(args) do pargs[k] = v end
        args = pargs
    elseif 'parent' == args.useargs then
        args = pargs
        if pargs.base and not args.base then
            args.base = pargs.base
        end
    end
    local base = args.base
    local userlang = frame:preprocess( '{{Int:Lang}}' )
    local tl, tlns = 'Template:', 10
    local tlb, fallback1, currenttemplate
    local fallback, contentlang = mw.text.split( userlang, '-', true )[1], mw.language.getContentLanguage():getCode()

    local createReturn = function(title)
        local ret
        local tlargs = {}
         -- When LUA is invoked, templates are already expanded. This must be respected.
        return frame:expandTemplate{ title = title, args = args }
    end

    if not base then
        return ("'autolang' in [[Module:Languages]] was called but the 'base' parameter could not be found." ..
            "The base parameter specifies the template that's subpages will be sought for a suitable translation.")
    end
    tlb = tl .. base .. '/'

    currenttemplate = tlb .. userlang
    local ok, exists = pcall( function()
        return mw.title.new( currenttemplate, tlns ).exists
    end )
    if ok and exists then
        return createReturn(currenttemplate)
    end

    fallback1 = frame:preprocess( '{{Fallback|1=' .. base .. '|2=' .. userlang .. '}}' )
    if fallback1 ~= contentlang then
        return createReturn(tlb .. fallback1)
    end

    currenttemplate = tlb .. fallback
    local ok, exists = pcall( function()
        return mw.title.new( currenttemplate, tlns ).exists
    end )
    if ok and exists then
        return createReturn(currenttemplate)
    end

    currenttemplate = tlb .. contentlang
    local ok, exists = pcall( function()
        return mw.title.new( currenttemplate, tlns ).exists
    end )
    if ok and exists then
        return createReturn(currenttemplate)
    end
    return createReturn(tl .. base)
end

--[=[
Usage:
{{#invoke:languages|isKnownLanguageTag|gsw}} -> 1
{{#invoke:languages|isKnownLanguageTag|doesNotExist}} ->
]=]
function p.isKnownLanguageTag(frame)
    return mw.language.isKnownLanguageTag( frame.args[1] or frame.args.tag or frame.args.code or '' ) and '1' or ''
end

function p.file_languages(frame)
    local M_link = require( 'Module:Link' )
    local contentLangInstance = mw.language.getContentLanguage()
    local pattern = frame.args.pattern or '%s (%s)'
    local original = frame.args.original or mw.title.getCurrentTitle().text
    local ext_start, _ = string.find( original, '\.%w+$' )
    local file_ext = string.sub( original, ext_start )
    original = string.sub( original, 0, ext_start - 1 )
    return frame:preprocess(
        '<gallery>\n' ..
        (table.concat(
            M_link.forEachLink(
                p.forEachLanguage({
                    args = { pattern = '[[$lc]]' }
                }),
                function( linkInfo )
                    local filename = mw.ustring.format( pattern, original, linkInfo.text ) .. file_ext
                    local ok, exists = pcall( function()
                            return mw.title.new( filename, 6 ).exists
                        end )
                    if ok and exists then
                        return mw.ustring.format( '%s|%s',
                            filename,
                            mw.language.fetchLanguageName( linkInfo.text )
                        )
                    else
                        return nil
                    end
                end
            ), '\n'
        )) ..
        '\n</gallery>'
    )
end

function p.runTests()
    return p.langLinksNonExpensive({
        args = {
            page = 'Module:Languages/testcases/test'
        },
        getParent = function() end
    }) ==
        '[[Module:Languages/testcases/test/de|<bdi lang="de">Deutsch</bdi>]]&nbsp;<b>∙</b>&#32;' ..
        '[[Module:Languages/testcases/test/en|<bdi lang="en">English</bdi>]]&nbsp;<b>∙</b>&#32;'
end

return p;