Scratchpad:Template limits

The MediaWiki software that powers Wikipedia has several parameters that limit the complexity of a page, and the amount of data that can be included. These limits mainly concern data that is transcluded or substituted during expansion of a page, as opposed to data directly in the source of the page itself. This page explains how and why these limits are applied, and how users can work within the limits.

What is this about?
The MediaWiki software, which generates the HTML of a page from its wiki source, uses a parser to deal with included data. This is done using a "preprocessor" which converts the wikitext into a data structure known as an XML tree, and then uses this tree to produce "expanded" wikitext, where double- and triple-braced structures are replaced by their result.

During the conversion process, the software uses several counters to track the complexity of the page that is being generated. When the parsing of a page begins, these counters are set to zero, but they are incremented during the parsing process, as described below. There are upper limits on these counters, and the parser does not allow these limits to be exceeded.

Why are there limits?
Very long or complicated pages are slow to parse. Not only is this an inconvenience for users, but it can also be used to mount a denial of service (DoS) attack on the servers, in which a page request forces the MediaWiki software to parse an unreasonably large quantity of data. The limits help to prevent this kind of attack, and ensure that pages are rendered in a reasonable time. (Nevertheless, sometimes a complex page within the limits gives a time-out error; this depends on how busy the servers are.)

Working within the limits
When a page reaches the template limits, the most common solution is to make the templates shorter, using methods described below. If this isn't possible, it may be necessary to include more data directly in the page source, rather than transcluding it from templates (e.g., formatting references by hand or using

(On wikis with a Module namespace the items "Lua time usage" and "Lua memory usage" are added to this list.)

Because of the way the counters are increased, the first three counts will usually be less than the limits. If any of these sizes are close to the limit, then it is likely that some of the templates have not been expanded. Each occurrence of an unexpanded template is identified in the page body by an HTML comment containing an error message.

Update 1 April 2013:

Expansion
Templates in non-executed branches of conditional parser functions are not expanded, and therefore not counted. For example, in the code, the template   is expanded, but the template   is not expanded. Nevertheless, it is possible for a template argument to contribute to the counts even though it does not appear in the final output. For example, if the code  is parsed, the length of the expanded version of template   will be added to the post-expand counter, because that template must be expanded to decide which branch of the conditional should be selected.

Preprocessor node count
The preprocessor node count measures the complexity of the page (not the volume of data). As the parser is expanding a page, it creates a data structure known as a tree that corresponds to the HTML structure of the page. Each node of the tree that is visited during expansion is counted towards the preprocessor node count. If this count is exceeded, the parser will abort parsing with the error "Node-count limit exceeded" visible in the generated HTML.

The count starts with 1 for plain text. A pair of nowiki tags counts for 3, a header for 2, etc. A link does not contribute to the count. For the expansion of  every checked condition adds 2 to the count. In the case of multiple expansions of the same template the content of a template without arguments counts only once, but that of a template with arguments (even if constant) counts multiple times. In contrast to this, the result of an expansion can be used multiple times while counting only once if it is assigned to a template parameter, and the template makes multiple use of this parameter.

Pages exceeding this limit are automatically categorized into Category:Pages where node count is exceeded.

Post-expand include size
The post-expand include size is the sum of the lengths of the expanded wikitexts generated by templates, parser functions and variables. Whenever the parser is instructed by the source code of a page to expand a template etc. (that is, to replace it by transclusion or substitution), the parser adds together the length of the expanded wikitext generated by the template etc. and the current counter value of the page. If this sum is more than the post-expand limit, the initial template etc. is not replaced and an error message is added as a comment in the output HTML. Otherwise the post-expand counter is increased to the new value, and parsing continues. A template that is expanded more than once in the page contributes more than once to its post-expand include size.

Template invocations with no arguments have an expanded text cache. So if  includes the second-level meta-template , then multiple invocations of   will only increment the post-expand include size for the fully-expanded  ; the secondary include   is only counted once. But if you included the same template multiple times with, then the secondary templates are counted each time, even if the argument is the same.

Pages exceeding the post-expand include size limit are automatically added to Category:Pages where template include size is exceeded.

Using comments, noinclude and onlyinclude
Only data that survives the preprocessor expansion stage is counted towards the post-expand counter. The length of HTML comments in the wikitext (which are not reproduced in the HTML source produced) is not included in the post-expand counter. Code which is either inside a &lt;noinclude&gt; section or outside an &lt;onlyinclude&gt; section does not get expanded, so these sections do not contribute to the post-expand size. This also means that category tags only contribute if they are included (to categorize pages calling the template).

Nested transclusions
Note that the sizes of the wikitexts of all expanded templates and parser functions are added, even in the case of nesting (see bugzilla 13260), so extra levels increase the count. If page A transcludes B and B does nothing but transclude C, then the size of C will be counted twice towards the post-expand include size on page A, and similarly if a template consists of a parser function call, or a parser function has a template call as parameter, etc. This can sometimes be mitigated by letting the parser function produce a template name instead of a template result, e.g. by replacing with

Template argument size
The "template argument size" counter keeps track of the total length of template arguments that have been substituted.

Example:

has a template argument size of 40 bytes: the argument  is counted 3 times, the argument   twice.

Arguments in the template call which do not match any parameter tag in the template do not count.

If a template contains a switch, use of template arguments beyond a match do not count. Up to and including the matching case, template arguments used on the left of the equals signs count twice. Those on the right of the equals sign count for the matching case only.

Pages exceeding the template argument size limit are automatically added to Category:Pages containing omitted template arguments.

Highest expansion depth
Pages exceeding this limit are automatically categorized into Category:Pages where expansion depth is exceeded.

Expensive parser function calls
There is a limit of 500 to the expensive parser function count, i.e., the number of calls of expensive parser functions, which are:
 * # ifexist – branching depending on whether a particular page exists. If the limit on this counter is exceeded, additional #ifexist calls will act as though the pages they query do not exist.
 * PAGESINCATEGORY or PAGESINCAT
 * PAGESIZE
 * CASCADINGSOURCES
 * REVISIONUSER, when used on a page other than the current page
 * REVISIONTIMESTAMP, when used on a page other than the current page
 * Some Lua functions, many equivalent to other items in this list:
 * mw.site.stats.pagesInCategory
 * Some title object properties and methods

It is also possible to manually increment the expensive parser function count from a Lua module by using mw.incrementExpensiveFunctionCount.

Pages that exceed this limit are automatically categorized into Category:Pages with too many expensive parser function calls.

See also: mw:Manual:$wgExpensiveParserFunctionLimit

#time
The total length of the format strings of function  is limited to 6000 characters. The error message is given by MediaWiki:Pfunc time too long). For each combination of the expanded wikitext of a format string and the expanded wikitext of an expression for the time (e.g. "1 Mar 2008 -1day"), repeated use is not counted, as the results are cached.

Unfortunately, the count is not in the limit report.

Special:Expandtemplates
When a page exceeds the limits, one crude way to solve the problem is to use Special:ExpandTemplates. As opposed to substitution it recursively expands all levels at once, without the need of specially preparing the templates with the code  or similar (see bug 2777). This reduces all counts to zero except the preprocessor node count, but even that will typically be reduced to a number that is well within the limit.

History
The inclusion limits were put into effect on the English Wikipedia by Tim Starling on 14 August 2006. A new preprocessor was enabled in January 2008, removing the "pre-expand include limit" and replacing it with a "preprocessor node count" limit.

The practice of using a template documentation page, while it can still be useful for other reasons, is no longer needed for avoiding documentation to be counted on pages that call the template.