Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
3 / 4
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Preprocessor
75.00% covered (warning)
75.00%
3 / 4
50.00% covered (danger)
50.00%
1 / 2
3.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 resetParser
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newFrame
n/a
0 / 0
n/a
0 / 0
0
 newCustomFrame
n/a
0 / 0
n/a
0 / 0
0
 newPartNodeArray
n/a
0 / 0
n/a
0 / 0
0
 preprocessToObj
n/a
0 / 0
n/a
0 / 0
0
1<?php
2/**
3 * Interfaces for preprocessors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24use MediaWiki\Parser\Parser;
25
26/**
27 * @ingroup Parser
28 */
29abstract class Preprocessor {
30    /** Transclusion mode flag for Preprocessor::preprocessToObj() */
31    public const DOM_FOR_INCLUSION = 1;
32    /** Language conversion construct omission flag for Preprocessor::preprocessToObj() */
33    public const DOM_LANG_CONVERSION_DISABLED = 2;
34    /** Preprocessor cache bypass flag for Preprocessor::preprocessToObj */
35    public const DOM_UNCACHED = 4;
36
37    /** @var Parser */
38    public $parser;
39
40    /** @var WANObjectCache */
41    protected $wanCache;
42
43    /** @var bool Whether language variant conversion is disabled */
44    protected $disableLangConversion;
45
46    /** @var array Brace matching rules */
47    protected $rules = [
48        '{' => [
49            'end' => '}',
50            'names' => [
51                2 => 'template',
52                3 => 'tplarg',
53            ],
54            'min' => 2,
55            'max' => 3,
56        ],
57        '[' => [
58            'end' => ']',
59            'names' => [ 2 => null ],
60            'min' => 2,
61            'max' => 2,
62        ],
63        '-{' => [
64            'end' => '}-',
65            'names' => [ 2 => null ],
66            'min' => 2,
67            'max' => 2,
68        ],
69    ];
70
71    /**
72     * @param Parser $parser
73     * @param WANObjectCache|null $wanCache
74     * @param array $options Map of additional options, including:
75     *      - disableLangConversion: disable language variant conversion. [Default: false]
76     */
77    public function __construct(
78        Parser $parser,
79        WANObjectCache $wanCache = null,
80        array $options = []
81    ) {
82        $this->parser = $parser;
83        $this->wanCache = $wanCache ?: WANObjectCache::newEmpty();
84        $this->disableLangConversion = !empty( $options['disableLangConversion'] );
85    }
86
87    /**
88     * Allows resetting the internal Parser reference after Preprocessor is
89     * cloned.
90     *
91     * Do not use this function in new code, since this method will be
92     * moved once Parser cloning goes away (T250448)
93     *
94     * @param ?Parser $parser
95     * @internal
96     */
97    public function resetParser( ?Parser $parser ) {
98        // @phan-suppress-next-line PhanPossiblyNullTypeMismatchProperty For internal use only
99        $this->parser = $parser;
100    }
101
102    /**
103     * Create a new top-level frame for expansion of a page
104     *
105     * @return PPFrame
106     */
107    abstract public function newFrame();
108
109    /**
110     * Create a new custom frame for programmatic use of parameter replacement
111     *
112     * This is useful for certain types of extensions
113     *
114     * @param array $args
115     * @return PPFrame
116     */
117    abstract public function newCustomFrame( $args );
118
119    /**
120     * Create a new custom node for programmatic use of parameter replacement
121     *
122     * This is useful for certain types of extensions
123     *
124     * @param array $values
125     */
126    abstract public function newPartNodeArray( $values );
127
128    /**
129     * Get the document object model for the given wikitext
130     *
131     * Any flag added to the $flags parameter here, or any other parameter liable to cause
132     * a change in the DOM tree for the given wikitext, must be passed through the section
133     * identifier in the section edit link and thus back to extractSections().
134     *
135     * @param string $text Wikitext
136     * @param int $flags Bit field of Preprocessor::DOM_* flags:
137     *   - Preprocessor::DOM_FOR_INCLUSION: treat the wikitext as transcluded content from
138     *      a page rather than direct content of a page or message. By default, the text is
139     *      assumed to be undergoing processing for use by direct page views. The use of this
140     *      flag causes text within <noinclude> tags to be ignored, text within <includeonly>
141     *      to be included, and text outside of <onlyinclude> to be ignored.
142     *   - Preprocessor::DOM_NO_LANG_CONV: do not parse "-{ ... }-" constructs, which are
143     *      involved in language variant conversion. (deprecated since 1.36)
144     *   - Preprocessor::DOM_UNCACHED: disable use of the preprocessor cache.
145     * @return PPNode
146     */
147    abstract public function preprocessToObj( $text, $flags = 0 );
148}