196 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
198 $xmlishElements = $this->parser->getStripList();
199 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
200 $enableOnlyinclude =
false;
201 if ( $forInclusion ) {
202 $ignoredTags = [
'includeonly',
'/includeonly' ];
203 $ignoredElements = [
'noinclude' ];
204 $xmlishElements[] =
'noinclude';
205 if ( strpos( $text,
'<onlyinclude>' ) !==
false
206 && strpos( $text,
'</onlyinclude>' ) !==
false
208 $enableOnlyinclude =
true;
211 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
212 $ignoredElements = [
'includeonly' ];
213 $xmlishElements[] =
'includeonly';
215 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
218 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
222 $searchBase =
"[{<\n"; # }
228 $revText = strrev( $text );
229 $lengthText = strlen( $text );
234 $accum =& $stack->getAccum();
246 $noMoreClosingTag = [];
248 $findOnlyinclude = $enableOnlyinclude;
250 $fakeLineStart =
true;
255 if ( $findOnlyinclude ) {
257 $startPos = strpos( $text,
'<onlyinclude>', $i );
258 if ( $startPos ===
false ) {
260 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $i ) ) .
'</ignore>';
263 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
264 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i ) ) .
'</ignore>';
266 $findOnlyinclude =
false;
269 if ( $fakeLineStart ) {
270 $found =
'line-start';
273 # Find next opening brace, closing brace or pipe
274 $search = $searchBase;
275 if ( $stack->top ===
false ) {
276 $currentClosing =
'';
278 $currentClosing = $stack->top->close;
279 $search .= $currentClosing;
289 # Output literal section, advance input counter
290 $literalLength = strcspn( $text, $search, $i );
291 if ( $literalLength > 0 ) {
292 $accum .= htmlspecialchars( substr( $text, $i, $literalLength ) );
293 $i += $literalLength;
295 if ( $i >= $lengthText ) {
296 if ( $currentClosing ==
"\n" ) {
305 $curChar = $curTwoChar = $text[$i];
306 if ( ( $i + 1 ) < $lengthText ) {
307 $curTwoChar .= $text[$i + 1];
309 if ( $curChar ==
'|' ) {
311 } elseif ( $curChar ==
'=' ) {
313 } elseif ( $curChar ==
'<' ) {
315 } elseif ( $curChar ==
"\n" ) {
319 $found =
'line-start';
321 } elseif ( $curTwoChar == $currentClosing ) {
323 $curChar = $curTwoChar;
324 } elseif ( $curChar == $currentClosing ) {
326 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
327 $curChar = $curTwoChar;
329 $rule = $this->rules[$curChar];
330 } elseif ( isset( $this->rules[$curChar] ) ) {
332 $rule = $this->rules[$curChar];
334 # Some versions of PHP have a strcspn which stops on
335 # null characters; ignore these and continue.
336 # We also may get '-' and '}' characters here which
337 # don't match -{ or $currentClosing. Add these to
338 # output and continue.
339 if ( $curChar ==
'-' || $curChar ==
'}' ) {
348 if ( $found ==
'angle' ) {
351 if ( $enableOnlyinclude
352 && substr( $text, $i, strlen(
'</onlyinclude>' ) ) ==
'</onlyinclude>'
354 $findOnlyinclude =
true;
359 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
373 $endPos = strpos( $text,
'-->', $i + 4 );
374 if ( $endPos ===
false ) {
376 $inner = substr( $text, $i );
377 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
381 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
385 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
389 $comments = [ [ $wsStart, $wsEnd ] ];
390 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
391 $c = strpos( $text,
'-->', $wsEnd + 4 );
392 if ( $c ===
false ) {
395 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
396 $comments[] = [ $wsEnd + 1, $c ];
404 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
405 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
409 $wsLength = $i - $wsStart;
411 && strspn( $accum,
" \t", -$wsLength ) === $wsLength
413 $accum = substr( $accum, 0, -$wsLength );
417 foreach ( $comments as $j => $com ) {
419 $endPos = $com[1] + 1;
420 if ( $j == ( count( $comments ) - 1 ) ) {
423 $inner = substr( $text, $startPos, $endPos - $startPos );
424 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
428 $fakeLineStart =
true;
436 $part = $stack->top->getCurrentPart();
437 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
438 $part->visualEnd = $wsStart;
441 $part->commentEnd = $endPos;
444 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
445 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
450 $lowerName = strtolower( $name );
451 $attrStart = $i + strlen( $name ) + 1;
454 $tagEndPos = $noMoreGT ?
false : strpos( $text,
'>', $attrStart );
455 if ( $tagEndPos ===
false ) {
465 if ( in_array( $lowerName, $ignoredTags ) ) {
467 . htmlspecialchars( substr( $text, $i, $tagEndPos - $i + 1 ) )
474 if ( $text[$tagEndPos - 1] ==
'/' ) {
475 $attrEnd = $tagEndPos - 1;
480 $attrEnd = $tagEndPos;
483 !isset( $noMoreClosingTag[$name] ) &&
484 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
485 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
487 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
489 $close =
'<close>' . htmlspecialchars(
$matches[0][0] ) .
'</close>';
492 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
494 $inner = substr( $text, $tagEndPos + 1 );
500 $accum .= htmlspecialchars( substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
502 $noMoreClosingTag[
$name] =
true;
508 if ( in_array( $lowerName, $ignoredElements ) ) {
509 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $tagStartPos, $i - $tagStartPos ) )
515 if ( $attrEnd <= $attrStart ) {
518 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
520 $accum .=
'<name>' . htmlspecialchars( $name ) .
'</name>' .
523 '<attr>' . htmlspecialchars( $attr ) .
'</attr>';
524 if ( $inner !==
null ) {
525 $accum .=
'<inner>' . htmlspecialchars( $inner ) .
'</inner>';
527 $accum .= $close .
'</ext>';
528 } elseif ( $found ==
'line-start' ) {
531 if ( $fakeLineStart ) {
532 $fakeLineStart =
false;
538 $count = strspn( $text,
'=', $i, 6 );
539 if ( $count == 1 && $findEquals ) {
545 } elseif ( $count > 0 ) {
549 'parts' => [
new PPDPart( str_repeat(
'=', $count ) ) ],
552 $stack->push( $piece );
553 $accum =& $stack->getAccum();
554 $stackFlags = $stack->getFlags();
555 if ( isset( $stackFlags[
'findEquals'] ) ) {
556 $findEquals = $stackFlags[
'findEquals'];
558 if ( isset( $stackFlags[
'findPipe'] ) ) {
559 $findPipe = $stackFlags[
'findPipe'];
561 if ( isset( $stackFlags[
'inHeading'] ) ) {
562 $inHeading = $stackFlags[
'inHeading'];
566 } elseif ( $found ==
'line-end' ) {
567 $piece = $stack->top;
569 assert( $piece->open ===
"\n" );
570 $part = $piece->getCurrentPart();
574 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
575 $searchStart = $i - $wsLength;
576 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
579 $searchStart = $part->visualEnd;
580 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
582 $count = $piece->count;
583 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
584 if ( $equalsLength > 0 ) {
585 if ( $searchStart - $equalsLength == $piece->startPos ) {
589 $count = $equalsLength;
593 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
596 $count = min( $equalsLength, $count );
600 $element =
"<h level=\"$count\" i=\"$headingIndex\">$accum</h>";
612 $accum =& $stack->getAccum();
613 $stackFlags = $stack->getFlags();
614 if ( isset( $stackFlags[
'findEquals'] ) ) {
615 $findEquals = $stackFlags[
'findEquals'];
617 if ( isset( $stackFlags[
'findPipe'] ) ) {
618 $findPipe = $stackFlags[
'findPipe'];
620 if ( isset( $stackFlags[
'inHeading'] ) ) {
621 $inHeading = $stackFlags[
'inHeading'];
631 } elseif ( $found ==
'open' ) {
632 # count opening brace characters
633 $curLen = strlen( $curChar );
634 $count = ( $curLen > 1 ) ?
635 # allow the
final character to repeat
636 strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
637 strspn( $text, $curChar, $i );
640 $lineStart = ( $i > 0 && $text[$i - 1] ==
"\n" );
642 if ( $curChar ===
"-{" && $count > $curLen ) {
648 $rule = $this->rules[$curChar];
651 # we need to add to stack only if opening brace count is enough for one of the rules
652 if ( $count >= $rule[
'min'] ) {
653 # Add it to the stack
656 'close' => $rule[
'end'],
657 'savedPrefix' => $savedPrefix,
659 'lineStart' => $lineStart,
662 $stack->push( $piece );
663 $accum =& $stack->getAccum();
664 $stackFlags = $stack->getFlags();
665 if ( isset( $stackFlags[
'findEquals'] ) ) {
666 $findEquals = $stackFlags[
'findEquals'];
668 if ( isset( $stackFlags[
'findPipe'] ) ) {
669 $findPipe = $stackFlags[
'findPipe'];
671 if ( isset( $stackFlags[
'inHeading'] ) ) {
672 $inHeading = $stackFlags[
'inHeading'];
675 # Add literal brace(s)
676 $accum .= htmlspecialchars( $savedPrefix . str_repeat( $curChar, $count ) );
679 } elseif ( $found ==
'close' ) {
680 $piece = $stack->top;
681 # lets check if there are enough characters for closing brace
682 $maxCount = $piece->count;
683 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
684 $maxCount--; # don
't try to match closing '-
' as a '}
'
686 $curLen = strlen( $curChar );
687 $count = ( $curLen > 1 ) ? $curLen :
688 strspn( $text, $curChar, $i, $maxCount );
690 # check for maximum matching characters (if there are 5 closing
691 # characters, we will probably need only 3 - depending on the rules)
692 $rule = $this->rules[$piece->open];
693 if ( $count > $rule['max
'] ) {
694 # The specified maximum exists in the callback array, unless the caller
696 $matchingCount = $rule['max
'];
698 # Count is less than the maximum
699 # Skip any gaps in the callback array to find the true largest match
700 # Need to use array_key_exists not isset because the callback can be null
701 $matchingCount = $count;
702 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
707 if ( $matchingCount <= 0 ) {
708 # No matching element found in callback array
709 # Output a literal closing brace and continue
710 $endText = substr( $text, $i, $count );
711 $accum .= htmlspecialchars( $endText );
715 $name = $rule['names
'][$matchingCount];
716 if ( $name === null ) {
717 // No element, just literal text
718 $endText = substr( $text, $i, $matchingCount );
719 $element = $piece->breakSyntax( $matchingCount ) . $endText;
722 # Note: $parts is already XML, does not need to be encoded further
723 $parts = $piece->parts;
724 $title = $parts[0]->out;
727 # The invocation is at the start of the line if lineStart is set in
728 # the stack, and all opening brackets are used up.
729 if ( $maxCount == $matchingCount &&
730 !empty( $piece->lineStart ) &&
731 strlen( $piece->savedPrefix ) == 0 ) {
732 $attr = ' lineStart=
"1"';
737 $element = "<$name$attr>";
738 $element .= "<title>$title</title>";
740 foreach ( $parts as $part ) {
741 if ( isset( $part->eqpos ) ) {
742 $argName = substr( $part->out, 0, $part->eqpos );
743 $argValue = substr( $part->out, $part->eqpos + 1 );
744 $element .= "<part><name>$argName</name>=<value>$argValue</value></part>";
746 $element .= "<part><name index=\"$argIndex\" /><value>{$part->out}</value></part>";
750 $element .= "</$name>";
753 # Advance input pointer
754 $i += $matchingCount;
758 $accum =& $stack->getAccum();
760 # Re-add the old stack element if it still has unmatched opening characters remaining
761 if ( $matchingCount < $piece->count ) {
762 $piece->parts = [ new PPDPart ];
763 $piece->count -= $matchingCount;
764 # do we still qualify for any callback with remaining count?
765 $min = $this->rules[$piece->open]['min
'];
766 if ( $piece->count >= $min ) {
767 $stack->push( $piece );
768 $accum =& $stack->getAccum();
769 } elseif ( $piece->count == 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
770 $piece->savedPrefix = '';
773 $piece->close = $this->rules[$piece->open]['end
'];
774 $stack->push( $piece );
775 $accum =& $stack->getAccum();
777 $s = substr( $piece->open, 0, -1 );
779 substr( $piece->open, -1 ),
780 $piece->count - strlen( $s )
782 $accum .= $piece->savedPrefix . $s;
784 } elseif ( $piece->savedPrefix !== '' ) {
785 $accum .= $piece->savedPrefix;
788 $stackFlags = $stack->getFlags();
789 if ( isset( $stackFlags['findEquals
'] ) ) {
790 $findEquals = $stackFlags['findEquals
'];
792 if ( isset( $stackFlags['findPipe
'] ) ) {
793 $findPipe = $stackFlags['findPipe
'];
795 if ( isset( $stackFlags['inHeading
'] ) ) {
796 $inHeading = $stackFlags['inHeading
'];
799 # Add XML element to the enclosing accumulator
801 } elseif ( $found == 'pipe
' ) {
802 $findEquals = true; // shortcut for getFlags()
804 $accum =& $stack->getAccum();
806 } elseif ( $found == 'equals
' ) {
807 $findEquals = false; // shortcut for getFlags()
808 $stack->getCurrentPart()->eqpos = strlen( $accum );
814 # Output any remaining unclosed brackets
815 foreach ( $stack->stack as $piece ) {
816 $stack->rootAccum .= $piece->breakSyntax();
818 $stack->rootAccum .= '</
root>
';
819 $xml = $stack->rootAccum;