Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 onParserFirstCallInit
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 getLocalName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\LabeledSectionTransclusion;
4
5use MediaWiki\Hook\ParserFirstCallInitHook;
6use Parser;
7
8class Hooks implements ParserFirstCallInitHook {
9
10    /**
11     * @param Parser $parser
12     */
13    public function onParserFirstCallInit( $parser ) {
14        $parser->setHook( 'section', [ LabeledSectionTransclusion::class, 'noop' ] );
15        // Register the localized version of <section> as a noop as well
16        $localName = self::getLocalName( 'section', $parser->getContentLanguage()->getCode() );
17        if ( $localName !== null ) {
18            $parser->setHook( $localName, [ LabeledSectionTransclusion::class, 'noop' ] );
19        }
20        $parser->setFunctionHook(
21            'lst', [ LabeledSectionTransclusion::class, 'pfuncIncludeObj' ], Parser::SFH_OBJECT_ARGS
22        );
23        $parser->setFunctionHook(
24            'lstx', [ LabeledSectionTransclusion::class, 'pfuncExcludeObj' ], Parser::SFH_OBJECT_ARGS
25        );
26        $parser->setFunctionHook( 'lsth', [ LabeledSectionTransclusion::class, 'pfuncIncludeHeading' ] );
27    }
28
29    /**
30     * MediaWiki supports localisation for the three kinds of magic words,
31     * such as variable {{NAME}}, behaviours __NAME__, and parser functions
32     * {{#name}}, but it does not support localisation of tag hooks, such
33     * as <name>. Work around that limitation by performing the localisation
34     * at run-time when calling Parser::setHook().
35     */
36    private const HOOK_TRANSLATION = [
37        'de' => [
38            // Tag name
39            'section' => 'Abschnitt',
40            // Tag attributes
41            'begin' => 'Anfang',
42            'end' => 'Ende',
43        ],
44        'he' => [
45            'section' => 'קטע',
46            'begin' => 'התחלה',
47            'end' => 'סוף',
48        ],
49        'pt' => [
50            'section' => 'trecho',
51            'begin' => 'começo',
52            'end' => 'fim',
53        ],
54    ];
55
56    /**
57     * Get local name for tag or tag attribute
58     * @param string $key
59     * @param string $lang
60     * @return string|null
61     */
62    public static function getLocalName( $key, $lang ) {
63        return self::HOOK_TRANSLATION[$lang][$key] ?? null;
64    }
65
66}