mediawiki-extensions-Cite/includes/CiteParserTagHooks.php
Thiemo Kreuz 7157c7f494 Add @license to all files
Note this codebase appears to be dual-licensed. Some files mention MIT,
but extension.json and some other files mention GPL.

Since WMDE typically uses GPL, I will continue to mark the files we
created as such.

Change-Id: I126da10f7fb13a6d4c99e96e72d024b2e5ecee06
2019-11-19 11:31:08 +01:00

74 lines
1.8 KiB
PHP

<?php
/**
* @license GPL-2.0-or-later
*/
class CiteParserTagHooks {
/**
* Enables the two <ref> and <references> tags.
*
* @param Parser $parser
*/
public static function initialize( Parser $parser ) {
$parser->setHook( 'ref', __CLASS__ . '::ref' );
$parser->setHook( 'references', __CLASS__ . '::references' );
}
/**
* Parser hook for the <ref> tag.
*
* @param string|null $content Raw wikitext content of the <ref> tag.
* @param string[] $attributes
* @param Parser $parser
* @param PPFrame $frame
*
* @return string HTML
*/
public static function ref( $content, array $attributes, Parser $parser, PPFrame $frame ) {
/** @var Cite $cite */
$cite = $parser->extCite;
// @phan-suppress-next-line SecurityCheck-XSS False positive
$result = $cite->ref( $content, $attributes, $parser );
if ( $result === false ) {
return htmlspecialchars( "<ref>$content</ref>" );
}
$parserOutput = $parser->getOutput();
$parserOutput->addModules( 'ext.cite.ux-enhancements' );
$parserOutput->addModuleStyles( 'ext.cite.styles' );
$frame->setVolatile();
return $result;
}
/**
* Parser hook for the <references> tag.
*
* @param string|null $content Raw wikitext content of the <references> tag.
* @param string[] $attributes
* @param Parser $parser
* @param PPFrame $frame
*
* @return string HTML
*/
public static function references( $content, array $attributes, Parser $parser, PPFrame $frame ) {
/** @var Cite $cite */
$cite = $parser->extCite;
// @phan-suppress-next-line SecurityCheck-XSS False positive
$result = $cite->references( $content, $attributes, $parser );
if ( $result === false ) {
return htmlspecialchars( $content === null
? "<references/>"
: "<references>$content</references>"
);
}
$frame->setVolatile();
return $result;
}
}