Wikipedia:Lua/Modul/WLink/en

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

WLink – Module with functions for strings in context of wikilinks and external links (URL).

Functions for templates

[Quelltext bearbeiten]

Most functions expect exactly one unnamed parameter (which should be provided to get a meaningful answer). Whitespace ahead and after content is ignored. HTML Entities would not disturb syntax and might be resolved. The parameter might contain single or double bracketed links which would be extracted if appropriate.

The return value is an empty string (“nothing”), if the parameter value does not fulfil the expectations. If there is a result or the query condition is true, at least one visible character will be returned. The result does not begin or end with a space.

ansiPercent
Convert string by ANSI encoding rather than UTF-8 encoding
Required for some 20th century servers
Optional parameter space – encoding of spaces:
  • space=+ – common for query components
  • space=_ – as for Wikis
  • Default: %20
formatURL
Create bracketed link, if not yet, from URL or domain
  • [http://example.org/about Homepage] yields [http://example.org/about Homepage]
  • http://example.org/about yields [http://example.org/about example.org/about]
  • example.org yields [http://example.org/ example.org]
getArticleBase
Retrieve generic page title, no fragment nor brackets
Use current page title, if omitted.
getBaseTitle
Retrieve last segment in subpage, no fragment
getEscapedTitle
Retrieve link title with visible square brackets and pipe [|] and enable source line breaks, but avoid <nowiki> for other wikisyntax.
getExtension
Retrieve media extension
Result is downcased (without leading dot).
getFile
Retrieve media page identifier
getFragment
Retrieve original (not decoded) fragment string after #
getLanguage
Retrieve language identifier
getLinktextProblem
Which problem has this presumable link text?
] → square brackets; \n → line break; <wiki> → illegal HTML or wikisyntax elements (block); ISBN PMID RFC
getNamespace
Retrieve namespace number
getNamespaced
Retrieve page name in namespace
Two mandatory parameters:
  1. Any kind of namespace specification
  2. Page title or prefixed page name or link to page
getPlain
Retrieve text with all links replaced by link titles
getProject
Retrieve project identifier within wikifarm (recommendable brief notation)
getTalkPage
Retrieve talk page, if permitted, or a talk page itself
getTarget
Retrieve first target (wikilink or URL)
getTargetPage
Retrieve first target page (page name or URL of page)
Same as getTarget if no fragment.
getTitle
Retrieve first link title (wikilink or URL), or wikilink target
getWeblink
Retrieve bracketed link from resource URL
getWikilink
Retrieve robust normalized internal link (in brackets)
  1. Page name or text with internal link
  2. Different link text (optional)
isBracketedLink
Does attempt match a bracketed link?
isBracketedURL
Does attempt match a bracketed URL?
isCategorization
Does attempt match a categorization?
isExternalLink
Does attempt match an external link?
isInterlanguage
Does attempt match an interlanguage link?
isInterwiki
Does attempt match an interwiki link?
isMedia
Does attempt match a media translusion?
isTalkPage
Does attempt match a talk page?
isTitledLink
Does attempt match a titled link?
isValidLink
Does attempt match a link?
isValidLinktext
Is attempt permitted as link text, does not contain other links or other structures?
isWeblink
Does attempt match an external link?
isWikilink
Does attempt match a wikilink?
pageLink
Make robust link on a wiki page, not beginning with *;:#.
failsafe
Version ID (local): 2022-05-09
Optional parameter:
  • 1 – Minimum version or wikidata or ~
{{#invoke:WLink|failsafe}} yields 2022-05-09
With a parameter as ISO date it is compared whether the current module satisfies this version or later.
  • {{#invoke:WLink|failsafe|2001-01-01}} returns: »2022-05-09«
  • {{#invoke:WLink|failsafe|2099-01-01}} returns: »« – (empty), if minimal version condition not matched
  • If keyword is wikidata, result is version ID registered on Wikidata (2022-05-09) 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 (test page)

[Quelltext bearbeiten]

A test page illustrates practical use.

Functions for Lua modules (API)

[Quelltext bearbeiten]

All functions described above can be used by other modules:

local lucky, WLink = pcall( require, "Module:WLink" )
if type( WLink ) == "table" then
    WLink = WLink.WLink()
else
    -- failure; WLink is the error message
    return "<span class=\"error\">" .. WLink .. "</span>"
end

Subsequently there are available:

  • WLink.ansiPercent(story, space)
  • WLink.formatURL()
  • WLink.getArticleBase()
  • WLink.getBaseTitle()
  • WLink.getExtension()
  • WLink.getFile()
  • WLink.getFragment()
    false, if not found; but empty string if empty fragment.
    No leading # in result.
  • WLink.getLanguage()
  • WLink.getLinktextProblem()
  • WLink.getNamespace()
  • WLink.getNamespaced(area,at)
    1. area – string or number
    2. at – string
  • WLink.getPlain()
  • WLink.getProject()
  • WLink.getTalkPage()
  • WLink.getTarget()
  • WLink.getTargetPage()
  • WLink.getTitle()
  • WLink.getWeblink()
  • WLink.getWikilink()
  • WLink.isBracketedLink()
  • WLink.isBracketedURL()
  • WLink.isCategorization()
  • WLink.isExternalLink()
  • WLink.isInterlanguage()
  • WLink.isInterwiki()
  • WLink.isMedia()
  • WLink.isTalkPage()
  • WLink.isTitledLink()
  • WLink.isValidLink()
  • WLink.isValidLinktext()
  • WLink.isWeblink()
  • WLink.isWikilink()
  • WLink.pageLink(attempt, appear, assure)
    Yields robust internal link to a page
    • attempt – presumable internal link; string oder table as of WLink.wikilink()
    • appear(optional) link text string oder true
    • assure(optional) method to protect against template syntax: URL or WIKI
  • WLink.pageTarget()
    Yields string with robust normalized internal link target
  • WLink.wikilink()
    Yields table with wikilink components, else false.
    Components might be, if provided:
    • lead – leading colon : present and required, if true
    • project – project interwiki within wikifarm (recommendable brief notation)
    • lang – known language version (downcased)
    • ns – number of namespace
    • space – local canonical name of namespace
    • title – page title as provided; at least empty string
  • WLink.failsafe(atleast)
Versioning interface
  1. atleast
    optional
    nil or minimum version or wikidata or ~ for synchronisation
returns: Version ID as string or false

If succeeding, the WLink.get*() return a string, the WLink.is*() true (if no exception mentioned); on failure always false.

Installation on other WMF projects

[Quelltext bearbeiten]

Follow the steps:

  1. Copy main module Module:WLink into your project.
    • If possible keep the name WLink.
    • 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:Q19363224.
  3. URLutil should be available; otherwise related functions will not work.
  4. Ready.
  5. Consider translation of doc page.

General library; no limitations.

Dependencies

[Quelltext bearbeiten]