Split parser related files to have one class in one file
[lhc/web/wiklou.git] / includes / MagicWordArray.php
1 <?php
2
3 /**
4 * See docs/magicword.txt.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Parser
23 */
24
25 use MediaWiki\Logger\LoggerFactory;
26 use MediaWiki\MediaWikiServices;
27
28 /**
29 * Class for handling an array of magic words
30 * @ingroup Parser
31 */
32 class MagicWordArray {
33 /** @var array */
34 public $names = [];
35
36 /** @var MagicWordFactory */
37 private $factory;
38
39 /** @var array */
40 private $hash;
41
42 private $baseRegex;
43
44 private $regex;
45
46 /**
47 * @param array $names
48 * @param MagicWordFactory|null $factory
49 */
50 public function __construct( $names = [], MagicWordFactory $factory = null ) {
51 $this->names = $names;
52 $this->factory = $factory;
53 if ( !$factory ) {
54 $this->factory = MediaWikiServices::getInstance()->getMagicWordFactory();
55 }
56 }
57
58 /**
59 * Add a magic word by name
60 *
61 * @param string $name
62 */
63 public function add( $name ) {
64 $this->names[] = $name;
65 $this->hash = $this->baseRegex = $this->regex = null;
66 }
67
68 /**
69 * Add a number of magic words by name
70 *
71 * @param array $names
72 */
73 public function addArray( $names ) {
74 $this->names = array_merge( $this->names, array_values( $names ) );
75 $this->hash = $this->baseRegex = $this->regex = null;
76 }
77
78 /**
79 * Get a 2-d hashtable for this array
80 * @return array
81 */
82 public function getHash() {
83 if ( is_null( $this->hash ) ) {
84 $this->hash = [ 0 => [], 1 => [] ];
85 foreach ( $this->names as $name ) {
86 $magic = $this->factory->get( $name );
87 $case = intval( $magic->isCaseSensitive() );
88 foreach ( $magic->getSynonyms() as $syn ) {
89 if ( !$case ) {
90 $syn = $this->factory->getContentLanguage()->lc( $syn );
91 }
92 $this->hash[$case][$syn] = $name;
93 }
94 }
95 }
96 return $this->hash;
97 }
98
99 /**
100 * Get the base regex
101 * @return array
102 */
103 public function getBaseRegex() {
104 if ( is_null( $this->baseRegex ) ) {
105 $this->baseRegex = [ 0 => '', 1 => '' ];
106 $allGroups = [];
107 foreach ( $this->names as $name ) {
108 $magic = $this->factory->get( $name );
109 $case = intval( $magic->isCaseSensitive() );
110 foreach ( $magic->getSynonyms() as $i => $syn ) {
111 // Group name must start with a non-digit in PCRE 8.34+
112 $it = strtr( $i, '0123456789', 'abcdefghij' );
113 $groupName = $it . '_' . $name;
114 $group = '(?P<' . $groupName . '>' . preg_quote( $syn, '/' ) . ')';
115 // look for same group names to avoid same named subpatterns in the regex
116 if ( isset( $allGroups[$groupName] ) ) {
117 throw new MWException(
118 __METHOD__ . ': duplicate internal name in magic word array: ' . $name
119 );
120 }
121 $allGroups[$groupName] = true;
122 if ( $this->baseRegex[$case] === '' ) {
123 $this->baseRegex[$case] = $group;
124 } else {
125 $this->baseRegex[$case] .= '|' . $group;
126 }
127 }
128 }
129 }
130 return $this->baseRegex;
131 }
132
133 /**
134 * Get an unanchored regex that does not match parameters
135 * @return array
136 */
137 public function getRegex() {
138 if ( is_null( $this->regex ) ) {
139 $base = $this->getBaseRegex();
140 $this->regex = [ '', '' ];
141 if ( $this->baseRegex[0] !== '' ) {
142 $this->regex[0] = "/{$base[0]}/iuS";
143 }
144 if ( $this->baseRegex[1] !== '' ) {
145 $this->regex[1] = "/{$base[1]}/S";
146 }
147 }
148 return $this->regex;
149 }
150
151 /**
152 * Get a regex for matching variables with parameters
153 *
154 * @return string
155 */
156 public function getVariableRegex() {
157 return str_replace( "\\$1", "(.*?)", $this->getRegex() );
158 }
159
160 /**
161 * Get a regex anchored to the start of the string that does not match parameters
162 *
163 * @return array
164 */
165 public function getRegexStart() {
166 $base = $this->getBaseRegex();
167 $newRegex = [ '', '' ];
168 if ( $base[0] !== '' ) {
169 $newRegex[0] = "/^(?:{$base[0]})/iuS";
170 }
171 if ( $base[1] !== '' ) {
172 $newRegex[1] = "/^(?:{$base[1]})/S";
173 }
174 return $newRegex;
175 }
176
177 /**
178 * Get an anchored regex for matching variables with parameters
179 *
180 * @return array
181 */
182 public function getVariableStartToEndRegex() {
183 $base = $this->getBaseRegex();
184 $newRegex = [ '', '' ];
185 if ( $base[0] !== '' ) {
186 $newRegex[0] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[0]})$/iuS" );
187 }
188 if ( $base[1] !== '' ) {
189 $newRegex[1] = str_replace( "\\$1", "(.*?)", "/^(?:{$base[1]})$/S" );
190 }
191 return $newRegex;
192 }
193
194 /**
195 * @since 1.20
196 * @return array
197 */
198 public function getNames() {
199 return $this->names;
200 }
201
202 /**
203 * Parse a match array from preg_match
204 * Returns array(magic word ID, parameter value)
205 * If there is no parameter value, that element will be false.
206 *
207 * @param array $m
208 *
209 * @throws MWException
210 * @return array
211 */
212 public function parseMatch( $m ) {
213 reset( $m );
214 while ( ( $key = key( $m ) ) !== null ) {
215 $value = current( $m );
216 next( $m );
217 if ( $key === 0 || $value === '' ) {
218 continue;
219 }
220 $parts = explode( '_', $key, 2 );
221 if ( count( $parts ) != 2 ) {
222 // This shouldn't happen
223 // continue;
224 throw new MWException( __METHOD__ . ': bad parameter name' );
225 }
226 list( /* $synIndex */, $magicName ) = $parts;
227 $paramValue = next( $m );
228 return [ $magicName, $paramValue ];
229 }
230 // This shouldn't happen either
231 throw new MWException( __METHOD__ . ': parameter not found' );
232 }
233
234 /**
235 * Match some text, with parameter capture
236 * Returns an array with the magic word name in the first element and the
237 * parameter in the second element.
238 * Both elements are false if there was no match.
239 *
240 * @param string $text
241 *
242 * @return array
243 */
244 public function matchVariableStartToEnd( $text ) {
245 $regexes = $this->getVariableStartToEndRegex();
246 foreach ( $regexes as $regex ) {
247 if ( $regex !== '' ) {
248 $m = [];
249 if ( preg_match( $regex, $text, $m ) ) {
250 return $this->parseMatch( $m );
251 }
252 }
253 }
254 return [ false, false ];
255 }
256
257 /**
258 * Match some text, without parameter capture
259 * Returns the magic word name, or false if there was no capture
260 *
261 * @param string $text
262 *
263 * @return string|bool False on failure
264 */
265 public function matchStartToEnd( $text ) {
266 $hash = $this->getHash();
267 if ( isset( $hash[1][$text] ) ) {
268 return $hash[1][$text];
269 }
270 $lc = $this->factory->getContentLanguage()->lc( $text );
271 return $hash[0][$lc] ?? false;
272 }
273
274 /**
275 * Returns an associative array, ID => param value, for all items that match
276 * Removes the matched items from the input string (passed by reference)
277 *
278 * @param string &$text
279 *
280 * @return array
281 */
282 public function matchAndRemove( &$text ) {
283 $found = [];
284 $regexes = $this->getRegex();
285 foreach ( $regexes as $regex ) {
286 if ( $regex === '' ) {
287 continue;
288 }
289 $matches = [];
290 $res = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
291 if ( $res === false ) {
292 LoggerFactory::getInstance( 'parser' )->warning( 'preg_match_all returned false', [
293 'code' => preg_last_error(),
294 'regex' => $regex,
295 'text' => $text,
296 ] );
297 } elseif ( $res ) {
298 foreach ( $matches as $m ) {
299 list( $name, $param ) = $this->parseMatch( $m );
300 $found[$name] = $param;
301 }
302 }
303 $res = preg_replace( $regex, '', $text );
304 if ( $res === null ) {
305 LoggerFactory::getInstance( 'parser' )->warning( 'preg_replace returned null', [
306 'code' => preg_last_error(),
307 'regex' => $regex,
308 'text' => $text,
309 ] );
310 }
311 $text = $res;
312 }
313 return $found;
314 }
315
316 /**
317 * Return the ID of the magic word at the start of $text, and remove
318 * the prefix from $text.
319 * Return false if no match found and $text is not modified.
320 * Does not match parameters.
321 *
322 * @param string &$text
323 *
324 * @return int|bool False on failure
325 */
326 public function matchStartAndRemove( &$text ) {
327 $regexes = $this->getRegexStart();
328 foreach ( $regexes as $regex ) {
329 if ( $regex === '' ) {
330 continue;
331 }
332 if ( preg_match( $regex, $text, $m ) ) {
333 list( $id, ) = $this->parseMatch( $m );
334 if ( strlen( $m[0] ) >= strlen( $text ) ) {
335 $text = '';
336 } else {
337 $text = substr( $text, strlen( $m[0] ) );
338 }
339 return $id;
340 }
341 }
342 return false;
343 }
344 }