MediaWiki  1.34.0
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'] ),
180  Parser::stripOuterParagraph( $parser->recursiveTagParseFully( $content, $frame ) )
181  );
182 
183  return '';
184  }
185 }
CoreTagHooks
Various tag hooks, registered in Parser::firstCallInit()
Definition: CoreTagHooks.php:28
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
CoreTagHooks\indicator
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...
Definition: CoreTagHooks.php:171
CoreTagHooks\nowiki
static nowiki( $content, $attributes, $parser)
Core parser tag hook function for 'nowiki'.
Definition: CoreTagHooks.php:127
CoreTagHooks\gallery
static gallery( $content, $attributes, $parser)
Core parser tag hook function for 'gallery'.
Definition: CoreTagHooks.php:156
$wgRawHtml
$wgRawHtml
Allow raw, unchecked HTML in "<html>...</html>" sections.
Definition: DefaultSettings.php:4265
MWException
MediaWiki exception.
Definition: MWException.php:26
CoreTagHooks\html
static html( $content, $attributes, $parser)
Core parser tag hook function for 'html', used only when $wgRawHtml is enabled.
Definition: CoreTagHooks.php:89
$content
$content
Definition: router.php:78
PPFrame
Definition: PPFrame.php:28
StringUtils\delimiterReplace
static delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags='')
Perform an operation equivalent to preg_replace() with flags.
Definition: StringUtils.php:248
CoreTagHooks\pre
static pre( $text, $attribs, $parser)
Core parser tag hook function for 'pre'.
Definition: CoreTagHooks.php:58