Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 onParserFirstCallInit
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onParserAfterParse
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 onRegistration
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 *
4 * @license MIT
5 * @file
6 *
7 * @author Yuri Astrakhan
8 */
9
10namespace Kartographer;
11
12use Kartographer\Tag\LegacyMapFrame;
13use Kartographer\Tag\LegacyMapLink;
14use Kartographer\Tag\LegacyTagHandler;
15use MediaWiki\Config\Config;
16use MediaWiki\Hook\ParserAfterParseHook;
17use MediaWiki\Hook\ParserFirstCallInitHook;
18use MediaWiki\Languages\LanguageNameUtils;
19use MediaWiki\Parser\Parser;
20use MediaWiki\Parser\StripState;
21use MediaWiki\Settings\SettingsBuilder;
22use MediaWiki\Title\TitleFormatter;
23
24/**
25 * @license MIT
26 */
27class Hooks implements
28    ParserFirstCallInitHook,
29    ParserAfterParseHook
30{
31    private LegacyMapLink $legacyMapLink;
32    private LegacyMapFrame $legacyMapFrame;
33
34    public function __construct(
35        Config $config,
36        LanguageNameUtils $languageCodeValidator,
37        TitleFormatter $titleFormatter
38    ) {
39        $this->legacyMapLink = new LegacyMapLink(
40            $config,
41            $languageCodeValidator,
42            $titleFormatter
43        );
44        $this->legacyMapFrame = new LegacyMapFrame(
45            $config,
46            $languageCodeValidator,
47            $titleFormatter
48        );
49    }
50
51    /**
52     * ParserFirstCallInit hook handler
53     * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
54     * @param Parser $parser
55     */
56    public function onParserFirstCallInit( $parser ) {
57        $parser->setHook( LegacyMapLink::TAG, [ $this->legacyMapLink, 'handle' ] );
58        $parser->setHook( LegacyMapFrame::TAG, [ $this->legacyMapFrame, 'handle' ] );
59    }
60
61    /**
62     * ParserAfterParse hook handler
63     * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterParse
64     * @param Parser $parser
65     * @param string &$text Text being parsed
66     * @param StripState $stripState StripState used
67     */
68    public function onParserAfterParse( $parser, &$text, $stripState ) {
69        $output = $parser->getOutput();
70        $state = State::getState( $output );
71
72        if ( $state ) {
73            $options = $parser->getOptions();
74            $isPreview = $options->getIsPreview() || $options->getIsSectionPreview();
75            $tracker = new ParserFunctionTracker( $parser );
76            LegacyTagHandler::finalParseStep( $state, $output, $isPreview, $tracker );
77        }
78    }
79
80    /**
81     * Sets $wgKartographerMapServer in integration test/CI setup
82     * This is needed by parserTests that define articles containing Kartographer content - parsing them when
83     * inserting them in the test DB requires $wgKartographerMapServer to be defined early.
84     */
85    public static function onRegistration( array $extInfo, SettingsBuilder $settings ) {
86        if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_QUIBBLE_CI' ) ) {
87            $settings->overrideConfigValue( 'KartographerMapServer', 'https://maps.wikimedia.org' );
88        }
89    }
90}