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