Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / parser / Preprocessor_Hash.php
blob2666c93f8703ff2bf35a7a15afe99cb51208012a
1 <?php
2 /**
3 * Preprocessor using PHP arrays
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.
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.
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
20 * @file
21 * @ingroup Parser
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>
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.
34 * Attributes are represented as children that have a node name starting with
35 * "@", and a single text node child.
37 * @todo: Consider replacing descriptor arrays with objects of a new class.
38 * Benchmark and measure resulting memory impact.
40 * @ingroup Parser
42 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
43 class Preprocessor_Hash extends Preprocessor {
44 // @codingStandardsIgnoreEnd
46 /**
47 * @var Parser
49 public $parser;
51 const CACHE_PREFIX = 'preprocess-hash';
52 const CACHE_VERSION = 2;
54 public function __construct( $parser ) {
55 $this->parser = $parser;
58 /**
59 * @return PPFrame_Hash
61 public function newFrame() {
62 return new PPFrame_Hash( $this );
65 /**
66 * @param array $args
67 * @return PPCustomFrame_Hash
69 public function newCustomFrame( $args ) {
70 return new PPCustomFrame_Hash( $this, $args );
73 /**
74 * @param array $values
75 * @return PPNode_Hash_Array
77 public function newPartNodeArray( $values ) {
78 $list = [];
80 foreach ( $values as $k => $val ) {
81 if ( is_int( $k ) ) {
82 $store = [ [ 'part', [
83 [ 'name', [ [ '@index', [ $k ] ] ] ],
84 [ 'value', [ strval( $val ) ] ],
85 ] ] ];
86 } else {
87 $store = [ [ 'part', [
88 [ 'name', [ strval( $k ) ] ],
89 '=',
90 [ 'value', [ strval( $val ) ] ],
91 ] ] ];
94 $list[] = new PPNode_Hash_Tree( $store, 0 );
97 $node = new PPNode_Hash_Array( $list );
98 return $node;
102 * Preprocess some wikitext and return the document tree.
104 * @param string $text The text to parse
105 * @param int $flags Bitwise combination of:
106 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<includeonly>" as if the text is being
107 * included. Default is to assume a direct page view.
109 * The generated DOM tree must depend only on the input text and the flags.
110 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
112 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
113 * change in the DOM tree for a given text, must be passed through the section identifier
114 * in the section edit link and thus back to extractSections().
116 * @throws MWException
117 * @return PPNode_Hash_Tree
119 public function preprocessToObj( $text, $flags = 0 ) {
120 global $wgDisableLangConversion;
122 $tree = $this->cacheGetTree( $text, $flags );
123 if ( $tree !== false ) {
124 $store = json_decode( $tree );
125 if ( is_array( $store ) ) {
126 return new PPNode_Hash_Tree( $store, 0 );
130 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
132 $xmlishElements = $this->parser->getStripList();
133 $xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ];
134 $enableOnlyinclude = false;
135 if ( $forInclusion ) {
136 $ignoredTags = [ 'includeonly', '/includeonly' ];
137 $ignoredElements = [ 'noinclude' ];
138 $xmlishElements[] = 'noinclude';
139 if ( strpos( $text, '<onlyinclude>' ) !== false
140 && strpos( $text, '</onlyinclude>' ) !== false
142 $enableOnlyinclude = true;
144 } else {
145 $ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ];
146 $ignoredElements = [ 'includeonly' ];
147 $xmlishElements[] = 'includeonly';
149 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
151 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
152 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
154 $stack = new PPDStack_Hash;
156 $searchBase = "[{<\n";
157 if ( !$wgDisableLangConversion ) {
158 // FIXME: disabled due to T153761
159 // $searchBase .= '-';
162 // For fast reverse searches
163 $revText = strrev( $text );
164 $lengthText = strlen( $text );
166 // Input pointer, starts out pointing to a pseudo-newline before the start
167 $i = 0;
168 // Current accumulator. See the doc comment for Preprocessor_Hash for the format.
169 $accum =& $stack->getAccum();
170 // True to find equals signs in arguments
171 $findEquals = false;
172 // True to take notice of pipe characters
173 $findPipe = false;
174 $headingIndex = 1;
175 // True if $i is inside a possible heading
176 $inHeading = false;
177 // True if there are no more greater-than (>) signs right of $i
178 $noMoreGT = false;
179 // Map of tag name => true if there are no more closing tags of given type right of $i
180 $noMoreClosingTag = [];
181 // True to ignore all input up to the next <onlyinclude>
182 $findOnlyinclude = $enableOnlyinclude;
183 // Do a line-start run without outputting an LF character
184 $fakeLineStart = true;
186 while ( true ) {
187 // $this->memCheck();
189 if ( $findOnlyinclude ) {
190 // Ignore all input up to the next <onlyinclude>
191 $startPos = strpos( $text, '<onlyinclude>', $i );
192 if ( $startPos === false ) {
193 // Ignored section runs to the end
194 $accum[] = [ 'ignore', [ substr( $text, $i ) ] ];
195 break;
197 $tagEndPos = $startPos + strlen( '<onlyinclude>' ); // past-the-end
198 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
199 $i = $tagEndPos;
200 $findOnlyinclude = false;
203 if ( $fakeLineStart ) {
204 $found = 'line-start';
205 $curChar = '';
206 } else {
207 # Find next opening brace, closing brace or pipe
208 $search = $searchBase;
209 if ( $stack->top === false ) {
210 $currentClosing = '';
211 } else {
212 $currentClosing = $stack->top->close;
213 $search .= $currentClosing;
215 if ( $findPipe ) {
216 $search .= '|';
218 if ( $findEquals ) {
219 // First equals will be for the template
220 $search .= '=';
222 $rule = null;
223 # Output literal section, advance input counter
224 $literalLength = strcspn( $text, $search, $i );
225 if ( $literalLength > 0 ) {
226 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
227 $i += $literalLength;
229 if ( $i >= $lengthText ) {
230 if ( $currentClosing == "\n" ) {
231 // Do a past-the-end run to finish off the heading
232 $curChar = '';
233 $found = 'line-end';
234 } else {
235 # All done
236 break;
238 } else {
239 $curChar = $curTwoChar = $text[$i];
240 if ( ( $i + 1 ) < $lengthText ) {
241 $curTwoChar .= $text[$i + 1];
243 if ( $curChar == '|' ) {
244 $found = 'pipe';
245 } elseif ( $curChar == '=' ) {
246 $found = 'equals';
247 } elseif ( $curChar == '<' ) {
248 $found = 'angle';
249 } elseif ( $curChar == "\n" ) {
250 if ( $inHeading ) {
251 $found = 'line-end';
252 } else {
253 $found = 'line-start';
255 } elseif ( $curTwoChar == $currentClosing ) {
256 $found = 'close';
257 $curChar = $curTwoChar;
258 } elseif ( $curChar == $currentClosing ) {
259 $found = 'close';
260 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
261 $curChar = $curTwoChar;
262 $found = 'open';
263 $rule = $this->rules[$curChar];
264 } elseif ( isset( $this->rules[$curChar] ) ) {
265 $found = 'open';
266 $rule = $this->rules[$curChar];
267 } elseif ( $curChar == '-' ) {
268 $found = 'dash';
269 } else {
270 # Some versions of PHP have a strcspn which stops on null characters
271 # Ignore and continue
272 ++$i;
273 continue;
278 if ( $found == 'angle' ) {
279 $matches = false;
280 // Handle </onlyinclude>
281 if ( $enableOnlyinclude
282 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
284 $findOnlyinclude = true;
285 continue;
288 // Determine element name
289 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
290 // Element name missing or not listed
291 self::addLiteral( $accum, '<' );
292 ++$i;
293 continue;
295 // Handle comments
296 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
298 // To avoid leaving blank lines, when a sequence of
299 // space-separated comments is both preceded and followed by
300 // a newline (ignoring spaces), then
301 // trim leading and trailing spaces and the trailing newline.
303 // Find the end
304 $endPos = strpos( $text, '-->', $i + 4 );
305 if ( $endPos === false ) {
306 // Unclosed comment in input, runs to end
307 $inner = substr( $text, $i );
308 $accum[] = [ 'comment', [ $inner ] ];
309 $i = $lengthText;
310 } else {
311 // Search backwards for leading whitespace
312 $wsStart = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
314 // Search forwards for trailing whitespace
315 // $wsEnd will be the position of the last space (or the '>' if there's none)
316 $wsEnd = $endPos + 2 + strspn( $text, " \t", $endPos + 3 );
318 // Keep looking forward as long as we're finding more
319 // comments.
320 $comments = [ [ $wsStart, $wsEnd ] ];
321 while ( substr( $text, $wsEnd + 1, 4 ) == '<!--' ) {
322 $c = strpos( $text, '-->', $wsEnd + 4 );
323 if ( $c === false ) {
324 break;
326 $c = $c + 2 + strspn( $text, " \t", $c + 3 );
327 $comments[] = [ $wsEnd + 1, $c ];
328 $wsEnd = $c;
331 // Eat the line if possible
332 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
333 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
334 // it's a possible beneficial b/c break.
335 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
336 && substr( $text, $wsEnd + 1, 1 ) == "\n"
338 // Remove leading whitespace from the end of the accumulator
339 $wsLength = $i - $wsStart;
340 $endIndex = count( $accum ) - 1;
342 // Sanity check
343 if ( $wsLength > 0
344 && $endIndex >= 0
345 && is_string( $accum[$endIndex] )
346 && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
348 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
351 // Dump all but the last comment to the accumulator
352 foreach ( $comments as $j => $com ) {
353 $startPos = $com[0];
354 $endPos = $com[1] + 1;
355 if ( $j == ( count( $comments ) - 1 ) ) {
356 break;
358 $inner = substr( $text, $startPos, $endPos - $startPos );
359 $accum[] = [ 'comment', [ $inner ] ];
362 // Do a line-start run next time to look for headings after the comment
363 $fakeLineStart = true;
364 } else {
365 // No line to eat, just take the comment itself
366 $startPos = $i;
367 $endPos += 2;
370 if ( $stack->top ) {
371 $part = $stack->top->getCurrentPart();
372 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
373 $part->visualEnd = $wsStart;
375 // Else comments abutting, no change in visual end
376 $part->commentEnd = $endPos;
378 $i = $endPos + 1;
379 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
380 $accum[] = [ 'comment', [ $inner ] ];
382 continue;
384 $name = $matches[1];
385 $lowerName = strtolower( $name );
386 $attrStart = $i + strlen( $name ) + 1;
388 // Find end of tag
389 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
390 if ( $tagEndPos === false ) {
391 // Infinite backtrack
392 // Disable tag search to prevent worst-case O(N^2) performance
393 $noMoreGT = true;
394 self::addLiteral( $accum, '<' );
395 ++$i;
396 continue;
399 // Handle ignored tags
400 if ( in_array( $lowerName, $ignoredTags ) ) {
401 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
402 $i = $tagEndPos + 1;
403 continue;
406 $tagStartPos = $i;
407 if ( $text[$tagEndPos - 1] == '/' ) {
408 // Short end tag
409 $attrEnd = $tagEndPos - 1;
410 $inner = null;
411 $i = $tagEndPos + 1;
412 $close = null;
413 } else {
414 $attrEnd = $tagEndPos;
415 // Find closing tag
416 if (
417 !isset( $noMoreClosingTag[$name] ) &&
418 preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
419 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
421 $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
422 $i = $matches[0][1] + strlen( $matches[0][0] );
423 $close = $matches[0][0];
424 } else {
425 // No end tag
426 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
427 // Let it run out to the end of the text.
428 $inner = substr( $text, $tagEndPos + 1 );
429 $i = $lengthText;
430 $close = null;
431 } else {
432 // Don't match the tag, treat opening tag as literal and resume parsing.
433 $i = $tagEndPos + 1;
434 self::addLiteral( $accum,
435 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
436 // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
437 $noMoreClosingTag[$name] = true;
438 continue;
442 // <includeonly> and <noinclude> just become <ignore> tags
443 if ( in_array( $lowerName, $ignoredElements ) ) {
444 $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
445 continue;
448 if ( $attrEnd <= $attrStart ) {
449 $attr = '';
450 } else {
451 // Note that the attr element contains the whitespace between name and attribute,
452 // this is necessary for precise reconstruction during pre-save transform.
453 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
456 $children = [
457 [ 'name', [ $name ] ],
458 [ 'attr', [ $attr ] ] ];
459 if ( $inner !== null ) {
460 $children[] = [ 'inner', [ $inner ] ];
462 if ( $close !== null ) {
463 $children[] = [ 'close', [ $close ] ];
465 $accum[] = [ 'ext', $children ];
466 } elseif ( $found == 'line-start' ) {
467 // Is this the start of a heading?
468 // Line break belongs before the heading element in any case
469 if ( $fakeLineStart ) {
470 $fakeLineStart = false;
471 } else {
472 self::addLiteral( $accum, $curChar );
473 $i++;
476 $count = strspn( $text, '=', $i, 6 );
477 if ( $count == 1 && $findEquals ) {
478 // DWIM: This looks kind of like a name/value separator.
479 // Let's let the equals handler have it and break the potential
480 // heading. This is heuristic, but AFAICT the methods for
481 // completely correct disambiguation are very complex.
482 } elseif ( $count > 0 ) {
483 $piece = [
484 'open' => "\n",
485 'close' => "\n",
486 'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ],
487 'startPos' => $i,
488 'count' => $count ];
489 $stack->push( $piece );
490 $accum =& $stack->getAccum();
491 extract( $stack->getFlags() );
492 $i += $count;
494 } elseif ( $found == 'line-end' ) {
495 $piece = $stack->top;
496 // A heading must be open, otherwise \n wouldn't have been in the search list
497 assert( $piece->open === "\n" );
498 $part = $piece->getCurrentPart();
499 // Search back through the input to see if it has a proper close.
500 // Do this using the reversed string since the other solutions
501 // (end anchor, etc.) are inefficient.
502 $wsLength = strspn( $revText, " \t", $lengthText - $i );
503 $searchStart = $i - $wsLength;
504 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
505 // Comment found at line end
506 // Search for equals signs before the comment
507 $searchStart = $part->visualEnd;
508 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
510 $count = $piece->count;
511 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
512 if ( $equalsLength > 0 ) {
513 if ( $searchStart - $equalsLength == $piece->startPos ) {
514 // This is just a single string of equals signs on its own line
515 // Replicate the doHeadings behavior /={count}(.+)={count}/
516 // First find out how many equals signs there really are (don't stop at 6)
517 $count = $equalsLength;
518 if ( $count < 3 ) {
519 $count = 0;
520 } else {
521 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
523 } else {
524 $count = min( $equalsLength, $count );
526 if ( $count > 0 ) {
527 // Normal match, output <h>
528 $element = [ [ 'possible-h',
529 array_merge(
531 [ '@level', [ $count ] ],
532 [ '@i', [ $headingIndex++ ] ]
534 $accum
536 ] ];
537 } else {
538 // Single equals sign on its own line, count=0
539 $element = $accum;
541 } else {
542 // No match, no <h>, just pass down the inner text
543 $element = $accum;
545 // Unwind the stack
546 $stack->pop();
547 $accum =& $stack->getAccum();
548 extract( $stack->getFlags() );
550 // Append the result to the enclosing accumulator
551 array_splice( $accum, count( $accum ), 0, $element );
553 // Note that we do NOT increment the input pointer.
554 // This is because the closing linebreak could be the opening linebreak of
555 // another heading. Infinite loops are avoided because the next iteration MUST
556 // hit the heading open case above, which unconditionally increments the
557 // input pointer.
558 } elseif ( $found == 'open' ) {
559 # count opening brace characters
560 $curLen = strlen( $curChar );
561 $count = ( $curLen > 1 ) ? 1 : strspn( $text, $curChar, $i );
563 # we need to add to stack only if opening brace count is enough for one of the rules
564 if ( $count >= $rule['min'] ) {
565 # Add it to the stack
566 $piece = [
567 'open' => $curChar,
568 'close' => $rule['end'],
569 'count' => $count,
570 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
573 $stack->push( $piece );
574 $accum =& $stack->getAccum();
575 extract( $stack->getFlags() );
576 } else {
577 # Add literal brace(s)
578 self::addLiteral( $accum, str_repeat( $curChar, $count ) );
580 $i += $curLen * $count;
581 } elseif ( $found == 'close' ) {
582 $piece = $stack->top;
583 # lets check if there are enough characters for closing brace
584 $maxCount = $piece->count;
585 $curLen = strlen( $curChar );
586 $count = ( $curLen > 1 ) ? 1 : strspn( $text, $curChar, $i, $maxCount );
588 # check for maximum matching characters (if there are 5 closing
589 # characters, we will probably need only 3 - depending on the rules)
590 $rule = $this->rules[$piece->open];
591 if ( $count > $rule['max'] ) {
592 # The specified maximum exists in the callback array, unless the caller
593 # has made an error
594 $matchingCount = $rule['max'];
595 } else {
596 # Count is less than the maximum
597 # Skip any gaps in the callback array to find the true largest match
598 # Need to use array_key_exists not isset because the callback can be null
599 $matchingCount = $count;
600 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
601 --$matchingCount;
605 if ( $matchingCount <= 0 ) {
606 # No matching element found in callback array
607 # Output a literal closing brace and continue
608 self::addLiteral( $accum, str_repeat( $curChar, $count ) );
609 $i += $curLen * $count;
610 continue;
612 $name = $rule['names'][$matchingCount];
613 if ( $name === null ) {
614 // No element, just literal text
615 $element = $piece->breakSyntax( $matchingCount );
616 self::addLiteral( $element, str_repeat( $rule['end'], $matchingCount ) );
617 } else {
618 # Create XML element
619 $parts = $piece->parts;
620 $titleAccum = $parts[0]->out;
621 unset( $parts[0] );
623 $children = [];
625 # The invocation is at the start of the line if lineStart is set in
626 # the stack, and all opening brackets are used up.
627 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
628 $children[] = [ '@lineStart', [ 1 ] ];
630 $titleNode = [ 'title', $titleAccum ];
631 $children[] = $titleNode;
632 $argIndex = 1;
633 foreach ( $parts as $part ) {
634 if ( isset( $part->eqpos ) ) {
635 $equalsNode = $part->out[$part->eqpos];
636 $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
637 $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
638 $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
639 $children[] = $partNode;
640 } else {
641 $nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ];
642 $valueNode = [ 'value', $part->out ];
643 $partNode = [ 'part', [ $nameNode, $valueNode ] ];
644 $children[] = $partNode;
647 $element = [ [ $name, $children ] ];
650 # Advance input pointer
651 $i += $curLen * $matchingCount;
653 # Unwind the stack
654 $stack->pop();
655 $accum =& $stack->getAccum();
657 # Re-add the old stack element if it still has unmatched opening characters remaining
658 if ( $matchingCount < $piece->count ) {
659 $piece->parts = [ new PPDPart_Hash ];
660 $piece->count -= $matchingCount;
661 # do we still qualify for any callback with remaining count?
662 $min = $this->rules[$piece->open]['min'];
663 if ( $piece->count >= $min ) {
664 $stack->push( $piece );
665 $accum =& $stack->getAccum();
666 } else {
667 self::addLiteral( $accum, str_repeat( $piece->open, $piece->count ) );
671 extract( $stack->getFlags() );
673 # Add XML element to the enclosing accumulator
674 array_splice( $accum, count( $accum ), 0, $element );
675 } elseif ( $found == 'pipe' ) {
676 $findEquals = true; // shortcut for getFlags()
677 $stack->addPart();
678 $accum =& $stack->getAccum();
679 ++$i;
680 } elseif ( $found == 'equals' ) {
681 $findEquals = false; // shortcut for getFlags()
682 $accum[] = [ 'equals', [ '=' ] ];
683 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
684 ++$i;
685 } elseif ( $found == 'dash' ) {
686 self::addLiteral( $accum, '-' );
687 ++$i;
691 # Output any remaining unclosed brackets
692 foreach ( $stack->stack as $piece ) {
693 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
696 # Enable top-level headings
697 foreach ( $stack->rootAccum as &$node ) {
698 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
699 $node[PPNode_Hash_Tree::NAME] = 'h';
703 $rootStore = [ [ 'root', $stack->rootAccum ] ];
704 $rootNode = new PPNode_Hash_Tree( $rootStore, 0 );
706 // Cache
707 $tree = json_encode( $rootStore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
708 if ( $tree !== false ) {
709 $this->cacheSetTree( $text, $flags, $tree );
712 return $rootNode;
715 private static function addLiteral( array &$accum, $text ) {
716 $n = count( $accum );
717 if ( $n && is_string( $accum[$n - 1] ) ) {
718 $accum[$n - 1] .= $text;
719 } else {
720 $accum[] = $text;
726 * Stack class to help Preprocessor::preprocessToObj()
727 * @ingroup Parser
729 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
730 class PPDStack_Hash extends PPDStack {
731 // @codingStandardsIgnoreEnd
733 public function __construct() {
734 $this->elementClass = 'PPDStackElement_Hash';
735 parent::__construct();
736 $this->rootAccum = [];
741 * @ingroup Parser
743 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
744 class PPDStackElement_Hash extends PPDStackElement {
745 // @codingStandardsIgnoreEnd
747 public function __construct( $data = [] ) {
748 $this->partClass = 'PPDPart_Hash';
749 parent::__construct( $data );
753 * Get the accumulator that would result if the close is not found.
755 * @param int|bool $openingCount
756 * @return array
758 public function breakSyntax( $openingCount = false ) {
759 if ( $this->open == "\n" ) {
760 $accum = $this->parts[0]->out;
761 } else {
762 if ( $openingCount === false ) {
763 $openingCount = $this->count;
765 $accum = [ str_repeat( $this->open, $openingCount ) ];
766 $lastIndex = 0;
767 $first = true;
768 foreach ( $this->parts as $part ) {
769 if ( $first ) {
770 $first = false;
771 } elseif ( is_string( $accum[$lastIndex] ) ) {
772 $accum[$lastIndex] .= '|';
773 } else {
774 $accum[++$lastIndex] = '|';
776 foreach ( $part->out as $node ) {
777 if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) {
778 $accum[$lastIndex] .= $node;
779 } else {
780 $accum[++$lastIndex] = $node;
785 return $accum;
790 * @ingroup Parser
792 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
793 class PPDPart_Hash extends PPDPart {
794 // @codingStandardsIgnoreEnd
796 public function __construct( $out = '' ) {
797 if ( $out !== '' ) {
798 $accum = [ $out ];
799 } else {
800 $accum = [];
802 parent::__construct( $accum );
807 * An expansion frame, used as a context to expand the result of preprocessToObj()
808 * @ingroup Parser
810 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
811 class PPFrame_Hash implements PPFrame {
812 // @codingStandardsIgnoreEnd
815 * @var Parser
817 public $parser;
820 * @var Preprocessor
822 public $preprocessor;
825 * @var Title
827 public $title;
828 public $titleCache;
831 * Hashtable listing templates which are disallowed for expansion in this frame,
832 * having been encountered previously in parent frames.
834 public $loopCheckHash;
837 * Recursion depth of this frame, top = 0
838 * Note that this is NOT the same as expansion depth in expand()
840 public $depth;
842 private $volatile = false;
843 private $ttl = null;
846 * @var array
848 protected $childExpansionCache;
851 * Construct a new preprocessor frame.
852 * @param Preprocessor $preprocessor The parent preprocessor
854 public function __construct( $preprocessor ) {
855 $this->preprocessor = $preprocessor;
856 $this->parser = $preprocessor->parser;
857 $this->title = $this->parser->mTitle;
858 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
859 $this->loopCheckHash = [];
860 $this->depth = 0;
861 $this->childExpansionCache = [];
865 * Create a new child frame
866 * $args is optionally a multi-root PPNode or array containing the template arguments
868 * @param array|bool|PPNode_Hash_Array $args
869 * @param Title|bool $title
870 * @param int $indexOffset
871 * @throws MWException
872 * @return PPTemplateFrame_Hash
874 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
875 $namedArgs = [];
876 $numberedArgs = [];
877 if ( $title === false ) {
878 $title = $this->title;
880 if ( $args !== false ) {
881 if ( $args instanceof PPNode_Hash_Array ) {
882 $args = $args->value;
883 } elseif ( !is_array( $args ) ) {
884 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
886 foreach ( $args as $arg ) {
887 $bits = $arg->splitArg();
888 if ( $bits['index'] !== '' ) {
889 // Numbered parameter
890 $index = $bits['index'] - $indexOffset;
891 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
892 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
893 wfEscapeWikiText( $this->title ),
894 wfEscapeWikiText( $title ),
895 wfEscapeWikiText( $index ) )->text() );
896 $this->parser->addTrackingCategory( 'duplicate-args-category' );
898 $numberedArgs[$index] = $bits['value'];
899 unset( $namedArgs[$index] );
900 } else {
901 // Named parameter
902 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
903 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
904 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
905 wfEscapeWikiText( $this->title ),
906 wfEscapeWikiText( $title ),
907 wfEscapeWikiText( $name ) )->text() );
908 $this->parser->addTrackingCategory( 'duplicate-args-category' );
910 $namedArgs[$name] = $bits['value'];
911 unset( $numberedArgs[$name] );
915 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
919 * @throws MWException
920 * @param string|int $key
921 * @param string|PPNode $root
922 * @param int $flags
923 * @return string
925 public function cachedExpand( $key, $root, $flags = 0 ) {
926 // we don't have a parent, so we don't have a cache
927 return $this->expand( $root, $flags );
931 * @throws MWException
932 * @param string|PPNode $root
933 * @param int $flags
934 * @return string
936 public function expand( $root, $flags = 0 ) {
937 static $expansionDepth = 0;
938 if ( is_string( $root ) ) {
939 return $root;
942 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
943 $this->parser->limitationWarn( 'node-count-exceeded',
944 $this->parser->mPPNodeCount,
945 $this->parser->mOptions->getMaxPPNodeCount()
947 return '<span class="error">Node-count limit exceeded</span>';
949 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
950 $this->parser->limitationWarn( 'expansion-depth-exceeded',
951 $expansionDepth,
952 $this->parser->mOptions->getMaxPPExpandDepth()
954 return '<span class="error">Expansion depth limit exceeded</span>';
956 ++$expansionDepth;
957 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
958 $this->parser->mHighestExpansionDepth = $expansionDepth;
961 $outStack = [ '', '' ];
962 $iteratorStack = [ false, $root ];
963 $indexStack = [ 0, 0 ];
965 while ( count( $iteratorStack ) > 1 ) {
966 $level = count( $outStack ) - 1;
967 $iteratorNode =& $iteratorStack[$level];
968 $out =& $outStack[$level];
969 $index =& $indexStack[$level];
971 if ( is_array( $iteratorNode ) ) {
972 if ( $index >= count( $iteratorNode ) ) {
973 // All done with this iterator
974 $iteratorStack[$level] = false;
975 $contextNode = false;
976 } else {
977 $contextNode = $iteratorNode[$index];
978 $index++;
980 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
981 if ( $index >= $iteratorNode->getLength() ) {
982 // All done with this iterator
983 $iteratorStack[$level] = false;
984 $contextNode = false;
985 } else {
986 $contextNode = $iteratorNode->item( $index );
987 $index++;
989 } else {
990 // Copy to $contextNode and then delete from iterator stack,
991 // because this is not an iterator but we do have to execute it once
992 $contextNode = $iteratorStack[$level];
993 $iteratorStack[$level] = false;
996 $newIterator = false;
997 $contextName = false;
998 $contextChildren = false;
1000 if ( $contextNode === false ) {
1001 // nothing to do
1002 } elseif ( is_string( $contextNode ) ) {
1003 $out .= $contextNode;
1004 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
1005 $newIterator = $contextNode;
1006 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
1007 // No output
1008 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
1009 $out .= $contextNode->value;
1010 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
1011 $contextName = $contextNode->name;
1012 $contextChildren = $contextNode->getRawChildren();
1013 } elseif ( is_array( $contextNode ) ) {
1014 // Node descriptor array
1015 if ( count( $contextNode ) !== 2 ) {
1016 throw new MWException( __METHOD__.
1017 ': found an array where a node descriptor should be' );
1019 list( $contextName, $contextChildren ) = $contextNode;
1020 } else {
1021 throw new MWException( __METHOD__ . ': Invalid parameter type' );
1024 // Handle node descriptor array or tree object
1025 if ( $contextName === false ) {
1026 // Not a node, already handled above
1027 } elseif ( $contextName[0] === '@' ) {
1028 // Attribute: no output
1029 } elseif ( $contextName === 'template' ) {
1030 # Double-brace expansion
1031 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1032 if ( $flags & PPFrame::NO_TEMPLATES ) {
1033 $newIterator = $this->virtualBracketedImplode(
1034 '{{', '|', '}}',
1035 $bits['title'],
1036 $bits['parts']
1038 } else {
1039 $ret = $this->parser->braceSubstitution( $bits, $this );
1040 if ( isset( $ret['object'] ) ) {
1041 $newIterator = $ret['object'];
1042 } else {
1043 $out .= $ret['text'];
1046 } elseif ( $contextName === 'tplarg' ) {
1047 # Triple-brace expansion
1048 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1049 if ( $flags & PPFrame::NO_ARGS ) {
1050 $newIterator = $this->virtualBracketedImplode(
1051 '{{{', '|', '}}}',
1052 $bits['title'],
1053 $bits['parts']
1055 } else {
1056 $ret = $this->parser->argSubstitution( $bits, $this );
1057 if ( isset( $ret['object'] ) ) {
1058 $newIterator = $ret['object'];
1059 } else {
1060 $out .= $ret['text'];
1063 } elseif ( $contextName === 'comment' ) {
1064 # HTML-style comment
1065 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1066 # Not in RECOVER_COMMENTS mode (msgnw) though.
1067 if ( ( $this->parser->ot['html']
1068 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
1069 || ( $flags & PPFrame::STRIP_COMMENTS )
1070 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
1072 $out .= '';
1073 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1074 # Add a strip marker in PST mode so that pstPass2() can
1075 # run some old-fashioned regexes on the result.
1076 # Not in RECOVER_COMMENTS mode (extractSections) though.
1077 $out .= $this->parser->insertStripItem( $contextChildren[0] );
1078 } else {
1079 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1080 $out .= $contextChildren[0];
1082 } elseif ( $contextName === 'ignore' ) {
1083 # Output suppression used by <includeonly> etc.
1084 # OT_WIKI will only respect <ignore> in substed templates.
1085 # The other output types respect it unless NO_IGNORE is set.
1086 # extractSections() sets NO_IGNORE and so never respects it.
1087 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
1088 || ( $flags & PPFrame::NO_IGNORE )
1090 $out .= $contextChildren[0];
1091 } else {
1092 // $out .= '';
1094 } elseif ( $contextName === 'ext' ) {
1095 # Extension tag
1096 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
1097 [ 'attr' => null, 'inner' => null, 'close' => null ];
1098 if ( $flags & PPFrame::NO_TAGS ) {
1099 $s = '<' . $bits['name']->getFirstChild()->value;
1100 if ( $bits['attr'] ) {
1101 $s .= $bits['attr']->getFirstChild()->value;
1103 if ( $bits['inner'] ) {
1104 $s .= '>' . $bits['inner']->getFirstChild()->value;
1105 if ( $bits['close'] ) {
1106 $s .= $bits['close']->getFirstChild()->value;
1108 } else {
1109 $s .= '/>';
1111 $out .= $s;
1112 } else {
1113 $out .= $this->parser->extensionSubstitution( $bits, $this );
1115 } elseif ( $contextName === 'h' ) {
1116 # Heading
1117 if ( $this->parser->ot['html'] ) {
1118 # Expand immediately and insert heading index marker
1119 $s = $this->expand( $contextChildren, $flags );
1120 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
1121 $titleText = $this->title->getPrefixedDBkey();
1122 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
1123 $serial = count( $this->parser->mHeadings ) - 1;
1124 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
1125 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1126 $this->parser->mStripState->addGeneral( $marker, '' );
1127 $out .= $s;
1128 } else {
1129 # Expand in virtual stack
1130 $newIterator = $contextChildren;
1132 } else {
1133 # Generic recursive expansion
1134 $newIterator = $contextChildren;
1137 if ( $newIterator !== false ) {
1138 $outStack[] = '';
1139 $iteratorStack[] = $newIterator;
1140 $indexStack[] = 0;
1141 } elseif ( $iteratorStack[$level] === false ) {
1142 // Return accumulated value to parent
1143 // With tail recursion
1144 while ( $iteratorStack[$level] === false && $level > 0 ) {
1145 $outStack[$level - 1] .= $out;
1146 array_pop( $outStack );
1147 array_pop( $iteratorStack );
1148 array_pop( $indexStack );
1149 $level--;
1153 --$expansionDepth;
1154 return $outStack[0];
1158 * @param string $sep
1159 * @param int $flags
1160 * @param string|PPNode $args,...
1161 * @return string
1163 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1164 $args = array_slice( func_get_args(), 2 );
1166 $first = true;
1167 $s = '';
1168 foreach ( $args as $root ) {
1169 if ( $root instanceof PPNode_Hash_Array ) {
1170 $root = $root->value;
1172 if ( !is_array( $root ) ) {
1173 $root = [ $root ];
1175 foreach ( $root as $node ) {
1176 if ( $first ) {
1177 $first = false;
1178 } else {
1179 $s .= $sep;
1181 $s .= $this->expand( $node, $flags );
1184 return $s;
1188 * Implode with no flags specified
1189 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1190 * @param string $sep
1191 * @param string|PPNode $args,...
1192 * @return string
1194 public function implode( $sep /*, ... */ ) {
1195 $args = array_slice( func_get_args(), 1 );
1197 $first = true;
1198 $s = '';
1199 foreach ( $args as $root ) {
1200 if ( $root instanceof PPNode_Hash_Array ) {
1201 $root = $root->value;
1203 if ( !is_array( $root ) ) {
1204 $root = [ $root ];
1206 foreach ( $root as $node ) {
1207 if ( $first ) {
1208 $first = false;
1209 } else {
1210 $s .= $sep;
1212 $s .= $this->expand( $node );
1215 return $s;
1219 * Makes an object that, when expand()ed, will be the same as one obtained
1220 * with implode()
1222 * @param string $sep
1223 * @param string|PPNode $args,...
1224 * @return PPNode_Hash_Array
1226 public function virtualImplode( $sep /*, ... */ ) {
1227 $args = array_slice( func_get_args(), 1 );
1228 $out = [];
1229 $first = true;
1231 foreach ( $args as $root ) {
1232 if ( $root instanceof PPNode_Hash_Array ) {
1233 $root = $root->value;
1235 if ( !is_array( $root ) ) {
1236 $root = [ $root ];
1238 foreach ( $root as $node ) {
1239 if ( $first ) {
1240 $first = false;
1241 } else {
1242 $out[] = $sep;
1244 $out[] = $node;
1247 return new PPNode_Hash_Array( $out );
1251 * Virtual implode with brackets
1253 * @param string $start
1254 * @param string $sep
1255 * @param string $end
1256 * @param string|PPNode $args,...
1257 * @return PPNode_Hash_Array
1259 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1260 $args = array_slice( func_get_args(), 3 );
1261 $out = [ $start ];
1262 $first = true;
1264 foreach ( $args as $root ) {
1265 if ( $root instanceof PPNode_Hash_Array ) {
1266 $root = $root->value;
1268 if ( !is_array( $root ) ) {
1269 $root = [ $root ];
1271 foreach ( $root as $node ) {
1272 if ( $first ) {
1273 $first = false;
1274 } else {
1275 $out[] = $sep;
1277 $out[] = $node;
1280 $out[] = $end;
1281 return new PPNode_Hash_Array( $out );
1284 public function __toString() {
1285 return 'frame{}';
1289 * @param bool $level
1290 * @return array|bool|string
1292 public function getPDBK( $level = false ) {
1293 if ( $level === false ) {
1294 return $this->title->getPrefixedDBkey();
1295 } else {
1296 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1301 * @return array
1303 public function getArguments() {
1304 return [];
1308 * @return array
1310 public function getNumberedArguments() {
1311 return [];
1315 * @return array
1317 public function getNamedArguments() {
1318 return [];
1322 * Returns true if there are no arguments in this frame
1324 * @return bool
1326 public function isEmpty() {
1327 return true;
1331 * @param int|string $name
1332 * @return bool Always false in this implementation.
1334 public function getArgument( $name ) {
1335 return false;
1339 * Returns true if the infinite loop check is OK, false if a loop is detected
1341 * @param Title $title
1343 * @return bool
1345 public function loopCheck( $title ) {
1346 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1350 * Return true if the frame is a template frame
1352 * @return bool
1354 public function isTemplate() {
1355 return false;
1359 * Get a title of frame
1361 * @return Title
1363 public function getTitle() {
1364 return $this->title;
1368 * Set the volatile flag
1370 * @param bool $flag
1372 public function setVolatile( $flag = true ) {
1373 $this->volatile = $flag;
1377 * Get the volatile flag
1379 * @return bool
1381 public function isVolatile() {
1382 return $this->volatile;
1386 * Set the TTL
1388 * @param int $ttl
1390 public function setTTL( $ttl ) {
1391 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
1392 $this->ttl = $ttl;
1397 * Get the TTL
1399 * @return int|null
1401 public function getTTL() {
1402 return $this->ttl;
1407 * Expansion frame with template arguments
1408 * @ingroup Parser
1410 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1411 class PPTemplateFrame_Hash extends PPFrame_Hash {
1412 // @codingStandardsIgnoreEnd
1414 public $numberedArgs, $namedArgs, $parent;
1415 public $numberedExpansionCache, $namedExpansionCache;
1418 * @param Preprocessor $preprocessor
1419 * @param bool|PPFrame $parent
1420 * @param array $numberedArgs
1421 * @param array $namedArgs
1422 * @param bool|Title $title
1424 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
1425 $namedArgs = [], $title = false
1427 parent::__construct( $preprocessor );
1429 $this->parent = $parent;
1430 $this->numberedArgs = $numberedArgs;
1431 $this->namedArgs = $namedArgs;
1432 $this->title = $title;
1433 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1434 $this->titleCache = $parent->titleCache;
1435 $this->titleCache[] = $pdbk;
1436 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1437 if ( $pdbk !== false ) {
1438 $this->loopCheckHash[$pdbk] = true;
1440 $this->depth = $parent->depth + 1;
1441 $this->numberedExpansionCache = $this->namedExpansionCache = [];
1444 public function __toString() {
1445 $s = 'tplframe{';
1446 $first = true;
1447 $args = $this->numberedArgs + $this->namedArgs;
1448 foreach ( $args as $name => $value ) {
1449 if ( $first ) {
1450 $first = false;
1451 } else {
1452 $s .= ', ';
1454 $s .= "\"$name\":\"" .
1455 str_replace( '"', '\\"', $value->__toString() ) . '"';
1457 $s .= '}';
1458 return $s;
1462 * @throws MWException
1463 * @param string|int $key
1464 * @param string|PPNode $root
1465 * @param int $flags
1466 * @return string
1468 public function cachedExpand( $key, $root, $flags = 0 ) {
1469 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
1470 return $this->parent->childExpansionCache[$key];
1472 $retval = $this->expand( $root, $flags );
1473 if ( !$this->isVolatile() ) {
1474 $this->parent->childExpansionCache[$key] = $retval;
1476 return $retval;
1480 * Returns true if there are no arguments in this frame
1482 * @return bool
1484 public function isEmpty() {
1485 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1489 * @return array
1491 public function getArguments() {
1492 $arguments = [];
1493 foreach ( array_merge(
1494 array_keys( $this->numberedArgs ),
1495 array_keys( $this->namedArgs ) ) as $key ) {
1496 $arguments[$key] = $this->getArgument( $key );
1498 return $arguments;
1502 * @return array
1504 public function getNumberedArguments() {
1505 $arguments = [];
1506 foreach ( array_keys( $this->numberedArgs ) as $key ) {
1507 $arguments[$key] = $this->getArgument( $key );
1509 return $arguments;
1513 * @return array
1515 public function getNamedArguments() {
1516 $arguments = [];
1517 foreach ( array_keys( $this->namedArgs ) as $key ) {
1518 $arguments[$key] = $this->getArgument( $key );
1520 return $arguments;
1524 * @param int $index
1525 * @return string|bool
1527 public function getNumberedArgument( $index ) {
1528 if ( !isset( $this->numberedArgs[$index] ) ) {
1529 return false;
1531 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1532 # No trimming for unnamed arguments
1533 $this->numberedExpansionCache[$index] = $this->parent->expand(
1534 $this->numberedArgs[$index],
1535 PPFrame::STRIP_COMMENTS
1538 return $this->numberedExpansionCache[$index];
1542 * @param string $name
1543 * @return string|bool
1545 public function getNamedArgument( $name ) {
1546 if ( !isset( $this->namedArgs[$name] ) ) {
1547 return false;
1549 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1550 # Trim named arguments post-expand, for backwards compatibility
1551 $this->namedExpansionCache[$name] = trim(
1552 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1554 return $this->namedExpansionCache[$name];
1558 * @param int|string $name
1559 * @return string|bool
1561 public function getArgument( $name ) {
1562 $text = $this->getNumberedArgument( $name );
1563 if ( $text === false ) {
1564 $text = $this->getNamedArgument( $name );
1566 return $text;
1570 * Return true if the frame is a template frame
1572 * @return bool
1574 public function isTemplate() {
1575 return true;
1578 public function setVolatile( $flag = true ) {
1579 parent::setVolatile( $flag );
1580 $this->parent->setVolatile( $flag );
1583 public function setTTL( $ttl ) {
1584 parent::setTTL( $ttl );
1585 $this->parent->setTTL( $ttl );
1590 * Expansion frame with custom arguments
1591 * @ingroup Parser
1593 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1594 class PPCustomFrame_Hash extends PPFrame_Hash {
1595 // @codingStandardsIgnoreEnd
1597 public $args;
1599 public function __construct( $preprocessor, $args ) {
1600 parent::__construct( $preprocessor );
1601 $this->args = $args;
1604 public function __toString() {
1605 $s = 'cstmframe{';
1606 $first = true;
1607 foreach ( $this->args as $name => $value ) {
1608 if ( $first ) {
1609 $first = false;
1610 } else {
1611 $s .= ', ';
1613 $s .= "\"$name\":\"" .
1614 str_replace( '"', '\\"', $value->__toString() ) . '"';
1616 $s .= '}';
1617 return $s;
1621 * @return bool
1623 public function isEmpty() {
1624 return !count( $this->args );
1628 * @param int|string $index
1629 * @return string|bool
1631 public function getArgument( $index ) {
1632 if ( !isset( $this->args[$index] ) ) {
1633 return false;
1635 return $this->args[$index];
1638 public function getArguments() {
1639 return $this->args;
1644 * @ingroup Parser
1646 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1647 class PPNode_Hash_Tree implements PPNode {
1648 // @codingStandardsIgnoreEnd
1650 public $name;
1653 * The store array for children of this node. It is "raw" in the sense that
1654 * nodes are two-element arrays ("descriptors") rather than PPNode_Hash_*
1655 * objects.
1657 private $rawChildren;
1660 * The store array for the siblings of this node, including this node itself.
1662 private $store;
1665 * The index into $this->store which contains the descriptor of this node.
1667 private $index;
1670 * The offset of the name within descriptors, used in some places for
1671 * readability.
1673 const NAME = 0;
1676 * The offset of the child list within descriptors, used in some places for
1677 * readability.
1679 const CHILDREN = 1;
1682 * Construct an object using the data from $store[$index]. The rest of the
1683 * store array can be accessed via getNextSibling().
1685 * @param array $store
1686 * @param integer $index
1688 public function __construct( array $store, $index ) {
1689 $this->store = $store;
1690 $this->index = $index;
1691 list( $this->name, $this->rawChildren ) = $this->store[$index];
1695 * Construct an appropriate PPNode_Hash_* object with a class that depends
1696 * on what is at the relevant store index.
1698 * @param array $store
1699 * @param integer $index
1700 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text
1702 public static function factory( array $store, $index ) {
1703 if ( !isset( $store[$index] ) ) {
1704 return false;
1707 $descriptor = $store[$index];
1708 if ( is_string( $descriptor ) ) {
1709 $class = 'PPNode_Hash_Text';
1710 } elseif ( is_array( $descriptor ) ) {
1711 if ( $descriptor[self::NAME][0] === '@' ) {
1712 $class = 'PPNode_Hash_Attr';
1713 } else {
1714 $class = 'PPNode_Hash_Tree';
1716 } else {
1717 throw new MWException( __METHOD__.': invalid node descriptor' );
1719 return new $class( $store, $index );
1723 * Convert a node to XML, for debugging
1725 public function __toString() {
1726 $inner = '';
1727 $attribs = '';
1728 for ( $node = $this->getFirstChild(); $node; $node = $node->getNextSibling() ) {
1729 if ( $node instanceof PPNode_Hash_Attr ) {
1730 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1731 } else {
1732 $inner .= $node->__toString();
1735 if ( $inner === '' ) {
1736 return "<{$this->name}$attribs/>";
1737 } else {
1738 return "<{$this->name}$attribs>$inner</{$this->name}>";
1743 * @return PPNode_Hash_Array
1745 public function getChildren() {
1746 $children = [];
1747 foreach ( $this->rawChildren as $i => $child ) {
1748 $children[] = self::factory( $this->rawChildren, $i );
1750 return new PPNode_Hash_Array( $children );
1754 * Get the first child, or false if there is none. Note that this will
1755 * return a temporary proxy object: different instances will be returned
1756 * if this is called more than once on the same node.
1758 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|boolean
1760 public function getFirstChild() {
1761 if ( !isset( $this->rawChildren[0] ) ) {
1762 return false;
1763 } else {
1764 return self::factory( $this->rawChildren, 0 );
1769 * Get the next sibling, or false if there is none. Note that this will
1770 * return a temporary proxy object: different instances will be returned
1771 * if this is called more than once on the same node.
1773 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|boolean
1775 public function getNextSibling() {
1776 return self::factory( $this->store, $this->index + 1 );
1780 * Get an array of the children with a given node name
1782 * @param string $name
1783 * @return PPNode_Hash_Array
1785 public function getChildrenOfType( $name ) {
1786 $children = [];
1787 foreach ( $this->rawChildren as $i => $child ) {
1788 if ( is_array( $child ) && $child[self::NAME] === $name ) {
1789 $children[] = self::factory( $this->rawChildren, $i );
1792 return new PPNode_Hash_Array( $children );
1796 * Get the raw child array. For internal use.
1797 * @return array
1799 public function getRawChildren() {
1800 return $this->rawChildren;
1804 * @return bool
1806 public function getLength() {
1807 return false;
1811 * @param int $i
1812 * @return bool
1814 public function item( $i ) {
1815 return false;
1819 * @return string
1821 public function getName() {
1822 return $this->name;
1826 * Split a "<part>" node into an associative array containing:
1827 * - name PPNode name
1828 * - index String index
1829 * - value PPNode value
1831 * @throws MWException
1832 * @return array
1834 public function splitArg() {
1835 return self::splitRawArg( $this->rawChildren );
1839 * Like splitArg() but for a raw child array. For internal use only.
1841 public static function splitRawArg( array $children ) {
1842 $bits = [];
1843 foreach ( $children as $i => $child ) {
1844 if ( !is_array( $child ) ) {
1845 continue;
1847 if ( $child[self::NAME] === 'name' ) {
1848 $bits['name'] = new self( $children, $i );
1849 if ( isset( $child[self::CHILDREN][0][self::NAME] )
1850 && $child[self::CHILDREN][0][self::NAME] === '@index'
1852 $bits['index'] = $child[self::CHILDREN][0][self::CHILDREN][0];
1854 } elseif ( $child[self::NAME] === 'value' ) {
1855 $bits['value'] = new self( $children, $i );
1859 if ( !isset( $bits['name'] ) ) {
1860 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1862 if ( !isset( $bits['index'] ) ) {
1863 $bits['index'] = '';
1865 return $bits;
1869 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1870 * All values in the resulting array are PPNodes. Inner and close are optional.
1872 * @throws MWException
1873 * @return array
1875 public function splitExt() {
1876 return self::splitRawExt( $this->rawChildren );
1880 * Like splitExt() but for a raw child array. For internal use only.
1882 public static function splitRawExt( array $children ) {
1883 $bits = [];
1884 foreach ( $children as $i => $child ) {
1885 if ( !is_array( $child ) ) {
1886 continue;
1888 switch ( $child[self::NAME] ) {
1889 case 'name':
1890 $bits['name'] = new self( $children, $i );
1891 break;
1892 case 'attr':
1893 $bits['attr'] = new self( $children, $i );
1894 break;
1895 case 'inner':
1896 $bits['inner'] = new self( $children, $i );
1897 break;
1898 case 'close':
1899 $bits['close'] = new self( $children, $i );
1900 break;
1903 if ( !isset( $bits['name'] ) ) {
1904 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1906 return $bits;
1910 * Split an "<h>" node
1912 * @throws MWException
1913 * @return array
1915 public function splitHeading() {
1916 if ( $this->name !== 'h' ) {
1917 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1919 return self::splitRawHeading( $this->rawChildren );
1923 * Like splitHeading() but for a raw child array. For internal use only.
1925 public static function splitRawHeading( array $children ) {
1926 $bits = [];
1927 foreach ( $children as $i => $child ) {
1928 if ( !is_array( $child ) ) {
1929 continue;
1931 if ( $child[self::NAME] === '@i' ) {
1932 $bits['i'] = $child[self::CHILDREN][0];
1933 } elseif ( $child[self::NAME] === '@level' ) {
1934 $bits['level'] = $child[self::CHILDREN][0];
1937 if ( !isset( $bits['i'] ) ) {
1938 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1940 return $bits;
1944 * Split a "<template>" or "<tplarg>" node
1946 * @throws MWException
1947 * @return array
1949 public function splitTemplate() {
1950 return self::splitRawTemplate( $this->rawChildren );
1954 * Like splitTemplate() but for a raw child array. For internal use only.
1956 public static function splitRawTemplate( array $children ) {
1957 $parts = [];
1958 $bits = [ 'lineStart' => '' ];
1959 foreach ( $children as $i => $child ) {
1960 if ( !is_array( $child ) ) {
1961 continue;
1963 switch ( $child[self::NAME] ) {
1964 case 'title':
1965 $bits['title'] = new self( $children, $i );
1966 break;
1967 case 'part':
1968 $parts[] = new self( $children, $i );
1969 break;
1970 case '@lineStart':
1971 $bits['lineStart'] = '1';
1972 break;
1975 if ( !isset( $bits['title'] ) ) {
1976 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
1978 $bits['parts'] = new PPNode_Hash_Array( $parts );
1979 return $bits;
1984 * @ingroup Parser
1986 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1987 class PPNode_Hash_Text implements PPNode {
1988 // @codingStandardsIgnoreEnd
1990 public $value;
1991 private $store, $index;
1994 * Construct an object using the data from $store[$index]. The rest of the
1995 * store array can be accessed via getNextSibling().
1997 * @param array $store
1998 * @param integer $index
2000 public function __construct( array $store, $index ) {
2001 $this->value = $store[$index];
2002 if ( !is_scalar( $this->value ) ) {
2003 throw new MWException( __CLASS__ . ' given object instead of string' );
2005 $this->store = $store;
2006 $this->index = $index;
2009 public function __toString() {
2010 return htmlspecialchars( $this->value );
2013 public function getNextSibling() {
2014 return PPNode_Hash_Tree::factory( $this->store, $this->index + 1 );
2017 public function getChildren() {
2018 return false;
2021 public function getFirstChild() {
2022 return false;
2025 public function getChildrenOfType( $name ) {
2026 return false;
2029 public function getLength() {
2030 return false;
2033 public function item( $i ) {
2034 return false;
2037 public function getName() {
2038 return '#text';
2041 public function splitArg() {
2042 throw new MWException( __METHOD__ . ': not supported' );
2045 public function splitExt() {
2046 throw new MWException( __METHOD__ . ': not supported' );
2049 public function splitHeading() {
2050 throw new MWException( __METHOD__ . ': not supported' );
2055 * @ingroup Parser
2057 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
2058 class PPNode_Hash_Array implements PPNode {
2059 // @codingStandardsIgnoreEnd
2061 public $value;
2063 public function __construct( $value ) {
2064 $this->value = $value;
2067 public function __toString() {
2068 return var_export( $this, true );
2071 public function getLength() {
2072 return count( $this->value );
2075 public function item( $i ) {
2076 return $this->value[$i];
2079 public function getName() {
2080 return '#nodelist';
2083 public function getNextSibling() {
2084 return false;
2087 public function getChildren() {
2088 return false;
2091 public function getFirstChild() {
2092 return false;
2095 public function getChildrenOfType( $name ) {
2096 return false;
2099 public function splitArg() {
2100 throw new MWException( __METHOD__ . ': not supported' );
2103 public function splitExt() {
2104 throw new MWException( __METHOD__ . ': not supported' );
2107 public function splitHeading() {
2108 throw new MWException( __METHOD__ . ': not supported' );
2113 * @ingroup Parser
2115 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
2116 class PPNode_Hash_Attr implements PPNode {
2117 // @codingStandardsIgnoreEnd
2119 public $name, $value;
2120 private $store, $index;
2123 * Construct an object using the data from $store[$index]. The rest of the
2124 * store array can be accessed via getNextSibling().
2126 * @param array $store
2127 * @param integer $index
2129 public function __construct( array $store, $index ) {
2130 $descriptor = $store[$index];
2131 if ( $descriptor[PPNode_Hash_Tree::NAME][0] !== '@' ) {
2132 throw new MWException( __METHOD__.': invalid name in attribute descriptor' );
2134 $this->name = substr( $descriptor[PPNode_Hash_Tree::NAME], 1 );
2135 $this->value = $descriptor[PPNode_Hash_Tree::CHILDREN][0];
2136 $this->store = $store;
2137 $this->index = $index;
2140 public function __toString() {
2141 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
2144 public function getName() {
2145 return $this->name;
2148 public function getNextSibling() {
2149 return PPNode_Hash_Tree::factory( $this->store, $this->index + 1 );
2152 public function getChildren() {
2153 return false;
2156 public function getFirstChild() {
2157 return false;
2160 public function getChildrenOfType( $name ) {
2161 return false;
2164 public function getLength() {
2165 return false;
2168 public function item( $i ) {
2169 return false;
2172 public function splitArg() {
2173 throw new MWException( __METHOD__ . ': not supported' );
2176 public function splitExt() {
2177 throw new MWException( __METHOD__ . ': not supported' );
2180 public function splitHeading() {
2181 throw new MWException( __METHOD__ . ': not supported' );