Manual:Parser.php/en: Difference between revisions
Appearance
Content deleted Content added
Updating to match new version of source page |
Updating to match new version of source page |
||
| Line 6: | Line 6: | ||
This file contains the class '''<code>Parser</code>''', which contains the method <code>parse</code>, which converts [[Special:MyLanguage/Wikitext|Wikitext]] to HTML. It performs, among other things, the following actions: |
This file contains the class '''<code>Parser</code>''', which contains the method <code>parse</code>, which converts [[Special:MyLanguage/Wikitext|Wikitext]] to HTML. It performs, among other things, the following actions: |
||
# Call helper function |
# Call helper function {{phpi|Parser::internalParse()}}, which in turns calls |
||
## |
## {{phpi|Parser::replaceVariables}}, which replaces magic variables, templates, and template arguments with the appropriate text. |
||
###It calls |
###It calls {{phpi|Parser::preprocessToDom}}, which preprocesses some wikitext and returns the document tree. |
||
###Next it creates a <code>[https://doc.wikimedia.org/mediawiki-core/master/php/classPPFrame__DOM.html PPFrame DOM]</code> object and calls its <code>expand()</code> method to do the actual template magic. |
###Next it creates a <code>[https://doc.wikimedia.org/mediawiki-core/master/php/classPPFrame__DOM.html PPFrame DOM]</code> object and calls its <code>expand()</code> method to do the actual template magic. |
||
## |
## {{phpi|Sanitizer::removeHTMLtags()}}, which cleans up HTML, removes dangerous tags and attributes, and removes HTML comments. |
||
## |
## {{phpi|Parser::doTableStuff}}, which handles and renders the wikitext for tables. |
||
## |
## {{phpi|Parser::doDoubleUnderscore}}, which removes valid [[Special:MyLanguage/Help:Magic_words#Behavior_switches|double-underscore items]], like <nowiki>__NOTOC__</nowiki>, and records them in array {{phpi|$Parser->mDoubleUnderscores}}. |
||
## |
## {{phpi|Parser::doHeadings}}, which parses and renders section headers. |
||
## |
## {{phpi|Parser::replaceInternalLinks}}, which processes internal links (<code>[[ ]]</code>) and stores them in <code>$Parser->mLinkHolders</code> (a [https://doc.wikimedia.org/mediawiki-core/master/php/classLinkHolderArray.html <code>LinkHolderArray</code> object]), |
||
## |
## {{phpi|Parser::doAllQuotes}}, which replaces single quotes with HTML markup (<nowiki><i>, <b></nowiki>, etc). |
||
## |
## {{phpi|Parser::replaceExternalLinks}}, which replaces and renders external links. |
||
## |
## {{phpi|Parser::doMagicLinks}}, which replaces special strings like "ISBN xxx" and "RFC xxx" with magic external links. |
||
## |
## {{phpi|Parser::formatHeadings}}, which: |
||
##* auto numbers headings if that options is enabled, |
##* auto numbers headings if that options is enabled, |
||
##* adds an <nowiki>[edit]</nowiki> link to sections for users who have enabled the option and can edit the page, |
##* adds an <nowiki>[edit]</nowiki> link to sections for users who have enabled the option and can edit the page, |
||
##* adds a Table of contents on the top for users who have enabled the option, and |
##* adds a Table of contents on the top for users who have enabled the option, and |
||
##* auto-anchors headings. |
##* auto-anchors headings. |
||
#Next, <code>parse()</code> calls |
#Next, <code>parse()</code> calls {{phpi|Parser::doBlockLevels}}, which renders lists from lines starting with ':', '*', '#', etc. |
||
# |
#{{phpi|Parser::replaceLinkHolders}} is called, which calls {{phpi|LinkHolderArray::replace}} on {{phpi|$Parser->mLinkHolders}} to replace link placeholders with actual links, in the buffer Placeholders created in Skin::makeLinkObj() <!-- Que? --> |
||
#Next, the text is language converted (when applicable) using the <code>convert</code> method of the appropriate [https://doc.wikimedia.org/mediawiki-core/master/php/classLanguage.html Language] object. |
#Next, the text is language converted (when applicable) using the <code>convert</code> method of the appropriate [https://doc.wikimedia.org/mediawiki-core/master/php/classLanguage.html Language] object. |
||
# |
#{{phpi|Parser::replaceTransparentTags}} is called, which replaces transparent tags with values which are provided by the callback functions in {{phpi|$Parser->mTransparentTagHooks}}. Transparent tag hooks are like regular XML-style tag hooks, except they operate late in the transformation sequence, on HTML instead of wikitext. |
||
# |
#{{phpi|Sanitizer::normalizeCharReferences}} is called, which ensures that any entities and character references are legal for XML and XHTML specifically. |
||
#If [[Special:MyLanguage/Manual:$wgUseTidy|HTML tidy]] is enabled, |
#If [[Special:MyLanguage/Manual:$wgUseTidy|HTML tidy]] is enabled, {{phpi|MWTidy::tidy}} is called to do the tidying. |
||
#Finally the rendered HTML result of the parse process is stored in the <code>[https://doc.wikimedia.org/mediawiki-core/master/php/classParserOutput.html ParserOutput]</code> object |
#Finally the rendered HTML result of the parse process is stored in the <code>[https://doc.wikimedia.org/mediawiki-core/master/php/classParserOutput.html ParserOutput]</code> object {{phpi|$Parser->mOutput}}, which is returned to the caller of {{phpi|Parser::parse}}. |
||
==[[Special:MyLanguage/Manual:Hooks|Hooks]]== |
==[[Special:MyLanguage/Manual:Hooks|Hooks]]== |
||
| Line 105: | Line 105: | ||
*<code>Parser::setFunctionHook()</code> Create a function, e.g. <code><nowiki>{{sum:1|2|3}}</nowiki></code>. The callback function should have the form: <code>function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }</code>. |
*<code>Parser::setFunctionHook()</code> Create a function, e.g. <code><nowiki>{{sum:1|2|3}}</nowiki></code>. The callback function should have the form: <code>function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }</code>. |
||
*<code>Parser::setFunctionTagHook()</code> Create a tag function, e.g. {{tmpl|0={{tag|test|content=$1}}|some stuff}}. Unlike tag hooks, tag functions are parsed at preprocessor level. Unlike parser functions, their content is not preprocessed. |
*<code>Parser::setFunctionTagHook()</code> Create a tag function, e.g. {{tmpl|0={{tag|test|content=$1}}|some stuff}}. Unlike tag hooks, tag functions are parsed at preprocessor level. Unlike parser functions, their content is not preprocessed. |
||
*<code>Parser::setHook()</code> Create an HTML-style tag, e.g. {{tmpl|0={{tag|yourtag|content=$1}}|special text}}. The callback should have the following form: |
*<code>Parser::setHook()</code> Create an HTML-style tag, e.g. {{tmpl|0={{tag|yourtag|content=$1}}|special text}}. The callback should have the following form: {{phpi|function myParserHook( $text, $params, $parser, $frame ) { ... }}} |
||
==See also== |
==See also== |
||
Revision as of 02:51, 12 June 2021
This page is outdated. |
| MediaWiki file: Parser.php | |
|---|---|
| Location: | includes/parser/ (includes/Parser/ in 1.46 or later) |
| Source code: | master • 1.46.0 • 1.45.4 • 1.43.9 |
| Classes: | Parser |
Description
This file contains the class Parser, which contains the method parse, which converts Wikitext to HTML. It performs, among other things, the following actions:
- Call helper function
Parser::internalParse(), which in turns callsParser::replaceVariables, which replaces magic variables, templates, and template arguments with the appropriate text.- It calls
Parser::preprocessToDom, which preprocesses some wikitext and returns the document tree. - Next it creates a
PPFrame DOMobject and calls itsexpand()method to do the actual template magic.
- It calls
Sanitizer::removeHTMLtags(), which cleans up HTML, removes dangerous tags and attributes, and removes HTML comments.Parser::doTableStuff, which handles and renders the wikitext for tables.Parser::doDoubleUnderscore, which removes valid double-underscore items, like __NOTOC__, and records them in array$Parser->mDoubleUnderscores.Parser::doHeadings, which parses and renders section headers.Parser::replaceInternalLinks, which processes internal links ([[ ]]) and stores them in$Parser->mLinkHolders(aLinkHolderArrayobject),Parser::doAllQuotes, which replaces single quotes with HTML markup (<i>, <b>, etc).Parser::replaceExternalLinks, which replaces and renders external links.Parser::doMagicLinks, which replaces special strings like "ISBN xxx" and "RFC xxx" with magic external links.Parser::formatHeadings, which:- auto numbers headings if that options is enabled,
- adds an [edit] link to sections for users who have enabled the option and can edit the page,
- adds a Table of contents on the top for users who have enabled the option, and
- auto-anchors headings.
- Next,
parse()callsParser::doBlockLevels, which renders lists from lines starting with ':', '*', '#', etc. Parser::replaceLinkHoldersis called, which callsLinkHolderArray::replaceon$Parser->mLinkHoldersto replace link placeholders with actual links, in the buffer Placeholders created in Skin::makeLinkObj()- Next, the text is language converted (when applicable) using the
convertmethod of the appropriate Language object. Parser::replaceTransparentTagsis called, which replaces transparent tags with values which are provided by the callback functions in$Parser->mTransparentTagHooks. Transparent tag hooks are like regular XML-style tag hooks, except they operate late in the transformation sequence, on HTML instead of wikitext.Sanitizer::normalizeCharReferencesis called, which ensures that any entities and character references are legal for XML and XHTML specifically.- If HTML tidy is enabled,
MWTidy::tidyis called to do the tidying. - Finally the rendered HTML result of the parse process is stored in the
ParserOutputobject$Parser->mOutput, which is returned to the caller ofParser::parse.
| Version | Hook | Description |
|---|---|---|
| 1.5.0 | ParserAfterStrip | (deprecated in 1.14.0) |
| 1.5.0 | ParserAfterTidy | Used to add some final processing to the fully-rendered page output. |
| 1.6.0 | ParserBeforeInternalParse | |
| 1.5.0 | ParserBeforeStrip | Used to process the raw wiki code before any internal processing is applied. |
| 1.5.0 | ParserBeforeTidy | Used to process the nearly-rendered HTML code for the page (but before any HTML tidying occurs). |
| 1.6.0 | ParserClearState | Called at the end of Parser::clearState(). |
| 1.6.0 | ParserGetVariableValueSwitch | Assigns a value to a user defined variable. |
| 1.6.0 | ParserGetVariableValueTs | Used to change the value of the time for the {{LOCAL...}} magic word. |
| 1.6.0 | ParserGetVariableValueVarCache | Used to change the value of the variable cache or return false to not use it. |
| 1.6.0 | ParserTestParser | Called when creating a new instance of Parser for parser tests. |
| 1.10.0 | InternalParseBeforeLinks | Used to process the expanded wiki code after <nowiki>, HTML-comments, and templates have been treated. |
| 1.10.1 | BeforeParserFetchTemplateAndtitle | Allows an extension to specify a version of a page to get for inclusion in a template. |
| 1.10.1 | BeforeParserrenderImageGallery | Allows an extension to modify an image gallery before it is rendered. |
| 1.12.0 | ParserFirstCallInit | Called when the parser initialises for the first time. |
| 1.12.0 | ParserLimitReport | Called at the end of Parser::parse() when the parser will include comments about size of the text parsed. |
| 1.12.0 | ParserMakeImageParams | Alter the parameters used to generate an image before it is generated. |
| 1.18.0 | BeforeParserFetchFileAndTitle | Before an image is rendered by Parser. |
| 1.19.0 | ParserSectionCreate | Called each time the parser creates a document section from wikitext. |
Accessors
getCustomDefaultSort ()Accessor for $mDefaultSort. Unlike getDefaultSort(), it will return false if none is set.getDefaultSort ()Accessor for $mDefaultSort. Will use the empty string if none is set.getOptions ()Get the ParserOptions object.getOutput ()Get the ParserOutput object.getPreprocessor ()Get a preprocessor object.getRevisionId ()Get the ID of the revision we are parsing.getRevisionTimestamp ()Get the timestamp associated with the current revision, adjusted for the default server-local timestamp.getRevisionUser ()Get the name of the user that edited the last revision.getTags ()Accessor.getTargetLanguage ()Get the target language for the content being parsed.getTitle ()Accessor for the Title object.getUser ()Get a User object either from $this->mUser, if set, or from the ParserOptions object otherwise.
Set
Parser::setFunctionHook()Create a function, e.g.{{sum:1|2|3}}. The callback function should have the form:function myParserFunction( &$parser, $arg1, $arg2, $arg3 ) { ... }.Parser::setFunctionTagHook()Create a tag function, e.g.<test>some stuff</test>. Unlike tag hooks, tag functions are parsed at preprocessor level. Unlike parser functions, their content is not preprocessed.Parser::setHook()Create an HTML-style tag, e.g.<yourtag>special text</yourtag>. The callback should have the following form:function myParserHook( $text, $params, $parser, $frame ) { ...}
See also
- Manual:Code – Overview of the key files and directories in the MediaWiki source code
- Manual:ParserOptions.php