Jump to content

Manual:Checking for page existence

From mediawiki.org

There are several methods to check if a page exists. The table below describes this. But first, here is what each column in the table means:

  • Method: The specific technique used to check if a page exists.
  • What links here: Indicates if the method impacts the "What links here" page.
  • Transclusion: Indicates if the page is counted as transcluded onto another page when this method is applied.
  • Limitation: Any limits associated with the method.
  • Usage: How the method is typically used.
  • Interwiki link: It's applicability to interwiki links.
  • Pagename with anchor: Indicates if the method checks the existence of both the page and the specific anchor within the page.
Method What links here Transclusion Limitation Usage Interwiki link[1] Pagename with anchor
#ifexist yes no[2] Expensive (max 100 by default or 500 on Wikimedia wikis) branching "doesn't exist" checks page existence correctly, but not the existence of the anchor
Using #ifeq to compare a transclusion and a link[3]. yes yes Transclusion counts against the post-expand include size limit (2048KiB by default) branching "exists" "exists"
{{PROTECTIONEXPIRY}}[4] no no none branching doesn't exist doesn't exist
linking: class="new" means non-existence yes no none seeing the page link in a style depending on existence class="extiw" checks page existence correctly, but not the existence of the anchor
Special:Allpages no no none seeing the page listed or not not listed n.a.
Special:PrefixIndex no no none seeing the page listed or not not listed n.a.

Examples:

  • {{#ifexist:Template:Tc|[[Template:Tc]] exists|[[Template:Tc]] does not exist}} -> >Template:Tc exists
  • {{#ifexist:existence a|[[existence a]] exists|[[existence a]] does not exist}} -> existence a does not exist
  • Using Manual:Checking for page existence/ifeq method to implement the #ifeq method:
    • {{/ifeq method|Template:Tc}} gives 1
    • {{/ifeq method|Template:Tc|then=[[Template:Tc]] exists|else=[[Template:Tc]] does not exist}} gives Template:Tc exists
    • {{/ifeq method|existence b}} gives 0
    • {{/ifeq method|existence b|then=[[existence b]] exists|else=[[existence b]] does not exist}} gives existence b does not exist
  • Using PROTECTIONEXPIRY method:
    • {{/PROTECTIONEXPIRY method|Template:Tc|[[Template:Tc]] exists|[[Template:Tc]] does not exist}} gives Template:Tc exists
    • {{/PROTECTIONEXPIRY method|existence c|''existence c'' exists|''existence c'' does not exist}} gives existence c does not exist
  • existence d - this is a redlink, indicating that the page does not exist
  • Special:Allpages/existence
  • Special:PrefixIndex/exist

{{Special:PrefixIndex/exist}} gives:

Another approach involves visiting the specified web address and checking either the content or receiving a message indicating that the page does not exist.

Branching applications

[edit]

An example of existence detection in practical use is:

  • Displaying the content of a webpage if it exists, otherwise showing default content.

A special case is:

  • Displaying the content of a webpage if it exists, otherwise displaying nothing.

If there are only a few possible pages, another option is to create them all, with extra pages using default values, and transclude them wherever necessary.

Notes

[edit]
  1. ↑ Interwiki links apply with the correct prefix, regardless of whether the linked page exists.
  2. ↑ In an #ifexist parser function condition, if the page is specified with the transclusion syntax, it counts regardless of whether the page exists. However, if the page name is used directly in the condition and another page is specified in the alternative part, only actual transclusions are considered valid.
  3. ↑ See Manual:Checking for page existence/ifeq method for more details on how this works
  4. ↑ See Manual:Checking for page existence/PROTECTIONEXPIRY method for more details on how this works

See also

[edit]