Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CiteParserTagHooks
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 ref
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 references
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 citeForParser
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Cite\Hooks;
4
5use Cite\Cite;
6use Parser;
7use PPFrame;
8
9/**
10 * @license GPL-2.0-or-later
11 */
12class CiteParserTagHooks {
13
14    /**
15     * Enables the two <ref> and <references> tags.
16     */
17    public static function register( Parser $parser ): void {
18        $parser->setHook( 'ref', [ __CLASS__, 'ref' ] );
19        $parser->setHook( 'references', [ __CLASS__, 'references' ] );
20    }
21
22    /**
23     * Parser hook for the <ref> tag.
24     *
25     * @param ?string $text Raw, untrimmed wikitext content of the <ref> tag, if any
26     * @param string[] $argv
27     * @param Parser $parser
28     * @param PPFrame $frame
29     *
30     * @return string HTML
31     */
32    public static function ref(
33        ?string $text,
34        array $argv,
35        Parser $parser,
36        PPFrame $frame
37    ): string {
38        $cite = self::citeForParser( $parser );
39        $result = $cite->ref( $parser, $text, $argv );
40
41        if ( $result === null ) {
42            return htmlspecialchars( "<ref>$text</ref>" );
43        }
44
45        $parserOutput = $parser->getOutput();
46        $parserOutput->addModules( [ 'ext.cite.ux-enhancements' ] );
47        $parserOutput->addModuleStyles( [ 'ext.cite.styles' ] );
48
49        $frame->setVolatile();
50        return $result;
51    }
52
53    /**
54     * Parser hook for the <references> tag.
55     *
56     * @param ?string $text Raw, untrimmed wikitext content of the <references> tag, if any
57     * @param string[] $argv
58     * @param Parser $parser
59     * @param PPFrame $frame
60     *
61     * @return string HTML
62     */
63    public static function references(
64        ?string $text,
65        array $argv,
66        Parser $parser,
67        PPFrame $frame
68    ): string {
69        $cite = self::citeForParser( $parser );
70        $result = $cite->references( $parser, $text, $argv );
71
72        if ( $result === null ) {
73            return htmlspecialchars( $text === null
74                ? "<references/>"
75                : "<references>$text</references>"
76            );
77        }
78
79        $frame->setVolatile();
80        return $result;
81    }
82
83    /**
84     * Get or create Cite state for this parser.
85     */
86    private static function citeForParser( Parser $parser ): Cite {
87        $parser->extCite ??= new Cite( $parser );
88        return $parser->extCite;
89    }
90
91}