Jump to content

Manual talk:Parser functions

Add topic
From mediawiki.org

Incomplete or incorrect example code[edit]

The examples provided do not work on a fresh installation of 1.26.3. One problem is the extension.json file references classes that are not defined.

Syntax Details[edit]

"This magicWords array is not optional. If it is omitted, the parser function simply will not work; the {{#example: hello | hi}} will be rendered as though the extension were not installed."

Additionally, the syntax is very important; do not omit the comma at the end of the magicWords array, even if there is only one entry.

Unparsed Input[edit]

I know what I'm asking is essentially what tag extensions do, but is there any to specify that input arguments of the callback function should not be parsed? —Sledged (talk) 18:28, 16 September 2007 (UTC)Reply

Change of Defaults in 1.12[edit]

It seems that commit 27667 changed the default for noparse and noarg from false to true in some situations. We have a function which returns a wikitext call to a template (for formatting) with a list of named parameters, and found that while it worked as expected in 1.9 it no longer works with recent SVN. Possibly this is a bug. We had to change our functions to ensure that to:

return array( $result, 'noparse' => false);

whereas previously they just:

return $result;

Perhaps someone knows if this was an anticipated change... Karora 01:10, 8 January 2008 (UTC)Reply

I just got bit by this, too ... initially, I just returned the generated wikitext from my new parser function, and couldn't figure out why it wasn't being parsed correctly (esp. since it makes extensive use of templates), nor why none of the documentation really specified how to trigger the necessary parsing myself. (It's not the same as for tag extensions!) I've just added a note on this topic to the Manual:Parser functions page. Paul Lustgarten 14:50, 16 April 2009 (UTC)Reply

Problem with Time parser function[edit]

I posted a question on the ParserFunctions help talk page having to do with a problem with time functions when having a 2 digit date. If anyone has a clue of how to fix it, it would be greatly appreciated! (apologies for not having a screen name; I have one on wikipedia I use a lot, but I just came here to try and figure out why the error was occurring in edits I made at wikipedia) Thanks. 24.236.101.233 09:49, 30 December 2008 (UTC)Reply

Determining the output of recursiveTagParse[edit]

I'm attempting to determine the output of recursiveTagParse, which I am calling from a parser function. To do so, I'm trying to return the output of recursiveTagParse wrapped in <pre></pre> tags, which I was hoping would show me the rendered HTML. I'm seeing some unexpected behavior. When I do this:

$parsedList = $parser->recursiveTagParse("* test");
return $parser->insertStripItem($parsedList, $parser->mStripState);

I get this:

  • test

But when I do this:

$parsedList = $parser->recursiveTagParse("* test");
return $parser->insertStripItem('<pre>' . $parsedList . '</pre>', $parser->mStripState);

I get this:

* test

It's almost like the rendered HTML is being converted back into wikitext. I'm completely lost. Does anyone have any insight on this?

--Csagedy 04:07, 31 October 2011 (UTC)Reply

Function execute on page reload[edit]

I followed the example parser function and made an extension that calls a web api for data. The function works great but only executes when the page is saved after editing. Can someone point me in the right direction on how to make the extension/parser function I just built execute when the wiki page is reloaded?

call $parser->disableCache() in your parser function. Bawolff (talk) 16:55, 29 November 2013 (UTC)Reply

Calling a parser function without parameters[edit]

{{ #yourfunctionname: }} ← WORKS
{{ #yourfunctionname }}  ← DOES NOT WORK

So the colon kind of acts like the call syntax ()

Parser function called multiple times when saving page[edit]

When saving a page, it apparently is parsed multiple times, which naturally runs the parser functions again. I'm storing data into a SQL database, which is quite resource-intensive. I'd like to avoid running the DB storage parts twice for the same time because it also creates duplicate entries. From my experiments, only the first parsing is stored in the cache and served to the visitor. Is there a possibility to find out if an invocation of the parser function will generate output the user will actually see? Clubfan22 (talk) 08:57, 5 September 2016 (UTC)Reply

How to update category based on ifexpr function[edit]

I have created a template that uses ifexpr to set 3 category based on the date entered by the wiki editor. The problem is that category section does not update once the date passes to either warning or expired (Parameter accepts date and it changes based on how close or past the date entered by wiki editor. Category only changes if I save it again.....


<includeonly> {{#ifexpr: {{#expr: {{#time: U|{{{1|{{REVISIONYEAR}}-{{REVISIONMONTH}}-{{REVISIONDAY}}}}}}} - {{#expr: {{{2|30}}} * 86400}}}} > {{#time: U}} | <sup>(<font color="green">✓</font> - {{{1}}})</sup>[[Category:Time Compliant]] | {{#ifexpr: {{#expr: {{#time: U|{{{1|{{REVISIONYEAR}}-{{REVISIONMONTH}}-{{REVISIONDAY}}}}}}}}} > {{#time: U}} | <sup>(<font color="orange">⚠</font> - {{{1}}})</sup>[[Category:Time Warning]] | <sup>(<font color="red">✘</font> - {{{1}}})</sup>[[Category:Time Expired]]}}}}[[Category:Pages with dates]] </includeonly> <noinclude> This template should be used to track down time-sensitive information and place KB articles in 3 different categories * (<font color="green">✓</font> - YYYY-MM-DD) * (<font color="orange">⚠</font> - YYYY-MM-DD) * (<font color="red">✘</font> - YYYY-MM-DD) <br /> '''Usage:''' <nowiki>{{Expiration|YYYY-MM-DD|NumberOfDaysBeforeWarning}}

Default values
YYYY-MM-DD: Last revision date
NumberOfDaysBeforeWarning: 30 days

Examples: If today was June 1 2017 and last revision was made on the same day...

  • <TemplateName|2017-01-01> - It will display as expired
  • <TemplateName|2017-12-01> - It will display as on time
  • <TemplateName|2017-06-15> - It will display as warning
  • <TemplateName|2017-06-15|10> - It will display as on time (due to shorter number of days before warning)
  • <TemplateName||-30> - It will display as compliant until 30 days have passed



</nowiki> [[Category:Formatting Templates]] </noinclude>

Problematic example[edit]

I copied the example and enabled it, and it shows MWException on my website.

Caching[edit]

Caching Parser::disableCache was removed in MediaWiki 1.35, if I am not mistaken, should be indicated… As a matter of fact, I was trying to write an extension to display different info depending on whether the user is logged in or not, and I find myself quite stuck as in my wiki external visitors cannot edit and by definition won't be able to force parsing the page… --Loizbec (talk) 09:15, 10 May 2022 (UTC)Reply

isHTML - almost but not entirely[edit]

The explanation for this flag reads "The returned text is HTML, armour it against wikitext transformation."

From what I experienced, this isn't entirely true, even when using this with HTML::rawElement (which comes with the same flag). If the HTML you want to output contains newlines and spaces at the beginning of a line, they are treated the same way as wiki syntax, resulting in the addition of paragraph tags and pre tags respectively.

is there a way to properly armour the html output against wikitext transformation? Cavila 15:47, 14 January 2023 (UTC)Reply

I've also noticed that anchor tags are split up if they contain multiple child nodes. E.g. when you have an image and a description. Makes CSS styling harder. However I haven't found a solution. Sindix (talk) 15:03, 6 January 2024 (UTC)Reply

broken link : documentation for Parser::setFunctionHook -> Not Found[edit]

see the documentation for Parser::setFunctionHook -> https://doc.wikimedia.org/mediawiki-core/master/php/classParser.html#a501ce7bb8f461a1b5fbd9f05970cab67 which no longer exists.

Please realign. Thanks. -- Christian 🇫🇷 FR (talk) 11:38, 9 May 2024 (UTC)Reply

Christian's link also didn't work but: https://doc.wikimedia.org/mediawiki-core/master/php/classMediaWiki_1_1Parser_1_1Parser.html#a34da5613dad9dd0a5e921aa7bceba3cf works for me today. ProbablePrime (talk) 08:26, 13 June 2024 (UTC)Reply