DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / parser / Preprocessor_Hash.php
blob8a4637ea1e5f580053de92e8caaa98d750eaeac4
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 $tree = $this->cacheGetTree( $text, $flags );
121 if ( $tree !== false ) {
122 $store = json_decode( $tree );
123 if ( is_array( $store ) ) {
124 return new PPNode_Hash_Tree( $store, 0 );
128 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
130 $xmlishElements = $this->parser->getStripList();
131 $xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ];
132 $enableOnlyinclude = false;
133 if ( $forInclusion ) {
134 $ignoredTags = [ 'includeonly', '/includeonly' ];
135 $ignoredElements = [ 'noinclude' ];
136 $xmlishElements[] = 'noinclude';
137 if ( strpos( $text, '<onlyinclude>' ) !== false
138 && strpos( $text, '</onlyinclude>' ) !== false
140 $enableOnlyinclude = true;
142 } else {
143 $ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ];
144 $ignoredElements = [ 'includeonly' ];
145 $xmlishElements[] = 'includeonly';
147 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
149 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
150 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
152 $stack = new PPDStack_Hash;
154 $searchBase = "[{<\n";
155 // For fast reverse searches
156 $revText = strrev( $text );
157 $lengthText = strlen( $text );
159 // Input pointer, starts out pointing to a pseudo-newline before the start
160 $i = 0;
161 // Current accumulator. See the doc comment for Preprocessor_Hash for the format.
162 $accum =& $stack->getAccum();
163 // True to find equals signs in arguments
164 $findEquals = false;
165 // True to take notice of pipe characters
166 $findPipe = false;
167 $headingIndex = 1;
168 // True if $i is inside a possible heading
169 $inHeading = false;
170 // True if there are no more greater-than (>) signs right of $i
171 $noMoreGT = false;
172 // Map of tag name => true if there are no more closing tags of given type right of $i
173 $noMoreClosingTag = [];
174 // True to ignore all input up to the next <onlyinclude>
175 $findOnlyinclude = $enableOnlyinclude;
176 // Do a line-start run without outputting an LF character
177 $fakeLineStart = true;
179 while ( true ) {
180 // $this->memCheck();
182 if ( $findOnlyinclude ) {
183 // Ignore all input up to the next <onlyinclude>
184 $startPos = strpos( $text, '<onlyinclude>', $i );
185 if ( $startPos === false ) {
186 // Ignored section runs to the end
187 $accum[] = [ 'ignore', [ substr( $text, $i ) ] ];
188 break;
190 $tagEndPos = $startPos + strlen( '<onlyinclude>' ); // past-the-end
191 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
192 $i = $tagEndPos;
193 $findOnlyinclude = false;
196 if ( $fakeLineStart ) {
197 $found = 'line-start';
198 $curChar = '';
199 } else {
200 # Find next opening brace, closing brace or pipe
201 $search = $searchBase;
202 if ( $stack->top === false ) {
203 $currentClosing = '';
204 } else {
205 $currentClosing = $stack->top->close;
206 $search .= $currentClosing;
208 if ( $findPipe ) {
209 $search .= '|';
211 if ( $findEquals ) {
212 // First equals will be for the template
213 $search .= '=';
215 $rule = null;
216 # Output literal section, advance input counter
217 $literalLength = strcspn( $text, $search, $i );
218 if ( $literalLength > 0 ) {
219 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
220 $i += $literalLength;
222 if ( $i >= $lengthText ) {
223 if ( $currentClosing == "\n" ) {
224 // Do a past-the-end run to finish off the heading
225 $curChar = '';
226 $found = 'line-end';
227 } else {
228 # All done
229 break;
231 } else {
232 $curChar = $text[$i];
233 if ( $curChar == '|' ) {
234 $found = 'pipe';
235 } elseif ( $curChar == '=' ) {
236 $found = 'equals';
237 } elseif ( $curChar == '<' ) {
238 $found = 'angle';
239 } elseif ( $curChar == "\n" ) {
240 if ( $inHeading ) {
241 $found = 'line-end';
242 } else {
243 $found = 'line-start';
245 } elseif ( $curChar == $currentClosing ) {
246 $found = 'close';
247 } elseif ( isset( $this->rules[$curChar] ) ) {
248 $found = 'open';
249 $rule = $this->rules[$curChar];
250 } else {
251 # Some versions of PHP have a strcspn which stops on null characters
252 # Ignore and continue
253 ++$i;
254 continue;
259 if ( $found == 'angle' ) {
260 $matches = false;
261 // Handle </onlyinclude>
262 if ( $enableOnlyinclude
263 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
265 $findOnlyinclude = true;
266 continue;
269 // Determine element name
270 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
271 // Element name missing or not listed
272 self::addLiteral( $accum, '<' );
273 ++$i;
274 continue;
276 // Handle comments
277 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
279 // To avoid leaving blank lines, when a sequence of
280 // space-separated comments is both preceded and followed by
281 // a newline (ignoring spaces), then
282 // trim leading and trailing spaces and the trailing newline.
284 // Find the end
285 $endPos = strpos( $text, '-->', $i + 4 );
286 if ( $endPos === false ) {
287 // Unclosed comment in input, runs to end
288 $inner = substr( $text, $i );
289 $accum[] = [ 'comment', [ $inner ] ];
290 $i = $lengthText;
291 } else {
292 // Search backwards for leading whitespace
293 $wsStart = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
295 // Search forwards for trailing whitespace
296 // $wsEnd will be the position of the last space (or the '>' if there's none)
297 $wsEnd = $endPos + 2 + strspn( $text, " \t", $endPos + 3 );
299 // Keep looking forward as long as we're finding more
300 // comments.
301 $comments = [ [ $wsStart, $wsEnd ] ];
302 while ( substr( $text, $wsEnd + 1, 4 ) == '<!--' ) {
303 $c = strpos( $text, '-->', $wsEnd + 4 );
304 if ( $c === false ) {
305 break;
307 $c = $c + 2 + strspn( $text, " \t", $c + 3 );
308 $comments[] = [ $wsEnd + 1, $c ];
309 $wsEnd = $c;
312 // Eat the line if possible
313 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
314 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
315 // it's a possible beneficial b/c break.
316 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
317 && substr( $text, $wsEnd + 1, 1 ) == "\n"
319 // Remove leading whitespace from the end of the accumulator
320 $wsLength = $i - $wsStart;
321 $endIndex = count( $accum ) - 1;
323 // Sanity check
324 if ( $wsLength > 0
325 && $endIndex >= 0
326 && is_string( $accum[$endIndex] )
327 && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
329 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
332 // Dump all but the last comment to the accumulator
333 foreach ( $comments as $j => $com ) {
334 $startPos = $com[0];
335 $endPos = $com[1] + 1;
336 if ( $j == ( count( $comments ) - 1 ) ) {
337 break;
339 $inner = substr( $text, $startPos, $endPos - $startPos );
340 $accum[] = [ 'comment', [ $inner ] ];
343 // Do a line-start run next time to look for headings after the comment
344 $fakeLineStart = true;
345 } else {
346 // No line to eat, just take the comment itself
347 $startPos = $i;
348 $endPos += 2;
351 if ( $stack->top ) {
352 $part = $stack->top->getCurrentPart();
353 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
354 $part->visualEnd = $wsStart;
356 // Else comments abutting, no change in visual end
357 $part->commentEnd = $endPos;
359 $i = $endPos + 1;
360 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
361 $accum[] = [ 'comment', [ $inner ] ];
363 continue;
365 $name = $matches[1];
366 $lowerName = strtolower( $name );
367 $attrStart = $i + strlen( $name ) + 1;
369 // Find end of tag
370 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
371 if ( $tagEndPos === false ) {
372 // Infinite backtrack
373 // Disable tag search to prevent worst-case O(N^2) performance
374 $noMoreGT = true;
375 self::addLiteral( $accum, '<' );
376 ++$i;
377 continue;
380 // Handle ignored tags
381 if ( in_array( $lowerName, $ignoredTags ) ) {
382 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
383 $i = $tagEndPos + 1;
384 continue;
387 $tagStartPos = $i;
388 if ( $text[$tagEndPos - 1] == '/' ) {
389 // Short end tag
390 $attrEnd = $tagEndPos - 1;
391 $inner = null;
392 $i = $tagEndPos + 1;
393 $close = null;
394 } else {
395 $attrEnd = $tagEndPos;
396 // Find closing tag
397 if (
398 !isset( $noMoreClosingTag[$name] ) &&
399 preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
400 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
402 $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
403 $i = $matches[0][1] + strlen( $matches[0][0] );
404 $close = $matches[0][0];
405 } else {
406 // No end tag
407 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
408 // Let it run out to the end of the text.
409 $inner = substr( $text, $tagEndPos + 1 );
410 $i = $lengthText;
411 $close = null;
412 } else {
413 // Don't match the tag, treat opening tag as literal and resume parsing.
414 $i = $tagEndPos + 1;
415 self::addLiteral( $accum,
416 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
417 // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
418 $noMoreClosingTag[$name] = true;
419 continue;
423 // <includeonly> and <noinclude> just become <ignore> tags
424 if ( in_array( $lowerName, $ignoredElements ) ) {
425 $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
426 continue;
429 if ( $attrEnd <= $attrStart ) {
430 $attr = '';
431 } else {
432 // Note that the attr element contains the whitespace between name and attribute,
433 // this is necessary for precise reconstruction during pre-save transform.
434 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
437 $children = [
438 [ 'name', [ $name ] ],
439 [ 'attr', [ $attr ] ] ];
440 if ( $inner !== null ) {
441 $children[] = [ 'inner', [ $inner ] ];
443 if ( $close !== null ) {
444 $children[] = [ 'close', [ $close ] ];
446 $accum[] = [ 'ext', $children ];
447 } elseif ( $found == 'line-start' ) {
448 // Is this the start of a heading?
449 // Line break belongs before the heading element in any case
450 if ( $fakeLineStart ) {
451 $fakeLineStart = false;
452 } else {
453 self::addLiteral( $accum, $curChar );
454 $i++;
457 $count = strspn( $text, '=', $i, 6 );
458 if ( $count == 1 && $findEquals ) {
459 // DWIM: This looks kind of like a name/value separator.
460 // Let's let the equals handler have it and break the potential
461 // heading. This is heuristic, but AFAICT the methods for
462 // completely correct disambiguation are very complex.
463 } elseif ( $count > 0 ) {
464 $piece = [
465 'open' => "\n",
466 'close' => "\n",
467 'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ],
468 'startPos' => $i,
469 'count' => $count ];
470 $stack->push( $piece );
471 $accum =& $stack->getAccum();
472 extract( $stack->getFlags() );
473 $i += $count;
475 } elseif ( $found == 'line-end' ) {
476 $piece = $stack->top;
477 // A heading must be open, otherwise \n wouldn't have been in the search list
478 assert( $piece->open === "\n" );
479 $part = $piece->getCurrentPart();
480 // Search back through the input to see if it has a proper close.
481 // Do this using the reversed string since the other solutions
482 // (end anchor, etc.) are inefficient.
483 $wsLength = strspn( $revText, " \t", $lengthText - $i );
484 $searchStart = $i - $wsLength;
485 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
486 // Comment found at line end
487 // Search for equals signs before the comment
488 $searchStart = $part->visualEnd;
489 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
491 $count = $piece->count;
492 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
493 if ( $equalsLength > 0 ) {
494 if ( $searchStart - $equalsLength == $piece->startPos ) {
495 // This is just a single string of equals signs on its own line
496 // Replicate the doHeadings behavior /={count}(.+)={count}/
497 // First find out how many equals signs there really are (don't stop at 6)
498 $count = $equalsLength;
499 if ( $count < 3 ) {
500 $count = 0;
501 } else {
502 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
504 } else {
505 $count = min( $equalsLength, $count );
507 if ( $count > 0 ) {
508 // Normal match, output <h>
509 $element = [ [ 'possible-h',
510 array_merge(
512 [ '@level', [ $count ] ],
513 [ '@i', [ $headingIndex++ ] ]
515 $accum
517 ] ];
518 } else {
519 // Single equals sign on its own line, count=0
520 $element = $accum;
522 } else {
523 // No match, no <h>, just pass down the inner text
524 $element = $accum;
526 // Unwind the stack
527 $stack->pop();
528 $accum =& $stack->getAccum();
529 extract( $stack->getFlags() );
531 // Append the result to the enclosing accumulator
532 array_splice( $accum, count( $accum ), 0, $element );
534 // Note that we do NOT increment the input pointer.
535 // This is because the closing linebreak could be the opening linebreak of
536 // another heading. Infinite loops are avoided because the next iteration MUST
537 // hit the heading open case above, which unconditionally increments the
538 // input pointer.
539 } elseif ( $found == 'open' ) {
540 # count opening brace characters
541 $count = strspn( $text, $curChar, $i );
543 # we need to add to stack only if opening brace count is enough for one of the rules
544 if ( $count >= $rule['min'] ) {
545 # Add it to the stack
546 $piece = [
547 'open' => $curChar,
548 'close' => $rule['end'],
549 'count' => $count,
550 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
553 $stack->push( $piece );
554 $accum =& $stack->getAccum();
555 extract( $stack->getFlags() );
556 } else {
557 # Add literal brace(s)
558 self::addLiteral( $accum, str_repeat( $curChar, $count ) );
560 $i += $count;
561 } elseif ( $found == 'close' ) {
562 $piece = $stack->top;
563 # lets check if there are enough characters for closing brace
564 $maxCount = $piece->count;
565 $count = strspn( $text, $curChar, $i, $maxCount );
567 # check for maximum matching characters (if there are 5 closing
568 # characters, we will probably need only 3 - depending on the rules)
569 $rule = $this->rules[$piece->open];
570 if ( $count > $rule['max'] ) {
571 # The specified maximum exists in the callback array, unless the caller
572 # has made an error
573 $matchingCount = $rule['max'];
574 } else {
575 # Count is less than the maximum
576 # Skip any gaps in the callback array to find the true largest match
577 # Need to use array_key_exists not isset because the callback can be null
578 $matchingCount = $count;
579 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
580 --$matchingCount;
584 if ( $matchingCount <= 0 ) {
585 # No matching element found in callback array
586 # Output a literal closing brace and continue
587 self::addLiteral( $accum, str_repeat( $curChar, $count ) );
588 $i += $count;
589 continue;
591 $name = $rule['names'][$matchingCount];
592 if ( $name === null ) {
593 // No element, just literal text
594 $element = $piece->breakSyntax( $matchingCount );
595 self::addLiteral( $element, str_repeat( $rule['end'], $matchingCount ) );
596 } else {
597 # Create XML element
598 $parts = $piece->parts;
599 $titleAccum = $parts[0]->out;
600 unset( $parts[0] );
602 $children = [];
604 # The invocation is at the start of the line if lineStart is set in
605 # the stack, and all opening brackets are used up.
606 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
607 $children[] = [ '@lineStart', [ 1 ] ];
609 $titleNode = [ 'title', $titleAccum ];
610 $children[] = $titleNode;
611 $argIndex = 1;
612 foreach ( $parts as $part ) {
613 if ( isset( $part->eqpos ) ) {
614 $equalsNode = $part->out[$part->eqpos];
615 $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
616 $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
617 $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
618 $children[] = $partNode;
619 } else {
620 $nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ];
621 $valueNode = [ 'value', $part->out ];
622 $partNode = [ 'part', [ $nameNode, $valueNode ] ];
623 $children[] = $partNode;
626 $element = [ [ $name, $children ] ];
629 # Advance input pointer
630 $i += $matchingCount;
632 # Unwind the stack
633 $stack->pop();
634 $accum =& $stack->getAccum();
636 # Re-add the old stack element if it still has unmatched opening characters remaining
637 if ( $matchingCount < $piece->count ) {
638 $piece->parts = [ new PPDPart_Hash ];
639 $piece->count -= $matchingCount;
640 # do we still qualify for any callback with remaining count?
641 $min = $this->rules[$piece->open]['min'];
642 if ( $piece->count >= $min ) {
643 $stack->push( $piece );
644 $accum =& $stack->getAccum();
645 } else {
646 self::addLiteral( $accum, str_repeat( $piece->open, $piece->count ) );
650 extract( $stack->getFlags() );
652 # Add XML element to the enclosing accumulator
653 array_splice( $accum, count( $accum ), 0, $element );
654 } elseif ( $found == 'pipe' ) {
655 $findEquals = true; // shortcut for getFlags()
656 $stack->addPart();
657 $accum =& $stack->getAccum();
658 ++$i;
659 } elseif ( $found == 'equals' ) {
660 $findEquals = false; // shortcut for getFlags()
661 $accum[] = [ 'equals', [ '=' ] ];
662 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
663 ++$i;
667 # Output any remaining unclosed brackets
668 foreach ( $stack->stack as $piece ) {
669 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
672 # Enable top-level headings
673 foreach ( $stack->rootAccum as &$node ) {
674 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
675 $node[PPNode_Hash_Tree::NAME] = 'h';
679 $rootStore = [ [ 'root', $stack->rootAccum ] ];
680 $rootNode = new PPNode_Hash_Tree( $rootStore, 0 );
682 // Cache
683 $tree = json_encode( $rootStore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
684 if ( $tree !== false ) {
685 $this->cacheSetTree( $text, $flags, $tree );
688 return $rootNode;
691 private static function addLiteral( array &$accum, $text ) {
692 $n = count( $accum );
693 if ( $n && is_string( $accum[$n - 1] ) ) {
694 $accum[$n - 1] .= $text;
695 } else {
696 $accum[] = $text;
702 * Stack class to help Preprocessor::preprocessToObj()
703 * @ingroup Parser
705 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
706 class PPDStack_Hash extends PPDStack {
707 // @codingStandardsIgnoreEnd
709 public function __construct() {
710 $this->elementClass = 'PPDStackElement_Hash';
711 parent::__construct();
712 $this->rootAccum = [];
717 * @ingroup Parser
719 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
720 class PPDStackElement_Hash extends PPDStackElement {
721 // @codingStandardsIgnoreEnd
723 public function __construct( $data = [] ) {
724 $this->partClass = 'PPDPart_Hash';
725 parent::__construct( $data );
729 * Get the accumulator that would result if the close is not found.
731 * @param int|bool $openingCount
732 * @return array
734 public function breakSyntax( $openingCount = false ) {
735 if ( $this->open == "\n" ) {
736 $accum = $this->parts[0]->out;
737 } else {
738 if ( $openingCount === false ) {
739 $openingCount = $this->count;
741 $accum = [ str_repeat( $this->open, $openingCount ) ];
742 $lastIndex = 0;
743 $first = true;
744 foreach ( $this->parts as $part ) {
745 if ( $first ) {
746 $first = false;
747 } elseif ( is_string( $accum[$lastIndex] ) ) {
748 $accum[$lastIndex] .= '|';
749 } else {
750 $accum[++$lastIndex] = '|';
752 foreach ( $part->out as $node ) {
753 if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) {
754 $accum[$lastIndex] .= $node;
755 } else {
756 $accum[++$lastIndex] = $node;
761 return $accum;
766 * @ingroup Parser
768 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
769 class PPDPart_Hash extends PPDPart {
770 // @codingStandardsIgnoreEnd
772 public function __construct( $out = '' ) {
773 if ( $out !== '' ) {
774 $accum = [ $out ];
775 } else {
776 $accum = [];
778 parent::__construct( $accum );
783 * An expansion frame, used as a context to expand the result of preprocessToObj()
784 * @ingroup Parser
786 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
787 class PPFrame_Hash implements PPFrame {
788 // @codingStandardsIgnoreEnd
791 * @var Parser
793 public $parser;
796 * @var Preprocessor
798 public $preprocessor;
801 * @var Title
803 public $title;
804 public $titleCache;
807 * Hashtable listing templates which are disallowed for expansion in this frame,
808 * having been encountered previously in parent frames.
810 public $loopCheckHash;
813 * Recursion depth of this frame, top = 0
814 * Note that this is NOT the same as expansion depth in expand()
816 public $depth;
818 private $volatile = false;
819 private $ttl = null;
822 * @var array
824 protected $childExpansionCache;
827 * Construct a new preprocessor frame.
828 * @param Preprocessor $preprocessor The parent preprocessor
830 public function __construct( $preprocessor ) {
831 $this->preprocessor = $preprocessor;
832 $this->parser = $preprocessor->parser;
833 $this->title = $this->parser->mTitle;
834 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
835 $this->loopCheckHash = [];
836 $this->depth = 0;
837 $this->childExpansionCache = [];
841 * Create a new child frame
842 * $args is optionally a multi-root PPNode or array containing the template arguments
844 * @param array|bool|PPNode_Hash_Array $args
845 * @param Title|bool $title
846 * @param int $indexOffset
847 * @throws MWException
848 * @return PPTemplateFrame_Hash
850 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
851 $namedArgs = [];
852 $numberedArgs = [];
853 if ( $title === false ) {
854 $title = $this->title;
856 if ( $args !== false ) {
857 if ( $args instanceof PPNode_Hash_Array ) {
858 $args = $args->value;
859 } elseif ( !is_array( $args ) ) {
860 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
862 foreach ( $args as $arg ) {
863 $bits = $arg->splitArg();
864 if ( $bits['index'] !== '' ) {
865 // Numbered parameter
866 $index = $bits['index'] - $indexOffset;
867 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
868 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
869 wfEscapeWikiText( $this->title ),
870 wfEscapeWikiText( $title ),
871 wfEscapeWikiText( $index ) )->text() );
872 $this->parser->addTrackingCategory( 'duplicate-args-category' );
874 $numberedArgs[$index] = $bits['value'];
875 unset( $namedArgs[$index] );
876 } else {
877 // Named parameter
878 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
879 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
880 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
881 wfEscapeWikiText( $this->title ),
882 wfEscapeWikiText( $title ),
883 wfEscapeWikiText( $name ) )->text() );
884 $this->parser->addTrackingCategory( 'duplicate-args-category' );
886 $namedArgs[$name] = $bits['value'];
887 unset( $numberedArgs[$name] );
891 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
895 * @throws MWException
896 * @param string|int $key
897 * @param string|PPNode $root
898 * @param int $flags
899 * @return string
901 public function cachedExpand( $key, $root, $flags = 0 ) {
902 // we don't have a parent, so we don't have a cache
903 return $this->expand( $root, $flags );
907 * @throws MWException
908 * @param string|PPNode $root
909 * @param int $flags
910 * @return string
912 public function expand( $root, $flags = 0 ) {
913 static $expansionDepth = 0;
914 if ( is_string( $root ) ) {
915 return $root;
918 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
919 $this->parser->limitationWarn( 'node-count-exceeded',
920 $this->parser->mPPNodeCount,
921 $this->parser->mOptions->getMaxPPNodeCount()
923 return '<span class="error">Node-count limit exceeded</span>';
925 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
926 $this->parser->limitationWarn( 'expansion-depth-exceeded',
927 $expansionDepth,
928 $this->parser->mOptions->getMaxPPExpandDepth()
930 return '<span class="error">Expansion depth limit exceeded</span>';
932 ++$expansionDepth;
933 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
934 $this->parser->mHighestExpansionDepth = $expansionDepth;
937 $outStack = [ '', '' ];
938 $iteratorStack = [ false, $root ];
939 $indexStack = [ 0, 0 ];
941 while ( count( $iteratorStack ) > 1 ) {
942 $level = count( $outStack ) - 1;
943 $iteratorNode =& $iteratorStack[$level];
944 $out =& $outStack[$level];
945 $index =& $indexStack[$level];
947 if ( is_array( $iteratorNode ) ) {
948 if ( $index >= count( $iteratorNode ) ) {
949 // All done with this iterator
950 $iteratorStack[$level] = false;
951 $contextNode = false;
952 } else {
953 $contextNode = $iteratorNode[$index];
954 $index++;
956 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
957 if ( $index >= $iteratorNode->getLength() ) {
958 // All done with this iterator
959 $iteratorStack[$level] = false;
960 $contextNode = false;
961 } else {
962 $contextNode = $iteratorNode->item( $index );
963 $index++;
965 } else {
966 // Copy to $contextNode and then delete from iterator stack,
967 // because this is not an iterator but we do have to execute it once
968 $contextNode = $iteratorStack[$level];
969 $iteratorStack[$level] = false;
972 $newIterator = false;
973 $contextName = false;
974 $contextChildren = false;
976 if ( $contextNode === false ) {
977 // nothing to do
978 } elseif ( is_string( $contextNode ) ) {
979 $out .= $contextNode;
980 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
981 $newIterator = $contextNode;
982 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
983 // No output
984 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
985 $out .= $contextNode->value;
986 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
987 $contextName = $contextNode->name;
988 $contextChildren = $contextNode->getRawChildren();
989 } elseif ( is_array( $contextNode ) ) {
990 // Node descriptor array
991 if ( count( $contextNode ) !== 2 ) {
992 throw new MWException( __METHOD__.
993 ': found an array where a node descriptor should be' );
995 list( $contextName, $contextChildren ) = $contextNode;
996 } else {
997 throw new MWException( __METHOD__ . ': Invalid parameter type' );
1000 // Handle node descriptor array or tree object
1001 if ( $contextName === false ) {
1002 // Not a node, already handled above
1003 } elseif ( $contextName[0] === '@' ) {
1004 // Attribute: no output
1005 } elseif ( $contextName === 'template' ) {
1006 # Double-brace expansion
1007 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1008 if ( $flags & PPFrame::NO_TEMPLATES ) {
1009 $newIterator = $this->virtualBracketedImplode(
1010 '{{', '|', '}}',
1011 $bits['title'],
1012 $bits['parts']
1014 } else {
1015 $ret = $this->parser->braceSubstitution( $bits, $this );
1016 if ( isset( $ret['object'] ) ) {
1017 $newIterator = $ret['object'];
1018 } else {
1019 $out .= $ret['text'];
1022 } elseif ( $contextName === 'tplarg' ) {
1023 # Triple-brace expansion
1024 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1025 if ( $flags & PPFrame::NO_ARGS ) {
1026 $newIterator = $this->virtualBracketedImplode(
1027 '{{{', '|', '}}}',
1028 $bits['title'],
1029 $bits['parts']
1031 } else {
1032 $ret = $this->parser->argSubstitution( $bits, $this );
1033 if ( isset( $ret['object'] ) ) {
1034 $newIterator = $ret['object'];
1035 } else {
1036 $out .= $ret['text'];
1039 } elseif ( $contextName === 'comment' ) {
1040 # HTML-style comment
1041 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1042 # Not in RECOVER_COMMENTS mode (msgnw) though.
1043 if ( ( $this->parser->ot['html']
1044 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
1045 || ( $flags & PPFrame::STRIP_COMMENTS )
1046 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
1048 $out .= '';
1049 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1050 # Add a strip marker in PST mode so that pstPass2() can
1051 # run some old-fashioned regexes on the result.
1052 # Not in RECOVER_COMMENTS mode (extractSections) though.
1053 $out .= $this->parser->insertStripItem( $contextChildren[0] );
1054 } else {
1055 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1056 $out .= $contextChildren[0];
1058 } elseif ( $contextName === 'ignore' ) {
1059 # Output suppression used by <includeonly> etc.
1060 # OT_WIKI will only respect <ignore> in substed templates.
1061 # The other output types respect it unless NO_IGNORE is set.
1062 # extractSections() sets NO_IGNORE and so never respects it.
1063 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
1064 || ( $flags & PPFrame::NO_IGNORE )
1066 $out .= $contextChildren[0];
1067 } else {
1068 // $out .= '';
1070 } elseif ( $contextName === 'ext' ) {
1071 # Extension tag
1072 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
1073 [ 'attr' => null, 'inner' => null, 'close' => null ];
1074 if ( $flags & PPFrame::NO_TAGS ) {
1075 $s = '<' . $bits['name']->getFirstChild()->value;
1076 if ( $bits['attr'] ) {
1077 $s .= $bits['attr']->getFirstChild()->value;
1079 if ( $bits['inner'] ) {
1080 $s .= '>' . $bits['inner']->getFirstChild()->value;
1081 if ( $bits['close'] ) {
1082 $s .= $bits['close']->getFirstChild()->value;
1084 } else {
1085 $s .= '/>';
1087 $out .= $s;
1088 } else {
1089 $out .= $this->parser->extensionSubstitution( $bits, $this );
1091 } elseif ( $contextName === 'h' ) {
1092 # Heading
1093 if ( $this->parser->ot['html'] ) {
1094 # Expand immediately and insert heading index marker
1095 $s = $this->expand( $contextChildren, $flags );
1096 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
1097 $titleText = $this->title->getPrefixedDBkey();
1098 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
1099 $serial = count( $this->parser->mHeadings ) - 1;
1100 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
1101 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1102 $this->parser->mStripState->addGeneral( $marker, '' );
1103 $out .= $s;
1104 } else {
1105 # Expand in virtual stack
1106 $newIterator = $contextChildren;
1108 } else {
1109 # Generic recursive expansion
1110 $newIterator = $contextChildren;
1113 if ( $newIterator !== false ) {
1114 $outStack[] = '';
1115 $iteratorStack[] = $newIterator;
1116 $indexStack[] = 0;
1117 } elseif ( $iteratorStack[$level] === false ) {
1118 // Return accumulated value to parent
1119 // With tail recursion
1120 while ( $iteratorStack[$level] === false && $level > 0 ) {
1121 $outStack[$level - 1] .= $out;
1122 array_pop( $outStack );
1123 array_pop( $iteratorStack );
1124 array_pop( $indexStack );
1125 $level--;
1129 --$expansionDepth;
1130 return $outStack[0];
1134 * @param string $sep
1135 * @param int $flags
1136 * @param string|PPNode $args,...
1137 * @return string
1139 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1140 $args = array_slice( func_get_args(), 2 );
1142 $first = true;
1143 $s = '';
1144 foreach ( $args as $root ) {
1145 if ( $root instanceof PPNode_Hash_Array ) {
1146 $root = $root->value;
1148 if ( !is_array( $root ) ) {
1149 $root = [ $root ];
1151 foreach ( $root as $node ) {
1152 if ( $first ) {
1153 $first = false;
1154 } else {
1155 $s .= $sep;
1157 $s .= $this->expand( $node, $flags );
1160 return $s;
1164 * Implode with no flags specified
1165 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1166 * @param string $sep
1167 * @param string|PPNode $args,...
1168 * @return string
1170 public function implode( $sep /*, ... */ ) {
1171 $args = array_slice( func_get_args(), 1 );
1173 $first = true;
1174 $s = '';
1175 foreach ( $args as $root ) {
1176 if ( $root instanceof PPNode_Hash_Array ) {
1177 $root = $root->value;
1179 if ( !is_array( $root ) ) {
1180 $root = [ $root ];
1182 foreach ( $root as $node ) {
1183 if ( $first ) {
1184 $first = false;
1185 } else {
1186 $s .= $sep;
1188 $s .= $this->expand( $node );
1191 return $s;
1195 * Makes an object that, when expand()ed, will be the same as one obtained
1196 * with implode()
1198 * @param string $sep
1199 * @param string|PPNode $args,...
1200 * @return PPNode_Hash_Array
1202 public function virtualImplode( $sep /*, ... */ ) {
1203 $args = array_slice( func_get_args(), 1 );
1204 $out = [];
1205 $first = true;
1207 foreach ( $args as $root ) {
1208 if ( $root instanceof PPNode_Hash_Array ) {
1209 $root = $root->value;
1211 if ( !is_array( $root ) ) {
1212 $root = [ $root ];
1214 foreach ( $root as $node ) {
1215 if ( $first ) {
1216 $first = false;
1217 } else {
1218 $out[] = $sep;
1220 $out[] = $node;
1223 return new PPNode_Hash_Array( $out );
1227 * Virtual implode with brackets
1229 * @param string $start
1230 * @param string $sep
1231 * @param string $end
1232 * @param string|PPNode $args,...
1233 * @return PPNode_Hash_Array
1235 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1236 $args = array_slice( func_get_args(), 3 );
1237 $out = [ $start ];
1238 $first = true;
1240 foreach ( $args as $root ) {
1241 if ( $root instanceof PPNode_Hash_Array ) {
1242 $root = $root->value;
1244 if ( !is_array( $root ) ) {
1245 $root = [ $root ];
1247 foreach ( $root as $node ) {
1248 if ( $first ) {
1249 $first = false;
1250 } else {
1251 $out[] = $sep;
1253 $out[] = $node;
1256 $out[] = $end;
1257 return new PPNode_Hash_Array( $out );
1260 public function __toString() {
1261 return 'frame{}';
1265 * @param bool $level
1266 * @return array|bool|string
1268 public function getPDBK( $level = false ) {
1269 if ( $level === false ) {
1270 return $this->title->getPrefixedDBkey();
1271 } else {
1272 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1277 * @return array
1279 public function getArguments() {
1280 return [];
1284 * @return array
1286 public function getNumberedArguments() {
1287 return [];
1291 * @return array
1293 public function getNamedArguments() {
1294 return [];
1298 * Returns true if there are no arguments in this frame
1300 * @return bool
1302 public function isEmpty() {
1303 return true;
1307 * @param int|string $name
1308 * @return bool Always false in this implementation.
1310 public function getArgument( $name ) {
1311 return false;
1315 * Returns true if the infinite loop check is OK, false if a loop is detected
1317 * @param Title $title
1319 * @return bool
1321 public function loopCheck( $title ) {
1322 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1326 * Return true if the frame is a template frame
1328 * @return bool
1330 public function isTemplate() {
1331 return false;
1335 * Get a title of frame
1337 * @return Title
1339 public function getTitle() {
1340 return $this->title;
1344 * Set the volatile flag
1346 * @param bool $flag
1348 public function setVolatile( $flag = true ) {
1349 $this->volatile = $flag;
1353 * Get the volatile flag
1355 * @return bool
1357 public function isVolatile() {
1358 return $this->volatile;
1362 * Set the TTL
1364 * @param int $ttl
1366 public function setTTL( $ttl ) {
1367 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
1368 $this->ttl = $ttl;
1373 * Get the TTL
1375 * @return int|null
1377 public function getTTL() {
1378 return $this->ttl;
1383 * Expansion frame with template arguments
1384 * @ingroup Parser
1386 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1387 class PPTemplateFrame_Hash extends PPFrame_Hash {
1388 // @codingStandardsIgnoreEnd
1390 public $numberedArgs, $namedArgs, $parent;
1391 public $numberedExpansionCache, $namedExpansionCache;
1394 * @param Preprocessor $preprocessor
1395 * @param bool|PPFrame $parent
1396 * @param array $numberedArgs
1397 * @param array $namedArgs
1398 * @param bool|Title $title
1400 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
1401 $namedArgs = [], $title = false
1403 parent::__construct( $preprocessor );
1405 $this->parent = $parent;
1406 $this->numberedArgs = $numberedArgs;
1407 $this->namedArgs = $namedArgs;
1408 $this->title = $title;
1409 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1410 $this->titleCache = $parent->titleCache;
1411 $this->titleCache[] = $pdbk;
1412 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1413 if ( $pdbk !== false ) {
1414 $this->loopCheckHash[$pdbk] = true;
1416 $this->depth = $parent->depth + 1;
1417 $this->numberedExpansionCache = $this->namedExpansionCache = [];
1420 public function __toString() {
1421 $s = 'tplframe{';
1422 $first = true;
1423 $args = $this->numberedArgs + $this->namedArgs;
1424 foreach ( $args as $name => $value ) {
1425 if ( $first ) {
1426 $first = false;
1427 } else {
1428 $s .= ', ';
1430 $s .= "\"$name\":\"" .
1431 str_replace( '"', '\\"', $value->__toString() ) . '"';
1433 $s .= '}';
1434 return $s;
1438 * @throws MWException
1439 * @param string|int $key
1440 * @param string|PPNode $root
1441 * @param int $flags
1442 * @return string
1444 public function cachedExpand( $key, $root, $flags = 0 ) {
1445 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
1446 return $this->parent->childExpansionCache[$key];
1448 $retval = $this->expand( $root, $flags );
1449 if ( !$this->isVolatile() ) {
1450 $this->parent->childExpansionCache[$key] = $retval;
1452 return $retval;
1456 * Returns true if there are no arguments in this frame
1458 * @return bool
1460 public function isEmpty() {
1461 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1465 * @return array
1467 public function getArguments() {
1468 $arguments = [];
1469 foreach ( array_merge(
1470 array_keys( $this->numberedArgs ),
1471 array_keys( $this->namedArgs ) ) as $key ) {
1472 $arguments[$key] = $this->getArgument( $key );
1474 return $arguments;
1478 * @return array
1480 public function getNumberedArguments() {
1481 $arguments = [];
1482 foreach ( array_keys( $this->numberedArgs ) as $key ) {
1483 $arguments[$key] = $this->getArgument( $key );
1485 return $arguments;
1489 * @return array
1491 public function getNamedArguments() {
1492 $arguments = [];
1493 foreach ( array_keys( $this->namedArgs ) as $key ) {
1494 $arguments[$key] = $this->getArgument( $key );
1496 return $arguments;
1500 * @param int $index
1501 * @return string|bool
1503 public function getNumberedArgument( $index ) {
1504 if ( !isset( $this->numberedArgs[$index] ) ) {
1505 return false;
1507 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1508 # No trimming for unnamed arguments
1509 $this->numberedExpansionCache[$index] = $this->parent->expand(
1510 $this->numberedArgs[$index],
1511 PPFrame::STRIP_COMMENTS
1514 return $this->numberedExpansionCache[$index];
1518 * @param string $name
1519 * @return string|bool
1521 public function getNamedArgument( $name ) {
1522 if ( !isset( $this->namedArgs[$name] ) ) {
1523 return false;
1525 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1526 # Trim named arguments post-expand, for backwards compatibility
1527 $this->namedExpansionCache[$name] = trim(
1528 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1530 return $this->namedExpansionCache[$name];
1534 * @param int|string $name
1535 * @return string|bool
1537 public function getArgument( $name ) {
1538 $text = $this->getNumberedArgument( $name );
1539 if ( $text === false ) {
1540 $text = $this->getNamedArgument( $name );
1542 return $text;
1546 * Return true if the frame is a template frame
1548 * @return bool
1550 public function isTemplate() {
1551 return true;
1554 public function setVolatile( $flag = true ) {
1555 parent::setVolatile( $flag );
1556 $this->parent->setVolatile( $flag );
1559 public function setTTL( $ttl ) {
1560 parent::setTTL( $ttl );
1561 $this->parent->setTTL( $ttl );
1566 * Expansion frame with custom arguments
1567 * @ingroup Parser
1569 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1570 class PPCustomFrame_Hash extends PPFrame_Hash {
1571 // @codingStandardsIgnoreEnd
1573 public $args;
1575 public function __construct( $preprocessor, $args ) {
1576 parent::__construct( $preprocessor );
1577 $this->args = $args;
1580 public function __toString() {
1581 $s = 'cstmframe{';
1582 $first = true;
1583 foreach ( $this->args as $name => $value ) {
1584 if ( $first ) {
1585 $first = false;
1586 } else {
1587 $s .= ', ';
1589 $s .= "\"$name\":\"" .
1590 str_replace( '"', '\\"', $value->__toString() ) . '"';
1592 $s .= '}';
1593 return $s;
1597 * @return bool
1599 public function isEmpty() {
1600 return !count( $this->args );
1604 * @param int|string $index
1605 * @return string|bool
1607 public function getArgument( $index ) {
1608 if ( !isset( $this->args[$index] ) ) {
1609 return false;
1611 return $this->args[$index];
1614 public function getArguments() {
1615 return $this->args;
1620 * @ingroup Parser
1622 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1623 class PPNode_Hash_Tree implements PPNode {
1624 // @codingStandardsIgnoreEnd
1626 public $name;
1629 * The store array for children of this node. It is "raw" in the sense that
1630 * nodes are two-element arrays ("descriptors") rather than PPNode_Hash_*
1631 * objects.
1633 private $rawChildren;
1636 * The store array for the siblings of this node, including this node itself.
1638 private $store;
1641 * The index into $this->store which contains the descriptor of this node.
1643 private $index;
1646 * The offset of the name within descriptors, used in some places for
1647 * readability.
1649 const NAME = 0;
1652 * The offset of the child list within descriptors, used in some places for
1653 * readability.
1655 const CHILDREN = 1;
1658 * Construct an object using the data from $store[$index]. The rest of the
1659 * store array can be accessed via getNextSibling().
1661 * @param array $store
1662 * @param integer $index
1664 public function __construct( array $store, $index ) {
1665 $this->store = $store;
1666 $this->index = $index;
1667 list( $this->name, $this->rawChildren ) = $this->store[$index];
1671 * Construct an appropriate PPNode_Hash_* object with a class that depends
1672 * on what is at the relevant store index.
1674 * @param array $store
1675 * @param integer $index
1676 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text
1678 public static function factory( array $store, $index ) {
1679 if ( !isset( $store[$index] ) ) {
1680 return false;
1683 $descriptor = $store[$index];
1684 if ( is_string( $descriptor ) ) {
1685 $class = 'PPNode_Hash_Text';
1686 } elseif ( is_array( $descriptor ) ) {
1687 if ( $descriptor[self::NAME][0] === '@' ) {
1688 $class = 'PPNode_Hash_Attr';
1689 } else {
1690 $class = 'PPNode_Hash_Tree';
1692 } else {
1693 throw new MWException( __METHOD__.': invalid node descriptor' );
1695 return new $class( $store, $index );
1699 * Convert a node to XML, for debugging
1701 public function __toString() {
1702 $inner = '';
1703 $attribs = '';
1704 for ( $node = $this->getFirstChild(); $node; $node = $node->getNextSibling() ) {
1705 if ( $node instanceof PPNode_Hash_Attr ) {
1706 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1707 } else {
1708 $inner .= $node->__toString();
1711 if ( $inner === '' ) {
1712 return "<{$this->name}$attribs/>";
1713 } else {
1714 return "<{$this->name}$attribs>$inner</{$this->name}>";
1719 * @return PPNode_Hash_Array
1721 public function getChildren() {
1722 $children = [];
1723 foreach ( $this->rawChildren as $i => $child ) {
1724 $children[] = self::factory( $this->rawChildren, $i );
1726 return new PPNode_Hash_Array( $children );
1730 * Get the first child, or false if there is none. Note that this will
1731 * return a temporary proxy object: different instances will be returned
1732 * if this is called more than once on the same node.
1734 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|boolean
1736 public function getFirstChild() {
1737 if ( !isset( $this->rawChildren[0] ) ) {
1738 return false;
1739 } else {
1740 return self::factory( $this->rawChildren, 0 );
1745 * Get the next sibling, or false if there is none. Note that this will
1746 * return a temporary proxy object: different instances will be returned
1747 * if this is called more than once on the same node.
1749 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|boolean
1751 public function getNextSibling() {
1752 return self::factory( $this->store, $this->index + 1 );
1756 * Get an array of the children with a given node name
1758 * @param string $name
1759 * @return PPNode_Hash_Array
1761 public function getChildrenOfType( $name ) {
1762 $children = [];
1763 foreach ( $this->rawChildren as $i => $child ) {
1764 if ( is_array( $child ) && $child[self::NAME] === $name ) {
1765 $children[] = self::factory( $this->rawChildren, $i );
1768 return new PPNode_Hash_Array( $children );
1772 * Get the raw child array. For internal use.
1773 * @return array
1775 public function getRawChildren() {
1776 return $this->rawChildren;
1780 * @return bool
1782 public function getLength() {
1783 return false;
1787 * @param int $i
1788 * @return bool
1790 public function item( $i ) {
1791 return false;
1795 * @return string
1797 public function getName() {
1798 return $this->name;
1802 * Split a "<part>" node into an associative array containing:
1803 * - name PPNode name
1804 * - index String index
1805 * - value PPNode value
1807 * @throws MWException
1808 * @return array
1810 public function splitArg() {
1811 return self::splitRawArg( $this->rawChildren );
1815 * Like splitArg() but for a raw child array. For internal use only.
1817 public static function splitRawArg( array $children ) {
1818 $bits = [];
1819 foreach ( $children as $i => $child ) {
1820 if ( !is_array( $child ) ) {
1821 continue;
1823 if ( $child[self::NAME] === 'name' ) {
1824 $bits['name'] = new self( $children, $i );
1825 if ( isset( $child[self::CHILDREN][0][self::NAME] )
1826 && $child[self::CHILDREN][0][self::NAME] === '@index'
1828 $bits['index'] = $child[self::CHILDREN][0][self::CHILDREN][0];
1830 } elseif ( $child[self::NAME] === 'value' ) {
1831 $bits['value'] = new self( $children, $i );
1835 if ( !isset( $bits['name'] ) ) {
1836 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1838 if ( !isset( $bits['index'] ) ) {
1839 $bits['index'] = '';
1841 return $bits;
1845 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1846 * All values in the resulting array are PPNodes. Inner and close are optional.
1848 * @throws MWException
1849 * @return array
1851 public function splitExt() {
1852 return self::splitRawExt( $this->rawChildren );
1856 * Like splitExt() but for a raw child array. For internal use only.
1858 public static function splitRawExt( array $children ) {
1859 $bits = [];
1860 foreach ( $children as $i => $child ) {
1861 if ( !is_array( $child ) ) {
1862 continue;
1864 switch ( $child[self::NAME] ) {
1865 case 'name':
1866 $bits['name'] = new self( $children, $i );
1867 break;
1868 case 'attr':
1869 $bits['attr'] = new self( $children, $i );
1870 break;
1871 case 'inner':
1872 $bits['inner'] = new self( $children, $i );
1873 break;
1874 case 'close':
1875 $bits['close'] = new self( $children, $i );
1876 break;
1879 if ( !isset( $bits['name'] ) ) {
1880 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1882 return $bits;
1886 * Split an "<h>" node
1888 * @throws MWException
1889 * @return array
1891 public function splitHeading() {
1892 if ( $this->name !== 'h' ) {
1893 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1895 return self::splitRawHeading( $this->rawChildren );
1899 * Like splitHeading() but for a raw child array. For internal use only.
1901 public static function splitRawHeading( array $children ) {
1902 $bits = [];
1903 foreach ( $children as $i => $child ) {
1904 if ( !is_array( $child ) ) {
1905 continue;
1907 if ( $child[self::NAME] === '@i' ) {
1908 $bits['i'] = $child[self::CHILDREN][0];
1909 } elseif ( $child[self::NAME] === '@level' ) {
1910 $bits['level'] = $child[self::CHILDREN][0];
1913 if ( !isset( $bits['i'] ) ) {
1914 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1916 return $bits;
1920 * Split a "<template>" or "<tplarg>" node
1922 * @throws MWException
1923 * @return array
1925 public function splitTemplate() {
1926 return self::splitRawTemplate( $this->rawChildren );
1930 * Like splitTemplate() but for a raw child array. For internal use only.
1932 public static function splitRawTemplate( array $children ) {
1933 $parts = [];
1934 $bits = [ 'lineStart' => '' ];
1935 foreach ( $children as $i => $child ) {
1936 if ( !is_array( $child ) ) {
1937 continue;
1939 switch ( $child[self::NAME] ) {
1940 case 'title':
1941 $bits['title'] = new self( $children, $i );
1942 break;
1943 case 'part':
1944 $parts[] = new self( $children, $i );
1945 break;
1946 case '@lineStart':
1947 $bits['lineStart'] = '1';
1948 break;
1951 if ( !isset( $bits['title'] ) ) {
1952 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
1954 $bits['parts'] = new PPNode_Hash_Array( $parts );
1955 return $bits;
1960 * @ingroup Parser
1962 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1963 class PPNode_Hash_Text implements PPNode {
1964 // @codingStandardsIgnoreEnd
1966 public $value;
1967 private $store, $index;
1970 * Construct an object using the data from $store[$index]. The rest of the
1971 * store array can be accessed via getNextSibling().
1973 * @param array $store
1974 * @param integer $index
1976 public function __construct( array $store, $index ) {
1977 $this->value = $store[$index];
1978 if ( !is_scalar( $this->value ) ) {
1979 throw new MWException( __CLASS__ . ' given object instead of string' );
1981 $this->store = $store;
1982 $this->index = $index;
1985 public function __toString() {
1986 return htmlspecialchars( $this->value );
1989 public function getNextSibling() {
1990 return PPNode_Hash_Tree::factory( $this->store, $this->index + 1 );
1993 public function getChildren() {
1994 return false;
1997 public function getFirstChild() {
1998 return false;
2001 public function getChildrenOfType( $name ) {
2002 return false;
2005 public function getLength() {
2006 return false;
2009 public function item( $i ) {
2010 return false;
2013 public function getName() {
2014 return '#text';
2017 public function splitArg() {
2018 throw new MWException( __METHOD__ . ': not supported' );
2021 public function splitExt() {
2022 throw new MWException( __METHOD__ . ': not supported' );
2025 public function splitHeading() {
2026 throw new MWException( __METHOD__ . ': not supported' );
2031 * @ingroup Parser
2033 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
2034 class PPNode_Hash_Array implements PPNode {
2035 // @codingStandardsIgnoreEnd
2037 public $value;
2039 public function __construct( $value ) {
2040 $this->value = $value;
2043 public function __toString() {
2044 return var_export( $this, true );
2047 public function getLength() {
2048 return count( $this->value );
2051 public function item( $i ) {
2052 return $this->value[$i];
2055 public function getName() {
2056 return '#nodelist';
2059 public function getNextSibling() {
2060 return false;
2063 public function getChildren() {
2064 return false;
2067 public function getFirstChild() {
2068 return false;
2071 public function getChildrenOfType( $name ) {
2072 return false;
2075 public function splitArg() {
2076 throw new MWException( __METHOD__ . ': not supported' );
2079 public function splitExt() {
2080 throw new MWException( __METHOD__ . ': not supported' );
2083 public function splitHeading() {
2084 throw new MWException( __METHOD__ . ': not supported' );
2089 * @ingroup Parser
2091 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
2092 class PPNode_Hash_Attr implements PPNode {
2093 // @codingStandardsIgnoreEnd
2095 public $name, $value;
2096 private $store, $index;
2099 * Construct an object using the data from $store[$index]. The rest of the
2100 * store array can be accessed via getNextSibling().
2102 * @param array $store
2103 * @param integer $index
2105 public function __construct( array $store, $index ) {
2106 $descriptor = $store[$index];
2107 if ( $descriptor[PPNode_Hash_Tree::NAME][0] !== '@' ) {
2108 throw new MWException( __METHOD__.': invalid name in attribute descriptor' );
2110 $this->name = substr( $descriptor[PPNode_Hash_Tree::NAME], 1 );
2111 $this->value = $descriptor[PPNode_Hash_Tree::CHILDREN][0];
2112 $this->store = $store;
2113 $this->index = $index;
2116 public function __toString() {
2117 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
2120 public function getName() {
2121 return $this->name;
2124 public function getNextSibling() {
2125 return PPNode_Hash_Tree::factory( $this->store, $this->index + 1 );
2128 public function getChildren() {
2129 return false;
2132 public function getFirstChild() {
2133 return false;
2136 public function getChildrenOfType( $name ) {
2137 return false;
2140 public function getLength() {
2141 return false;
2144 public function item( $i ) {
2145 return false;
2148 public function splitArg() {
2149 throw new MWException( __METHOD__ . ': not supported' );
2152 public function splitExt() {
2153 throw new MWException( __METHOD__ . ': not supported' );
2156 public function splitHeading() {
2157 throw new MWException( __METHOD__ . ': not supported' );