80 foreach ( $values
as $k => $val ) {
82 $store = [ [
'part', [
83 [
'name', [ [
'@index', [ $k ] ] ] ],
84 [
'value', [ strval( $val ) ] ],
87 $store = [ [
'part', [
88 [
'name', [ strval( $k ) ] ],
90 [
'value', [ strval( $val ) ] ],
123 if ( $tree !==
false ) {
124 $store = json_decode( $tree );
125 if ( is_array( $store ) ) {
130 $forInclusion =
$flags & Parser::PTD_FOR_INCLUSION;
132 $xmlishElements = $this->
parser->getStripList();
133 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
134 $enableOnlyinclude =
false;
135 if ( $forInclusion ) {
136 $ignoredTags = [
'includeonly',
'/includeonly' ];
137 $ignoredElements = [
'noinclude' ];
138 $xmlishElements[] =
'noinclude';
139 if ( strpos( $text,
'<onlyinclude>' ) !==
false
140 && strpos( $text,
'</onlyinclude>' ) !==
false
142 $enableOnlyinclude =
true;
145 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
146 $ignoredElements = [
'includeonly' ];
147 $xmlishElements[] =
'includeonly';
149 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
152 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
156 $searchBase =
"[{<\n";
162 $revText = strrev( $text );
163 $lengthText = strlen( $text );
168 $accum =& $stack->getAccum();
179 $noMoreClosingTag = [];
181 $findOnlyinclude = $enableOnlyinclude;
183 $fakeLineStart =
true;
188 if ( $findOnlyinclude ) {
190 $startPos = strpos( $text,
'<onlyinclude>', $i );
191 if ( $startPos ===
false ) {
193 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
196 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
197 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
199 $findOnlyinclude =
false;
202 if ( $fakeLineStart ) {
203 $found =
'line-start';
206 # Find next opening brace, closing brace or pipe
207 $search = $searchBase;
208 if ( $stack->top ===
false ) {
209 $currentClosing =
'';
211 $stack->top->close ===
'}-' &&
212 $stack->top->count > 2
214 # adjust closing for -{{{...{{
215 $currentClosing =
'}';
216 $search .= $currentClosing;
218 $currentClosing = $stack->top->close;
219 $search .= $currentClosing;
229 # Output literal section, advance input counter
230 $literalLength = strcspn( $text, $search, $i );
231 if ( $literalLength > 0 ) {
233 $i += $literalLength;
235 if ( $i >= $lengthText ) {
236 if ( $currentClosing ==
"\n" ) {
245 $curChar = $curTwoChar = $text[$i];
246 if ( ( $i + 1 ) < $lengthText ) {
247 $curTwoChar .= $text[$i + 1];
249 if ( $curChar ==
'|' ) {
251 } elseif ( $curChar ==
'=' ) {
253 } elseif ( $curChar ==
'<' ) {
255 } elseif ( $curChar ==
"\n" ) {
259 $found =
'line-start';
261 } elseif ( $curTwoChar == $currentClosing ) {
263 $curChar = $curTwoChar;
264 } elseif ( $curChar == $currentClosing ) {
266 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
267 $curChar = $curTwoChar;
269 $rule = $this->rules[$curChar];
270 } elseif ( isset( $this->rules[$curChar] ) ) {
272 $rule = $this->rules[$curChar];
274 # Some versions of PHP have a strcspn which stops on
275 # null characters; ignore these and continue.
276 # We also may get '-' and '}' characters here which
277 # don't match -{ or $currentClosing. Add these to
278 # output and continue.
279 if ( $curChar ==
'-' || $curChar ==
'}' ) {
288 if ( $found ==
'angle' ) {
291 if ( $enableOnlyinclude
292 && substr( $text, $i, strlen(
'</onlyinclude>' ) ) ==
'</onlyinclude>'
294 $findOnlyinclude =
true;
299 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
313 $endPos = strpos( $text,
'-->', $i + 4 );
314 if ( $endPos ===
false ) {
316 $inner = substr( $text, $i );
317 $accum[] = [
'comment', [ $inner ] ];
321 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
325 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
329 $comments = [ [ $wsStart, $wsEnd ] ];
330 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
331 $c = strpos( $text,
'-->', $wsEnd + 4 );
332 if ( $c ===
false ) {
335 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
336 $comments[] = [ $wsEnd + 1, $c ];
344 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
345 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
348 $wsLength = $i - $wsStart;
349 $endIndex =
count( $accum ) - 1;
354 && is_string( $accum[$endIndex] )
355 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
357 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
361 foreach ( $comments
as $j => $com ) {
363 $endPos = $com[1] + 1;
364 if ( $j == (
count( $comments ) - 1 ) ) {
367 $inner = substr( $text, $startPos, $endPos - $startPos );
368 $accum[] = [
'comment', [ $inner ] ];
372 $fakeLineStart =
true;
380 $part = $stack->top->getCurrentPart();
381 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
382 $part->visualEnd = $wsStart;
385 $part->commentEnd = $endPos;
388 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
389 $accum[] = [
'comment', [ $inner ] ];
394 $lowerName = strtolower(
$name );
395 $attrStart = $i + strlen(
$name ) + 1;
398 $tagEndPos = $noMoreGT ?
false : strpos( $text,
'>', $attrStart );
399 if ( $tagEndPos ===
false ) {
409 if ( in_array( $lowerName, $ignoredTags ) ) {
410 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
416 if ( $text[$tagEndPos - 1] ==
'/' ) {
418 $attrEnd = $tagEndPos - 1;
423 $attrEnd = $tagEndPos;
426 !isset( $noMoreClosingTag[
$name] ) &&
427 preg_match(
"/<\/" . preg_quote(
$name,
'/' ) .
"\s*>/i",
428 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
430 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
435 if ( in_array(
$name, $xmlishAllowMissingEndTag ) ) {
437 $inner = substr( $text, $tagEndPos + 1 );
444 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
446 $noMoreClosingTag[
$name] =
true;
452 if ( in_array( $lowerName, $ignoredElements ) ) {
453 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
457 if ( $attrEnd <= $attrStart ) {
462 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
466 [
'name', [
$name ] ],
467 [
'attr', [ $attr ] ] ];
468 if ( $inner !==
null ) {
469 $children[] = [
'inner', [ $inner ] ];
471 if ( $close !==
null ) {
472 $children[] = [
'close', [ $close ] ];
474 $accum[] = [
'ext', $children ];
475 } elseif ( $found ==
'line-start' ) {
478 if ( $fakeLineStart ) {
479 $fakeLineStart =
false;
485 $count = strspn( $text,
'=', $i, 6 );
486 if ( $count == 1 && $findEquals ) {
491 } elseif ( $count > 0 ) {
495 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
498 $stack->push( $piece );
499 $accum =& $stack->getAccum();
500 extract( $stack->getFlags() );
503 } elseif ( $found ==
'line-end' ) {
504 $piece = $stack->top;
506 assert( $piece->open ===
"\n" );
507 $part = $piece->getCurrentPart();
511 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
512 $searchStart = $i - $wsLength;
513 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
516 $searchStart = $part->visualEnd;
517 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
519 $count = $piece->count;
520 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
521 if ( $equalsLength > 0 ) {
522 if ( $searchStart - $equalsLength == $piece->startPos ) {
526 $count = $equalsLength;
530 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
533 $count = min( $equalsLength, $count );
537 $element = [ [
'possible-h',
540 [
'@level', [ $count ] ],
541 [
'@i', [ $headingIndex++ ] ]
556 $accum =& $stack->getAccum();
557 extract( $stack->getFlags() );
560 array_splice( $accum,
count( $accum ), 0, $element );
567 } elseif ( $found ==
'open' ) {
568 # count opening brace characters
569 $curLen = strlen( $curChar );
570 $count = ( $curLen > 1 ) ?
571 # allow the
final character to repeat
572 strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
573 strspn( $text, $curChar, $i );
575 # we need to add to stack only if opening brace count is enough for one of the rules
576 if ( $count >= $rule[
'min'] ) {
577 # Add it to the stack
580 'close' => $rule[
'end'],
582 'lineStart' => ( $i > 0 && $text[$i - 1] ==
"\n" ),
585 $stack->push( $piece );
586 $accum =& $stack->getAccum();
587 extract( $stack->getFlags() );
589 # Add literal brace(s)
593 } elseif ( $found ==
'close' ) {
594 $piece = $stack->top;
595 # lets check if there are enough characters for closing brace
596 $maxCount = $piece->count;
597 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
598 $maxCount--; # don
't try to match closing '-
' as a '}
'
600 $curLen = strlen( $curChar );
601 $count = ( $curLen > 1 ) ? $curLen :
602 strspn( $text, $curChar, $i, $maxCount );
604 # check for maximum matching characters (if there are 5 closing
605 # characters, we will probably need only 3 - depending on the rules)
606 $rule = $this->rules[$piece->open];
607 if ( $piece->close === '}-
' && $piece->count > 2 ) {
608 # tweak for -{..{{ }}..}-
609 $rule = $this->rules['{
'];
611 if ( $count > $rule['max
'] ) {
612 # The specified maximum exists in the callback array, unless the caller
614 $matchingCount = $rule['max
'];
616 # Count is less than the maximum
617 # Skip any gaps in the callback array to find the true largest match
618 # Need to use array_key_exists not isset because the callback can be null
619 $matchingCount = $count;
620 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
625 if ( $matchingCount <= 0 ) {
626 # No matching element found in callback array
627 # Output a literal closing brace and continue
628 $endText = substr( $text, $i, $count );
629 self::addLiteral( $accum, $endText );
633 $name = $rule['names'][$matchingCount];
634 if ( $name === null ) {
635 // No element, just literal text
636 $endText = substr( $text, $i, $matchingCount );
637 $element = $piece->breakSyntax( $matchingCount );
638 self::addLiteral( $element, $endText );
641 $parts = $piece->parts;
642 $titleAccum = $parts[0]->out;
647 # The invocation is at the start of the line if lineStart is set in
648 # the stack, and all opening brackets are used up.
649 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
650 $children[] = [ '@lineStart
', [ 1 ] ];
652 $titleNode = [ 'title', $titleAccum ];
653 $children[] = $titleNode;
655 foreach ( $parts as $part ) {
656 if ( isset( $part->eqpos ) ) {
657 $equalsNode = $part->out[$part->eqpos];
658 $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
659 $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
660 $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
661 $children[] = $partNode;
663 $nameNode = [ 'name', [ [ '@index
', [ $argIndex++ ] ] ] ];
664 $valueNode = [ 'value', $part->out ];
665 $partNode = [ 'part', [ $nameNode, $valueNode ] ];
666 $children[] = $partNode;
669 $element = [ [ $name, $children ] ];
672 # Advance input pointer
673 $i += $matchingCount;
677 $accum =& $stack->getAccum();
679 # Re-add the old stack element if it still has unmatched opening characters remaining
680 if ( $matchingCount < $piece->count ) {
681 $piece->parts = [ new PPDPart_Hash ];
682 $piece->count -= $matchingCount;
683 # do we still qualify for any callback with remaining count?
684 $min = $this->rules[$piece->open]['min
'];
685 if ( $piece->count >= $min ) {
686 $stack->push( $piece );
687 $accum =& $stack->getAccum();
689 $s = substr( $piece->open, 0, -1 );
691 substr( $piece->open, -1 ),
692 $piece->count - strlen( $s )
694 self::addLiteral( $accum, $s );
698 extract( $stack->getFlags() );
700 # Add XML element to the enclosing accumulator
701 array_splice( $accum, count( $accum ), 0, $element );
702 } elseif ( $found == 'pipe
' ) {
703 $findEquals = true; // shortcut for getFlags()
705 $accum =& $stack->getAccum();
707 } elseif ( $found == 'equals
' ) {
708 $findEquals = false; // shortcut for getFlags()
709 $accum[] = [ 'equals
', [ '=
' ] ];
710 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
712 } elseif ( $found == 'dash
' ) {
713 self::addLiteral( $accum, '-
' );
718 # Output any remaining unclosed brackets
719 foreach ( $stack->stack as $piece ) {
720 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
723 # Enable top-level headings
724 foreach ( $stack->rootAccum as &$node ) {
725 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
726 $node[PPNode_Hash_Tree::NAME] = 'h
';
730 $rootStore = [ [ 'root', $stack->rootAccum ] ];
731 $rootNode = new PPNode_Hash_Tree( $rootStore, 0 );
734 $tree = json_encode( $rootStore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
735 if ( $tree !== false ) {
736 $this->cacheSetTree( $text, $flags, $tree );
742 private static function addLiteral( array &$accum, $text ) {
743 $n = count( $accum );
744 if ( $n && is_string( $accum[$n - 1] ) ) {
745 $accum[$n - 1] .= $text;
756 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
757 class PPDStack_Hash extends PPDStack {
758 // @codingStandardsIgnoreEnd
760 public function __construct() {
762 parent::__construct();
763 $this->rootAccum = [];
770 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
771 class PPDStackElement_Hash extends PPDStackElement {
772 // @codingStandardsIgnoreEnd
774 public function __construct( $data = [] ) {
776 parent::__construct( $data );
785 public function breakSyntax( $openingCount = false ) {
786 if ( $this->open == "\n" ) {
787 $accum = $this->parts[0]->out;
789 if ( $openingCount === false ) {
790 $openingCount = $this->count;
792 $s = substr( $this->open, 0, -1 );
794 substr( $this->open, -1 ),
795 $openingCount - strlen( $s )
800 foreach ( $this->parts as $part ) {
803 } elseif ( is_string( $accum[$lastIndex] ) ) {
804 $accum[$lastIndex] .= '|
';
806 $accum[++$lastIndex] = '|
';
808 foreach ( $part->out as $node ) {
809 if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) {
810 $accum[$lastIndex] .= $node;
812 $accum[++$lastIndex] = $node;
824 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
825 class PPDPart_Hash extends PPDPart {
826 // @codingStandardsIgnoreEnd
828 public function __construct( $out = '' ) {
834 parent::__construct( $accum );
842 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
843 class PPFrame_Hash implements PPFrame {
844 // @codingStandardsIgnoreEnd
854 public $preprocessor;
866 public $loopCheckHash;
874 private $volatile = false;
880 protected $childExpansionCache;
886 public function __construct( $preprocessor ) {
887 $this->preprocessor = $preprocessor;
888 $this->parser = $preprocessor->parser;
889 $this->title = $this->parser->mTitle;
890 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
891 $this->loopCheckHash = [];
893 $this->childExpansionCache = [];
906 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
909 if ( $title === false ) {
910 $title = $this->title;
912 if ( $args !== false ) {
913 if ( $args instanceof PPNode_Hash_Array ) {
914 $args = $args->value;
915 } elseif ( !is_array( $args ) ) {
918 foreach ( $args as $arg ) {
919 $bits = $arg->splitArg();
920 if ( $bits['index
'] !== '' ) {
921 // Numbered parameter
922 $index = $bits['index
'] - $indexOffset;
923 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
924 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-
args-warning
',
925 wfEscapeWikiText( $this->title ),
926 wfEscapeWikiText( $title ),
927 wfEscapeWikiText( $index ) )->text() );
928 $this->parser->addTrackingCategory( 'duplicate-
args-category
' );
930 $numberedArgs[$index] = $bits['value'];
931 unset( $namedArgs[$index] );
934 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
935 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
936 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-
args-warning
',
937 wfEscapeWikiText( $this->title ),
938 wfEscapeWikiText( $title ),
939 wfEscapeWikiText( $name ) )->text() );
940 $this->parser->addTrackingCategory( 'duplicate-
args-category
' );
942 $namedArgs[$name] = $bits['value'];
943 unset( $numberedArgs[$name] );
947 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
957 public function cachedExpand( $key, $root, $flags = 0 ) {
958 // we don't have
a parent,
so we don
't have a cache
959 return $this->expand( $root, $flags );
968 public function expand( $root, $flags = 0 ) {
969 static $expansionDepth = 0;
970 if ( is_string( $root ) ) {
974 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
975 $this->parser->limitationWarn( 'node-
count-exceeded
',
976 $this->parser->mPPNodeCount,
977 $this->parser->mOptions->getMaxPPNodeCount()
979 return '<span
class=
"error">Node-
count limit exceeded</span>
';
981 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
982 $this->parser->limitationWarn( 'expansion-depth-exceeded
',
984 $this->parser->mOptions->getMaxPPExpandDepth()
986 return '<span
class=
"error">Expansion depth limit exceeded</span>
';
989 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
990 $this->parser->mHighestExpansionDepth = $expansionDepth;
993 $outStack = [ '', '' ];
994 $iteratorStack = [ false, $root ];
995 $indexStack = [ 0, 0 ];
997 while ( count( $iteratorStack ) > 1 ) {
998 $level = count( $outStack ) - 1;
999 $iteratorNode =& $iteratorStack[$level];
1000 $out =& $outStack[$level];
1001 $index =& $indexStack[$level];
1003 if ( is_array( $iteratorNode ) ) {
1004 if ( $index >= count( $iteratorNode ) ) {
1005 // All done with this iterator
1006 $iteratorStack[$level] = false;
1007 $contextNode = false;
1009 $contextNode = $iteratorNode[$index];
1012 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
1013 if ( $index >= $iteratorNode->getLength() ) {
1014 // All done with this iterator
1015 $iteratorStack[$level] = false;
1016 $contextNode = false;
1018 $contextNode = $iteratorNode->item( $index );
1022 // Copy to $contextNode and then delete from iterator stack,
1023 // because this is not an iterator but we do have to execute it once
1024 $contextNode = $iteratorStack[$level];
1025 $iteratorStack[$level] = false;
1028 $newIterator = false;
1029 $contextName = false;
1030 $contextChildren = false;
1032 if ( $contextNode === false ) {
1034 } elseif ( is_string( $contextNode ) ) {
1035 $out .= $contextNode;
1036 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
1037 $newIterator = $contextNode;
1038 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
1040 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
1041 $out .= $contextNode->value;
1042 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
1043 $contextName = $contextNode->name;
1044 $contextChildren = $contextNode->getRawChildren();
1045 } elseif ( is_array( $contextNode ) ) {
1046 // Node descriptor array
1047 if ( count( $contextNode ) !== 2 ) {
1048 throw new MWException( __METHOD__.
1049 ': found an
array where
a node descriptor should be
' );
1051 list( $contextName, $contextChildren ) = $contextNode;
1053 throw new MWException( __METHOD__ . ': Invalid parameter
type' );
1056 // Handle node descriptor array or tree object
1057 if ( $contextName === false ) {
1058 // Not a node, already handled above
1059 } elseif ( $contextName[0] === '@
' ) {
1060 // Attribute: no output
1061 } elseif ( $contextName === 'template' ) {
1062 # Double-brace expansion
1063 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1064 if ( $flags & PPFrame::NO_TEMPLATES ) {
1065 $newIterator = $this->virtualBracketedImplode(
1071 $ret = $this->parser->braceSubstitution( $bits, $this );
1072 if ( isset( $ret['object'] ) ) {
1073 $newIterator = $ret['object'];
1075 $out .= $ret['text'];
1078 } elseif ( $contextName === 'tplarg
' ) {
1079 # Triple-brace expansion
1080 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1081 if ( $flags & PPFrame::NO_ARGS ) {
1082 $newIterator = $this->virtualBracketedImplode(
1088 $ret = $this->parser->argSubstitution( $bits, $this );
1089 if ( isset( $ret['object'] ) ) {
1090 $newIterator = $ret['object'];
1092 $out .= $ret['text'];
1095 } elseif ( $contextName === 'comment
' ) {
1096 # HTML-style comment
1097 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1098 # Not in RECOVER_COMMENTS mode (msgnw) though.
1099 if ( ( $this->parser->ot['html
']
1100 || ( $this->parser->ot['pre
'] && $this->parser->mOptions->getRemoveComments() )
1101 || ( $flags & PPFrame::STRIP_COMMENTS )
1102 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
1105 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1106 # Add a strip marker in PST mode so that pstPass2() can
1107 # run some old-fashioned regexes on the result.
1108 # Not in RECOVER_COMMENTS mode (extractSections) though.
1109 $out .= $this->parser->insertStripItem( $contextChildren[0] );
1111 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1112 $out .= $contextChildren[0];
1114 } elseif ( $contextName === 'ignore
' ) {
1115 # Output suppression used by <includeonly> etc.
1116 # OT_WIKI will only respect <ignore> in substed templates.
1117 # The other output types respect it unless NO_IGNORE is set.
1118 # extractSections() sets NO_IGNORE and so never respects it.
1119 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
1120 || ( $flags & PPFrame::NO_IGNORE )
1122 $out .= $contextChildren[0];
1126 } elseif ( $contextName === 'ext
' ) {
1128 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
1129 [ 'attr
' => null, 'inner
' => null, 'close
' => null ];
1130 if ( $flags & PPFrame::NO_TAGS ) {
1131 $s = '<
' . $bits['name']->getFirstChild()->value;
1132 if ( $bits['attr
'] ) {
1133 $s .= $bits['attr
']->getFirstChild()->value;
1135 if ( $bits['inner
'] ) {
1136 $s .= '>
' . $bits['inner
']->getFirstChild()->value;
1137 if ( $bits['close
'] ) {
1138 $s .= $bits['close
']->getFirstChild()->value;
1145 $out .= $this->parser->extensionSubstitution( $bits, $this );
1147 } elseif ( $contextName === 'h
' ) {
1149 if ( $this->parser->ot['html
'] ) {
1150 # Expand immediately and insert heading index marker
1151 $s = $this->expand( $contextChildren, $flags );
1152 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
1153 $titleText = $this->title->getPrefixedDBkey();
1154 $this->parser->mHeadings[] = [ $titleText, $bits['i
'] ];
1155 $serial = count( $this->parser->mHeadings ) - 1;
1156 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
1157 $s = substr( $s, 0, $bits['level
'] ) . $marker . substr( $s, $bits['level
'] );
1158 $this->parser->mStripState->addGeneral( $marker, '' );
1161 # Expand in virtual stack
1162 $newIterator = $contextChildren;
1165 # Generic recursive expansion
1166 $newIterator = $contextChildren;
1169 if ( $newIterator !== false ) {
1171 $iteratorStack[] = $newIterator;
1173 } elseif ( $iteratorStack[$level] === false ) {
1174 // Return accumulated value to parent
1175 // With tail recursion
1176 while ( $iteratorStack[$level] === false && $level > 0 ) {
1177 $outStack[$level - 1] .= $out;
1178 array_pop( $outStack );
1179 array_pop( $iteratorStack );
1180 array_pop( $indexStack );
1186 return $outStack[0];
1195 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1196 $args = array_slice( func_get_args(), 2 );
1200 foreach ( $args as $root ) {
1201 if ( $root instanceof PPNode_Hash_Array ) {
1202 $root = $root->value;
1204 if ( !is_array( $root ) ) {
1207 foreach ( $root as $node ) {
1213 $s .= $this->expand( $node, $flags );
1226 public function implode( $sep /*, ... */ ) {
1227 $args = array_slice( func_get_args(), 1 );
1231 foreach ( $args as $root ) {
1232 if ( $root instanceof PPNode_Hash_Array ) {
1233 $root = $root->value;
1235 if ( !is_array( $root ) ) {
1238 foreach ( $root as $node ) {
1244 $s .= $this->expand( $node );
1258 public function virtualImplode( $sep /*, ... */ ) {
1259 $args = array_slice( func_get_args(), 1 );
1263 foreach ( $args as $root ) {
1264 if ( $root instanceof PPNode_Hash_Array ) {
1265 $root = $root->value;
1267 if ( !is_array( $root ) ) {
1270 foreach ( $root as $node ) {
1279 return new PPNode_Hash_Array( $out );
1291 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1292 $args = array_slice( func_get_args(), 3 );
1296 foreach ( $args as $root ) {
1297 if ( $root instanceof PPNode_Hash_Array ) {
1298 $root = $root->value;
1300 if ( !is_array( $root ) ) {
1303 foreach ( $root as $node ) {
1313 return new PPNode_Hash_Array( $out );
1316 public function __toString() {
1324 public function getPDBK( $level = false ) {
1325 if ( $level === false ) {
1326 return $this->title->getPrefixedDBkey();
1328 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1335 public function getArguments() {
1342 public function getNumberedArguments() {
1349 public function getNamedArguments() {
1358 public function isEmpty() {
1366 public function getArgument( $name ) {
1377 public function loopCheck( $title ) {
1378 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1386 public function isTemplate() {
1395 public function getTitle() {
1396 return $this->title;
1404 public function setVolatile( $flag = true ) {
1405 $this->volatile = $flag;
1413 public function isVolatile() {
1414 return $this->volatile;
1422 public function setTTL( $ttl ) {
1423 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
1433 public function getTTL() {
1442 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1443 class PPTemplateFrame_Hash extends PPFrame_Hash {
1444 // @codingStandardsIgnoreEnd
1446 public $numberedArgs, $namedArgs, $parent;
1447 public $numberedExpansionCache, $namedExpansionCache;
1456 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
1457 $namedArgs = [], $title = false
1459 parent::__construct( $preprocessor );
1461 $this->parent = $parent;
1462 $this->numberedArgs = $numberedArgs;
1463 $this->namedArgs = $namedArgs;
1464 $this->title = $title;
1465 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1466 $this->titleCache = $parent->titleCache;
1467 $this->titleCache[] = $pdbk;
1468 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1469 if ( $pdbk !== false ) {
1470 $this->loopCheckHash[$pdbk] = true;
1472 $this->depth = $parent->depth + 1;
1473 $this->numberedExpansionCache = $this->namedExpansionCache = [];
1476 public function __toString() {
1479 $args = $this->numberedArgs + $this->namedArgs;
1480 foreach ( $args as $name => $value ) {
1486 $s .= "\"$name\":\"" .
1487 str_replace( '"', '\\"', $value->__toString() ) . '"';
1500 public function cachedExpand( $key, $root, $flags = 0 ) {
1501 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
1502 return $this->parent->childExpansionCache[$key];
1504 $retval = $this->expand( $root, $flags );
1505 if ( !$this->isVolatile() ) {
1506 $this->parent->childExpansionCache[$key] = $retval;
1516 public function isEmpty() {
1517 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1523 public function getArguments() {
1525 foreach ( array_merge(
1526 array_keys( $this->numberedArgs ),
1527 array_keys( $this->namedArgs ) ) as $key ) {
1528 $arguments[$key] = $this->getArgument( $key );
1536 public function getNumberedArguments() {
1538 foreach ( array_keys( $this->numberedArgs ) as $key ) {
1539 $arguments[$key] = $this->getArgument( $key );
1547 public function getNamedArguments() {
1549 foreach ( array_keys( $this->namedArgs ) as $key ) {
1550 $arguments[$key] = $this->getArgument( $key );
1559 public function getNumberedArgument( $index ) {
1560 if ( !isset( $this->numberedArgs[$index] ) ) {
1563 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1564 # No trimming for unnamed arguments
1565 $this->numberedExpansionCache[$index] = $this->parent->expand(
1566 $this->numberedArgs[$index],
1567 PPFrame::STRIP_COMMENTS
1570 return $this->numberedExpansionCache[$index];
1577 public function getNamedArgument( $name ) {
1578 if ( !isset( $this->namedArgs[$name] ) ) {
1581 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1582 # Trim named arguments post-expand, for backwards compatibility
1583 $this->namedExpansionCache[$name] = trim(
1584 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1586 return $this->namedExpansionCache[$name];
1593 public function getArgument( $name ) {
1594 $text = $this->getNumberedArgument( $name );
1595 if ( $text === false ) {
1596 $text = $this->getNamedArgument( $name );
1606 public function isTemplate() {
1610 public function setVolatile( $flag = true ) {
1611 parent::setVolatile( $flag );
1612 $this->parent->setVolatile( $flag );
1615 public function setTTL( $ttl ) {
1616 parent::setTTL( $ttl );
1617 $this->parent->setTTL( $ttl );
1625 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1626 class PPCustomFrame_Hash extends PPFrame_Hash {
1627 // @codingStandardsIgnoreEnd
1631 public function __construct( $preprocessor, $args ) {
1632 parent::__construct( $preprocessor );
1633 $this->args = $args;
1636 public function __toString() {
1639 foreach ( $this->args as $name => $value ) {
1645 $s .= "\
"$name\":\"" .
1646 str_replace(
'"',
'\\"',
$value->__toString() ) .
'"';
1664 if ( !isset( $this->
args[$index] ) ) {
1667 return $this->
args[$index];
1721 $this->
store = $store;
1722 $this->index = $index;
1723 list( $this->
name, $this->rawChildren ) = $this->
store[$index];
1735 if ( !isset( $store[$index] ) ) {
1739 $descriptor = $store[$index];
1740 if ( is_string( $descriptor ) ) {
1741 $class =
'PPNode_Hash_Text';
1742 } elseif ( is_array( $descriptor ) ) {
1743 if ( $descriptor[self::NAME][0] ===
'@' ) {
1744 $class =
'PPNode_Hash_Attr';
1746 $class =
'PPNode_Hash_Tree';
1749 throw new MWException( __METHOD__.
': invalid node descriptor' );
1751 return new $class( $store, $index );
1760 for ( $node = $this->getFirstChild(); $node; $node = $node->getNextSibling() ) {
1762 $attribs .=
' ' . $node->name .
'="' . htmlspecialchars( $node->value ) .
'"';
1764 $inner .= $node->__toString();
1767 if ( $inner ===
'' ) {
1768 return "<{$this->name}$attribs/>";
1770 return "<{$this->name}$attribs>$inner</{$this->name}>";
1779 foreach ( $this->rawChildren
as $i => $child ) {
1780 $children[] = self::factory( $this->rawChildren, $i );
1793 if ( !isset( $this->rawChildren[0] ) ) {
1796 return self::factory( $this->rawChildren, 0 );
1808 return self::factory( $this->
store, $this->index + 1 );
1819 foreach ( $this->rawChildren
as $i => $child ) {
1820 if ( is_array( $child ) && $child[self::NAME] ===
$name ) {
1821 $children[] = self::factory( $this->rawChildren, $i );
1832 return $this->rawChildren;
1867 return self::splitRawArg( $this->rawChildren );
1877 foreach ( $children
as $i => $child ) {
1878 if ( !is_array( $child ) ) {
1881 if ( $child[self::NAME] ===
'name' ) {
1882 $bits[
'name'] =
new self( $children, $i );
1883 if ( isset( $child[self::CHILDREN][0][self::NAME] )
1884 && $child[self::CHILDREN][0][self::NAME] ===
'@index'
1886 $bits[
'index'] = $child[self::CHILDREN][0][self::CHILDREN][0];
1888 } elseif ( $child[self::NAME] ===
'value' ) {
1889 $bits[
'value'] =
new self( $children, $i );
1893 if ( !isset( $bits[
'name'] ) ) {
1894 throw new MWException(
'Invalid brace node passed to ' . __METHOD__ );
1896 if ( !isset( $bits[
'index'] ) ) {
1897 $bits[
'index'] =
'';
1910 return self::splitRawExt( $this->rawChildren );
1920 foreach ( $children
as $i => $child ) {
1921 if ( !is_array( $child ) ) {
1924 switch ( $child[self::NAME] ) {
1926 $bits[
'name'] =
new self( $children, $i );
1929 $bits[
'attr'] =
new self( $children, $i );
1932 $bits[
'inner'] =
new self( $children, $i );
1935 $bits[
'close'] =
new self( $children, $i );
1939 if ( !isset( $bits[
'name'] ) ) {
1940 throw new MWException(
'Invalid ext node passed to ' . __METHOD__ );
1952 if ( $this->
name !==
'h' ) {
1953 throw new MWException(
'Invalid h node passed to ' . __METHOD__ );
1955 return self::splitRawHeading( $this->rawChildren );
1965 foreach ( $children
as $i => $child ) {
1966 if ( !is_array( $child ) ) {
1969 if ( $child[self::NAME] ===
'@i' ) {
1970 $bits[
'i'] = $child[self::CHILDREN][0];
1971 } elseif ( $child[self::NAME] ===
'@level' ) {
1972 $bits[
'level'] = $child[self::CHILDREN][0];
1975 if ( !isset( $bits[
'i'] ) ) {
1976 throw new MWException(
'Invalid h node passed to ' . __METHOD__ );
1988 return self::splitRawTemplate( $this->rawChildren );
1998 $bits = [
'lineStart' =>
'' ];
1999 foreach ( $children
as $i => $child ) {
2000 if ( !is_array( $child ) ) {
2003 switch ( $child[self::NAME] ) {
2005 $bits[
'title'] =
new self( $children, $i );
2008 $parts[] =
new self( $children, $i );
2011 $bits[
'lineStart'] =
'1';
2015 if ( !isset( $bits[
'title'] ) ) {
2016 throw new MWException(
'Invalid node passed to ' . __METHOD__ );
2041 $this->
value = $store[$index];
2042 if ( !is_scalar( $this->
value ) ) {
2043 throw new MWException( __CLASS__ .
' given object instead of string' );
2045 $this->
store = $store;
2046 $this->index = $index;
2050 return htmlspecialchars( $this->
value );
2082 throw new MWException( __METHOD__ .
': not supported' );
2086 throw new MWException( __METHOD__ .
': not supported' );
2090 throw new MWException( __METHOD__ .
': not supported' );
2108 return var_export( $this,
true );
2116 return $this->
value[$i];
2140 throw new MWException( __METHOD__ .
': not supported' );
2144 throw new MWException( __METHOD__ .
': not supported' );
2148 throw new MWException( __METHOD__ .
': not supported' );
2170 $descriptor = $store[$index];
2172 throw new MWException( __METHOD__.
': invalid name in attribute descriptor' );
2176 $this->
store = $store;
2177 $this->index = $index;
2181 return "<@{$this->name}>" . htmlspecialchars( $this->
value ) .
"</@{$this->name}>";
2213 throw new MWException( __METHOD__ .
': not supported' );
2217 throw new MWException( __METHOD__ .
': not supported' );
2221 throw new MWException( __METHOD__ .
': not supported' );