Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParserHelper
85.71% covered (warning)
85.71%
6 / 7
66.67% covered (warning)
66.67%
2 / 3
3.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 expandTemplateArgs
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 fetchTemplateTextAndTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace ProofreadPage\Index;
4
5use MediaWiki\MediaWikiServices;
6use MediaWiki\Title\Title;
7use Parser;
8use ParserOptions;
9
10/**
11 * @license GPL-2.0-or-later
12 *
13 * Content of a Index: page
14 */
15class ParserHelper {
16
17    /**
18     * @var Parser
19     */
20    private $parser;
21
22    /**
23     * @param Title|null $title
24     * @param ParserOptions $options
25     */
26    public function __construct( ?Title $title, ParserOptions $options ) {
27        $this->parser = MediaWikiServices::getInstance()->getParserFactory()->create();
28        $this->parser->startExternalParse( $title, $options, Parser::OT_PREPROCESS );
29    }
30
31    /**
32     * @param string $text the wikitext that should see its template arguments expanded
33     * @param string[] $args the arguments to use during the expansion
34     * @return string
35     */
36    public function expandTemplateArgs( $text, array $args ) {
37        // We build a frame with the arguments for the template
38        $frame = $this->parser->getPreprocessor()->newCustomFrame( $args );
39
40        // We replace the arguments calls by their values
41        $dom = $this->parser->preprocessToDom( $text, Parser::PTD_FOR_INCLUSION );
42        $text = $frame->expand( $dom );
43
44        // We take care of removing the tags placeholders
45        return $this->parser->getStripState()->unstripBoth( $text );
46    }
47
48    /**
49     * Fetches unparsed text of a template
50     * @param Title $title
51     * @see \Parser::fetchTemplateAndTitle()
52     * @return array (string|bool, Title)
53     */
54    public function fetchTemplateTextAndTitle( Title $title ) {
55        return $this->parser->fetchTemplateAndTitle( $title );
56    }
57}