Jump to content

Help:Magic words: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
mNo edit summary
Other: add {{int:}}
Line 134: Line 134:
* <code><nowiki>{{#property:</nowiki> ''arg1'' | ''arg2'' | ... <nowiki>}}</nowiki></code> (see [[m:Wikidata/Notes/Inclusion syntax v0.3]])
* <code><nowiki>{{#property:</nowiki> ''arg1'' | ''arg2'' | ... <nowiki>}}</nowiki></code> (see [[m:Wikidata/Notes/Inclusion syntax v0.3]])
* <code><nowiki>{{#target:</nowiki> ''noticeboard'' <nowiki>}}</nowiki></code> - used with the [[m:MassMessage|MassMessage extension]]; see also [[mw:Help:Extension:MassMessage]], which shows an additional option to specify another wiki on which the post is made. However, the ability to post to another wiki is disabled from the English Wikipedia.
* <code><nowiki>{{#target:</nowiki> ''noticeboard'' <nowiki>}}</nowiki></code> - used with the [[m:MassMessage|MassMessage extension]]; see also [[mw:Help:Extension:MassMessage]], which shows an additional option to specify another wiki on which the post is made. However, the ability to post to another wiki is disabled from the English Wikipedia.
* <code><nowiki>{{int:</nowiki> ''messagename'' <nowiki>}}</nowiki></code> (transcludes a page in [[Help:MediaWiki namespace|MediaWiki namespace]] – such as a system message – translated into the language set by the user's preferences)


== See also ==
== See also ==

Revision as of 16:22, 9 March 2014

Magic words (which include parser functions, variables and behavior switches) are features of wiki markup that enable various instructions to be given to the MediaWiki software. For example, the magic words may be used to suppress or position the table of contents, or to produce other variable outputs, as is often required in templates.

A quick reference for magic words can be found on this page. For more complete documentation, see the following pages on the MediaWiki site:

General information

There are three types of magic words:

  1. Behavior switches: uppercase words surrounded by double underscores, e.g., __NOTOC__ or other keywords using parser function syntax
  2. Variables: uppercase words surrounded by double braces (thus resembling templates), e.g., {{PAGENAME}}
  3. Parser functions: keywords in double braces with parameters after a colon (some beginning with #), e.g., {{#expr:2+2}}

Some magic words are case-insensitive, but not all. White space is stripped from the start and end of keywords and parameters, as in template syntax.

Page-dependent magic words will affect or return data about the current page, even if the word is added through a transcluded template or included system message.

It is possible to substitute parser functions and most variables in the same way that templates are substituted (using the subst: keyword). This causes their current value (as evaluated at the time of substitution) to be written into the wikitext.

Behavior switches

  • __NOTOC__ (can appear anywhere in the wikitext; suppresses the table of contents)
  • __FORCETOC__ (can appear anywhere in the wikitext; makes a table of contents appear in its normal position above the first heading)
  • __TOC__ (places a table of contents at the word's position)
  • __NOEDITSECTION__ (hides the section "edit" links beside all headings on the page) (use <h2> tags to hide the edit link for one heading only)
  • __NEWSECTIONLINK__ (adds a "+" link for adding a new section on a non-"Talk" page)
  • __NONEWSECTIONLINK__ (removes the "+" link on "Talk" pages)
  • __NOGALLERY__ (on a category page, replaces thumbnails with normal links)
  • __HIDDENCAT__ (on a category page, makes it a hidden category)
  • __INDEX__ (tells search engines to index the page)
  • __NOINDEX__ (tells search engines not to index the page)
  • __STATICREDIRECT__ (On redirect pages, don't allow MediaWiki to automatically update the link when someone moves a page and checks "Update any redirects that point to the original title", also used to tell interwikibots that this redirect might be seen as an article)
  • __DISAMBIG__ (marks a page as a disambiguation page and places it in Special:DisambiguationPages, and places inward links in Special:DisambiguationPageLinks). See mw:Extension:Disambiguator.
  • {{DISPLAYTITLE:title}} (changes the displayed form of the page title)
  • {{DEFAULTSORT:sortkey}} (sets a default category sort key)
  • {{noexternallanglinks}} to suppress all Wikidata automatic interlanguage links. Equivalent to {{noexternallanglinks:*}}. Links for individual languages can be suppressed by specifying language codes in a pipe-separated list, e.g. {{noexternallanglinks:fr|es|ja}}. See mw:Extension:Wikibase Client#noexternallanglinks.

Variables

  • {{ARTICLEPAGENAME}} (associated non-talk page) (identical to {{SUBJECTPAGENAME}} )
  • {{ARTICLESPACE}} (associated non-talk namespace) (identical to {{SUBJECTSPACE}} )
  • {{BASEPAGENAME}} (page title excluding current subpage and namespace - effectively the parent page without the namespace)
  • {{FULLPAGENAME}} (page title including namespace)
  • {{NAMESPACE}} (namespace of current page)
  • {{PAGENAME}} (page title excluding namespace)
  • {{SUBJECTPAGENAME}} (associated non-talk page) (identical to {{ARTICLEPAGENAME}} )
  • {{SUBJECTSPACE}} (associated non-talk namespace) (identical to {{ARTICLESPACE}} )
  • {{SUBPAGENAME}} (subpage part of title)
  • {{TALKPAGENAME}} (associated talk page)
  • {{TALKSPACE}} (associated talk namespace)
  • {{FULLPAGENAMEE}}, {{NAMESPACEE}} etc. (equivalents encoded for use in MediaWiki URLs)

Note: all of the previous variables can each take a parameter, to operate on a page other than the current page. For example {{TALKPAGENAME:WP:MOS}} returns "Wikipedia talk:MOS". A colon (:) is used to pass the parameter rather than a pipe (|), which is used in templates.

  • {{SITENAME}} (Wikipedia)
  • {{SERVER}} (//en.wikipedia.org)
  • {{SERVERNAME}} (en.wikipedia.org)
  • {{SCRIPTPATH}} (/w)
  • {{CURRENTVERSION}} (current MediaWiki version)
  • {{REVISIONID}} (latest revision to current page—it can't be substituted and also doesn't work in preview)
  • {{REVISIONDAY}}, {{REVISIONDAY2}}, {{REVISIONMONTH}}, {{REVISIONYEAR}}, {{REVISIONTIMESTAMP}}, {{REVISIONUSER}} (date, time, editor at last edit)
  • {{CURRENTYEAR}}, {{CURRENTMONTH}}, {{CURRENTMONTHNAME}}, {{CURRENTMONTHABBREV}}, {{CURRENTDAY}}, {{CURRENTDAY2}}, {{CURRENTDOW}}, {{CURRENTDAYNAME}}, {{CURRENTTIME}}, {{CURRENTHOUR}}, {{CURRENTWEEK}}, {{CURRENTTIMESTAMP}} (current date/time variables)
  • {{LOCALYEAR}} etc. (as previously, based on site's local time)
  • {{NUMBEROFPAGES}}, {{NUMBEROFARTICLES}}, {{NUMBEROFFILES}}, {{NUMBEROFEDITS}}, {{NUMBEROFVIEWS}}, {{NUMBEROFUSERS}}, {{NUMBEROFADMINS}}, {{NUMBEROFACTIVEUSERS}} (statistics on English Wikipedia; add :R to return numbers without commas)

Parser functions

Metadata

  • {{PAGEID}} (unique page identifier number)
  • {{PAGESIZE:page name}} (size of page in bytes)
  • {{PROTECTIONLEVEL:action|pagename}} (protection level for given action (e.g. "edit", "move") on the specified page)
  • {{PENDINGCHANGELEVEL}} (level of pending changes protection on the current page)
  • {{PAGESINCATEGORY:categoryname}} (number of pages in the given category)
  • {{NUMBERINGROUP:groupname}} (number of users in a specific group)

Add |R to return numbers without commas.

Formatting

  • {{lc:string}} (convert to lower case)
  • {{lcfirst:string}} (convert first character to lower case)
  • {{uc:string}} (convert to upper case)
  • {{ucfirst:string}} (convert first character to upper case)
  • {{formatnum:unformatted num}} (format a number with comma separators; add |R to unformat a number)
  • {{#formatdate:date|format}} (formats a date according to user preferences; a default can be given as an optional case-sensitive second parameter for users without date preference; can convert a date from an existing format to any of dmy, mdy, ymd or ISO 8601 formats, with the user's preference overriding the specified format)
  • {{padleft:xyz|stringlength}}, {{padright:xyz|stringlength}} (pad with zeros to the right or left; an alternative padding string can be given as a third parameter; the alternative padding string may be truncated if its length does not evenly divide the required number of characters)
  • {{plural:n|is|are}} (produces alternative text according to whether n is greater than 1)
  • {{#time:format string|date/time object}} (for date/time formatting; also #timel for local time. Covered at the extension documentation page. #timel is based on the local time defined for the entire wiki. On the English Wikipedia #timel is functionally identical to #time as the wiki's local time is set to UTC.)
  • {{gender:username|masculine|feminine|neutral}} (produces alternative text according to the gender specified by the given user in his/her preferences)
  • {{#tag:tagname|content|parameter1=value1|parameter2=value2}} (equivalent to an HTML tag or pair of tags; can be used for nesting references)

Paths

  • {{localurl:page name}}, {{localurl:page name|query string}} (relative path to the title)
  • {{fullurl:page name}}, {{fullurl:page name|query_string}} (absolute path to the title, without a protocol prefix)
  • {{canonicalurl:page name}}, {{canonicalurl:page name|query_string}} (absolute path to the title, with a protocol prefix)
  • {{filepath:file name}} (absolute URL to a media file)
  • {{urlencode:string}} (input encoded for use in URL query strings: like+this)
    • {{urlencode:string|PATH}} (input encoded for use in URL paths: like%20this)
    • {{urlencode:string|WIKI}} (input encoded for use as MediaWiki page names: like_this)
  • {{anchorencode:string}} (input encoded for use in MediaWiki URL section anchors)
  • {{ns:n}} (name for the namespace with index n; use {{nse:}} for the equivalent encoded for MediaWiki URLs)
  • {{#rel2abs: path }} (converts a relative file path to absolute; see the extension documentation)
  • {{#titleparts: pagename | number of segments to return | first segment to return }} (splits title into parts; see the extension documentation)

Conditional expressions

  • {{#expr: expression }} (evaluates the given expression; see Help:Calculation)
  • {{#if: test string | value if non-empty | value if empty }} (selects one of two values based on whether the test string is empty)
  • {{#ifeq: string 1 | string 2 | value if equal | value if unequal }} (selects one of two values based on whether the test strings are equal – numerically if applicable)
  • {{#iferror: test string | value if error | value if correct }} (selects value based on whether the test string generates a parser error)
  • {{#ifexpr: expression | value if true | value if false }} (selects value based on evaluation of expression)
  • {{#ifexist: page title | value if exists | value if doesn't exist }} (selects value depending on whether a page title exists)
  • {{#switch: test | case1 = value for case 1 | ... | default }} (provides alternatives based on the value of the test string)

Note that with #if: expressions, a variable like {{{1}}} always requires a final pipe: {{{1|}}}. If it is absent, then whenever the parameter 1 is absent, instead of leaving the field blank the software will use the actual text {{{1}}} and the field will never be empty.

For the use of these functions in tables, see WP:Conditional tables.

For examples and more explanation, see Help:Conditional expressions and Help:Switch parser function.

Other

  • {{#babel: babelcode1 | babelcode2 | ... }} (replacement for the {{babel}} template; see Extension:Babel)
  • {{#coordinates: arg1 | arg2 | ... }} (see Extension:GeoData)
  • {{#invoke: module | function | arg1 | arg2 | ... }} (calls a function located in a Scribunto module; see Wikipedia:Lua)
  • {{#language: code1 | code2 }} (converts a language code into a language name; see mw:Help:Magic words#Miscellaneous)
    • {{#language:ar}} → العربية
    • {{#language:ar|en}} → Arabic
    • {{#language:ar|fr}} → arabe
  • {{#property: arg1 | arg2 | ... }} (see m:Wikidata/Notes/Inclusion syntax v0.3)
  • {{#target: noticeboard }} - used with the MassMessage extension; see also mw:Help:Extension:MassMessage, which shows an additional option to specify another wiki on which the post is made. However, the ability to post to another wiki is disabled from the English Wikipedia.
  • {{int: messagename }} (transcludes a page in MediaWiki namespace – such as a system message – translated into the language set by the user's preferences)

See also