MediaWiki  master
CoreTagHooks.php
Go to the documentation of this file.
1 <?php
25 
30 class CoreTagHooks {
36  public static function register( $parser ) {
37  global $wgRawHtml;
38  $parser->setHook( 'pre', [ __CLASS__, 'pre' ] );
39  $parser->setHook( 'nowiki', [ __CLASS__, 'nowiki' ] );
40  $parser->setHook( 'gallery', [ __CLASS__, 'gallery' ] );
41  $parser->setHook( 'indicator', [ __CLASS__, 'indicator' ] );
42  $parser->setHook( 'langconvert', [ __CLASS__, 'langconvert' ] );
43  if ( $wgRawHtml ) {
44  $parser->setHook( 'html', [ __CLASS__, 'html' ] );
45  }
46  }
47 
59  public static function pre( ?string $content, array $attribs, Parser $parser ): string {
60  // Backwards-compatibility hack
61  $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $content ?? '', 'i' );
62 
63  $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
64  // We need to let both '"' and '&' through,
65  // for strip markers and entities respectively.
66  $content = str_replace(
67  [ '>', '<' ],
68  [ '&gt;', '&lt;' ],
69  $content
70  );
71  return Html::rawElement( 'pre', $attribs, $content );
72  }
73 
91  public static function html( ?string $content, array $attributes, Parser $parser ) {
92  global $wgRawHtml;
93  if ( $wgRawHtml ) {
94  if ( $parser->getOptions()->getAllowUnsafeRawHtml() ) {
95  return [ $content ?? '', 'markerType' => 'nowiki' ];
96  } else {
97  // In a system message where raw html is
98  // not allowed (but it is allowed in other
99  // contexts).
100  return Html::rawElement(
101  'span',
102  [ 'class' => 'error' ],
103  // Using ->text() not ->parse() as
104  // a paranoia measure against a loop.
105  wfMessage( 'rawhtml-notallowed' )->escaped()
106  );
107  }
108  } else {
109  throw new MWException( '<html> extension tag encountered unexpectedly' );
110  }
111  }
112 
130  public static function nowiki( ?string $content, array $attributes, Parser $parser ): array {
131  $content = strtr( $content ?? '', [
132  // lang converter
133  '-{' => '-&#123;',
134  '}-' => '&#125;-',
135  // html tags
136  '<' => '&lt;',
137  '>' => '&gt;'
138  // Note: Both '"' and '&' are not converted.
139  // This allows strip markers and entities through.
140  ] );
141  return [ $content, 'markerType' => 'nowiki' ];
142  }
143 
160  public static function gallery( ?string $content, array $attributes, Parser $parser ):string {
161  return $parser->renderImageGallery( $content ?? '', $attributes );
162  }
163 
176  public static function indicator( ?string $content, array $attributes, Parser $parser, PPFrame $frame ):string {
177  if ( !isset( $attributes['name'] ) || trim( $attributes['name'] ) === '' ) {
178  return '<span class="error">' .
179  wfMessage( 'invalid-indicator-name' )->inContentLanguage()->parse() .
180  '</span>';
181  }
182 
183  $parser->getOutput()->setIndicator(
184  trim( $attributes['name'] ),
185  Parser::stripOuterParagraph( $parser->recursiveTagParseFully( $content ?? '', $frame ) )
186  );
187 
188  return '';
189  }
190 
202  public static function langconvert( ?string $content, array $attributes, Parser $parser, PPFrame $frame ): string {
203  if ( isset( $attributes['from'] ) && isset( $attributes['to'] ) ) {
204  $fromArg = trim( $attributes['from'] );
205  $toArg = trim( $attributes['to'] );
206  $fromLangCode = explode( '-', $fromArg )[0];
207  if ( $fromLangCode && $fromLangCode === explode( '-', $toArg )[0] ) {
208  $lang = MediaWikiServices::getInstance()->getLanguageFactory()
209  ->getLanguage( $fromLangCode );
210  $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
211  ->getLanguageConverter( $lang );
212 
213  # ensure that variants are available,
214  # and the variants are valid BCP 47 codes
215  if ( $converter->hasVariants()
216  && strcasecmp( $fromArg, LanguageCode::bcp47( $fromArg ) ) === 0
217  && strcasecmp( $toArg, LanguageCode::bcp47( $toArg ) ) === 0 ) {
218 
219  $toVariant = $converter->validateVariant( $toArg );
220 
221  if ( $toVariant ) {
222  return $converter->autoConvert(
223  $parser->recursiveTagParse( $content ?? '', $frame ),
224  $toVariant
225  );
226  }
227  }
228  }
229  }
230 
231  return Html::rawElement(
232  'span',
233  [ 'class' => 'error' ],
234  wfMessage( 'invalid-langconvert-attrs' )->inContentLanguage()->parse()
235  );
236  }
237 
238 }
CoreTagHooks\nowiki
static nowiki(?string $content, array $attributes, Parser $parser)
Core parser tag hook function for 'nowiki'.
Definition: CoreTagHooks.php:130
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:173
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
CoreTagHooks
Various tag hooks, registered in every Parser.
Definition: CoreTagHooks.php:30
Parser\recursiveTagParseFully
recursiveTagParseFully( $text, $frame=false)
Fully parse wikitext to fully parsed HTML.
Definition: Parser.php:859
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1231
CoreTagHooks\html
static html(?string $content, array $attributes, Parser $parser)
Core parser tag hook function for 'html', used only when $wgRawHtml is enabled.
Definition: CoreTagHooks.php:91
CoreTagHooks\gallery
static gallery(?string $content, array $attributes, Parser $parser)
Core parser tag hook function for 'gallery'.
Definition: CoreTagHooks.php:160
Parser\getOptions
getOptions()
Definition: Parser.php:1057
$wgRawHtml
$wgRawHtml
Allow raw, unchecked HTML in "<html>...</html>" sections.
Definition: DefaultSettings.php:4758
MWException
MediaWiki exception.
Definition: MWException.php:29
CoreTagHooks\indicator
static indicator(?string $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:176
CoreTagHooks\pre
static pre(?string $content, array $attribs, Parser $parser)
Core parser tag hook function for 'pre'.
Definition: CoreTagHooks.php:59
Parser\recursiveTagParse
recursiveTagParse( $text, $frame=false)
Half-parse wikitext to half-parsed HTML.
Definition: Parser.php:835
$content
$content
Definition: router.php:76
Sanitizer\validateTagAttributes
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:390
PPFrame
Definition: PPFrame.php:28
Parser\renderImageGallery
renderImageGallery( $text, array $params)
Renders an image gallery from a text with one line per image.
Definition: Parser.php:5013
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:87
Parser\stripOuterParagraph
static stripOuterParagraph( $html)
Strip outer.
Definition: Parser.php:6304
LanguageCode\bcp47
static bcp47( $code)
Get the normalised IETF language tag See unit test for examples.
Definition: LanguageCode.php:175
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
Parser\getOutput
getOutput()
Definition: Parser.php:1050
StringUtils\delimiterReplace
static delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags='')
Perform an operation equivalent to preg_replace() with flags.
Definition: StringUtils.php:248
CoreTagHooks\langconvert
static langconvert(?string $content, array $attributes, Parser $parser, PPFrame $frame)
Returns content converted into the requested language variant, using LanguageConverter.
Definition: CoreTagHooks.php:202