Manual:Hooks/ParserBeforeStrip
From MediaWiki.org
| ParserBeforeStrip | |
|---|---|
| Available from version 1.5.0 Used to process the raw wiki code before any internal processing is applied |
|
*Define function: |
function fnMyHook(&$parser, &$text, &$strip_state) { ... }
|
*Attach hook: |
$wgHooks['ParserBeforeStrip'][] = 'fnMyHook'; |
| Called from: | Parser.php |
*For more information about attaching hooks, see Manual:Hooks.
*For examples of extensions using this hook, see Category:ParserBeforeStrip extensions.
[edit] Details
$text will hold the text for the page. To change the page text, modify this value. E.g. to add the phrase "The mighty oracle gives forth this proclamation: " to the front of the page text, you would use the following code:
$text = "The mighty oracle gives forth this proclamation: " . $text;
Note that this hook is called on the edit page (and possibly elsewhere) with the contents of the disclaimer under the edit box. If you use this function to add content to the page, which shouldn't be added in these circumstances, you should check what action was used to view the page, and only add the extra content if it is not being edited. I will add more info about this once I have figured it out. --HappyDog 04:24, 17 June 2006 (UTC)
- One way to check this out is to check the global
$actionvariable, since it is defined to be the current action. Make sure you setup will ignore pages where$action === 'edit'and you should be good in that respect. I've also ignored the history pages with$action === 'history'and the Special and Help pages with(strpos($parser->mTitle->mPrefixedText, "Special:") === false)and the like. I've still got some more tweaking to do, but otherwise it works out pretty nicely. -- Chad.burrus 00:34, 20 December 2006 (UTC)
-
- You should not be accessing the private class variables (begininning with little 'm') directly - there should be wrapper functions to get these for you (I'm guessing $parser->getTitle() and $title->getPrefixedText()...). Also, you should check the namespace using the numeric namespace ID, rather than the hard-coded name, otherwise your page will only work on English wikis. --HappyDog 01:24, 8 January 2007 (UTC)
-
-
- To get the numeric ID of namespaces here, use $parser->getTitle()->getNamespace(). --Mlogic 23:11 , 17 April 2008 (UTC)
-
- Multiple runs of the parser hook can happen at any time, not just during editing. For example, the galery feature will run its own parsing within one page, and many extensions might do the same if they hook to certain <tags> (in which case they get raw wiki text that they need to parse on their own). I do not know yet of any way to distinguish those parses other than by checking their content. The parser objects look just the same, I think, though maybe the parser options could sometimes give some hint. But filing a proper bug might be more useful to really solve the issue. Idea: the parser or its parser options contain a property that states their purpose from a set of predefined constants, and parsing the main article text is just one value which is not the default. Two other relevant cases might be: parsing a message (e.g. the disclaimer) and parsing inline pieces of text (e.g. the galery, and many extensions). --Markus Krötzsch 17:38, 23 March 2007 (UTC)
-
- The following code seems to work:
$title = $parser->getTitle(); $article = new Article($title); if ($article->getContent() != $text) return;
-
- --HappyDog 06:10, 8 November 2007 (UTC)

