Difference between revisions of "Module:Languages"

From Multilingual Bookbinding Dictionary
Jump to navigation Jump to search
(Created page with "local export = {} --[=[ This function checks for things that could plausibly be a language code: two or three lowercase letters, two or three groups of three lowercase le...")
 
Template>Verdy p
Line 1: Line 1:
local export = {}
+
--[=[
 +
Not globally exposed. Internal function only.
  
--[=[ This function checks for things that could plausibly be a language code:
+
language_subpages( frame, transform, options )
two or three lowercase letters, two or three groups of three lowercase
+
Parameters
letters with hyphens between them. If such a pattern is not found,
+
    frame:    The frame that was passed to the method invoked. The first argument or the page argument will be respected.
it is likely the editor simply forgot to enter a language code. ]=]
+
    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 export.err(langCode, param, text)
+
]=]
local ordinals = { "first", "second", "third", "fourth" }
+
function language_subpages( frame, transform, options )
+
    local args, pargs, options = frame.args, ( frame:getParent() or {} ).args or {}, options or {};
local paramType = type(param)
+
    local title = args.page or args[1] or pargs.page or pargs[1] or "";
if paramType == "number" then
+
    local abort = options.abort or {};
ordinal = ordinals[param]
+
    local at, clock = type( abort.on ), os.clock();
param = ordinal .. ' parameter'
+
    local ac = function()
elseif paramType == "string" then
+
        if  at == 'function' or ( at == 'table' and getmetatable(abort.on).__call ) then
param = 'parameter "' .. param .. '"'
+
            abort.on();
else
+
        end
error("The parameter name is "
+
    end
.. (paramType == "table" and "a table" or tostring(param))
+
    local tt = type( transform );
.. ", but it should be a number or a string.")
+
    local page = require( 'Module:Page' );
end
 
 
-- Can use string.find because language codes only contain ASCII.
 
if not langCode or langCode == "" then
 
error("The " .. param .. " (" .. (text or "language code") .. ") is missing.", 2)
 
elseif langCode:find("^%l%l%l?$")
 
or langCode:find("^%l%l%l%-%l%l%l$")
 
or langCode:find("^%l%l%l%-%l%l%l%-%l%l%l$") then
 
error("The language code \"" .. langCode .. "\" is not valid.", 2)
 
else
 
error("Please enter a " .. (text or "language code") .. " in the " .. param .. ".", 2)
 
end
 
end
 
 
 
local Language = {}
 
 
 
function Language:getCode()
 
return self._code
 
end
 
 
 
 
 
function Language:getCanonicalName()
 
return self._rawData[1] or self._rawData.canonicalName
 
end
 
  
 +
    title = page.clean(title);
  
function Language:getOtherNames()
+
    if tt == 'function' or ( tt == 'table' and getmetatable(transform).__call ) then
return self._rawData.otherNames or {}
+
        local fetch, pages, langcode, langname = mw.language.fetchLanguageName, {};
end
+
--[==[
 
 
 
 
function Language:getType()
 
return self._rawData.type or "regular"
 
end
 
 
 
 
 
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
 
 
 
function Language:getWikidataItem()
 
return self._rawData[2] or self._rawData.wikidata_item
 
end
 
  
function Language:getScripts()
+
    / \
if not self._scriptObjects then
+
    / | \
local m_scripts = require("Module:scripts")
+
  /  ·  \
self._scriptObjects = {}
+
  ¯¯¯¯¯¯¯
+
  Page.subpages() no longer works because it attempted to parse the HTML content generated by
for _, sc in ipairs(self._rawData.scripts or { "None" }) do
+
  calling the parser function "Special:Prefixindex:" which is no longer expanded in Lua but
table.insert(self._scriptObjects, m_scripts.getByCode(sc))
+
  converted to a "stripped tag" (containing a unique identifier surrounded by ASCII DEL characters)
end
+
  representing the tag name and its parameters.
end
+
  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
return self._scriptObjects
+
  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 Language:getScriptCodes()
+
function cloneArgs(frame)
return self._rawData.scripts or { "None" }
+
    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
 
end
  
function Language:getFamily()
 
if self._familyObject then
 
return self._familyObject
 
end
 
 
local family = self._rawData[3] or self._rawData.family
 
if family then
 
self._familyObject = require("Module:families").getByCode(family)
 
end
 
 
return self._familyObject
 
end
 
  
  
function Language:getAncestors()
+
local p = {};
if not self._ancestorObjects then
 
self._ancestorObjects = {}
 
 
if self._rawData.ancestors then
 
for _, ancestor in ipairs(self._rawData.ancestors) do
 
table.insert(self._ancestorObjects, export.getByCode(ancestor) or require("Module:etymology languages").getByCode(ancestor))
 
end
 
else
 
local fam = self:getFamily()
 
local protoLang = fam and fam:getProtoLanguage() or nil
 
 
-- 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)
+
--[=[
for _, ancestor in ipairs(node:getAncestors()) do
+
Usage:
if ancestor then
+
{{#invoke:languages|internal|Template:Adjective}}
local ret = func(ancestor) or iterateOverAncestorTree(ancestor, func)
+
]=]
if ret then
+
function p.internal(frame)
return ret
+
    return table.concat(
end
+
        language_subpages( frame,
end
+
            function( title, page, code, name )
end
+
                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
 
end
  
function Language:getAncestorChain()
+
--[=[
if not self._ancestorChain then
+
Usage:
self._ancestorChain = {}
+
{{#invoke:languages|external|Template:Adjective}}
local step = #self:getAncestors() == 1 and self:getAncestors()[1] or nil
+
]=]
+
function p.external(frame)
while step do
+
    return table.concat(
table.insert(self._ancestorChain, 1, step)
+
        language_subpages( frame,
step = #step:getAncestors() == 1 and step:getAncestors()[1] or nil
+
            function( title, page, code, name )
end
+
                return mw.ustring.format(
end
+
                    '<bdi class="language lang-%s" lang="%s">[%s/%s %s]</bdi>',
+
                    code, code,
return self._ancestorChain
+
                    tostring( mw.uri.fullUrl( title ) ), page:gsub( ' ', '_' ),
 +
                    name
 +
                );
 +
            end
 +
        ),
 +
        '&nbsp;<b>·</b>&#32;'
 +
    );
 
end
 
end
  
 +
--[=[
 +
forEachLanguage
  
function Language:hasAncestor(otherlang)
+
This function iterates over all language codes known to MediaWiki based on a maintained list
local function compare(ancestor)
+
replacing patterns in a pattern-string for each language
return ancestor:getCode() == otherlang:getCode()
 
end
 
 
return iterateOverAncestorTree(self, compare) or false
 
end
 
 
 
  
function Language:getCategoryName()
+
Usage
local name = self:getCanonicalName()
+
{{#invoke:Languages|forEachLanguage
+
  |pattern=patternstring
-- If the name already has "language" in it, don't add it.
+
  |before=string to insert before iteration
if name:find("[Ll]anguage$") then
+
  |after=string added after iteration
return name
+
  |sep=separator string between iterations
else
+
  |inLang=langcode used for $lnTrP and $lnTrUC1
return name .. " language"
+
}}
end
 
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:getStandardCharacters()
+
Warning
return self._rawData.standardChars
+
    The output is still not prepreprocessed by default: so parser functions and magic keywords generated by the pattern are still not executed and replaced,
end
+
    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>}}
 +
]=]
  
function Language:makeEntryName(text)
+
-- =p.forEachLanguage({ args= { pattern = "$lc - $lnTrP\n", inLang = "en" } })
text = mw.ustring.gsub(text, "^[¿¡]", "")
+
function p.forEachLanguage(frame)
text = mw.ustring.gsub(text, "(.)[؟?!;՛՜ ՞ ՟?!︖︕।॥။၊་།]$", "%1")
+
    local l = require("Module:Languages/List")
 
if self:getCode() == "ar" then
 
local U = mw.ustring.char
 
local taTwiil = U(0x640)
 
local waSla = U(0x671)
 
-- diacritics ordinarily removed by entry_name replacements
 
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
 
for i, from in ipairs(self._rawData.entry_name.from) do
 
local to = self._rawData.entry_name.to[i] or ""
 
text = mw.ustring.gsub(text, from, to)
 
end
 
end
 
 
--[=[ For instance, ᾰ (alpha-breve) + combining smooth breathing is converted
 
to alpha + combining smooth breathing by the entry_name replacements.
 
It must be re-combined to alpha-smooth breathing (ἀ) so that
 
allowSelfLink in [[Module:links]] will work properly. ]=]
 
if self:getCode() == "grc" then
 
text = mw.ustring.toNFC(text)
 
end
 
 
return text
 
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 ""
  
-- Add to data tables?
+
    local langNameUCFirstReq          = not not pattern:find( "$lnUC1", 1, true )
local has_dotted_undotted_i = {
+
    local langNameReq                  = not not pattern:find( "$lnP", 1, true ) or langNameUCFirstReq
["az"] = true,
+
    local langNameTranslatedUCFirstReq = not not pattern:find( "$lnTrUC1", 1, true )
["crh"] = true,
+
    local langNameTranslatedReq        = not not pattern:find( "$lnTrP", 1, true ) or langNameTranslatedUCFirstReq
["gag"] = true,
+
    local contentLangInstance = mw.language.getContentLanguage()
["kaa"] = true,
+
    local inLangLangInstance
["tt"] = true,
+
    local l = mw.language.fetchLanguageNames() -- autonyms
["tr"] = true,
+
    local lTr
["zza"] = true,
+
    local lcIdList = require('Module:Languages/List').getSortedList( l )
}
 
  
function Language:makeSortKey(name, sc)
+
    if langNameTranslatedReq then
if has_dotted_undotted_i[self:getCode()] then
+
        inLangLangInstance = --[==[
name = mw.ustring.gsub(name, "I", "ı")
+
            mw.getLanguage( inLang ) -- Quota hit in :ucfirst() if using too many langInstances
end
+
            --]==] contentLangInstance
+
        lTr = mw.language.fetchLanguageNames( inLang ) -- translated names
name = mw.ustring.lower(name)
+
    end
 
-- 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
 
for i, from in ipairs(self._rawData.sort_key.from) do
 
local to = self._rawData.sort_key.to[i] or ""
 
name = mw.ustring.gsub(name, from, to)
 
end
 
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 = mw.ustring.gsub(name, "i", "İ")
 
end
 
 
return mw.ustring.upper(name)
 
end
 
  
function Language:overrideManualTranslit()
+
    for _, lcId in pairs( lcIdList ) do
if self._rawData.override_translit then
+
        local subst = lcId:gsub('%%', '%%%%')
return true
+
        line = pattern:gsub( "%$lc", subst )
else
+
        local langName, langInstance
return false
+
        -- autonym (name of lcId in locale lcId)
end
+
        if langNameReq then
end
+
            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
  
function Language:transliterate(text, sc, module_override)
+
        table.insert(ret, line)
if not ((module_override or self._rawData.translit_module) and text) then
+
    end
return nil
+
    ret = prefix .. table.concat( ret, sep ) .. postfix
end
+
    if preprocess ~= '' then
+
        ret = frame:preprocess(ret)
if module_override then
+
    end
require("Module:debug").track("module_override")
+
    return ret
end
 
 
return require("Module:" .. (module_override or self._rawData.translit_module)).tr(text, self:getCode(), sc and sc:getCode() or nil)
 
 
end
 
end
  
function Language:hasTranslit()
+
--[=[
return self._rawData.translit_module and true or false
+
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
 
end
  
 
+
--[=[
function Language:link_tr()
+
Provide logic for [[Template:Ll]] (Language Links internal, to be substituted, language names written exactly as #language would provide them)
return self._rawData.link_tr and true or false
+
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
 
end
  
  
function Language:toJSON()
+
--------------------------------------------------------
local entryNamePatterns = nil
+
--- Different approaches for [[Template:Lang links]] ---
+
--------------------------------------------------------
if self._rawData.entry_name 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
 
 
local ret = {
 
ancestors = self._rawData.ancestors,
 
canonicalName = self:getCanonicalName(),
 
categoryName = self:getCategoryName(),
 
code = self._code,
 
entryNamePatterns = entryNamePatterns,
 
family = self._rawData[3] or self._rawData.family,
 
otherNames = self:getOtherNames(),
 
scripts = self._rawData.scripts,
 
type = self:getType(),
 
wikimediaLanguages = self._rawData.wikimedia_codes,
 
wikidataItem = self:getWikidataItem(),
 
}
 
 
return require("Module:JSON").toJSON(ret)
 
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
 +
]=]
  
-- Do NOT use this method!
+
-- =p.langLinksNonExpensive({ args= { page='Commons:Picture of the Year/2010' }, getParent=function() end })
-- All uses should be pre-approved on the talk page!
+
-- =p.langLinksNonExpensive({ args= { page='Main Page' }, getParent=function() end })
function Language:getRawData()
+
-- =p.langLinksNonExpensive({ args= { page='Template:No_source_since' }, getParent=function() end })
return self._rawData
+
-- =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
 
end
  
Language.__index = Language
+
---------------------------------------------------------
 +
----------------- [[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()
  
function export.getDataModuleName(code)
+
    local createReturn = function(title)
if code:find("^%l%l$") then
+
        local ret
return "languages/data2"
+
        local tlargs = {}
elseif code:find("^%l%l%l$") then
+
        -- When LUA is invoked, templates are already expanded. This must be respected.
local prefix = code:sub(1, 1)
+
        return frame:expandTemplate{ title = title, args = args }
return "languages/data3/" .. prefix
+
    end
elseif code:find("^[%l-]+$") then
 
return "languages/datax"
 
else
 
return nil
 
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 .. '/'
  
local function getRawLanguageData(code)
+
    currenttemplate = tlb .. userlang
local modulename = export.getDataModuleName(code)
+
    local ok, exists = pcall( function()
return modulename and mw.loadData("Module:" .. modulename)[code] or nil
+
        return mw.title.new( currenttemplate, tlns ).exists
end
+
    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
  
function export.makeObject(code, data)
+
    currenttemplate = tlb .. fallback
if data and data.deprecated then
+
    local ok, exists = pcall( function()
require("Module:debug").track {
+
        return mw.title.new( currenttemplate, tlns ).exists
"languages/deprecated",
+
    end )
"languages/deprecated/" .. code
+
    if ok and exists then
}
+
        return createReturn(currenttemplate)
end
+
    end
 
return data and setmetatable({ _rawData = data, _code = code }, Language) or nil
 
end
 
  
 
+
    currenttemplate = tlb .. contentlang
function export.getByCode(code)
+
    local ok, exists = pcall( function()
if type(code) ~= "string" then
+
        return mw.title.new( currenttemplate, tlns ).exists
error("The function getByCode expects a string as its first argument, but received " .. (code == nil and "nil" or "a " .. type(code)) .. ".")
+
    end )
end
+
    if ok and exists then
+
        return createReturn(currenttemplate)
return export.makeObject(code, getRawLanguageData(code))
+
    end
 +
    return createReturn(tl .. base)
 
end
 
end
  
 
+
--[=[
function export.getByName(name)
+
Usage:
local byName = mw.loadData("Module:languages/by name")
+
{{#invoke:languages|isKnownLanguageTag|gsw}} -> 1
local code = byName.all and byName.all[name] or byName[name]
+
{{#invoke:languages|isKnownLanguageTag|doesNotExist}} ->
+
]=]
if not code then
+
function p.isKnownLanguageTag(frame)
return nil
+
    return mw.language.isKnownLanguageTag( frame.args[1] or frame.args.tag or frame.args.code or '' ) and '1' or ''
end
 
 
return export.makeObject(code, getRawLanguageData(code))
 
 
end
 
end
  
 
+
function p.file_languages(frame)
function export.getByCanonicalName(name)
+
    local M_link = require( 'Module:Link' )
local byName = mw.loadData("Module:languages/canonical names")
+
    local contentLangInstance = mw.language.getContentLanguage()
local code = byName and byName[name]
+
    local pattern = frame.args.pattern or '%s (%s)'
+
    local original = frame.args.original or mw.title.getCurrentTitle().text
if not code then
+
    local ext_start, _ = string.find( original, '\.%w+$' )
return nil
+
    local file_ext = string.sub( original, ext_start )
end
+
    original = string.sub( original, 0, ext_start - 1 )
+
    return frame:preprocess(
return export.makeObject(code, getRawLanguageData(code))
+
        '<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
 
end
  
 
+
function p.runTests()
function export.iterateAll()
+
    return p.langLinksNonExpensive({
mw.incrementExpensiveFunctionCount()
+
        args = {
local m_data = mw.loadData("Module:languages/alldata")
+
            page = 'Module:Languages/testcases/test'
local func, t, var = pairs(m_data)
+
        },
+
        getParent = function() end
return function()
+
    }) ==
local code, data = func(t, var)
+
        '[[Module:Languages/testcases/test/de|<bdi lang="de">Deutsch</bdi>]]&nbsp;<b>∙</b>&#32;' ..
return export.makeObject(code, data)
+
        '[[Module:Languages/testcases/test/en|<bdi lang="en">English</bdi>]]&nbsp;<b>∙</b>&#32;'
end
 
 
end
 
end
  
return export
+
return p;

Revision as of 19:57, 2 May 2020

Suzy is working on this.


--[=[
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;