Merge "resourceloader: Clean up and better document module list (un)packing"
[lhc/web/wiklou.git] / includes / parser / Preprocessor_Hash.php
1 <?php
2 /**
3 * Preprocessor using PHP arrays
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
24 /**
25 * Differences from DOM schema:
26 * * attribute nodes are children
27 * * "<h>" nodes that aren't at the top are replaced with <possible-h>
28 *
29 * Nodes are stored in a recursive array data structure. A node store is an
30 * array where each element may be either a scalar (representing a text node)
31 * or a "descriptor", which is a two-element array where the first element is
32 * the node name and the second element is the node store for the children.
33 *
34 * Attributes are represented as children that have a node name starting with
35 * "@", and a single text node child.
36 *
37 * @todo: Consider replacing descriptor arrays with objects of a new class.
38 * Benchmark and measure resulting memory impact.
39 *
40 * @ingroup Parser
41 */
42 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
43 class Preprocessor_Hash extends Preprocessor {
44
45 /**
46 * @var Parser
47 */
48 public $parser;
49
50 const CACHE_PREFIX = 'preprocess-hash';
51 const CACHE_VERSION = 2;
52
53 public function __construct( $parser ) {
54 $this->parser = $parser;
55 }
56
57 /**
58 * @return PPFrame_Hash
59 */
60 public function newFrame() {
61 return new PPFrame_Hash( $this );
62 }
63
64 /**
65 * @param array $args
66 * @return PPCustomFrame_Hash
67 */
68 public function newCustomFrame( $args ) {
69 return new PPCustomFrame_Hash( $this, $args );
70 }
71
72 /**
73 * @param array $values
74 * @return PPNode_Hash_Array
75 */
76 public function newPartNodeArray( $values ) {
77 $list = [];
78
79 foreach ( $values as $k => $val ) {
80 if ( is_int( $k ) ) {
81 $store = [ [ 'part', [
82 [ 'name', [ [ '@index', [ $k ] ] ] ],
83 [ 'value', [ strval( $val ) ] ],
84 ] ] ];
85 } else {
86 $store = [ [ 'part', [
87 [ 'name', [ strval( $k ) ] ],
88 '=',
89 [ 'value', [ strval( $val ) ] ],
90 ] ] ];
91 }
92
93 $list[] = new PPNode_Hash_Tree( $store, 0 );
94 }
95
96 $node = new PPNode_Hash_Array( $list );
97 return $node;
98 }
99
100 /**
101 * Preprocess some wikitext and return the document tree.
102 *
103 * @param string $text The text to parse
104 * @param int $flags Bitwise combination of:
105 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<includeonly>" as if the text is being
106 * included. Default is to assume a direct page view.
107 *
108 * The generated DOM tree must depend only on the input text and the flags.
109 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of T6899.
110 *
111 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
112 * change in the DOM tree for a given text, must be passed through the section identifier
113 * in the section edit link and thus back to extractSections().
114 *
115 * @throws MWException
116 * @return PPNode_Hash_Tree
117 */
118 public function preprocessToObj( $text, $flags = 0 ) {
119 global $wgDisableLangConversion;
120
121 $tree = $this->cacheGetTree( $text, $flags );
122 if ( $tree !== false ) {
123 $store = json_decode( $tree );
124 if ( is_array( $store ) ) {
125 return new PPNode_Hash_Tree( $store, 0 );
126 }
127 }
128
129 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
130
131 $xmlishElements = $this->parser->getStripList();
132 $xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ];
133 $enableOnlyinclude = false;
134 if ( $forInclusion ) {
135 $ignoredTags = [ 'includeonly', '/includeonly' ];
136 $ignoredElements = [ 'noinclude' ];
137 $xmlishElements[] = 'noinclude';
138 if ( strpos( $text, '<onlyinclude>' ) !== false
139 && strpos( $text, '</onlyinclude>' ) !== false
140 ) {
141 $enableOnlyinclude = true;
142 }
143 } else {
144 $ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ];
145 $ignoredElements = [ 'includeonly' ];
146 $xmlishElements[] = 'includeonly';
147 }
148 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
149
150 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
151 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
152
153 $stack = new PPDStack_Hash;
154
155 $searchBase = "[{<\n";
156 if ( !$wgDisableLangConversion ) {
157 $searchBase .= '-';
158 }
159
160 // For fast reverse searches
161 $revText = strrev( $text );
162 $lengthText = strlen( $text );
163
164 // Input pointer, starts out pointing to a pseudo-newline before the start
165 $i = 0;
166 // Current accumulator. See the doc comment for Preprocessor_Hash for the format.
167 $accum =& $stack->getAccum();
168 // True to find equals signs in arguments
169 $findEquals = false;
170 // True to take notice of pipe characters
171 $findPipe = false;
172 $headingIndex = 1;
173 // True if $i is inside a possible heading
174 $inHeading = false;
175 // True if there are no more greater-than (>) signs right of $i
176 $noMoreGT = false;
177 // Map of tag name => true if there are no more closing tags of given type right of $i
178 $noMoreClosingTag = [];
179 // True to ignore all input up to the next <onlyinclude>
180 $findOnlyinclude = $enableOnlyinclude;
181 // Do a line-start run without outputting an LF character
182 $fakeLineStart = true;
183
184 while ( true ) {
185 // $this->memCheck();
186
187 if ( $findOnlyinclude ) {
188 // Ignore all input up to the next <onlyinclude>
189 $startPos = strpos( $text, '<onlyinclude>', $i );
190 if ( $startPos === false ) {
191 // Ignored section runs to the end
192 $accum[] = [ 'ignore', [ substr( $text, $i ) ] ];
193 break;
194 }
195 $tagEndPos = $startPos + strlen( '<onlyinclude>' ); // past-the-end
196 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
197 $i = $tagEndPos;
198 $findOnlyinclude = false;
199 }
200
201 if ( $fakeLineStart ) {
202 $found = 'line-start';
203 $curChar = '';
204 } else {
205 # Find next opening brace, closing brace or pipe
206 $search = $searchBase;
207 if ( $stack->top === false ) {
208 $currentClosing = '';
209 } elseif (
210 $stack->top->close === '}-' &&
211 $stack->top->count > 2
212 ) {
213 # adjust closing for -{{{...{{
214 $currentClosing = '}';
215 $search .= $currentClosing;
216 } else {
217 $currentClosing = $stack->top->close;
218 $search .= $currentClosing;
219 }
220 if ( $findPipe ) {
221 $search .= '|';
222 }
223 if ( $findEquals ) {
224 // First equals will be for the template
225 $search .= '=';
226 }
227 $rule = null;
228 # Output literal section, advance input counter
229 $literalLength = strcspn( $text, $search, $i );
230 if ( $literalLength > 0 ) {
231 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
232 $i += $literalLength;
233 }
234 if ( $i >= $lengthText ) {
235 if ( $currentClosing == "\n" ) {
236 // Do a past-the-end run to finish off the heading
237 $curChar = '';
238 $found = 'line-end';
239 } else {
240 # All done
241 break;
242 }
243 } else {
244 $curChar = $curTwoChar = $text[$i];
245 if ( ( $i + 1 ) < $lengthText ) {
246 $curTwoChar .= $text[$i + 1];
247 }
248 if ( $curChar == '|' ) {
249 $found = 'pipe';
250 } elseif ( $curChar == '=' ) {
251 $found = 'equals';
252 } elseif ( $curChar == '<' ) {
253 $found = 'angle';
254 } elseif ( $curChar == "\n" ) {
255 if ( $inHeading ) {
256 $found = 'line-end';
257 } else {
258 $found = 'line-start';
259 }
260 } elseif ( $curTwoChar == $currentClosing ) {
261 $found = 'close';
262 $curChar = $curTwoChar;
263 } elseif ( $curChar == $currentClosing ) {
264 $found = 'close';
265 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
266 $curChar = $curTwoChar;
267 $found = 'open';
268 $rule = $this->rules[$curChar];
269 } elseif ( isset( $this->rules[$curChar] ) ) {
270 $found = 'open';
271 $rule = $this->rules[$curChar];
272 } else {
273 # Some versions of PHP have a strcspn which stops on
274 # null characters; ignore these and continue.
275 # We also may get '-' and '}' characters here which
276 # don't match -{ or $currentClosing. Add these to
277 # output and continue.
278 if ( $curChar == '-' || $curChar == '}' ) {
279 self::addLiteral( $accum, $curChar );
280 }
281 ++$i;
282 continue;
283 }
284 }
285 }
286
287 if ( $found == 'angle' ) {
288 $matches = false;
289 // Handle </onlyinclude>
290 if ( $enableOnlyinclude
291 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
292 ) {
293 $findOnlyinclude = true;
294 continue;
295 }
296
297 // Determine element name
298 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
299 // Element name missing or not listed
300 self::addLiteral( $accum, '<' );
301 ++$i;
302 continue;
303 }
304 // Handle comments
305 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
306 // To avoid leaving blank lines, when a sequence of
307 // space-separated comments is both preceded and followed by
308 // a newline (ignoring spaces), then
309 // trim leading and trailing spaces and the trailing newline.
310
311 // Find the end
312 $endPos = strpos( $text, '-->', $i + 4 );
313 if ( $endPos === false ) {
314 // Unclosed comment in input, runs to end
315 $inner = substr( $text, $i );
316 $accum[] = [ 'comment', [ $inner ] ];
317 $i = $lengthText;
318 } else {
319 // Search backwards for leading whitespace
320 $wsStart = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
321
322 // Search forwards for trailing whitespace
323 // $wsEnd will be the position of the last space (or the '>' if there's none)
324 $wsEnd = $endPos + 2 + strspn( $text, " \t", $endPos + 3 );
325
326 // Keep looking forward as long as we're finding more
327 // comments.
328 $comments = [ [ $wsStart, $wsEnd ] ];
329 while ( substr( $text, $wsEnd + 1, 4 ) == '<!--' ) {
330 $c = strpos( $text, '-->', $wsEnd + 4 );
331 if ( $c === false ) {
332 break;
333 }
334 $c = $c + 2 + strspn( $text, " \t", $c + 3 );
335 $comments[] = [ $wsEnd + 1, $c ];
336 $wsEnd = $c;
337 }
338
339 // Eat the line if possible
340 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
341 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
342 // it's a possible beneficial b/c break.
343 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
344 && substr( $text, $wsEnd + 1, 1 ) == "\n"
345 ) {
346 // Remove leading whitespace from the end of the accumulator
347 $wsLength = $i - $wsStart;
348 $endIndex = count( $accum ) - 1;
349
350 // Sanity check
351 if ( $wsLength > 0
352 && $endIndex >= 0
353 && is_string( $accum[$endIndex] )
354 && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
355 ) {
356 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
357 }
358
359 // Dump all but the last comment to the accumulator
360 foreach ( $comments as $j => $com ) {
361 $startPos = $com[0];
362 $endPos = $com[1] + 1;
363 if ( $j == ( count( $comments ) - 1 ) ) {
364 break;
365 }
366 $inner = substr( $text, $startPos, $endPos - $startPos );
367 $accum[] = [ 'comment', [ $inner ] ];
368 }
369
370 // Do a line-start run next time to look for headings after the comment
371 $fakeLineStart = true;
372 } else {
373 // No line to eat, just take the comment itself
374 $startPos = $i;
375 $endPos += 2;
376 }
377
378 if ( $stack->top ) {
379 $part = $stack->top->getCurrentPart();
380 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
381 $part->visualEnd = $wsStart;
382 }
383 // Else comments abutting, no change in visual end
384 $part->commentEnd = $endPos;
385 }
386 $i = $endPos + 1;
387 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
388 $accum[] = [ 'comment', [ $inner ] ];
389 }
390 continue;
391 }
392 $name = $matches[1];
393 $lowerName = strtolower( $name );
394 $attrStart = $i + strlen( $name ) + 1;
395
396 // Find end of tag
397 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
398 if ( $tagEndPos === false ) {
399 // Infinite backtrack
400 // Disable tag search to prevent worst-case O(N^2) performance
401 $noMoreGT = true;
402 self::addLiteral( $accum, '<' );
403 ++$i;
404 continue;
405 }
406
407 // Handle ignored tags
408 if ( in_array( $lowerName, $ignoredTags ) ) {
409 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
410 $i = $tagEndPos + 1;
411 continue;
412 }
413
414 $tagStartPos = $i;
415 if ( $text[$tagEndPos - 1] == '/' ) {
416 // Short end tag
417 $attrEnd = $tagEndPos - 1;
418 $inner = null;
419 $i = $tagEndPos + 1;
420 $close = null;
421 } else {
422 $attrEnd = $tagEndPos;
423 // Find closing tag
424 if (
425 !isset( $noMoreClosingTag[$name] ) &&
426 preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
427 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
428 ) {
429 $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
430 $i = $matches[0][1] + strlen( $matches[0][0] );
431 $close = $matches[0][0];
432 } else {
433 // No end tag
434 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
435 // Let it run out to the end of the text.
436 $inner = substr( $text, $tagEndPos + 1 );
437 $i = $lengthText;
438 $close = null;
439 } else {
440 // Don't match the tag, treat opening tag as literal and resume parsing.
441 $i = $tagEndPos + 1;
442 self::addLiteral( $accum,
443 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
444 // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
445 $noMoreClosingTag[$name] = true;
446 continue;
447 }
448 }
449 }
450 // <includeonly> and <noinclude> just become <ignore> tags
451 if ( in_array( $lowerName, $ignoredElements ) ) {
452 $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
453 continue;
454 }
455
456 if ( $attrEnd <= $attrStart ) {
457 $attr = '';
458 } else {
459 // Note that the attr element contains the whitespace between name and attribute,
460 // this is necessary for precise reconstruction during pre-save transform.
461 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
462 }
463
464 $children = [
465 [ 'name', [ $name ] ],
466 [ 'attr', [ $attr ] ] ];
467 if ( $inner !== null ) {
468 $children[] = [ 'inner', [ $inner ] ];
469 }
470 if ( $close !== null ) {
471 $children[] = [ 'close', [ $close ] ];
472 }
473 $accum[] = [ 'ext', $children ];
474 } elseif ( $found == 'line-start' ) {
475 // Is this the start of a heading?
476 // Line break belongs before the heading element in any case
477 if ( $fakeLineStart ) {
478 $fakeLineStart = false;
479 } else {
480 self::addLiteral( $accum, $curChar );
481 $i++;
482 }
483
484 $count = strspn( $text, '=', $i, 6 );
485 if ( $count == 1 && $findEquals ) {
486 // DWIM: This looks kind of like a name/value separator.
487 // Let's let the equals handler have it and break the potential
488 // heading. This is heuristic, but AFAICT the methods for
489 // completely correct disambiguation are very complex.
490 } elseif ( $count > 0 ) {
491 $piece = [
492 'open' => "\n",
493 'close' => "\n",
494 'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ],
495 'startPos' => $i,
496 'count' => $count ];
497 $stack->push( $piece );
498 $accum =& $stack->getAccum();
499 $stackFlags = $stack->getFlags();
500 if ( isset( $stackFlags['findEquals'] ) ) {
501 $findEquals = $stackFlags['findEquals'];
502 }
503 if ( isset( $stackFlags['findPipe'] ) ) {
504 $findPipe = $stackFlags['findPipe'];
505 }
506 if ( isset( $stackFlags['inHeading'] ) ) {
507 $inHeading = $stackFlags['inHeading'];
508 }
509 $i += $count;
510 }
511 } elseif ( $found == 'line-end' ) {
512 $piece = $stack->top;
513 // A heading must be open, otherwise \n wouldn't have been in the search list
514 assert( $piece->open === "\n" );
515 $part = $piece->getCurrentPart();
516 // Search back through the input to see if it has a proper close.
517 // Do this using the reversed string since the other solutions
518 // (end anchor, etc.) are inefficient.
519 $wsLength = strspn( $revText, " \t", $lengthText - $i );
520 $searchStart = $i - $wsLength;
521 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
522 // Comment found at line end
523 // Search for equals signs before the comment
524 $searchStart = $part->visualEnd;
525 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
526 }
527 $count = $piece->count;
528 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
529 if ( $equalsLength > 0 ) {
530 if ( $searchStart - $equalsLength == $piece->startPos ) {
531 // This is just a single string of equals signs on its own line
532 // Replicate the doHeadings behavior /={count}(.+)={count}/
533 // First find out how many equals signs there really are (don't stop at 6)
534 $count = $equalsLength;
535 if ( $count < 3 ) {
536 $count = 0;
537 } else {
538 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
539 }
540 } else {
541 $count = min( $equalsLength, $count );
542 }
543 if ( $count > 0 ) {
544 // Normal match, output <h>
545 $element = [ [ 'possible-h',
546 array_merge(
547 [
548 [ '@level', [ $count ] ],
549 [ '@i', [ $headingIndex++ ] ]
550 ],
551 $accum
552 )
553 ] ];
554 } else {
555 // Single equals sign on its own line, count=0
556 $element = $accum;
557 }
558 } else {
559 // No match, no <h>, just pass down the inner text
560 $element = $accum;
561 }
562 // Unwind the stack
563 $stack->pop();
564 $accum =& $stack->getAccum();
565 $stackFlags = $stack->getFlags();
566 if ( isset( $stackFlags['findEquals'] ) ) {
567 $findEquals = $stackFlags['findEquals'];
568 }
569 if ( isset( $stackFlags['findPipe'] ) ) {
570 $findPipe = $stackFlags['findPipe'];
571 }
572 if ( isset( $stackFlags['inHeading'] ) ) {
573 $inHeading = $stackFlags['inHeading'];
574 }
575
576 // Append the result to the enclosing accumulator
577 array_splice( $accum, count( $accum ), 0, $element );
578
579 // Note that we do NOT increment the input pointer.
580 // This is because the closing linebreak could be the opening linebreak of
581 // another heading. Infinite loops are avoided because the next iteration MUST
582 // hit the heading open case above, which unconditionally increments the
583 // input pointer.
584 } elseif ( $found == 'open' ) {
585 # count opening brace characters
586 $curLen = strlen( $curChar );
587 $count = ( $curLen > 1 ) ?
588 # allow the final character to repeat
589 strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
590 strspn( $text, $curChar, $i );
591
592 # we need to add to stack only if opening brace count is enough for one of the rules
593 if ( $count >= $rule['min'] ) {
594 # Add it to the stack
595 $piece = [
596 'open' => $curChar,
597 'close' => $rule['end'],
598 'count' => $count,
599 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
600 ];
601
602 $stack->push( $piece );
603 $accum =& $stack->getAccum();
604 $stackFlags = $stack->getFlags();
605 if ( isset( $stackFlags['findEquals'] ) ) {
606 $findEquals = $stackFlags['findEquals'];
607 }
608 if ( isset( $stackFlags['findPipe'] ) ) {
609 $findPipe = $stackFlags['findPipe'];
610 }
611 if ( isset( $stackFlags['inHeading'] ) ) {
612 $inHeading = $stackFlags['inHeading'];
613 }
614 } else {
615 # Add literal brace(s)
616 self::addLiteral( $accum, str_repeat( $curChar, $count ) );
617 }
618 $i += $count;
619 } elseif ( $found == 'close' ) {
620 $piece = $stack->top;
621 # lets check if there are enough characters for closing brace
622 $maxCount = $piece->count;
623 if ( $piece->close === '}-' && $curChar === '}' ) {
624 $maxCount--; # don't try to match closing '-' as a '}'
625 }
626 $curLen = strlen( $curChar );
627 $count = ( $curLen > 1 ) ? $curLen :
628 strspn( $text, $curChar, $i, $maxCount );
629
630 # check for maximum matching characters (if there are 5 closing
631 # characters, we will probably need only 3 - depending on the rules)
632 $rule = $this->rules[$piece->open];
633 if ( $piece->close === '}-' && $piece->count > 2 ) {
634 # tweak for -{..{{ }}..}-
635 $rule = $this->rules['{'];
636 }
637 if ( $count > $rule['max'] ) {
638 # The specified maximum exists in the callback array, unless the caller
639 # has made an error
640 $matchingCount = $rule['max'];
641 } else {
642 # Count is less than the maximum
643 # Skip any gaps in the callback array to find the true largest match
644 # Need to use array_key_exists not isset because the callback can be null
645 $matchingCount = $count;
646 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
647 --$matchingCount;
648 }
649 }
650
651 if ( $matchingCount <= 0 ) {
652 # No matching element found in callback array
653 # Output a literal closing brace and continue
654 $endText = substr( $text, $i, $count );
655 self::addLiteral( $accum, $endText );
656 $i += $count;
657 continue;
658 }
659 $name = $rule['names'][$matchingCount];
660 if ( $name === null ) {
661 // No element, just literal text
662 $endText = substr( $text, $i, $matchingCount );
663 $element = $piece->breakSyntax( $matchingCount );
664 self::addLiteral( $element, $endText );
665 } else {
666 # Create XML element
667 $parts = $piece->parts;
668 $titleAccum = $parts[0]->out;
669 unset( $parts[0] );
670
671 $children = [];
672
673 # The invocation is at the start of the line if lineStart is set in
674 # the stack, and all opening brackets are used up.
675 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
676 $children[] = [ '@lineStart', [ 1 ] ];
677 }
678 $titleNode = [ 'title', $titleAccum ];
679 $children[] = $titleNode;
680 $argIndex = 1;
681 foreach ( $parts as $part ) {
682 if ( isset( $part->eqpos ) ) {
683 $equalsNode = $part->out[$part->eqpos];
684 $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
685 $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
686 $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
687 $children[] = $partNode;
688 } else {
689 $nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ];
690 $valueNode = [ 'value', $part->out ];
691 $partNode = [ 'part', [ $nameNode, $valueNode ] ];
692 $children[] = $partNode;
693 }
694 }
695 $element = [ [ $name, $children ] ];
696 }
697
698 # Advance input pointer
699 $i += $matchingCount;
700
701 # Unwind the stack
702 $stack->pop();
703 $accum =& $stack->getAccum();
704
705 # Re-add the old stack element if it still has unmatched opening characters remaining
706 if ( $matchingCount < $piece->count ) {
707 $piece->parts = [ new PPDPart_Hash ];
708 $piece->count -= $matchingCount;
709 # do we still qualify for any callback with remaining count?
710 $min = $this->rules[$piece->open]['min'];
711 if ( $piece->count >= $min ) {
712 $stack->push( $piece );
713 $accum =& $stack->getAccum();
714 } else {
715 $s = substr( $piece->open, 0, -1 );
716 $s .= str_repeat(
717 substr( $piece->open, -1 ),
718 $piece->count - strlen( $s )
719 );
720 self::addLiteral( $accum, $s );
721 }
722 }
723
724 $stackFlags = $stack->getFlags();
725 if ( isset( $stackFlags['findEquals'] ) ) {
726 $findEquals = $stackFlags['findEquals'];
727 }
728 if ( isset( $stackFlags['findPipe'] ) ) {
729 $findPipe = $stackFlags['findPipe'];
730 }
731 if ( isset( $stackFlags['inHeading'] ) ) {
732 $inHeading = $stackFlags['inHeading'];
733 }
734
735 # Add XML element to the enclosing accumulator
736 array_splice( $accum, count( $accum ), 0, $element );
737 } elseif ( $found == 'pipe' ) {
738 $findEquals = true; // shortcut for getFlags()
739 $stack->addPart();
740 $accum =& $stack->getAccum();
741 ++$i;
742 } elseif ( $found == 'equals' ) {
743 $findEquals = false; // shortcut for getFlags()
744 $accum[] = [ 'equals', [ '=' ] ];
745 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
746 ++$i;
747 }
748 }
749
750 # Output any remaining unclosed brackets
751 foreach ( $stack->stack as $piece ) {
752 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
753 }
754
755 # Enable top-level headings
756 foreach ( $stack->rootAccum as &$node ) {
757 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
758 $node[PPNode_Hash_Tree::NAME] = 'h';
759 }
760 }
761
762 $rootStore = [ [ 'root', $stack->rootAccum ] ];
763 $rootNode = new PPNode_Hash_Tree( $rootStore, 0 );
764
765 // Cache
766 $tree = json_encode( $rootStore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
767 if ( $tree !== false ) {
768 $this->cacheSetTree( $text, $flags, $tree );
769 }
770
771 return $rootNode;
772 }
773
774 private static function addLiteral( array &$accum, $text ) {
775 $n = count( $accum );
776 if ( $n && is_string( $accum[$n - 1] ) ) {
777 $accum[$n - 1] .= $text;
778 } else {
779 $accum[] = $text;
780 }
781 }
782 }
783
784 /**
785 * Stack class to help Preprocessor::preprocessToObj()
786 * @ingroup Parser
787 */
788 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
789 class PPDStack_Hash extends PPDStack {
790
791 public function __construct() {
792 $this->elementClass = PPDStackElement_Hash::class;
793 parent::__construct();
794 $this->rootAccum = [];
795 }
796 }
797
798 /**
799 * @ingroup Parser
800 */
801 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
802 class PPDStackElement_Hash extends PPDStackElement {
803
804 public function __construct( $data = [] ) {
805 $this->partClass = PPDPart_Hash::class;
806 parent::__construct( $data );
807 }
808
809 /**
810 * Get the accumulator that would result if the close is not found.
811 *
812 * @param int|bool $openingCount
813 * @return array
814 */
815 public function breakSyntax( $openingCount = false ) {
816 if ( $this->open == "\n" ) {
817 $accum = $this->parts[0]->out;
818 } else {
819 if ( $openingCount === false ) {
820 $openingCount = $this->count;
821 }
822 $s = substr( $this->open, 0, -1 );
823 $s .= str_repeat(
824 substr( $this->open, -1 ),
825 $openingCount - strlen( $s )
826 );
827 $accum = [ $s ];
828 $lastIndex = 0;
829 $first = true;
830 foreach ( $this->parts as $part ) {
831 if ( $first ) {
832 $first = false;
833 } elseif ( is_string( $accum[$lastIndex] ) ) {
834 $accum[$lastIndex] .= '|';
835 } else {
836 $accum[++$lastIndex] = '|';
837 }
838 foreach ( $part->out as $node ) {
839 if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) {
840 $accum[$lastIndex] .= $node;
841 } else {
842 $accum[++$lastIndex] = $node;
843 }
844 }
845 }
846 }
847 return $accum;
848 }
849 }
850
851 /**
852 * @ingroup Parser
853 */
854 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
855 class PPDPart_Hash extends PPDPart {
856
857 public function __construct( $out = '' ) {
858 if ( $out !== '' ) {
859 $accum = [ $out ];
860 } else {
861 $accum = [];
862 }
863 parent::__construct( $accum );
864 }
865 }
866
867 /**
868 * An expansion frame, used as a context to expand the result of preprocessToObj()
869 * @ingroup Parser
870 */
871 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
872 class PPFrame_Hash implements PPFrame {
873
874 /**
875 * @var Parser
876 */
877 public $parser;
878
879 /**
880 * @var Preprocessor
881 */
882 public $preprocessor;
883
884 /**
885 * @var Title
886 */
887 public $title;
888 public $titleCache;
889
890 /**
891 * Hashtable listing templates which are disallowed for expansion in this frame,
892 * having been encountered previously in parent frames.
893 */
894 public $loopCheckHash;
895
896 /**
897 * Recursion depth of this frame, top = 0
898 * Note that this is NOT the same as expansion depth in expand()
899 */
900 public $depth;
901
902 private $volatile = false;
903 private $ttl = null;
904
905 /**
906 * @var array
907 */
908 protected $childExpansionCache;
909
910 /**
911 * Construct a new preprocessor frame.
912 * @param Preprocessor $preprocessor The parent preprocessor
913 */
914 public function __construct( $preprocessor ) {
915 $this->preprocessor = $preprocessor;
916 $this->parser = $preprocessor->parser;
917 $this->title = $this->parser->mTitle;
918 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
919 $this->loopCheckHash = [];
920 $this->depth = 0;
921 $this->childExpansionCache = [];
922 }
923
924 /**
925 * Create a new child frame
926 * $args is optionally a multi-root PPNode or array containing the template arguments
927 *
928 * @param array|bool|PPNode_Hash_Array $args
929 * @param Title|bool $title
930 * @param int $indexOffset
931 * @throws MWException
932 * @return PPTemplateFrame_Hash
933 */
934 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
935 $namedArgs = [];
936 $numberedArgs = [];
937 if ( $title === false ) {
938 $title = $this->title;
939 }
940 if ( $args !== false ) {
941 if ( $args instanceof PPNode_Hash_Array ) {
942 $args = $args->value;
943 } elseif ( !is_array( $args ) ) {
944 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
945 }
946 foreach ( $args as $arg ) {
947 $bits = $arg->splitArg();
948 if ( $bits['index'] !== '' ) {
949 // Numbered parameter
950 $index = $bits['index'] - $indexOffset;
951 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
952 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
953 wfEscapeWikiText( $this->title ),
954 wfEscapeWikiText( $title ),
955 wfEscapeWikiText( $index ) )->text() );
956 $this->parser->addTrackingCategory( 'duplicate-args-category' );
957 }
958 $numberedArgs[$index] = $bits['value'];
959 unset( $namedArgs[$index] );
960 } else {
961 // Named parameter
962 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
963 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
964 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
965 wfEscapeWikiText( $this->title ),
966 wfEscapeWikiText( $title ),
967 wfEscapeWikiText( $name ) )->text() );
968 $this->parser->addTrackingCategory( 'duplicate-args-category' );
969 }
970 $namedArgs[$name] = $bits['value'];
971 unset( $numberedArgs[$name] );
972 }
973 }
974 }
975 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
976 }
977
978 /**
979 * @throws MWException
980 * @param string|int $key
981 * @param string|PPNode $root
982 * @param int $flags
983 * @return string
984 */
985 public function cachedExpand( $key, $root, $flags = 0 ) {
986 // we don't have a parent, so we don't have a cache
987 return $this->expand( $root, $flags );
988 }
989
990 /**
991 * @throws MWException
992 * @param string|PPNode $root
993 * @param int $flags
994 * @return string
995 */
996 public function expand( $root, $flags = 0 ) {
997 static $expansionDepth = 0;
998 if ( is_string( $root ) ) {
999 return $root;
1000 }
1001
1002 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
1003 $this->parser->limitationWarn( 'node-count-exceeded',
1004 $this->parser->mPPNodeCount,
1005 $this->parser->mOptions->getMaxPPNodeCount()
1006 );
1007 return '<span class="error">Node-count limit exceeded</span>';
1008 }
1009 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
1010 $this->parser->limitationWarn( 'expansion-depth-exceeded',
1011 $expansionDepth,
1012 $this->parser->mOptions->getMaxPPExpandDepth()
1013 );
1014 return '<span class="error">Expansion depth limit exceeded</span>';
1015 }
1016 ++$expansionDepth;
1017 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
1018 $this->parser->mHighestExpansionDepth = $expansionDepth;
1019 }
1020
1021 $outStack = [ '', '' ];
1022 $iteratorStack = [ false, $root ];
1023 $indexStack = [ 0, 0 ];
1024
1025 while ( count( $iteratorStack ) > 1 ) {
1026 $level = count( $outStack ) - 1;
1027 $iteratorNode =& $iteratorStack[$level];
1028 $out =& $outStack[$level];
1029 $index =& $indexStack[$level];
1030
1031 if ( is_array( $iteratorNode ) ) {
1032 if ( $index >= count( $iteratorNode ) ) {
1033 // All done with this iterator
1034 $iteratorStack[$level] = false;
1035 $contextNode = false;
1036 } else {
1037 $contextNode = $iteratorNode[$index];
1038 $index++;
1039 }
1040 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
1041 if ( $index >= $iteratorNode->getLength() ) {
1042 // All done with this iterator
1043 $iteratorStack[$level] = false;
1044 $contextNode = false;
1045 } else {
1046 $contextNode = $iteratorNode->item( $index );
1047 $index++;
1048 }
1049 } else {
1050 // Copy to $contextNode and then delete from iterator stack,
1051 // because this is not an iterator but we do have to execute it once
1052 $contextNode = $iteratorStack[$level];
1053 $iteratorStack[$level] = false;
1054 }
1055
1056 $newIterator = false;
1057 $contextName = false;
1058 $contextChildren = false;
1059
1060 if ( $contextNode === false ) {
1061 // nothing to do
1062 } elseif ( is_string( $contextNode ) ) {
1063 $out .= $contextNode;
1064 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
1065 $newIterator = $contextNode;
1066 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
1067 // No output
1068 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
1069 $out .= $contextNode->value;
1070 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
1071 $contextName = $contextNode->name;
1072 $contextChildren = $contextNode->getRawChildren();
1073 } elseif ( is_array( $contextNode ) ) {
1074 // Node descriptor array
1075 if ( count( $contextNode ) !== 2 ) {
1076 throw new MWException( __METHOD__.
1077 ': found an array where a node descriptor should be' );
1078 }
1079 list( $contextName, $contextChildren ) = $contextNode;
1080 } else {
1081 throw new MWException( __METHOD__ . ': Invalid parameter type' );
1082 }
1083
1084 // Handle node descriptor array or tree object
1085 if ( $contextName === false ) {
1086 // Not a node, already handled above
1087 } elseif ( $contextName[0] === '@' ) {
1088 // Attribute: no output
1089 } elseif ( $contextName === 'template' ) {
1090 # Double-brace expansion
1091 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1092 if ( $flags & PPFrame::NO_TEMPLATES ) {
1093 $newIterator = $this->virtualBracketedImplode(
1094 '{{', '|', '}}',
1095 $bits['title'],
1096 $bits['parts']
1097 );
1098 } else {
1099 $ret = $this->parser->braceSubstitution( $bits, $this );
1100 if ( isset( $ret['object'] ) ) {
1101 $newIterator = $ret['object'];
1102 } else {
1103 $out .= $ret['text'];
1104 }
1105 }
1106 } elseif ( $contextName === 'tplarg' ) {
1107 # Triple-brace expansion
1108 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1109 if ( $flags & PPFrame::NO_ARGS ) {
1110 $newIterator = $this->virtualBracketedImplode(
1111 '{{{', '|', '}}}',
1112 $bits['title'],
1113 $bits['parts']
1114 );
1115 } else {
1116 $ret = $this->parser->argSubstitution( $bits, $this );
1117 if ( isset( $ret['object'] ) ) {
1118 $newIterator = $ret['object'];
1119 } else {
1120 $out .= $ret['text'];
1121 }
1122 }
1123 } elseif ( $contextName === 'comment' ) {
1124 # HTML-style comment
1125 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1126 # Not in RECOVER_COMMENTS mode (msgnw) though.
1127 if ( ( $this->parser->ot['html']
1128 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
1129 || ( $flags & PPFrame::STRIP_COMMENTS )
1130 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
1131 ) {
1132 $out .= '';
1133 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1134 # Add a strip marker in PST mode so that pstPass2() can
1135 # run some old-fashioned regexes on the result.
1136 # Not in RECOVER_COMMENTS mode (extractSections) though.
1137 $out .= $this->parser->insertStripItem( $contextChildren[0] );
1138 } else {
1139 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1140 $out .= $contextChildren[0];
1141 }
1142 } elseif ( $contextName === 'ignore' ) {
1143 # Output suppression used by <includeonly> etc.
1144 # OT_WIKI will only respect <ignore> in substed templates.
1145 # The other output types respect it unless NO_IGNORE is set.
1146 # extractSections() sets NO_IGNORE and so never respects it.
1147 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
1148 || ( $flags & PPFrame::NO_IGNORE )
1149 ) {
1150 $out .= $contextChildren[0];
1151 } else {
1152 // $out .= '';
1153 }
1154 } elseif ( $contextName === 'ext' ) {
1155 # Extension tag
1156 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
1157 [ 'attr' => null, 'inner' => null, 'close' => null ];
1158 if ( $flags & PPFrame::NO_TAGS ) {
1159 $s = '<' . $bits['name']->getFirstChild()->value;
1160 if ( $bits['attr'] ) {
1161 $s .= $bits['attr']->getFirstChild()->value;
1162 }
1163 if ( $bits['inner'] ) {
1164 $s .= '>' . $bits['inner']->getFirstChild()->value;
1165 if ( $bits['close'] ) {
1166 $s .= $bits['close']->getFirstChild()->value;
1167 }
1168 } else {
1169 $s .= '/>';
1170 }
1171 $out .= $s;
1172 } else {
1173 $out .= $this->parser->extensionSubstitution( $bits, $this );
1174 }
1175 } elseif ( $contextName === 'h' ) {
1176 # Heading
1177 if ( $this->parser->ot['html'] ) {
1178 # Expand immediately and insert heading index marker
1179 $s = $this->expand( $contextChildren, $flags );
1180 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
1181 $titleText = $this->title->getPrefixedDBkey();
1182 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
1183 $serial = count( $this->parser->mHeadings ) - 1;
1184 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
1185 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1186 $this->parser->mStripState->addGeneral( $marker, '' );
1187 $out .= $s;
1188 } else {
1189 # Expand in virtual stack
1190 $newIterator = $contextChildren;
1191 }
1192 } else {
1193 # Generic recursive expansion
1194 $newIterator = $contextChildren;
1195 }
1196
1197 if ( $newIterator !== false ) {
1198 $outStack[] = '';
1199 $iteratorStack[] = $newIterator;
1200 $indexStack[] = 0;
1201 } elseif ( $iteratorStack[$level] === false ) {
1202 // Return accumulated value to parent
1203 // With tail recursion
1204 while ( $iteratorStack[$level] === false && $level > 0 ) {
1205 $outStack[$level - 1] .= $out;
1206 array_pop( $outStack );
1207 array_pop( $iteratorStack );
1208 array_pop( $indexStack );
1209 $level--;
1210 }
1211 }
1212 }
1213 --$expansionDepth;
1214 return $outStack[0];
1215 }
1216
1217 /**
1218 * @param string $sep
1219 * @param int $flags
1220 * @param string|PPNode $args,...
1221 * @return string
1222 */
1223 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1224 $args = array_slice( func_get_args(), 2 );
1225
1226 $first = true;
1227 $s = '';
1228 foreach ( $args as $root ) {
1229 if ( $root instanceof PPNode_Hash_Array ) {
1230 $root = $root->value;
1231 }
1232 if ( !is_array( $root ) ) {
1233 $root = [ $root ];
1234 }
1235 foreach ( $root as $node ) {
1236 if ( $first ) {
1237 $first = false;
1238 } else {
1239 $s .= $sep;
1240 }
1241 $s .= $this->expand( $node, $flags );
1242 }
1243 }
1244 return $s;
1245 }
1246
1247 /**
1248 * Implode with no flags specified
1249 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1250 * @param string $sep
1251 * @param string|PPNode $args,...
1252 * @return string
1253 */
1254 public function implode( $sep /*, ... */ ) {
1255 $args = array_slice( func_get_args(), 1 );
1256
1257 $first = true;
1258 $s = '';
1259 foreach ( $args as $root ) {
1260 if ( $root instanceof PPNode_Hash_Array ) {
1261 $root = $root->value;
1262 }
1263 if ( !is_array( $root ) ) {
1264 $root = [ $root ];
1265 }
1266 foreach ( $root as $node ) {
1267 if ( $first ) {
1268 $first = false;
1269 } else {
1270 $s .= $sep;
1271 }
1272 $s .= $this->expand( $node );
1273 }
1274 }
1275 return $s;
1276 }
1277
1278 /**
1279 * Makes an object that, when expand()ed, will be the same as one obtained
1280 * with implode()
1281 *
1282 * @param string $sep
1283 * @param string|PPNode $args,...
1284 * @return PPNode_Hash_Array
1285 */
1286 public function virtualImplode( $sep /*, ... */ ) {
1287 $args = array_slice( func_get_args(), 1 );
1288 $out = [];
1289 $first = true;
1290
1291 foreach ( $args as $root ) {
1292 if ( $root instanceof PPNode_Hash_Array ) {
1293 $root = $root->value;
1294 }
1295 if ( !is_array( $root ) ) {
1296 $root = [ $root ];
1297 }
1298 foreach ( $root as $node ) {
1299 if ( $first ) {
1300 $first = false;
1301 } else {
1302 $out[] = $sep;
1303 }
1304 $out[] = $node;
1305 }
1306 }
1307 return new PPNode_Hash_Array( $out );
1308 }
1309
1310 /**
1311 * Virtual implode with brackets
1312 *
1313 * @param string $start
1314 * @param string $sep
1315 * @param string $end
1316 * @param string|PPNode $args,...
1317 * @return PPNode_Hash_Array
1318 */
1319 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1320 $args = array_slice( func_get_args(), 3 );
1321 $out = [ $start ];
1322 $first = true;
1323
1324 foreach ( $args as $root ) {
1325 if ( $root instanceof PPNode_Hash_Array ) {
1326 $root = $root->value;
1327 }
1328 if ( !is_array( $root ) ) {
1329 $root = [ $root ];
1330 }
1331 foreach ( $root as $node ) {
1332 if ( $first ) {
1333 $first = false;
1334 } else {
1335 $out[] = $sep;
1336 }
1337 $out[] = $node;
1338 }
1339 }
1340 $out[] = $end;
1341 return new PPNode_Hash_Array( $out );
1342 }
1343
1344 public function __toString() {
1345 return 'frame{}';
1346 }
1347
1348 /**
1349 * @param bool $level
1350 * @return array|bool|string
1351 */
1352 public function getPDBK( $level = false ) {
1353 if ( $level === false ) {
1354 return $this->title->getPrefixedDBkey();
1355 } else {
1356 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1357 }
1358 }
1359
1360 /**
1361 * @return array
1362 */
1363 public function getArguments() {
1364 return [];
1365 }
1366
1367 /**
1368 * @return array
1369 */
1370 public function getNumberedArguments() {
1371 return [];
1372 }
1373
1374 /**
1375 * @return array
1376 */
1377 public function getNamedArguments() {
1378 return [];
1379 }
1380
1381 /**
1382 * Returns true if there are no arguments in this frame
1383 *
1384 * @return bool
1385 */
1386 public function isEmpty() {
1387 return true;
1388 }
1389
1390 /**
1391 * @param int|string $name
1392 * @return bool Always false in this implementation.
1393 */
1394 public function getArgument( $name ) {
1395 return false;
1396 }
1397
1398 /**
1399 * Returns true if the infinite loop check is OK, false if a loop is detected
1400 *
1401 * @param Title $title
1402 *
1403 * @return bool
1404 */
1405 public function loopCheck( $title ) {
1406 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1407 }
1408
1409 /**
1410 * Return true if the frame is a template frame
1411 *
1412 * @return bool
1413 */
1414 public function isTemplate() {
1415 return false;
1416 }
1417
1418 /**
1419 * Get a title of frame
1420 *
1421 * @return Title
1422 */
1423 public function getTitle() {
1424 return $this->title;
1425 }
1426
1427 /**
1428 * Set the volatile flag
1429 *
1430 * @param bool $flag
1431 */
1432 public function setVolatile( $flag = true ) {
1433 $this->volatile = $flag;
1434 }
1435
1436 /**
1437 * Get the volatile flag
1438 *
1439 * @return bool
1440 */
1441 public function isVolatile() {
1442 return $this->volatile;
1443 }
1444
1445 /**
1446 * Set the TTL
1447 *
1448 * @param int $ttl
1449 */
1450 public function setTTL( $ttl ) {
1451 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
1452 $this->ttl = $ttl;
1453 }
1454 }
1455
1456 /**
1457 * Get the TTL
1458 *
1459 * @return int|null
1460 */
1461 public function getTTL() {
1462 return $this->ttl;
1463 }
1464 }
1465
1466 /**
1467 * Expansion frame with template arguments
1468 * @ingroup Parser
1469 */
1470 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
1471 class PPTemplateFrame_Hash extends PPFrame_Hash {
1472
1473 public $numberedArgs, $namedArgs, $parent;
1474 public $numberedExpansionCache, $namedExpansionCache;
1475
1476 /**
1477 * @param Preprocessor $preprocessor
1478 * @param bool|PPFrame $parent
1479 * @param array $numberedArgs
1480 * @param array $namedArgs
1481 * @param bool|Title $title
1482 */
1483 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
1484 $namedArgs = [], $title = false
1485 ) {
1486 parent::__construct( $preprocessor );
1487
1488 $this->parent = $parent;
1489 $this->numberedArgs = $numberedArgs;
1490 $this->namedArgs = $namedArgs;
1491 $this->title = $title;
1492 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1493 $this->titleCache = $parent->titleCache;
1494 $this->titleCache[] = $pdbk;
1495 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1496 if ( $pdbk !== false ) {
1497 $this->loopCheckHash[$pdbk] = true;
1498 }
1499 $this->depth = $parent->depth + 1;
1500 $this->numberedExpansionCache = $this->namedExpansionCache = [];
1501 }
1502
1503 public function __toString() {
1504 $s = 'tplframe{';
1505 $first = true;
1506 $args = $this->numberedArgs + $this->namedArgs;
1507 foreach ( $args as $name => $value ) {
1508 if ( $first ) {
1509 $first = false;
1510 } else {
1511 $s .= ', ';
1512 }
1513 $s .= "\"$name\":\"" .
1514 str_replace( '"', '\\"', $value->__toString() ) . '"';
1515 }
1516 $s .= '}';
1517 return $s;
1518 }
1519
1520 /**
1521 * @throws MWException
1522 * @param string|int $key
1523 * @param string|PPNode $root
1524 * @param int $flags
1525 * @return string
1526 */
1527 public function cachedExpand( $key, $root, $flags = 0 ) {
1528 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
1529 return $this->parent->childExpansionCache[$key];
1530 }
1531 $retval = $this->expand( $root, $flags );
1532 if ( !$this->isVolatile() ) {
1533 $this->parent->childExpansionCache[$key] = $retval;
1534 }
1535 return $retval;
1536 }
1537
1538 /**
1539 * Returns true if there are no arguments in this frame
1540 *
1541 * @return bool
1542 */
1543 public function isEmpty() {
1544 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1545 }
1546
1547 /**
1548 * @return array
1549 */
1550 public function getArguments() {
1551 $arguments = [];
1552 foreach ( array_merge(
1553 array_keys( $this->numberedArgs ),
1554 array_keys( $this->namedArgs ) ) as $key ) {
1555 $arguments[$key] = $this->getArgument( $key );
1556 }
1557 return $arguments;
1558 }
1559
1560 /**
1561 * @return array
1562 */
1563 public function getNumberedArguments() {
1564 $arguments = [];
1565 foreach ( array_keys( $this->numberedArgs ) as $key ) {
1566 $arguments[$key] = $this->getArgument( $key );
1567 }
1568 return $arguments;
1569 }
1570
1571 /**
1572 * @return array
1573 */
1574 public function getNamedArguments() {
1575 $arguments = [];
1576 foreach ( array_keys( $this->namedArgs ) as $key ) {
1577 $arguments[$key] = $this->getArgument( $key );
1578 }
1579 return $arguments;
1580 }
1581
1582 /**
1583 * @param int $index
1584 * @return string|bool
1585 */
1586 public function getNumberedArgument( $index ) {
1587 if ( !isset( $this->numberedArgs[$index] ) ) {
1588 return false;
1589 }
1590 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1591 # No trimming for unnamed arguments
1592 $this->numberedExpansionCache[$index] = $this->parent->expand(
1593 $this->numberedArgs[$index],
1594 PPFrame::STRIP_COMMENTS
1595 );
1596 }
1597 return $this->numberedExpansionCache[$index];
1598 }
1599
1600 /**
1601 * @param string $name
1602 * @return string|bool
1603 */
1604 public function getNamedArgument( $name ) {
1605 if ( !isset( $this->namedArgs[$name] ) ) {
1606 return false;
1607 }
1608 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1609 # Trim named arguments post-expand, for backwards compatibility
1610 $this->namedExpansionCache[$name] = trim(
1611 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1612 }
1613 return $this->namedExpansionCache[$name];
1614 }
1615
1616 /**
1617 * @param int|string $name
1618 * @return string|bool
1619 */
1620 public function getArgument( $name ) {
1621 $text = $this->getNumberedArgument( $name );
1622 if ( $text === false ) {
1623 $text = $this->getNamedArgument( $name );
1624 }
1625 return $text;
1626 }
1627
1628 /**
1629 * Return true if the frame is a template frame
1630 *
1631 * @return bool
1632 */
1633 public function isTemplate() {
1634 return true;
1635 }
1636
1637 public function setVolatile( $flag = true ) {
1638 parent::setVolatile( $flag );
1639 $this->parent->setVolatile( $flag );
1640 }
1641
1642 public function setTTL( $ttl ) {
1643 parent::setTTL( $ttl );
1644 $this->parent->setTTL( $ttl );
1645 }
1646 }
1647
1648 /**
1649 * Expansion frame with custom arguments
1650 * @ingroup Parser
1651 */
1652 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
1653 class PPCustomFrame_Hash extends PPFrame_Hash {
1654
1655 public $args;
1656
1657 public function __construct( $preprocessor, $args ) {
1658 parent::__construct( $preprocessor );
1659 $this->args = $args;
1660 }
1661
1662 public function __toString() {
1663 $s = 'cstmframe{';
1664 $first = true;
1665 foreach ( $this->args as $name => $value ) {
1666 if ( $first ) {
1667 $first = false;
1668 } else {
1669 $s .= ', ';
1670 }
1671 $s .= "\"$name\":\"" .
1672 str_replace( '"', '\\"', $value->__toString() ) . '"';
1673 }
1674 $s .= '}';
1675 return $s;
1676 }
1677
1678 /**
1679 * @return bool
1680 */
1681 public function isEmpty() {
1682 return !count( $this->args );
1683 }
1684
1685 /**
1686 * @param int|string $index
1687 * @return string|bool
1688 */
1689 public function getArgument( $index ) {
1690 if ( !isset( $this->args[$index] ) ) {
1691 return false;
1692 }
1693 return $this->args[$index];
1694 }
1695
1696 public function getArguments() {
1697 return $this->args;
1698 }
1699 }
1700
1701 /**
1702 * @ingroup Parser
1703 */
1704 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
1705 class PPNode_Hash_Tree implements PPNode {
1706
1707 public $name;
1708
1709 /**
1710 * The store array for children of this node. It is "raw" in the sense that
1711 * nodes are two-element arrays ("descriptors") rather than PPNode_Hash_*
1712 * objects.
1713 */
1714 private $rawChildren;
1715
1716 /**
1717 * The store array for the siblings of this node, including this node itself.
1718 */
1719 private $store;
1720
1721 /**
1722 * The index into $this->store which contains the descriptor of this node.
1723 */
1724 private $index;
1725
1726 /**
1727 * The offset of the name within descriptors, used in some places for
1728 * readability.
1729 */
1730 const NAME = 0;
1731
1732 /**
1733 * The offset of the child list within descriptors, used in some places for
1734 * readability.
1735 */
1736 const CHILDREN = 1;
1737
1738 /**
1739 * Construct an object using the data from $store[$index]. The rest of the
1740 * store array can be accessed via getNextSibling().
1741 *
1742 * @param array $store
1743 * @param int $index
1744 */
1745 public function __construct( array $store, $index ) {
1746 $this->store = $store;
1747 $this->index = $index;
1748 list( $this->name, $this->rawChildren ) = $this->store[$index];
1749 }
1750
1751 /**
1752 * Construct an appropriate PPNode_Hash_* object with a class that depends
1753 * on what is at the relevant store index.
1754 *
1755 * @param array $store
1756 * @param int $index
1757 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text
1758 */
1759 public static function factory( array $store, $index ) {
1760 if ( !isset( $store[$index] ) ) {
1761 return false;
1762 }
1763
1764 $descriptor = $store[$index];
1765 if ( is_string( $descriptor ) ) {
1766 $class = PPNode_Hash_Text::class;
1767 } elseif ( is_array( $descriptor ) ) {
1768 if ( $descriptor[self::NAME][0] === '@' ) {
1769 $class = PPNode_Hash_Attr::class;
1770 } else {
1771 $class = self::class;
1772 }
1773 } else {
1774 throw new MWException( __METHOD__.': invalid node descriptor' );
1775 }
1776 return new $class( $store, $index );
1777 }
1778
1779 /**
1780 * Convert a node to XML, for debugging
1781 */
1782 public function __toString() {
1783 $inner = '';
1784 $attribs = '';
1785 for ( $node = $this->getFirstChild(); $node; $node = $node->getNextSibling() ) {
1786 if ( $node instanceof PPNode_Hash_Attr ) {
1787 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1788 } else {
1789 $inner .= $node->__toString();
1790 }
1791 }
1792 if ( $inner === '' ) {
1793 return "<{$this->name}$attribs/>";
1794 } else {
1795 return "<{$this->name}$attribs>$inner</{$this->name}>";
1796 }
1797 }
1798
1799 /**
1800 * @return PPNode_Hash_Array
1801 */
1802 public function getChildren() {
1803 $children = [];
1804 foreach ( $this->rawChildren as $i => $child ) {
1805 $children[] = self::factory( $this->rawChildren, $i );
1806 }
1807 return new PPNode_Hash_Array( $children );
1808 }
1809
1810 /**
1811 * Get the first child, or false if there is none. Note that this will
1812 * return a temporary proxy object: different instances will be returned
1813 * if this is called more than once on the same node.
1814 *
1815 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|bool
1816 */
1817 public function getFirstChild() {
1818 if ( !isset( $this->rawChildren[0] ) ) {
1819 return false;
1820 } else {
1821 return self::factory( $this->rawChildren, 0 );
1822 }
1823 }
1824
1825 /**
1826 * Get the next sibling, or false if there is none. Note that this will
1827 * return a temporary proxy object: different instances will be returned
1828 * if this is called more than once on the same node.
1829 *
1830 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|bool
1831 */
1832 public function getNextSibling() {
1833 return self::factory( $this->store, $this->index + 1 );
1834 }
1835
1836 /**
1837 * Get an array of the children with a given node name
1838 *
1839 * @param string $name
1840 * @return PPNode_Hash_Array
1841 */
1842 public function getChildrenOfType( $name ) {
1843 $children = [];
1844 foreach ( $this->rawChildren as $i => $child ) {
1845 if ( is_array( $child ) && $child[self::NAME] === $name ) {
1846 $children[] = self::factory( $this->rawChildren, $i );
1847 }
1848 }
1849 return new PPNode_Hash_Array( $children );
1850 }
1851
1852 /**
1853 * Get the raw child array. For internal use.
1854 * @return array
1855 */
1856 public function getRawChildren() {
1857 return $this->rawChildren;
1858 }
1859
1860 /**
1861 * @return bool
1862 */
1863 public function getLength() {
1864 return false;
1865 }
1866
1867 /**
1868 * @param int $i
1869 * @return bool
1870 */
1871 public function item( $i ) {
1872 return false;
1873 }
1874
1875 /**
1876 * @return string
1877 */
1878 public function getName() {
1879 return $this->name;
1880 }
1881
1882 /**
1883 * Split a "<part>" node into an associative array containing:
1884 * - name PPNode name
1885 * - index String index
1886 * - value PPNode value
1887 *
1888 * @throws MWException
1889 * @return array
1890 */
1891 public function splitArg() {
1892 return self::splitRawArg( $this->rawChildren );
1893 }
1894
1895 /**
1896 * Like splitArg() but for a raw child array. For internal use only.
1897 * @param array $children
1898 * @return array
1899 */
1900 public static function splitRawArg( array $children ) {
1901 $bits = [];
1902 foreach ( $children as $i => $child ) {
1903 if ( !is_array( $child ) ) {
1904 continue;
1905 }
1906 if ( $child[self::NAME] === 'name' ) {
1907 $bits['name'] = new self( $children, $i );
1908 if ( isset( $child[self::CHILDREN][0][self::NAME] )
1909 && $child[self::CHILDREN][0][self::NAME] === '@index'
1910 ) {
1911 $bits['index'] = $child[self::CHILDREN][0][self::CHILDREN][0];
1912 }
1913 } elseif ( $child[self::NAME] === 'value' ) {
1914 $bits['value'] = new self( $children, $i );
1915 }
1916 }
1917
1918 if ( !isset( $bits['name'] ) ) {
1919 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1920 }
1921 if ( !isset( $bits['index'] ) ) {
1922 $bits['index'] = '';
1923 }
1924 return $bits;
1925 }
1926
1927 /**
1928 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1929 * All values in the resulting array are PPNodes. Inner and close are optional.
1930 *
1931 * @throws MWException
1932 * @return array
1933 */
1934 public function splitExt() {
1935 return self::splitRawExt( $this->rawChildren );
1936 }
1937
1938 /**
1939 * Like splitExt() but for a raw child array. For internal use only.
1940 * @param array $children
1941 * @return array
1942 */
1943 public static function splitRawExt( array $children ) {
1944 $bits = [];
1945 foreach ( $children as $i => $child ) {
1946 if ( !is_array( $child ) ) {
1947 continue;
1948 }
1949 switch ( $child[self::NAME] ) {
1950 case 'name':
1951 $bits['name'] = new self( $children, $i );
1952 break;
1953 case 'attr':
1954 $bits['attr'] = new self( $children, $i );
1955 break;
1956 case 'inner':
1957 $bits['inner'] = new self( $children, $i );
1958 break;
1959 case 'close':
1960 $bits['close'] = new self( $children, $i );
1961 break;
1962 }
1963 }
1964 if ( !isset( $bits['name'] ) ) {
1965 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1966 }
1967 return $bits;
1968 }
1969
1970 /**
1971 * Split an "<h>" node
1972 *
1973 * @throws MWException
1974 * @return array
1975 */
1976 public function splitHeading() {
1977 if ( $this->name !== 'h' ) {
1978 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1979 }
1980 return self::splitRawHeading( $this->rawChildren );
1981 }
1982
1983 /**
1984 * Like splitHeading() but for a raw child array. For internal use only.
1985 * @param array $children
1986 * @return array
1987 */
1988 public static function splitRawHeading( array $children ) {
1989 $bits = [];
1990 foreach ( $children as $i => $child ) {
1991 if ( !is_array( $child ) ) {
1992 continue;
1993 }
1994 if ( $child[self::NAME] === '@i' ) {
1995 $bits['i'] = $child[self::CHILDREN][0];
1996 } elseif ( $child[self::NAME] === '@level' ) {
1997 $bits['level'] = $child[self::CHILDREN][0];
1998 }
1999 }
2000 if ( !isset( $bits['i'] ) ) {
2001 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
2002 }
2003 return $bits;
2004 }
2005
2006 /**
2007 * Split a "<template>" or "<tplarg>" node
2008 *
2009 * @throws MWException
2010 * @return array
2011 */
2012 public function splitTemplate() {
2013 return self::splitRawTemplate( $this->rawChildren );
2014 }
2015
2016 /**
2017 * Like splitTemplate() but for a raw child array. For internal use only.
2018 * @param array $children
2019 * @return array
2020 */
2021 public static function splitRawTemplate( array $children ) {
2022 $parts = [];
2023 $bits = [ 'lineStart' => '' ];
2024 foreach ( $children as $i => $child ) {
2025 if ( !is_array( $child ) ) {
2026 continue;
2027 }
2028 switch ( $child[self::NAME] ) {
2029 case 'title':
2030 $bits['title'] = new self( $children, $i );
2031 break;
2032 case 'part':
2033 $parts[] = new self( $children, $i );
2034 break;
2035 case '@lineStart':
2036 $bits['lineStart'] = '1';
2037 break;
2038 }
2039 }
2040 if ( !isset( $bits['title'] ) ) {
2041 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
2042 }
2043 $bits['parts'] = new PPNode_Hash_Array( $parts );
2044 return $bits;
2045 }
2046 }
2047
2048 /**
2049 * @ingroup Parser
2050 */
2051 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
2052 class PPNode_Hash_Text implements PPNode {
2053
2054 public $value;
2055 private $store, $index;
2056
2057 /**
2058 * Construct an object using the data from $store[$index]. The rest of the
2059 * store array can be accessed via getNextSibling().
2060 *
2061 * @param array $store
2062 * @param int $index
2063 */
2064 public function __construct( array $store, $index ) {
2065 $this->value = $store[$index];
2066 if ( !is_scalar( $this->value ) ) {
2067 throw new MWException( __CLASS__ . ' given object instead of string' );
2068 }
2069 $this->store = $store;
2070 $this->index = $index;
2071 }
2072
2073 public function __toString() {
2074 return htmlspecialchars( $this->value );
2075 }
2076
2077 public function getNextSibling() {
2078 return PPNode_Hash_Tree::factory( $this->store, $this->index + 1 );
2079 }
2080
2081 public function getChildren() {
2082 return false;
2083 }
2084
2085 public function getFirstChild() {
2086 return false;
2087 }
2088
2089 public function getChildrenOfType( $name ) {
2090 return false;
2091 }
2092
2093 public function getLength() {
2094 return false;
2095 }
2096
2097 public function item( $i ) {
2098 return false;
2099 }
2100
2101 public function getName() {
2102 return '#text';
2103 }
2104
2105 public function splitArg() {
2106 throw new MWException( __METHOD__ . ': not supported' );
2107 }
2108
2109 public function splitExt() {
2110 throw new MWException( __METHOD__ . ': not supported' );
2111 }
2112
2113 public function splitHeading() {
2114 throw new MWException( __METHOD__ . ': not supported' );
2115 }
2116 }
2117
2118 /**
2119 * @ingroup Parser
2120 */
2121 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
2122 class PPNode_Hash_Array implements PPNode {
2123
2124 public $value;
2125
2126 public function __construct( $value ) {
2127 $this->value = $value;
2128 }
2129
2130 public function __toString() {
2131 return var_export( $this, true );
2132 }
2133
2134 public function getLength() {
2135 return count( $this->value );
2136 }
2137
2138 public function item( $i ) {
2139 return $this->value[$i];
2140 }
2141
2142 public function getName() {
2143 return '#nodelist';
2144 }
2145
2146 public function getNextSibling() {
2147 return false;
2148 }
2149
2150 public function getChildren() {
2151 return false;
2152 }
2153
2154 public function getFirstChild() {
2155 return false;
2156 }
2157
2158 public function getChildrenOfType( $name ) {
2159 return false;
2160 }
2161
2162 public function splitArg() {
2163 throw new MWException( __METHOD__ . ': not supported' );
2164 }
2165
2166 public function splitExt() {
2167 throw new MWException( __METHOD__ . ': not supported' );
2168 }
2169
2170 public function splitHeading() {
2171 throw new MWException( __METHOD__ . ': not supported' );
2172 }
2173 }
2174
2175 /**
2176 * @ingroup Parser
2177 */
2178 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
2179 class PPNode_Hash_Attr implements PPNode {
2180
2181 public $name, $value;
2182 private $store, $index;
2183
2184 /**
2185 * Construct an object using the data from $store[$index]. The rest of the
2186 * store array can be accessed via getNextSibling().
2187 *
2188 * @param array $store
2189 * @param int $index
2190 */
2191 public function __construct( array $store, $index ) {
2192 $descriptor = $store[$index];
2193 if ( $descriptor[PPNode_Hash_Tree::NAME][0] !== '@' ) {
2194 throw new MWException( __METHOD__.': invalid name in attribute descriptor' );
2195 }
2196 $this->name = substr( $descriptor[PPNode_Hash_Tree::NAME], 1 );
2197 $this->value = $descriptor[PPNode_Hash_Tree::CHILDREN][0];
2198 $this->store = $store;
2199 $this->index = $index;
2200 }
2201
2202 public function __toString() {
2203 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
2204 }
2205
2206 public function getName() {
2207 return $this->name;
2208 }
2209
2210 public function getNextSibling() {
2211 return PPNode_Hash_Tree::factory( $this->store, $this->index + 1 );
2212 }
2213
2214 public function getChildren() {
2215 return false;
2216 }
2217
2218 public function getFirstChild() {
2219 return false;
2220 }
2221
2222 public function getChildrenOfType( $name ) {
2223 return false;
2224 }
2225
2226 public function getLength() {
2227 return false;
2228 }
2229
2230 public function item( $i ) {
2231 return false;
2232 }
2233
2234 public function splitArg() {
2235 throw new MWException( __METHOD__ . ': not supported' );
2236 }
2237
2238 public function splitExt() {
2239 throw new MWException( __METHOD__ . ': not supported' );
2240 }
2241
2242 public function splitHeading() {
2243 throw new MWException( __METHOD__ . ': not supported' );
2244 }
2245 }