201 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
203 $xmlishElements = $this->parser->getStripList();
204 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
205 $enableOnlyinclude =
false;
206 if ( $forInclusion ) {
207 $ignoredTags = [
'includeonly',
'/includeonly' ];
208 $ignoredElements = [
'noinclude' ];
209 $xmlishElements[] =
'noinclude';
210 if ( strpos( $text,
'<onlyinclude>' ) !==
false
211 && strpos( $text,
'</onlyinclude>' ) !==
false
213 $enableOnlyinclude =
true;
216 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
217 $ignoredElements = [
'includeonly' ];
218 $xmlishElements[] =
'includeonly';
220 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
223 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
227 $searchBase =
"[{<\n"; # }
233 $revText = strrev( $text );
234 $lengthText = strlen( $text );
239 $accum =& $stack->getAccum();
251 $noMoreClosingTag = [];
253 $findOnlyinclude = $enableOnlyinclude;
255 $fakeLineStart =
true;
260 if ( $findOnlyinclude ) {
262 $startPos = strpos( $text,
'<onlyinclude>', $i );
263 if ( $startPos ===
false ) {
265 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $i ) ) .
'</ignore>';
268 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
269 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i ) ) .
'</ignore>';
271 $findOnlyinclude =
false;
274 if ( $fakeLineStart ) {
275 $found =
'line-start';
278 # Find next opening brace, closing brace or pipe
279 $search = $searchBase;
280 if ( $stack->top ===
false ) {
281 $currentClosing =
'';
283 $currentClosing = $stack->top->close;
284 $search .= $currentClosing;
294 # Output literal section, advance input counter
295 $literalLength = strcspn( $text, $search, $i );
296 if ( $literalLength > 0 ) {
297 $accum .= htmlspecialchars( substr( $text, $i, $literalLength ) );
298 $i += $literalLength;
300 if ( $i >= $lengthText ) {
301 if ( $currentClosing ==
"\n" ) {
310 $curChar = $curTwoChar = $text[$i];
311 if ( ( $i + 1 ) < $lengthText ) {
312 $curTwoChar .= $text[$i + 1];
314 if ( $curChar ==
'|' ) {
316 } elseif ( $curChar ==
'=' ) {
318 } elseif ( $curChar ==
'<' ) {
320 } elseif ( $curChar ==
"\n" ) {
324 $found =
'line-start';
326 } elseif ( $curTwoChar == $currentClosing ) {
328 $curChar = $curTwoChar;
329 } elseif ( $curChar == $currentClosing ) {
331 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
332 $curChar = $curTwoChar;
334 $rule = $this->rules[$curChar];
335 } elseif ( isset( $this->rules[$curChar] ) ) {
337 $rule = $this->rules[$curChar];
339 # Some versions of PHP have a strcspn which stops on
340 # null characters; ignore these and continue.
341 # We also may get '-' and '}' characters here which
342 # don't match -{ or $currentClosing. Add these to
343 # output and continue.
344 if ( $curChar ==
'-' || $curChar ==
'}' ) {
353 if ( $found ==
'angle' ) {
356 if ( $enableOnlyinclude
357 && substr( $text, $i, strlen(
'</onlyinclude>' ) ) ==
'</onlyinclude>'
359 $findOnlyinclude =
true;
364 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
378 $endPos = strpos( $text,
'-->', $i + 4 );
379 if ( $endPos ===
false ) {
381 $inner = substr( $text, $i );
382 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
386 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
390 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
394 $comments = [ [ $wsStart, $wsEnd ] ];
395 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
396 $c = strpos( $text,
'-->', $wsEnd + 4 );
397 if ( $c ===
false ) {
400 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
401 $comments[] = [ $wsEnd + 1, $c ];
409 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
410 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
414 $wsLength = $i - $wsStart;
416 && strspn( $accum,
" \t", -$wsLength ) === $wsLength
418 $accum = substr( $accum, 0, -$wsLength );
422 foreach ( $comments as $j => $com ) {
424 $endPos = $com[1] + 1;
425 if ( $j == ( count( $comments ) - 1 ) ) {
428 $inner = substr( $text, $startPos, $endPos - $startPos );
429 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
433 $fakeLineStart =
true;
441 $part = $stack->top->getCurrentPart();
442 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
443 $part->visualEnd = $wsStart;
446 $part->commentEnd = $endPos;
449 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
450 $accum .=
'<comment>' . htmlspecialchars( $inner ) .
'</comment>';
455 $lowerName = strtolower( $name );
456 $attrStart = $i + strlen( $name ) + 1;
459 $tagEndPos = $noMoreGT ? false : strpos( $text,
'>', $attrStart );
460 if ( $tagEndPos ===
false ) {
470 if ( in_array( $lowerName, $ignoredTags ) ) {
472 . htmlspecialchars( substr( $text, $i, $tagEndPos - $i + 1 ) )
479 if ( $text[$tagEndPos - 1] ==
'/' ) {
480 $attrEnd = $tagEndPos - 1;
485 $attrEnd = $tagEndPos;
488 !isset( $noMoreClosingTag[$name] ) &&
489 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
490 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
492 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
494 $close =
'<close>' . htmlspecialchars(
$matches[0][0] ) .
'</close>';
497 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
499 $inner = substr( $text, $tagEndPos + 1 );
505 $accum .= htmlspecialchars( substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
507 $noMoreClosingTag[$name] =
true;
513 if ( in_array( $lowerName, $ignoredElements ) ) {
514 $accum .=
'<ignore>' . htmlspecialchars( substr( $text, $tagStartPos, $i - $tagStartPos ) )
520 if ( $attrEnd <= $attrStart ) {
523 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
525 $accum .=
'<name>' . htmlspecialchars( $name ) .
'</name>' .
528 '<attr>' . htmlspecialchars( $attr ) .
'</attr>';
529 if ( $inner !==
null ) {
530 $accum .=
'<inner>' . htmlspecialchars( $inner ) .
'</inner>';
532 $accum .= $close .
'</ext>';
533 } elseif ( $found ==
'line-start' ) {
536 if ( $fakeLineStart ) {
537 $fakeLineStart =
false;
543 $count = strspn( $text,
'=', $i, 6 );
544 if ( $count == 1 && $findEquals ) {
550 } elseif ( $count > 0 ) {
554 'parts' => [
new PPDPart( str_repeat(
'=', $count ) ) ],
557 $stack->push( $piece );
558 $accum =& $stack->getAccum();
559 $stackFlags = $stack->getFlags();
560 if ( isset( $stackFlags[
'findEquals'] ) ) {
561 $findEquals = $stackFlags[
'findEquals'];
563 if ( isset( $stackFlags[
'findPipe'] ) ) {
564 $findPipe = $stackFlags[
'findPipe'];
566 if ( isset( $stackFlags[
'inHeading'] ) ) {
567 $inHeading = $stackFlags[
'inHeading'];
571 } elseif ( $found ==
'line-end' ) {
572 $piece = $stack->top;
576 assert( $piece->open ===
"\n" );
577 $part = $piece->getCurrentPart();
581 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
582 $searchStart = $i - $wsLength;
583 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
586 $searchStart = $part->visualEnd;
587 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
589 $count = $piece->count;
590 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
591 if ( $equalsLength > 0 ) {
592 if ( $searchStart - $equalsLength == $piece->startPos ) {
596 $count = $equalsLength;
600 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
603 $count = min( $equalsLength, $count );
607 $element =
"<h level=\"$count\" i=\"$headingIndex\">$accum</h>";
619 $accum =& $stack->getAccum();
620 $stackFlags = $stack->getFlags();
621 if ( isset( $stackFlags[
'findEquals'] ) ) {
622 $findEquals = $stackFlags[
'findEquals'];
624 if ( isset( $stackFlags[
'findPipe'] ) ) {
625 $findPipe = $stackFlags[
'findPipe'];
627 if ( isset( $stackFlags[
'inHeading'] ) ) {
628 $inHeading = $stackFlags[
'inHeading'];
638 } elseif ( $found ==
'open' ) {
639 # count opening brace characters
640 $curLen = strlen( $curChar );
641 $count = ( $curLen > 1 ) ?
642 # allow the
final character to repeat
643 strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
644 strspn( $text, $curChar, $i );
647 $lineStart = ( $i > 0 && $text[$i - 1] ==
"\n" );
649 if ( $curChar ===
"-{" && $count > $curLen ) {
655 $rule = $this->rules[$curChar];
658 # we need to add to stack only if opening brace count is enough for one of the rules
659 if ( $count >= $rule[
'min'] ) {
660 # Add it to the stack
663 'close' => $rule[
'end'],
664 'savedPrefix' => $savedPrefix,
666 'lineStart' => $lineStart,
669 $stack->push( $piece );
670 $accum =& $stack->getAccum();
671 $stackFlags = $stack->getFlags();
672 if ( isset( $stackFlags[
'findEquals'] ) ) {
673 $findEquals = $stackFlags[
'findEquals'];
675 if ( isset( $stackFlags[
'findPipe'] ) ) {
676 $findPipe = $stackFlags[
'findPipe'];
678 if ( isset( $stackFlags[
'inHeading'] ) ) {
679 $inHeading = $stackFlags[
'inHeading'];
682 # Add literal brace(s)
683 $accum .= htmlspecialchars( $savedPrefix . str_repeat( $curChar, $count ) );
686 } elseif ( $found ==
'close' ) {
687 $piece = $stack->top;
688 # lets check if there are enough characters for closing brace
689 $maxCount = $piece->count;
690 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
691 $maxCount--; # don
't try to match closing '-
' as a '}
'
693 $curLen = strlen( $curChar );
694 $count = ( $curLen > 1 ) ? $curLen :
695 strspn( $text, $curChar, $i, $maxCount );
697 # check for maximum matching characters (if there are 5 closing
698 # characters, we will probably need only 3 - depending on the rules)
699 $rule = $this->rules[$piece->open];
700 if ( $count > $rule['max
'] ) {
701 # The specified maximum exists in the callback array, unless the caller
703 $matchingCount = $rule['max
'];
705 # Count is less than the maximum
706 # Skip any gaps in the callback array to find the true largest match
707 # Need to use array_key_exists not isset because the callback can be null
708 $matchingCount = $count;
709 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
714 if ( $matchingCount <= 0 ) {
715 # No matching element found in callback array
716 # Output a literal closing brace and continue
717 $endText = substr( $text, $i, $count );
718 $accum .= htmlspecialchars( $endText );
722 $name = $rule['names
'][$matchingCount];
723 if ( $name === null ) {
724 // No element, just literal text
725 $endText = substr( $text, $i, $matchingCount );
726 $element = $piece->breakSyntax( $matchingCount ) . $endText;
729 # Note: $parts is already XML, does not need to be encoded further
730 $parts = $piece->parts;
731 $title = $parts[0]->out;
734 # The invocation is at the start of the line if lineStart is set in
735 # the stack, and all opening brackets are used up.
736 if ( $maxCount == $matchingCount &&
737 !empty( $piece->lineStart ) &&
738 strlen( $piece->savedPrefix ) == 0 ) {
739 $attr = ' lineStart=
"1"';
744 $element = "<$name$attr>";
745 $element .= "<title>$title</title>";
747 foreach ( $parts as $part ) {
748 if ( isset( $part->eqpos ) ) {
749 $argName = substr( $part->out, 0, $part->eqpos );
750 $argValue = substr( $part->out, $part->eqpos + 1 );
751 $element .= "<part><name>$argName</name>=<value>$argValue</value></part>";
753 $element .= "<part><name index=\"$argIndex\" /><value>{$part->out}</value></part>";
757 $element .= "</$name>";
760 # Advance input pointer
761 $i += $matchingCount;
765 $accum =& $stack->getAccum();
767 # Re-add the old stack element if it still has unmatched opening characters remaining
768 if ( $matchingCount < $piece->count ) {
769 $piece->parts = [ new PPDPart ];
770 $piece->count -= $matchingCount;
771 # do we still qualify for any callback with remaining count?
772 $min = $this->rules[$piece->open]['min
'];
773 if ( $piece->count >= $min ) {
774 $stack->push( $piece );
775 $accum =& $stack->getAccum();
776 } elseif ( $piece->count == 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
777 $piece->savedPrefix = '';
780 $piece->close = $this->rules[$piece->open]['end
'];
781 $stack->push( $piece );
782 $accum =& $stack->getAccum();
784 $s = substr( $piece->open, 0, -1 );
786 substr( $piece->open, -1 ),
787 $piece->count - strlen( $s )
789 $accum .= $piece->savedPrefix . $s;
791 } elseif ( $piece->savedPrefix !== '' ) {
792 $accum .= $piece->savedPrefix;
795 $stackFlags = $stack->getFlags();
796 if ( isset( $stackFlags['findEquals
'] ) ) {
797 $findEquals = $stackFlags['findEquals
'];
799 if ( isset( $stackFlags['findPipe
'] ) ) {
800 $findPipe = $stackFlags['findPipe
'];
802 if ( isset( $stackFlags['inHeading
'] ) ) {
803 $inHeading = $stackFlags['inHeading
'];
806 # Add XML element to the enclosing accumulator
808 } elseif ( $found == 'pipe
' ) {
809 $findEquals = true; // shortcut for getFlags()
811 $accum =& $stack->getAccum();
813 } elseif ( $found == 'equals
' ) {
814 $findEquals = false; // shortcut for getFlags()
815 $stack->getCurrentPart()->eqpos = strlen( $accum );
821 # Output any remaining unclosed brackets
822 foreach ( $stack->stack as $piece ) {
823 $stack->rootAccum .= $piece->breakSyntax();
825 $stack->rootAccum .= '</root>
';
826 $xml = $stack->rootAccum;