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 );