Wikipedia:Lua/Modul/ISO3166/en

Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation
Weiterleitung der Diskussionsseite fehlt

ISO3166 – Module for conversion of territory code from ISO 3166 into one of the most current ones.

Historic countries are difficult to retrieve, since code assignment started about 1970 and they were partially reused for other targets later. Division of states does not point to current country, but unification. Only codes known to or reserved by ISO are covered, while other organisations like IOC often collide.

Functions for templates Werkeln

f
Core functionality: convert code
Parameters:
  • 1 – code to be converted (mandatory)
  • 2 – requested format of result
    • A2 or A3 or NUM or A4
    • Default: A3
  • 3 – redirect into other territory
    • 1 – historic state
    • Default: 0 – current country
failsafe
Version ID (local): 2019-10-14
Optional parameter:
  • 1 – Minimum version or wikidata or ~
{{#invoke:ISO3166|failsafe}} yields 2019-10-14
With a parameter as ISO date it is compared whether the current module satisfies this version or later.
  • {{#invoke:ISO3166|failsafe|2001-01-01}} returns: »2019-10-14«
  • {{#invoke:ISO3166|failsafe|2099-01-01}} returns: »« – (empty), if minimal version condition not matched
  • If keyword is wikidata, result is version ID registered on Wikidata (2019-10-14) or local if not found there.
  • If keyword is ~, result is empty if local and Wikidata version are the same: ().
Returns:
  • Empty, if minimal version condition not matched, or ~ and local version is synchronized.
  • Version ID if no parameter, or if wikidata, or local if not found there, or not synchronized.

Examples and test page Werkeln

There are tests available to illustrate this in practice.

Use in another Lua module Werkeln

All of the above functions can be called from other Lua modules. Use require(); the following code checks for errors loading it:

local lucky, ISO3166 = pcall( require, "Module:ISO3166" )
if type( ISO3166 ) == "table" then
    ISO3166 = ISO3166.ISO3166()
else
    -- In the event of errors, ISO3166 is an error message.
    return "<span class=\"error\">" .. ISO3166 .. "</span>"
end

Then there is available:

ISO3166.f( access, alter, ancient )
  1. access – code to be converted, string
  2. alter – requested format of result, string, optional, A3
  3. ancient – do not redirect into other territory, boolean, optional, false
returns: Code as string or false if not recognized
ISO3166.fetch( alert )
(internal) Retrieval of mw.loadData(), extend library object
  1. alert – true for abort by error()
    • Default: false – empty table
ISO3166.failsafe( atleast )
  1. atleast
    optional
    nil or minimum version or wikidata or ~ for synchronisation
returns: Version ID as string or false

Internal sub modules Werkeln

/loadData Werkeln

Global data will be converted into a single mw.loadData() table once per rendered page if needed.

  • Yields object with components:
    • codes – map A2 → space separated sequence
    • reverse – map anything → A2
    • failsafe – serial ID

/maintain Werkeln

Internal – code used as generator of commons:Data:ISO3166/reverse.tab data.

ISO3166.factory()
returns: JSON code, string
ISO3166.failsafe( atleast )

Supports template invocations:

Global data Werkeln

commons:Data:ISO3166/codes.tab
Assignment of A2 code on space separated list of related codes of the analogous territory.
In the list the most significant A2 A3 NUM A4 code is to be mentioned first.
Locally available as mw.loadData()-Module:ISO3166/loadData
commons:Data:ISO3166/reverse.tab
Assignment of all codes to the main A2 code.
Assignment of the same A2 code does mean that this code is valid.
Locally available as mw.loadData()-Module:ISO3166/loadData
Generated by /maintain.

Data is centralized and immediately updated, available to all wikis.

Maintainenance Werkeln

When changing an entry in commons:Data:ISO3166/codes.tab updating of commons:Data:ISO3166/reverse.tab is required by Test/maintain.

Installation on other WMF projects Werkeln

Follow the steps:

  1. Copy main module Module:ISO3166 into your project.
    • If possible keep the name ISO3166.
    • If another name is required due to conflict or naming convention or non-latin script then choose a different one.
  2. Register this module at d:Q69799761.
  3. Copy the following sub module, and keep the chosen root name:
  4. Ready.
  5. Consider translation of doc page.

This is a general library; use it anywhere.

Dependencies Werkeln

None.