MediaWiki  master
CoreTagHooks.php
Go to the documentation of this file.
1 <?php
28 class CoreTagHooks {
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;' ],
68  $content
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'] ),
181  );
182 
183  return '';
184  }
185 }
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
static nowiki( $content, $attributes, $parser)
Core parser tag hook function for &#39;nowiki&#39;.
static gallery( $content, $attributes, $parser)
Core parser tag hook function for &#39;gallery&#39;.
$wgRawHtml
Allow raw, unchecked HTML in "<html>...</html>" sections.
static stripOuterParagraph( $html)
Strip outer.
Definition: Parser.php:6502
static validateTagAttributes( $attribs, $element)
Take an array of attribute names and values and normalize or discard illegal values for the given ele...
Definition: Sanitizer.php:791
static pre( $text, $attribs, $parser)
Core parser tag hook function for &#39;pre&#39;.
getOutput()
Get the ParserOutput object.
Definition: Parser.php:1007
static html( $content, $attributes, $parser)
Core parser tag hook function for &#39;html&#39;, used only when $wgRawHtml is enabled.
recursiveTagParseFully( $text, $frame=false)
Fully parse wikitext to fully parsed HTML.
Definition: Parser.php:821
$content
Definition: router.php:78
Various tag hooks, registered in Parser::firstCallInit()
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
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 delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags='')
Perform an operation equivalent to preg_replace() with flags.