MediaWiki REL1_34
CoreTagHooks.php
Go to the documentation of this file.
1<?php
33 public static function register( $parser ) {
34 global $wgRawHtml;
35 $parser->setHook( 'pre', [ __CLASS__, 'pre' ] );
36 $parser->setHook( 'nowiki', [ __CLASS__, 'nowiki' ] );
37 $parser->setHook( 'gallery', [ __CLASS__, 'gallery' ] );
38 $parser->setHook( 'indicator', [ __CLASS__, 'indicator' ] );
39 if ( $wgRawHtml ) {
40 $parser->setHook( 'html', [ __CLASS__, 'html' ] );
41 }
42 }
43
58 public static function pre( $text, $attribs, $parser ) {
59 // Backwards-compatibility hack
60 $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $text, 'i' );
61
62 $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
63 // We need to let both '"' and '&' through,
64 // for strip markers and entities respectively.
65 $content = str_replace(
66 [ '>', '<' ],
67 [ '&gt;', '&lt;' ],
69 );
70 return Html::rawElement( 'pre', $attribs, $content );
71 }
72
89 public static function html( $content, $attributes, $parser ) {
90 global $wgRawHtml;
91 if ( $wgRawHtml ) {
92 if ( $parser->getOptions()->getAllowUnsafeRawHtml() ) {
93 return [ $content, 'markerType' => 'nowiki' ];
94 } else {
95 // In a system message where raw html is
96 // not allowed (but it is allowed in other
97 // contexts).
98 return Html::rawElement(
99 'span',
100 [ 'class' => 'error' ],
101 // Using ->text() not ->parse() as
102 // a paranoia measure against a loop.
103 wfMessage( 'rawhtml-notallowed' )->escaped()
104 );
105 }
106 } else {
107 throw new MWException( '<html> extension tag encountered unexpectedly' );
108 }
109 }
110
127 public static function nowiki( $content, $attributes, $parser ) {
128 $content = strtr( $content, [
129 // lang converter
130 '-{' => '-&#123;',
131 '}-' => '&#125;-',
132 // html tags
133 '<' => '&lt;',
134 '>' => '&gt;'
135 // Note: Both '"' and '&' are not converted.
136 // This allows strip markers and entities through.
137 ] );
138 return [ $content, 'markerType' => 'nowiki' ];
139 }
140
156 public static function gallery( $content, $attributes, $parser ) {
157 return $parser->renderImageGallery( $content, $attributes );
158 }
159
171 public static function indicator( $content, array $attributes, Parser $parser, PPFrame $frame ) {
172 if ( !isset( $attributes['name'] ) || trim( $attributes['name'] ) === '' ) {
173 return '<span class="error">' .
174 wfMessage( 'invalid-indicator-name' )->inContentLanguage()->parse() .
175 '</span>';
176 }
177
178 $parser->getOutput()->setIndicator(
179 trim( $attributes['name'] ),
180 Parser::stripOuterParagraph( $parser->recursiveTagParseFully( $content, $frame ) )
181 );
182
183 return '';
184 }
185}
$wgRawHtml
Allow raw, unchecked HTML in "<html>...</html>" sections.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Various tag hooks, registered in Parser::firstCallInit()
static pre( $text, $attribs, $parser)
Core parser tag hook function for 'pre'.
static indicator( $content, array $attributes, Parser $parser, PPFrame $frame)
XML-style tag for page status indicators: icons (or short text snippets) usually displayed in the top...
static gallery( $content, $attributes, $parser)
Core parser tag hook function for 'gallery'.
static nowiki( $content, $attributes, $parser)
Core parser tag hook function for 'nowiki'.
static html( $content, $attributes, $parser)
Core parser tag hook function for 'html', used only when $wgRawHtml is enabled.
MediaWiki exception.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:74
getOutput()
Get the ParserOutput object.
Definition Parser.php:983
recursiveTagParseFully( $text, $frame=false)
Fully parse wikitext to fully parsed HTML.
Definition Parser.php:823
static delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags='')
Perform an operation equivalent to preg_replace() with flags.
$content
Definition router.php:78