Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Example

{{#invoke:MainPages|maybe_link_to_localised_mainpage}}
See also: Template:Lang-mp.
<div class="hlist">
{{#invoke:MainPages|list_link_all}}
</div>

Code

local dir = require('Module:Dir')

-- Detects unintended global variables
require('strict')

-- Format:
--
-- [key]: Lowercase MediaWiki language code.
-- title: (Required, string) Page title in this language.
-- outdated: (Optional, boolean) Set to true if this link should be greyed out
--           in the list of all main page links from [[Template:Lang-pm]].
--           To instead hide a link, use "--" to disable the entry.
--
local mainpages = {
  -- mo is needless, because Romanian page exists ["mo"] = { title = "Паджина принчипалэ", outdated = true, },
  ["af"] = { title = "Tuisblad", },
  ["als"] = { title = "Houptsyte", },
  ["an"] = { title = "Portalada", },
  ["ang"] = { title = "Hēafodtramet", },
  ["ar"] = { title = "الصفحة الرئيسية", },
  ["ast"] = { title = "Entamu", },
  ["av"] = { title = "БетӀераб гьумер", },
  ["bar"] = { title = "Hauptseiten", },
  ["be"] = { title = "Першая старонка", },
  ["be-tarask"] = { title = "Галоўная старонка", },
  ["bg"] = { title = "Начална страница", },
  ["bh"] = { title = "मुख्य पन्ना", },
  ["bjn"] = { title = "Tungkaran Tatambaian", },
  ["bn"] = { title = "প্রধান পাতা", },
  ["br"] = { title = "Degemer", },
  ["bs"] = { title = "Početna strana", },
  ["ca"] = { title = "Pàgina principal", },
  ["cdo"] = { title = "Tàu-hiĕk", },
  ["ckb"] = { title = "دەستپێک", },
  ["cs"] = { title = "Hlavní strana", },
  ["cy"] = { title = "Hafan", },
  ["da"] = { title = "Forside", },
  ["de"] = { title = "Hauptseite", },
  ["diq"] = { title = "Pela Seri", },
  ["el"] = { title = "Αρχική σελίδα", },
  ["en"] = { title = "Main Page", },
  ["eo"] = { title = "Ĉefpaĝo", },
  ["es"] = { title = "Portada", },
  ["et"] = { title = "Esileht", },
  ["eu"] = { title = "Azala", },
  ["ext"] = { title = "Páhina prencipal", },
  ["fa"] = { title = "صفحهٔ اصلی", },
  ["fi"] = { title = "Etusivu", },
  ["fo"] = { title = "Forsíða/fo", },
  ["fr"] = { title = "Accueil", },
  ["fy"] = { title = "Haadside", },
  ["ga"] = { title = "Príomhleathanach", },
  ["gl"] = { title = "Portada galega", },
  ["grc"] = { title = "Κυρία Δέλτος", },
  ["gu"] = { title = "મુખપૃષ્ઠ", },
  ["hak"] = { title = "Thèu-ya̍p", },
  ["he"] = { title = "עמוד ראשי", },
  ["hi"] = { title = "मुखपृष्ठ (hi)", },
  ["hr"] = { title = "Glavna stranica", },
  ["hsb"] = { title = "Hłowna strona", },
  ["hu"] = { title = "Kezdőlap", },
  ["hy"] = { title = "Գլխավոր Էջ", },
  ["ia"] = { title = "Pagina principal", },
  ["id"] = { title = "Halaman Utama", },
  ["is"] = { title = "Forsíða/is", },
  ["it"] = { title = "Pagina principale", },
  ["ja"] = { title = "メインページ", },
  ["jv"] = { title = "Kaca Utama", },
  ["ka"] = { title = "მთავარი გვერდი", },
  ["kk"] = { title = "Басты бет", },
  ["km"] = { title = "ទំព័រដើម", },
  ["kn"] = { title = "ಮುಖ್ಯ ಪುಟ", },
  ["ko"] = { title = "대문", },
  ["ko-kp"] = { title = "조선말", },
  ["ku"] = { title = "Destpêk", },
  ["la"] = { title = "Pagina prima", },
  ["lb"] = { title = "Haaptsäit", },
  ["lt"] = { title = "Pagrindinis puslapis", },
  ["lv"] = { title = "Sākumlapa", },
  ["mg"] = { title = "Fandraisana", },
  ["mk"] = { title = "Главна страница", },
  ["ml"] = { title = "പ്രധാന താൾ", },
  ["mr"] = { title = "मुखपृष्ठ (mr)", },
  ["ms"] = { title = "Laman Utama", },
  ["mt"] = { title = "Il-Paġna prinċipali", },
  ["mwl"] = { title = "Páigina percipal", },
  ["my"] = { title = "ဗဟိုစာမျက်နှာ", },
  ["mzn"] = { title = "گت ولگ", },
  ["nah"] = { title = "Calīxatl", },
  ["nap"] = { title = "Paggena prencepale", },
  ["nds"] = { title = "Hööftsiet", },
  ["nds-nl"] = { title = "Vöärblad", },
  ["ne"] = { title = "गृह पृष्ठ", },
  ["nl"] = { title = "Hoofdpagina", },
  ["nn"] = { title = "Hovudside", },
  ["no"] = { title = "Hovedside", },
  ["oc"] = { title = "Acuèlh", },
  ["or"] = { title = "ପ୍ରଧାନ ପୃଷ୍ଠା", },
  ["os"] = { title = "Сæйраг фарс", },
  ["pa"] = { title = "ਮੁੱਖ ਸਫ਼ਾ", },
  ["pam"] = { title = "Pamuklat", },
  ["pcd"] = { title = "Pache picarte", },
  ["pl"] = { title = "Strona główna", },
  ["ps"] = { title = "لومړی مخ", },
  ["pt"] = { title = "Página principal", },
  ["qu"] = { title = "Qhapaq p'anqa", },
  ["ro"] = { title = "Pagina principală", },
  ["ru"] = { title = "Заглавная страница", },
  ["sa"] = { title = "मुख्यपृष्ठम्", },
  ["sc"] = { title = "Pàgina printzipale", },
  ["scn"] = { title = "Pàggina principali", },
  ["sco"] = { title = "Scots Commons", },
  ["sh"] = { title = "Glavna stranica (sh)", },
  ["si"] = { title = "මුල් පිටුව", },
  ["simple"] = { title = "Simple English", },
  ["sk"] = { title = "Hlavná stránka", },
  ["sl"] = { title = "Glavna stran", },
  ["so"] = { title = "Bogga Hore", },
  ["sq"] = { title = "Faqja kryesore", },
  ["sr"] = { title = "Главна страна", },
  ["stq"] = { title = "Haudsiede Commons", },
  ["su"] = { title = "Tepas", },
  ["sv"] = { title = "Huvudsida", },
  ["szl"] = { title = "Przodniŏ zajta", },
  ["ta"] = { title = "முதற் பக்கம்", },
  ["tg"] = { title = "Саҳифаи Аслӣ", },
  ["th"] = { title = "หน้าหลัก", },
  ["tl"] = { title = "Unang Pahina", },
  ["tr"] = { title = "Anasayfa", },
  ["udm"] = { title = "Кутскон бам", },
  ["uk"] = { title = "Головна сторінка", },
  ["ur"] = { title = "صفحۂ_اول", },
  ["vec"] = { title = "Pajina prinsipałe", },
  ["vi"] = { title = "Trang Chính", },
  ["vo"] = { title = "Cifapad", },
  ["war"] = { title = "Syahan nga Pakli", },
  ["yi"] = { title = "ערשטע זײַט", },
  ["yo"] = { title = "Ojúewé Àkọ́kọ́", },
  ["yue"] = { title = "頭版", },
  ["zh"] = { title = "Thâu-ia̍h", },
  ["zh"] = { title = "首页", },
  ["zh-classical"] = { title = "卷首", },
}

-- Functions for {{#invoke}}

local p = {}

-- Link to all main pages.
--
-- Used in [[Template:Lang-mp]].
--
function p.list_link_all( frame )
  local wikitext = ""
  -- TODO: Use instead:
  -- local autonyms = mw.language.fetchLanguageNames()
  for langcode, mp in pairs( mainpages ) do
    -- {{#language: .. }}
    local autonym = frame:callParserFunction( '#language', langcode )
    local langdir = dir.select( langcode, 'rtl', 'ltr' )

    local link = '<bdi>'
      .. '<span lang="' .. langcode .. '" dir="' .. langdir .. '" class="description ' .. langcode .. '" style="white-space: nowrap;">'
      .. '[[' .. mp.title .. '|'
        .. ( mp.outdated and '<span style="color: gray;">' or '' )
        .. autonym
        .. ( mp.outdated and '</span>' or '' )
      .. ']]'
      .. '</span></bdi>'

    wikitext = wikitext .. '* ' .. link .. '\n'
  end

  return wikitext
end

-- When a user is viewing the default [[Main Page]] but their preferences,
-- or anonymous uselang parameter, show a link to to a localised main page
-- if we can find one for their language.
--
-- This considers fallback chains as well. It renders nothing if the most suitable
-- destination is already the page they are on.
--
-- Used via [[Template:Main Page Template]].
--
-- In the years prior to 2021, this logic was performed after the fact with
-- JavaScript from [[MediaWiki:MainPages.js]]. That logic stopped working and
-- instead of fixing that slow and layout-shifting approach, it was re-created
-- here. The current limitation is that this considers only the language the user
-- has given us via the language selector. It does work for logged-out users, but
-- does not work automatically for example with the browser language if the user
-- has not previously choosen to tell us.
--
function p.maybe_link_to_localised_mainpage( frame )
  -- Get relevant Main Page destination for the current user's interface language
  -- Similar to {{int:Lang}} and [[MediaWiki:Lang/*]],
  -- but for [[MediaWiki:Mainpage/*]].
  --
  -- We can't use LangSwitch or subpage queries because for most languages we do not
  -- actually have a [[MediaWiki:Mainpage]] subpage. We only create those for
  -- languages where the page name differs from the default (e.g. due to an ambiguity).
  -- Also, by using "int" it is much faster, and has the benefit of supporting
  -- fallback chains automatically (e.g. en-gb => en).
  local dest = frame:callParserFunction( 'int', 'Mainpage' )
  local destTitle = mw.title.new( dest )
  local templateTitle = mw.title.new( 'Template:Main Page Template' )
  if destTitle == nil
    or mw.title.equals( mw.title.getCurrentTitle(), destTitle )
    or mw.title.equals( mw.title.getCurrentTitle(), templateTitle )
    or not destTitle.exists
  then
    -- Invalid title.
    -- or, We're already there.
    -- or, The destination doesn't exist.
    return ''
  end

  return '<div style="margin: 0.5em 0; font-size: larger; font-weight: bold;">→ '
    .. '[[' .. dest .. ']]'
    .. '</div>'
end

return p