79 foreach ( $values
as $k => $val ) {
81 $store = [ [
'part', [
82 [
'name', [ [
'@index', [ $k ] ] ] ],
83 [
'value', [ strval( $val ) ] ],
86 $store = [ [
'part', [
87 [
'name', [ strval( $k ) ] ],
89 [
'value', [ strval( $val ) ] ],
122 if ( $tree !==
false ) {
123 $store = json_decode( $tree );
124 if ( is_array( $store ) ) {
129 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
131 $xmlishElements = $this->
parser->getStripList();
132 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
133 $enableOnlyinclude =
false;
134 if ( $forInclusion ) {
135 $ignoredTags = [
'includeonly',
'/includeonly' ];
136 $ignoredElements = [
'noinclude' ];
137 $xmlishElements[] =
'noinclude';
138 if ( strpos( $text,
'<onlyinclude>' ) !==
false
139 && strpos( $text,
'</onlyinclude>' ) !==
false
141 $enableOnlyinclude =
true;
144 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
145 $ignoredElements = [
'includeonly' ];
146 $xmlishElements[] =
'includeonly';
148 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
151 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
155 $searchBase =
"[{<\n";
161 $revText = strrev( $text );
162 $lengthText = strlen( $text );
167 $accum =& $stack->getAccum();
178 $noMoreClosingTag = [];
180 $findOnlyinclude = $enableOnlyinclude;
182 $fakeLineStart =
true;
187 if ( $findOnlyinclude ) {
189 $startPos = strpos( $text,
'<onlyinclude>', $i );
190 if ( $startPos ===
false ) {
192 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
195 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
196 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
198 $findOnlyinclude =
false;
201 if ( $fakeLineStart ) {
202 $found =
'line-start';
205 # Find next opening brace, closing brace or pipe
206 $search = $searchBase;
207 if ( $stack->top ===
false ) {
208 $currentClosing =
'';
210 $currentClosing = $stack->top->close;
211 $search .= $currentClosing;
221 # Output literal section, advance input counter
222 $literalLength = strcspn( $text, $search, $i );
223 if ( $literalLength > 0 ) {
225 $i += $literalLength;
227 if ( $i >= $lengthText ) {
228 if ( $currentClosing ==
"\n" ) {
237 $curChar = $curTwoChar = $text[$i];
238 if ( ( $i + 1 ) < $lengthText ) {
239 $curTwoChar .= $text[$i + 1];
241 if ( $curChar ==
'|' ) {
243 } elseif ( $curChar ==
'=' ) {
245 } elseif ( $curChar ==
'<' ) {
247 } elseif ( $curChar ==
"\n" ) {
251 $found =
'line-start';
253 } elseif ( $curTwoChar == $currentClosing ) {
255 $curChar = $curTwoChar;
256 } elseif ( $curChar == $currentClosing ) {
258 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
259 $curChar = $curTwoChar;
261 $rule = $this->rules[$curChar];
262 } elseif ( isset( $this->rules[$curChar] ) ) {
264 $rule = $this->rules[$curChar];
266 # Some versions of PHP have a strcspn which stops on
267 # null characters; ignore these and continue.
268 # We also may get '-' and '}' characters here which
269 # don't match -{ or $currentClosing. Add these to
270 # output and continue.
271 if ( $curChar ==
'-' || $curChar ==
'}' ) {
280 if ( $found ==
'angle' ) {
283 if ( $enableOnlyinclude
284 && substr( $text, $i, strlen(
'</onlyinclude>' ) ) ==
'</onlyinclude>'
286 $findOnlyinclude =
true;
291 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
305 $endPos = strpos( $text,
'-->', $i + 4 );
306 if ( $endPos ===
false ) {
308 $inner = substr( $text, $i );
309 $accum[] = [
'comment', [ $inner ] ];
313 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
317 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
321 $comments = [ [ $wsStart, $wsEnd ] ];
322 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
323 $c = strpos( $text,
'-->', $wsEnd + 4 );
324 if ( $c ===
false ) {
327 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
328 $comments[] = [ $wsEnd + 1, $c ];
336 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
337 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
340 $wsLength = $i - $wsStart;
341 $endIndex =
count( $accum ) - 1;
346 && is_string( $accum[$endIndex] )
347 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
349 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
353 foreach ( $comments
as $j => $com ) {
355 $endPos = $com[1] + 1;
356 if ( $j == (
count( $comments ) - 1 ) ) {
359 $inner = substr( $text, $startPos, $endPos - $startPos );
360 $accum[] = [
'comment', [ $inner ] ];
364 $fakeLineStart =
true;
372 $part = $stack->top->getCurrentPart();
373 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
374 $part->visualEnd = $wsStart;
377 $part->commentEnd = $endPos;
380 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
381 $accum[] = [
'comment', [ $inner ] ];
386 $lowerName = strtolower(
$name );
387 $attrStart = $i + strlen(
$name ) + 1;
390 $tagEndPos = $noMoreGT ?
false : strpos( $text,
'>', $attrStart );
391 if ( $tagEndPos ===
false ) {
401 if ( in_array( $lowerName, $ignoredTags ) ) {
402 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
408 if ( $text[$tagEndPos - 1] ==
'/' ) {
410 $attrEnd = $tagEndPos - 1;
415 $attrEnd = $tagEndPos;
418 !isset( $noMoreClosingTag[
$name] ) &&
419 preg_match(
"/<\/" . preg_quote(
$name,
'/' ) .
"\s*>/i",
420 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
422 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
427 if ( in_array(
$name, $xmlishAllowMissingEndTag ) ) {
429 $inner = substr( $text, $tagEndPos + 1 );
436 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
438 $noMoreClosingTag[
$name] =
true;
444 if ( in_array( $lowerName, $ignoredElements ) ) {
445 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
449 if ( $attrEnd <= $attrStart ) {
454 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
458 [
'name', [
$name ] ],
459 [
'attr', [ $attr ] ] ];
460 if ( $inner !==
null ) {
461 $children[] = [
'inner', [ $inner ] ];
463 if ( $close !==
null ) {
464 $children[] = [
'close', [ $close ] ];
466 $accum[] = [
'ext', $children ];
467 } elseif ( $found ==
'line-start' ) {
470 if ( $fakeLineStart ) {
471 $fakeLineStart =
false;
477 $count = strspn( $text,
'=', $i, 6 );
478 if ( $count == 1 && $findEquals ) {
483 } elseif ( $count > 0 ) {
487 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
490 $stack->push( $piece );
491 $accum =& $stack->getAccum();
492 $stackFlags = $stack->getFlags();
493 if ( isset( $stackFlags[
'findEquals'] ) ) {
494 $findEquals = $stackFlags[
'findEquals'];
496 if ( isset( $stackFlags[
'findPipe'] ) ) {
497 $findPipe = $stackFlags[
'findPipe'];
499 if ( isset( $stackFlags[
'inHeading'] ) ) {
500 $inHeading = $stackFlags[
'inHeading'];
504 } elseif ( $found ==
'line-end' ) {
505 $piece = $stack->top;
507 assert( $piece->open ===
"\n" );
508 $part = $piece->getCurrentPart();
512 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
513 $searchStart = $i - $wsLength;
514 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
517 $searchStart = $part->visualEnd;
518 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
520 $count = $piece->count;
521 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
522 if ( $equalsLength > 0 ) {
523 if ( $searchStart - $equalsLength == $piece->startPos ) {
527 $count = $equalsLength;
531 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
534 $count = min( $equalsLength, $count );
538 $element = [ [
'possible-h',
541 [
'@level', [ $count ] ],
542 [
'@i', [ $headingIndex++ ] ]
557 $accum =& $stack->getAccum();
558 $stackFlags = $stack->getFlags();
559 if ( isset( $stackFlags[
'findEquals'] ) ) {
560 $findEquals = $stackFlags[
'findEquals'];
562 if ( isset( $stackFlags[
'findPipe'] ) ) {
563 $findPipe = $stackFlags[
'findPipe'];
565 if ( isset( $stackFlags[
'inHeading'] ) ) {
566 $inHeading = $stackFlags[
'inHeading'];
570 array_splice( $accum,
count( $accum ), 0, $element );
577 } elseif ( $found ==
'open' ) {
578 # count opening brace characters
579 $curLen = strlen( $curChar );
580 $count = ( $curLen > 1 ) ?
581 # allow the
final character to repeat
582 strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
583 strspn( $text, $curChar, $i );
586 $lineStart = ( $i > 0 && $text[$i - 1] ==
"\n" );
588 if ( $curChar ===
"-{" && $count > $curLen ) {
594 $rule = $this->rules[$curChar];
597 # we need to add to stack only if opening brace count is enough for one of the rules
598 if ( $count >= $rule[
'min'] ) {
599 # Add it to the stack
602 'close' => $rule[
'end'],
603 'savedPrefix' => $savedPrefix,
605 'lineStart' => $lineStart,
608 $stack->push( $piece );
609 $accum =& $stack->getAccum();
610 $stackFlags = $stack->getFlags();
611 if ( isset( $stackFlags[
'findEquals'] ) ) {
612 $findEquals = $stackFlags[
'findEquals'];
614 if ( isset( $stackFlags[
'findPipe'] ) ) {
615 $findPipe = $stackFlags[
'findPipe'];
617 if ( isset( $stackFlags[
'inHeading'] ) ) {
618 $inHeading = $stackFlags[
'inHeading'];
621 # Add literal brace(s)
625 } elseif ( $found ==
'close' ) {
626 $piece = $stack->top;
627 # lets check if there are enough characters for closing brace
628 $maxCount = $piece->count;
629 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
630 $maxCount--; # don
't try to match closing '-
' as a '}
'
632 $curLen = strlen( $curChar );
633 $count = ( $curLen > 1 ) ? $curLen :
634 strspn( $text, $curChar, $i, $maxCount );
636 # check for maximum matching characters (if there are 5 closing
637 # characters, we will probably need only 3 - depending on the rules)
638 $rule = $this->rules[$piece->open];
639 if ( $count > $rule['max
'] ) {
640 # The specified maximum exists in the callback array, unless the caller
642 $matchingCount = $rule['max
'];
644 # Count is less than the maximum
645 # Skip any gaps in the callback array to find the true largest match
646 # Need to use array_key_exists not isset because the callback can be null
647 $matchingCount = $count;
648 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
653 if ( $matchingCount <= 0 ) {
654 # No matching element found in callback array
655 # Output a literal closing brace and continue
656 $endText = substr( $text, $i, $count );
657 self::addLiteral( $accum, $endText );
661 $name = $rule['names'][$matchingCount];
662 if ( $name === null ) {
663 // No element, just literal text
664 $endText = substr( $text, $i, $matchingCount );
665 $element = $piece->breakSyntax( $matchingCount );
666 self::addLiteral( $element, $endText );
669 $parts = $piece->parts;
670 $titleAccum = $parts[0]->out;
675 # The invocation is at the start of the line if lineStart is set in
676 # the stack, and all opening brackets are used up.
677 if ( $maxCount == $matchingCount &&
678 !empty( $piece->lineStart ) &&
679 strlen( $piece->savedPrefix ) == 0 ) {
680 $children[] = [ '@lineStart
', [ 1 ] ];
682 $titleNode = [ 'title', $titleAccum ];
683 $children[] = $titleNode;
685 foreach ( $parts as $part ) {
686 if ( isset( $part->eqpos ) ) {
687 $equalsNode = $part->out[$part->eqpos];
688 $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
689 $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
690 $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
691 $children[] = $partNode;
693 $nameNode = [ 'name', [ [ '@index
', [ $argIndex++ ] ] ] ];
694 $valueNode = [ 'value', $part->out ];
695 $partNode = [ 'part', [ $nameNode, $valueNode ] ];
696 $children[] = $partNode;
699 $element = [ [ $name, $children ] ];
702 # Advance input pointer
703 $i += $matchingCount;
707 $accum =& $stack->getAccum();
709 # Re-add the old stack element if it still has unmatched opening characters remaining
710 if ( $matchingCount < $piece->count ) {
711 $piece->parts = [ new PPDPart_Hash ];
712 $piece->count -= $matchingCount;
713 # do we still qualify for any callback with remaining count?
714 $min = $this->rules[$piece->open]['min
'];
715 if ( $piece->count >= $min ) {
716 $stack->push( $piece );
717 $accum =& $stack->getAccum();
718 } elseif ( $piece->count == 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
719 $piece->savedPrefix = '';
722 $piece->close = $this->rules[$piece->open]['end
'];
723 $stack->push( $piece );
724 $accum =& $stack->getAccum();
726 $s = substr( $piece->open, 0, -1 );
728 substr( $piece->open, -1 ),
729 $piece->count - strlen( $s )
731 self::addLiteral( $accum, $piece->savedPrefix . $s );
733 } elseif ( $piece->savedPrefix !== '' ) {
734 self::addLiteral( $accum, $piece->savedPrefix );
737 $stackFlags = $stack->getFlags();
738 if ( isset( $stackFlags['findEquals
'] ) ) {
739 $findEquals = $stackFlags['findEquals
'];
741 if ( isset( $stackFlags['findPipe
'] ) ) {
742 $findPipe = $stackFlags['findPipe
'];
744 if ( isset( $stackFlags['inHeading
'] ) ) {
745 $inHeading = $stackFlags['inHeading
'];
748 # Add XML element to the enclosing accumulator
749 array_splice( $accum, count( $accum ), 0, $element );
750 } elseif ( $found == 'pipe
' ) {
751 $findEquals = true; // shortcut for getFlags()
753 $accum =& $stack->getAccum();
755 } elseif ( $found == 'equals
' ) {
756 $findEquals = false; // shortcut for getFlags()
757 $accum[] = [ 'equals
', [ '=
' ] ];
758 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
763 # Output any remaining unclosed brackets
764 foreach ( $stack->stack as $piece ) {
765 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
768 # Enable top-level headings
769 foreach ( $stack->rootAccum as &$node ) {
770 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
771 $node[PPNode_Hash_Tree::NAME] = 'h
';
775 $rootStore = [ [ 'root', $stack->rootAccum ] ];
776 $rootNode = new PPNode_Hash_Tree( $rootStore, 0 );
779 $tree = json_encode( $rootStore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
780 if ( $tree !== false ) {
781 $this->cacheSetTree( $text, $flags, $tree );
787 private static function addLiteral( array &$accum, $text ) {
788 $n = count( $accum );
789 if ( $n && is_string( $accum[$n - 1] ) ) {
790 $accum[$n - 1] .= $text;
801 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
802 class PPDStack_Hash extends PPDStack {
804 public function __construct() {
805 $this->elementClass = PPDStackElement_Hash::class;
806 parent::__construct();
807 $this->rootAccum = [];
814 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
815 class PPDStackElement_Hash extends PPDStackElement {
817 public function __construct( $data = [] ) {
818 $this->partClass = PPDPart_Hash::class;
819 parent::__construct( $data );
828 public function breakSyntax( $openingCount = false ) {
829 if ( $this->open == "\n" ) {
830 $accum = array_merge( [ $this->savedPrefix ], $this->parts[0]->out );
832 if ( $openingCount === false ) {
833 $openingCount = $this->count;
835 $s = substr( $this->open, 0, -1 );
837 substr( $this->open, -1 ),
838 $openingCount - strlen( $s )
840 $accum = [ $this->savedPrefix . $s ];
843 foreach ( $this->parts as $part ) {
846 } elseif ( is_string( $accum[$lastIndex] ) ) {
847 $accum[$lastIndex] .= '|
';
849 $accum[++$lastIndex] = '|
';
851 foreach ( $part->out as $node ) {
852 if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) {
853 $accum[$lastIndex] .= $node;
855 $accum[++$lastIndex] = $node;
867 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
868 class PPDPart_Hash extends PPDPart {
870 public function __construct( $out = '' ) {
876 parent::__construct( $accum );
884 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
885 class PPFrame_Hash implements PPFrame {
895 public $preprocessor;
907 public $loopCheckHash;
915 private $volatile = false;
921 protected $childExpansionCache;
927 public function __construct( $preprocessor ) {
928 $this->preprocessor = $preprocessor;
929 $this->parser = $preprocessor->parser;
930 $this->title = $this->parser->mTitle;
931 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
932 $this->loopCheckHash = [];
934 $this->childExpansionCache = [];
947 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
950 if ( $title === false ) {
951 $title = $this->title;
953 if ( $args !== false ) {
954 if ( $args instanceof PPNode_Hash_Array ) {
955 $args = $args->value;
956 } elseif ( !is_array( $args ) ) {
959 foreach ( $args as $arg ) {
960 $bits = $arg->splitArg();
961 if ( $bits['index
'] !== '' ) {
962 // Numbered parameter
963 $index = $bits['index
'] - $indexOffset;
964 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
965 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-
args-warning
',
966 wfEscapeWikiText( $this->title ),
967 wfEscapeWikiText( $title ),
968 wfEscapeWikiText( $index ) )->text() );
969 $this->parser->addTrackingCategory( 'duplicate-
args-category
' );
971 $numberedArgs[$index] = $bits['value'];
972 unset( $namedArgs[$index] );
975 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
976 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
977 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-
args-warning
',
978 wfEscapeWikiText( $this->title ),
979 wfEscapeWikiText( $title ),
980 wfEscapeWikiText( $name ) )->text() );
981 $this->parser->addTrackingCategory( 'duplicate-
args-category
' );
983 $namedArgs[$name] = $bits['value'];
984 unset( $numberedArgs[$name] );
988 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
998 public function cachedExpand( $key, $root, $flags = 0 ) {
999 // we don't have
a parent,
so we don
't have a cache
1000 return $this->expand( $root, $flags );
1009 public function expand( $root, $flags = 0 ) {
1010 static $expansionDepth = 0;
1011 if ( is_string( $root ) ) {
1015 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
1016 $this->parser->limitationWarn( 'node-
count-exceeded
',
1017 $this->parser->mPPNodeCount,
1018 $this->parser->mOptions->getMaxPPNodeCount()
1020 return '<span
class=
"error">Node-
count limit exceeded</span>
';
1022 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
1023 $this->parser->limitationWarn( 'expansion-depth-exceeded
',
1025 $this->parser->mOptions->getMaxPPExpandDepth()
1027 return '<span
class=
"error">Expansion depth limit exceeded</span>
';
1030 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
1031 $this->parser->mHighestExpansionDepth = $expansionDepth;
1034 $outStack = [ '', '' ];
1035 $iteratorStack = [ false, $root ];
1036 $indexStack = [ 0, 0 ];
1038 while ( count( $iteratorStack ) > 1 ) {
1039 $level = count( $outStack ) - 1;
1040 $iteratorNode =& $iteratorStack[$level];
1041 $out =& $outStack[$level];
1042 $index =& $indexStack[$level];
1044 if ( is_array( $iteratorNode ) ) {
1045 if ( $index >= count( $iteratorNode ) ) {
1046 // All done with this iterator
1047 $iteratorStack[$level] = false;
1048 $contextNode = false;
1050 $contextNode = $iteratorNode[$index];
1053 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
1054 if ( $index >= $iteratorNode->getLength() ) {
1055 // All done with this iterator
1056 $iteratorStack[$level] = false;
1057 $contextNode = false;
1059 $contextNode = $iteratorNode->item( $index );
1063 // Copy to $contextNode and then delete from iterator stack,
1064 // because this is not an iterator but we do have to execute it once
1065 $contextNode = $iteratorStack[$level];
1066 $iteratorStack[$level] = false;
1069 $newIterator = false;
1070 $contextName = false;
1071 $contextChildren = false;
1073 if ( $contextNode === false ) {
1075 } elseif ( is_string( $contextNode ) ) {
1076 $out .= $contextNode;
1077 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
1078 $newIterator = $contextNode;
1079 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
1081 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
1082 $out .= $contextNode->value;
1083 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
1084 $contextName = $contextNode->name;
1085 $contextChildren = $contextNode->getRawChildren();
1086 } elseif ( is_array( $contextNode ) ) {
1087 // Node descriptor array
1088 if ( count( $contextNode ) !== 2 ) {
1089 throw new MWException( __METHOD__.
1090 ': found an
array where
a node descriptor should be
' );
1092 list( $contextName, $contextChildren ) = $contextNode;
1094 throw new MWException( __METHOD__ . ': Invalid parameter
type' );
1097 // Handle node descriptor array or tree object
1098 if ( $contextName === false ) {
1099 // Not a node, already handled above
1100 } elseif ( $contextName[0] === '@
' ) {
1101 // Attribute: no output
1102 } elseif ( $contextName === 'template' ) {
1103 # Double-brace expansion
1104 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1105 if ( $flags & PPFrame::NO_TEMPLATES ) {
1106 $newIterator = $this->virtualBracketedImplode(
1112 $ret = $this->parser->braceSubstitution( $bits, $this );
1113 if ( isset( $ret['object'] ) ) {
1114 $newIterator = $ret['object'];
1116 $out .= $ret['text'];
1119 } elseif ( $contextName === 'tplarg
' ) {
1120 # Triple-brace expansion
1121 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1122 if ( $flags & PPFrame::NO_ARGS ) {
1123 $newIterator = $this->virtualBracketedImplode(
1129 $ret = $this->parser->argSubstitution( $bits, $this );
1130 if ( isset( $ret['object'] ) ) {
1131 $newIterator = $ret['object'];
1133 $out .= $ret['text'];
1136 } elseif ( $contextName === 'comment
' ) {
1137 # HTML-style comment
1138 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1139 # Not in RECOVER_COMMENTS mode (msgnw) though.
1140 if ( ( $this->parser->ot['html
']
1141 || ( $this->parser->ot['pre
'] && $this->parser->mOptions->getRemoveComments() )
1142 || ( $flags & PPFrame::STRIP_COMMENTS )
1143 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
1146 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1147 # Add a strip marker in PST mode so that pstPass2() can
1148 # run some old-fashioned regexes on the result.
1149 # Not in RECOVER_COMMENTS mode (extractSections) though.
1150 $out .= $this->parser->insertStripItem( $contextChildren[0] );
1152 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1153 $out .= $contextChildren[0];
1155 } elseif ( $contextName === 'ignore
' ) {
1156 # Output suppression used by <includeonly> etc.
1157 # OT_WIKI will only respect <ignore> in substed templates.
1158 # The other output types respect it unless NO_IGNORE is set.
1159 # extractSections() sets NO_IGNORE and so never respects it.
1160 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
1161 || ( $flags & PPFrame::NO_IGNORE )
1163 $out .= $contextChildren[0];
1167 } elseif ( $contextName === 'ext
' ) {
1169 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
1170 [ 'attr
' => null, 'inner
' => null, 'close
' => null ];
1171 if ( $flags & PPFrame::NO_TAGS ) {
1172 $s = '<
' . $bits['name']->getFirstChild()->value;
1173 if ( $bits['attr
'] ) {
1174 $s .= $bits['attr
']->getFirstChild()->value;
1176 if ( $bits['inner
'] ) {
1177 $s .= '>
' . $bits['inner
']->getFirstChild()->value;
1178 if ( $bits['close
'] ) {
1179 $s .= $bits['close
']->getFirstChild()->value;
1186 $out .= $this->parser->extensionSubstitution( $bits, $this );
1188 } elseif ( $contextName === 'h
' ) {
1190 if ( $this->parser->ot['html
'] ) {
1191 # Expand immediately and insert heading index marker
1192 $s = $this->expand( $contextChildren, $flags );
1193 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
1194 $titleText = $this->title->getPrefixedDBkey();
1195 $this->parser->mHeadings[] = [ $titleText, $bits['i
'] ];
1196 $serial = count( $this->parser->mHeadings ) - 1;
1197 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
1198 $s = substr( $s, 0, $bits['level
'] ) . $marker . substr( $s, $bits['level
'] );
1199 $this->parser->mStripState->addGeneral( $marker, '' );
1202 # Expand in virtual stack
1203 $newIterator = $contextChildren;
1206 # Generic recursive expansion
1207 $newIterator = $contextChildren;
1210 if ( $newIterator !== false ) {
1212 $iteratorStack[] = $newIterator;
1214 } elseif ( $iteratorStack[$level] === false ) {
1215 // Return accumulated value to parent
1216 // With tail recursion
1217 while ( $iteratorStack[$level] === false && $level > 0 ) {
1218 $outStack[$level - 1] .= $out;
1219 array_pop( $outStack );
1220 array_pop( $iteratorStack );
1221 array_pop( $indexStack );
1227 return $outStack[0];
1236 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1237 $args = array_slice( func_get_args(), 2 );
1241 foreach ( $args as $root ) {
1242 if ( $root instanceof PPNode_Hash_Array ) {
1243 $root = $root->value;
1245 if ( !is_array( $root ) ) {
1248 foreach ( $root as $node ) {
1254 $s .= $this->expand( $node, $flags );
1267 public function implode( $sep /*, ... */ ) {
1268 $args = array_slice( func_get_args(), 1 );
1272 foreach ( $args as $root ) {
1273 if ( $root instanceof PPNode_Hash_Array ) {
1274 $root = $root->value;
1276 if ( !is_array( $root ) ) {
1279 foreach ( $root as $node ) {
1285 $s .= $this->expand( $node );
1299 public function virtualImplode( $sep /*, ... */ ) {
1300 $args = array_slice( func_get_args(), 1 );
1304 foreach ( $args as $root ) {
1305 if ( $root instanceof PPNode_Hash_Array ) {
1306 $root = $root->value;
1308 if ( !is_array( $root ) ) {
1311 foreach ( $root as $node ) {
1320 return new PPNode_Hash_Array( $out );
1332 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1333 $args = array_slice( func_get_args(), 3 );
1337 foreach ( $args as $root ) {
1338 if ( $root instanceof PPNode_Hash_Array ) {
1339 $root = $root->value;
1341 if ( !is_array( $root ) ) {
1344 foreach ( $root as $node ) {
1354 return new PPNode_Hash_Array( $out );
1357 public function __toString() {
1365 public function getPDBK( $level = false ) {
1366 if ( $level === false ) {
1367 return $this->title->getPrefixedDBkey();
1369 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1376 public function getArguments() {
1383 public function getNumberedArguments() {
1390 public function getNamedArguments() {
1399 public function isEmpty() {
1407 public function getArgument( $name ) {
1418 public function loopCheck( $title ) {
1419 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1427 public function isTemplate() {
1436 public function getTitle() {
1437 return $this->title;
1445 public function setVolatile( $flag = true ) {
1446 $this->volatile = $flag;
1454 public function isVolatile() {
1455 return $this->volatile;
1463 public function setTTL( $ttl ) {
1464 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
1474 public function getTTL() {
1483 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
1484 class PPTemplateFrame_Hash extends PPFrame_Hash {
1486 public $numberedArgs, $namedArgs, $parent;
1487 public $numberedExpansionCache, $namedExpansionCache;
1496 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
1497 $namedArgs = [], $title = false
1499 parent::__construct( $preprocessor );
1501 $this->parent = $parent;
1502 $this->numberedArgs = $numberedArgs;
1503 $this->namedArgs = $namedArgs;
1504 $this->title = $title;
1505 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1506 $this->titleCache = $parent->titleCache;
1507 $this->titleCache[] = $pdbk;
1508 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1509 if ( $pdbk !== false ) {
1510 $this->loopCheckHash[$pdbk] = true;
1512 $this->depth = $parent->depth + 1;
1513 $this->numberedExpansionCache = $this->namedExpansionCache = [];
1516 public function __toString() {
1519 $args = $this->numberedArgs + $this->namedArgs;
1520 foreach ( $args as $name => $value ) {
1526 $s .= "\"$name\":\"" .
1527 str_replace( '"', '\\"', $value->__toString() ) . '"';
1540 public function cachedExpand( $key, $root, $flags = 0 ) {
1541 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
1542 return $this->parent->childExpansionCache[$key];
1544 $retval = $this->expand( $root, $flags );
1545 if ( !$this->isVolatile() ) {
1546 $this->parent->childExpansionCache[$key] = $retval;
1556 public function isEmpty() {
1557 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1563 public function getArguments() {
1565 foreach ( array_merge(
1566 array_keys( $this->numberedArgs ),
1567 array_keys( $this->namedArgs ) ) as $key ) {
1568 $arguments[$key] = $this->getArgument( $key );
1576 public function getNumberedArguments() {
1578 foreach ( array_keys( $this->numberedArgs ) as $key ) {
1579 $arguments[$key] = $this->getArgument( $key );
1587 public function getNamedArguments() {
1589 foreach ( array_keys( $this->namedArgs ) as $key ) {
1590 $arguments[$key] = $this->getArgument( $key );
1599 public function getNumberedArgument( $index ) {
1600 if ( !isset( $this->numberedArgs[$index] ) ) {
1603 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1604 # No trimming for unnamed arguments
1605 $this->numberedExpansionCache[$index] = $this->parent->expand(
1606 $this->numberedArgs[$index],
1607 PPFrame::STRIP_COMMENTS
1610 return $this->numberedExpansionCache[$index];
1617 public function getNamedArgument( $name ) {
1618 if ( !isset( $this->namedArgs[$name] ) ) {
1621 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1622 # Trim named arguments post-expand, for backwards compatibility
1623 $this->namedExpansionCache[$name] = trim(
1624 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1626 return $this->namedExpansionCache[$name];
1633 public function getArgument( $name ) {
1634 $text = $this->getNumberedArgument( $name );
1635 if ( $text === false ) {
1636 $text = $this->getNamedArgument( $name );
1646 public function isTemplate() {
1650 public function setVolatile( $flag = true ) {
1651 parent::setVolatile( $flag );
1652 $this->parent->setVolatile( $flag );
1655 public function setTTL( $ttl ) {
1656 parent::setTTL( $ttl );
1657 $this->parent->setTTL( $ttl );
1665 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
1666 class PPCustomFrame_Hash extends PPFrame_Hash {
1670 public function __construct( $preprocessor, $args ) {
1671 parent::__construct( $preprocessor );
1672 $this->args = $args;
1675 public function __toString() {
1678 foreach ( $this->args as $name => $value ) {
1684 $s .= "\
"$name\":\"" .
1685 str_replace(
'"',
'\\"',
$value->__toString() ) .
'"';
1703 if ( !isset( $this->
args[$index] ) ) {
1706 return $this->
args[$index];
1759 $this->
store = $store;
1760 $this->index = $index;
1761 list( $this->
name, $this->rawChildren ) = $this->
store[$index];
1773 if ( !isset( $store[$index] ) ) {
1777 $descriptor = $store[$index];
1778 if ( is_string( $descriptor ) ) {
1780 } elseif ( is_array( $descriptor ) ) {
1781 if ( $descriptor[self::NAME][0] ===
'@' ) {
1787 throw new MWException( __METHOD__.
': invalid node descriptor' );
1789 return new $class( $store, $index );
1798 for ( $node = $this->getFirstChild(); $node; $node = $node->getNextSibling() ) {
1800 $attribs .=
' ' . $node->name .
'="' . htmlspecialchars( $node->value ) .
'"';
1802 $inner .= $node->__toString();
1805 if ( $inner ===
'' ) {
1806 return "<{$this->name}$attribs/>";
1808 return "<{$this->name}$attribs>$inner</{$this->name}>";
1817 foreach ( $this->rawChildren
as $i => $child ) {
1818 $children[] = self::factory( $this->rawChildren, $i );
1831 if ( !isset( $this->rawChildren[0] ) ) {
1834 return self::factory( $this->rawChildren, 0 );
1846 return self::factory( $this->
store, $this->index + 1 );
1857 foreach ( $this->rawChildren
as $i => $child ) {
1858 if ( is_array( $child ) && $child[self::NAME] ===
$name ) {
1859 $children[] = self::factory( $this->rawChildren, $i );
1870 return $this->rawChildren;
1905 return self::splitRawArg( $this->rawChildren );
1915 foreach ( $children
as $i => $child ) {
1916 if ( !is_array( $child ) ) {
1919 if ( $child[self::NAME] ===
'name' ) {
1920 $bits[
'name'] =
new self( $children, $i );
1921 if ( isset( $child[self::CHILDREN][0][self::NAME] )
1922 && $child[self::CHILDREN][0][self::NAME] ===
'@index'
1924 $bits[
'index'] = $child[self::CHILDREN][0][self::CHILDREN][0];
1926 } elseif ( $child[self::NAME] ===
'value' ) {
1927 $bits[
'value'] =
new self( $children, $i );
1931 if ( !isset( $bits[
'name'] ) ) {
1932 throw new MWException(
'Invalid brace node passed to ' . __METHOD__ );
1934 if ( !isset( $bits[
'index'] ) ) {
1935 $bits[
'index'] =
'';
1948 return self::splitRawExt( $this->rawChildren );
1958 foreach ( $children
as $i => $child ) {
1959 if ( !is_array( $child ) ) {
1962 switch ( $child[self::NAME] ) {
1964 $bits[
'name'] =
new self( $children, $i );
1967 $bits[
'attr'] =
new self( $children, $i );
1970 $bits[
'inner'] =
new self( $children, $i );
1973 $bits[
'close'] =
new self( $children, $i );
1977 if ( !isset( $bits[
'name'] ) ) {
1978 throw new MWException(
'Invalid ext node passed to ' . __METHOD__ );
1990 if ( $this->
name !==
'h' ) {
1991 throw new MWException(
'Invalid h node passed to ' . __METHOD__ );
1993 return self::splitRawHeading( $this->rawChildren );
2003 foreach ( $children
as $i => $child ) {
2004 if ( !is_array( $child ) ) {
2007 if ( $child[self::NAME] ===
'@i' ) {
2008 $bits[
'i'] = $child[self::CHILDREN][0];
2009 } elseif ( $child[self::NAME] ===
'@level' ) {
2010 $bits[
'level'] = $child[self::CHILDREN][0];
2013 if ( !isset( $bits[
'i'] ) ) {
2014 throw new MWException(
'Invalid h node passed to ' . __METHOD__ );
2026 return self::splitRawTemplate( $this->rawChildren );
2036 $bits = [
'lineStart' =>
'' ];
2037 foreach ( $children
as $i => $child ) {
2038 if ( !is_array( $child ) ) {
2041 switch ( $child[self::NAME] ) {
2043 $bits[
'title'] =
new self( $children, $i );
2046 $parts[] =
new self( $children, $i );
2049 $bits[
'lineStart'] =
'1';
2053 if ( !isset( $bits[
'title'] ) ) {
2054 throw new MWException(
'Invalid node passed to ' . __METHOD__ );
2078 $this->
value = $store[$index];
2079 if ( !is_scalar( $this->
value ) ) {
2080 throw new MWException( __CLASS__ .
' given object instead of string' );
2082 $this->
store = $store;
2083 $this->index = $index;
2087 return htmlspecialchars( $this->
value );
2119 throw new MWException( __METHOD__ .
': not supported' );
2123 throw new MWException( __METHOD__ .
': not supported' );
2127 throw new MWException( __METHOD__ .
': not supported' );
2144 return var_export( $this,
true );
2152 return $this->
value[$i];
2176 throw new MWException( __METHOD__ .
': not supported' );
2180 throw new MWException( __METHOD__ .
': not supported' );
2184 throw new MWException( __METHOD__ .
': not supported' );
2205 $descriptor = $store[$index];
2207 throw new MWException( __METHOD__.
': invalid name in attribute descriptor' );
2211 $this->
store = $store;
2212 $this->index = $index;
2216 return "<@{$this->name}>" . htmlspecialchars( $this->
value ) .
"</@{$this->name}>";
2248 throw new MWException( __METHOD__ .
': not supported' );
2252 throw new MWException( __METHOD__ .
': not supported' );
2256 throw new MWException( __METHOD__ .
': not supported' );