Debian Codegraph @

extensions/Cite/src/Hooks/CiteParserTagHooks.php

raw symbols SCIP
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 * @license GPL-2.0-or-later
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 * Enables the two <ref> and <references> tags.
25 */
26 public function register( Parser $parser ): void {
27 $parser->setHook( 'ref', [ $this, 'ref' ] );
28 $parser->setHook( 'references', [ $this, 'references' ] );
29 }
30
31 /**
32 * Parser hook for the <ref> tag.
33 *
34 * @param ?string $text Raw, untrimmed wikitext content of the <ref> tag, if any
35 * @param string[] $argv
36 * @param Parser $parser
37 * @param PPFrame $frame
38 *
39 * @return string HTML
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 * Parser hook for the <references> tag.
64 *
65 * @param ?string $text Raw, untrimmed wikitext content of the <references> tag, if any
66 * @param string[] $argv
67 * @param Parser $parser
68 * @param PPFrame $frame
69 *
70 * @return string HTML
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 * Get or create Cite state for this parser.
94 */
95 private function citeForParser( Parser $parser ): Cite {
96 $parser->extCite ??= new Cite( $parser, $this->config );
97 return $parser->extCite;
98 }
99
100 }