| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cite\Hooks; |
| 4 |
|
| 5 |
use Cite\Cite; |
| 6 |
use MediaWiki\Config\Config; |
| 7 |
use MediaWiki\Parser\Parser; |
| 8 |
use MediaWiki\Parser\PPFrame; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class CiteParserTagHooks { |
| 14 |
|
| 15 |
private Config $config; |
| 16 |
|
| 17 |
public function __construct( |
| 18 |
Config $config |
| 19 |
) { |
| 20 |
$this->config = $config; |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function register( Parser $parser ): void { |
| 27 |
$parser->setHook( 'ref', [ $this, 'ref' ] ); |
| 28 |
$parser->setHook( 'references', [ $this, 'references' ] ); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function ref( |
| 42 |
?string $text, |
| 43 |
array $argv, |
| 44 |
Parser $parser, |
| 45 |
PPFrame $frame |
| 46 |
): string { |
| 47 |
$cite = $this->citeForParser( $parser ); |
| 48 |
$result = $cite->ref( $parser, $text, $argv ); |
| 49 |
|
| 50 |
if ( $result === null ) { |
| 51 |
return htmlspecialchars( "<ref>$text</ref>" ); |
| 52 |
} |
| 53 |
|
| 54 |
$parserOutput = $parser->getOutput(); |
| 55 |
$parserOutput->addModules( [ 'ext.cite.ux-enhancements' ] ); |
| 56 |
$parserOutput->addModuleStyles( [ 'ext.cite.styles' ] ); |
| 57 |
|
| 58 |
$frame->setVolatile(); |
| 59 |
return $result; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function references( |
| 73 |
?string $text, |
| 74 |
array $argv, |
| 75 |
Parser $parser, |
| 76 |
PPFrame $frame |
| 77 |
): string { |
| 78 |
$cite = $this->citeForParser( $parser ); |
| 79 |
$result = $cite->references( $parser, $text, $argv ); |
| 80 |
|
| 81 |
if ( $result === null ) { |
| 82 |
return htmlspecialchars( $text === null |
| 83 |
? "<references/>" |
| 84 |
: "<references>$text</references>" |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
$frame->setVolatile(); |
| 89 |
return $result; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private function citeForParser( Parser $parser ): Cite { |
| 96 |
$parser->extCite ??= new Cite( $parser, $this->config ); |
| 97 |
return $parser->extCite; |
| 98 |
} |
| 99 |
|
| 100 |
} |