Jump to content

Extension:Scribunto/Victor's API proposal

From mediawiki.org


MediaWiki Lua API

[edit]

The aim of Scribunto Lua in-script API is to provide the scripts an interface to certain features of MediaWiki software which are written in PHP and most of which are not feasible to implement in Lua. The first-priority target is to provide access to all those interfaces which were previously exposed to parser as magic words and parser functions.

Here we try to keep to the table arguments convention whenever it is feasible. func{a, b, c} means that you should invoke function as func{a = "whatever", b = "argument", c = "trolling"}.

Whenever a property is defined, it is either read-only ([ro]), write-only ([wo]) or read-write ([rw]).

Provided interfaces

[edit]

All interfaces are the part mw package.

  • mw.lang β€” internationalization-related functions.
  • mw.page β€” interface to provide information about the current page (title, etc) and do direct manipulations with it.
  • mw.query β€” functions which require database queries in order to work. The total amount of calls to those functions is limited; the limit is shared with parser's expensive function count.
  • mw.site β€” functions which provide the information about the site.
  • mw.time β€” functions which provide interfaces for time manipulations.
  • mw.Title β€” provides an object which represents title
  • mw.text β€” functions which are used to handle the wikitext.
  • mw.url β€” functions which provide access to URL-related functions.

Data structures

[edit]

The time is passed in a following structure, which extends over Lua's standard date/time structure:

  • Standard members:
    • year
    • month
    • day
    • hour
    • min
    • sec
    • wday β€” week day (Monday is 1)
    • yday β€” day of the year
  • Extensions:
    • monthname β€” localized month name
    • timezone β€” the timezone in which timestamp is supplied

The revision information is passed in the following structure:

  • id
  • author
  • timestamp (always UTC)

The titles have a special object, which is described in an individual section below.

Detailed interface description

[edit]

mw.lang

[edit]
  • mw.lang.contentLanguage [ro] β€” the language code of the content language, i.e. the main language of the wiki.
  • mw.lang.UILanguage [ro] β€” the language code of the UI language, i.e. language in which user has his interface now.
  • mw.lang.languageName{code[, language]} β€” returns the language name of language with code. If language is not specified, return in the language itself.
  • mw.lang.message(messageName, ...) β€” formats the message and returns it.
  • mw.lang.plural(number, form1, form2...) β€” similar to {{plural:number|form1|form2|...|}}.
  • mw.lang.formatNumber(number) β€” formats the number according to the language conventions.
  • mw.lang.gender(username, ...) β€” picks the right version of the string depending on the user gender.
  • mw.lang.specialPageName(page) β€” returns the localized name of a given special page.

mw.page

[edit]
  • mw.page.title [ro] β€” returns the title structure
  • mw.page.currentRevision [ro] β€” returns the revision structure
  • mw.page.defaultSort [wo] β€” similar to {{DEFAULTSORT}}
  • mw.page.displayTitle [wo] β€” similar to {{DISPLAYTITLE}}

mw.query

[edit]

The query module has different configurable limit-related variables:

  • blockSize β€” defaults to 100
  • listLimit β€” defaults to 500

In case when the limit is exceeded, the error is thrown.

  • mw.query.blockSize [ro] β€” the blockSize.
  • mw.query.listLimit [ro] β€” the listLimit.
  • mw.query.expensiveFunctionLimit [ro] β€” the limit of allowed calls to expensive functions.
  • mw.query.expensiveFunctionRemaining [ro] β€” how much more calls to expensive functions are allowed
  • mw.query.pagesExist(pages) β€” checks whether the pages exist and returns the result in form of page->existence table. Note that page name in the resulting table is normalized. This is counted as one expensive query, but for every blockSize of pages this count is increased by 1.
  • mw.query.pageInformation{pages, props} β€” returns the information about pages. The information to return is specified in props array. Currently available are size and is_redirect. This is counted as one expensive query, but for every blockSize of pages this count is increased by 1.
  • mw.query.prefixIndex{prefix, startWith, limit} β€” list the pages beginning with prefix, starting with startWith. Returns at most limit pages, or listLimit, whatever is smaller.
  • there will be more at the later stage

mw.site

[edit]
  • mw.site.siteName [ro] β€” returns the name of the site.
  • mw.site.version [ro] β€” returns MediaWiki software version.
  • mw.site.namespaces [ro] β€” returns localized namespace ID to namespace name map.
  • mw.site.canonicalNamespaces [ro] β€” returns non-localized namespace ID to namespace name map.
  • mw.site.interwikiTable [ro] β€” returns the interwiki table in format { interwiki prefix -> { url, api, wikiID, isLocal, isTrans } }
  • mw.site.numberOfPages [ro]
  • mw.site.numberOfArticles [ro]
  • mw.site.numberOfFiles [ro]
  • mw.site.numberOfEdits [ro]
  • mw.site.numberOfViews [ro]
  • mw.site.numberOfUsers [ro]
  • mw.site.numberOfAdmins [ro]
  • mw.site.numberOfActiveUsers [ro]

mw.Title

[edit]

Unlike other packages, this is an object class. It may be returned by any API method or be constructed by user using one of the following constructors:

  • mw.Title(text) β€” creates a title from its text form; returns nil if the title is invalid.
  • mw.Title(data) β€” creates a title from its data. Currently the only accepted fields are text, namespaceID and namespaceName, allowing to create a title object from namespace + text data. If the data is insufficient, conflicting or invalid, nil is returned.

The title object has the following fields:

  • namespace β€” namespace ID
  • namespaceName β€” namespace name (localized)
  • name β€” the name of the page, without namespace
  • fullName β€” full name of the page, with namespace
  • fullText β€” the full normalized title, including interwiki prefix
  • interwiki β€” the interwiki prefix, if is there
  • fragment β€” the destination fragment

It also has the following methods:

  • titleObj:localURI([query]) β€” returns a local (relative) URL to the title, optionally with query
  • titleObj:fullURI([query]) β€” same as above, but uses full URL instead of local one (includes server name).
  • titleObj:canonicalURI([query]) β€” same as above, but has a protocol prefix.

mw.time

[edit]

This interface provides access to MediaWiki's advanced date and time handling, parsing and internationalization interfaces.

  • mw.time.UTC [ro] β€” returns the current time in UTC.
  • mw.time.local [ro] β€” returns the current time in the wiki timezone.
  • mw.time.unixTimestamp [ro] β€” returns the exact Unix timestamp in seconds, but with highest floating-point precision possible.
  • mw.time.toLocal(timestamp) β€” translates the timestamp to the wiki timezone.
  • mw.time.toUTC(timestamp) β€” translates the timestamp to UTC.
  • mw.time.parse(text) β€” parses the text and returns a timestamp object, assuming by default that timezone is timezone (UTC if not specified).
  • mw.time.format{timestamp, format} β€” formats the date according to the format specification.

mw.text

[edit]
  • mw.text.escape(text) β€” escapes wikitext.
  • mw.text.tag{name, contents, params} β€” creates a tag marker for tag named name. Similar to {{#tag}}.

mw.uri

[edit]
  • mw.uri.encode(text) β€” escapes a URL string
  • mw.uri.encodeAnchor(text) β€” escapes a URL anchor string
  • mw.uri.server [ro] β€” similar to {{SERVER}}.
  • mw.uri.serverName [ro] β€” similar to {{SERVERNAME}}.
  • mw.uri.scriptPath [ro] β€” similar to {{SCRIPTPATH}}.

ustring API

[edit]

The ustring module provides UTF-8 strings manipulations. It aims to be similar to built-in string module in Lua; however, it extends it in some features and it does not provide pattern matching (a separate regular expression library will be provided for that later). Also, it does not provide an OOP interface to strings[1]. There are the following functions in the ustring library:

  • ustring.find(s, needle[, init]) β€” does a substring search, and returns the start and the end point of the match (or nil, if not found). Important differences:
    • The needle argument is not a pattern.
    • In case of using empty string as a needle, raises an error.
    • The first two returns are start- and endpoint as a string offset in characters; the third and fourth arguments are raw byte offsets.
  • ustring.len(s) β€” returns the string length in code points.
  • ustring.lower(s) β€” converts the string to all-lowercase
  • ustring.pairs(s[, start, end]) β€” allows to iterate over all codepoints in the string, or in a substring (from start to end).
  • ustring.split(str, separator[, limit]) β€” splits the str into at most limit substrings (default limit is infinity)
  • ustring.sub(s, i[, j]) β€” returns the substring; the syntax is similar to string.sub.
  • ustring.trim(s) β€” trims all the whitespace at the beginning and at the end of the string.
  • ustring.upper(s) β€” converts the string to all-uppercase
  • ustring.upperFirst(s) β€” converts the first character of the string into uppercase

All functions index the offsets in string by codepoints, not bytes. If invalid UTF-8 is supplied, an error is raised.

For all ustring functions which accept target string as a first argument, a similar function with "u" prefix is provided in usual string metatable. For example, ustring.trim(str) may also be called as str:utrim().

Footnotes

[edit]
  1. ↑ Such an interface was considered, but it is impossible to adequately implement it in pure Lua.