64 $this->cacheThreshold = $options[
'cacheThreshold'] ??
false;
89 foreach ( $values as $k => $val ) {
91 $store = [ [
'part', [
92 [
'name', [ [
'@index', [ $k ] ] ] ],
93 [
'value', [ strval( $val ) ] ],
96 $store = [ [
'part', [
97 [
'name', [ strval( $k ) ] ],
99 [
'value', [ strval( $val ) ] ],
110 if ( $this->disableLangConversion ) {
116 $this->cacheThreshold !==
false &&
117 strlen( $text ) >= $this->cacheThreshold &&
118 ( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED
120 $domTreeArray = $this->wanCache->getWithSetCallback(
121 $this->wanCache->makeKey(
'preprocess-hash', sha1( $text ), $flags ),
122 $this->wanCache::TTL_DAY,
123 function () use ( $text, $flags ) {
144 $xmlishElements = $this->parser->getStripList();
145 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
146 $enableOnlyinclude =
false;
147 if ( $forInclusion ) {
148 $ignoredTags = [
'includeonly',
'/includeonly' ];
149 $ignoredElements = [
'noinclude' ];
150 $xmlishElements[] =
'noinclude';
151 if ( strpos( $text,
'<onlyinclude>' ) !==
false
152 && strpos( $text,
'</onlyinclude>' ) !== false
154 $enableOnlyinclude =
true;
157 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
158 $ignoredElements = [
'includeonly' ];
159 $xmlishElements[] =
'includeonly';
161 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
164 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
168 $searchBase =
"[{<\n";
169 if ( !$langConversionDisabled ) {
174 $revText = strrev( $text );
175 $lengthText = strlen( $text );
180 $accum =& $stack->getAccum();
191 $noMoreClosingTag = [];
193 $findOnlyinclude = $enableOnlyinclude;
195 $fakeLineStart =
true;
198 if ( $findOnlyinclude ) {
200 $startPos = strpos( $text,
'<onlyinclude>', $i );
201 if ( $startPos ===
false ) {
203 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
206 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
207 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
209 $findOnlyinclude =
false;
212 if ( $fakeLineStart ) {
213 $found =
'line-start';
216 # Find next opening brace, closing brace or pipe
217 $search = $searchBase;
218 if ( $stack->top ===
false ) {
219 $currentClosing =
'';
221 $currentClosing = $stack->top->close;
222 $search .= $currentClosing;
232 # Output literal section, advance input counter
233 $literalLength = strcspn( $text, $search, $i );
234 if ( $literalLength > 0 ) {
236 $i += $literalLength;
238 if ( $i >= $lengthText ) {
239 if ( $currentClosing ==
"\n" ) {
248 $curChar = $curTwoChar = $text[$i];
249 if ( ( $i + 1 ) < $lengthText ) {
250 $curTwoChar .= $text[$i + 1];
252 if ( $curChar ==
'|' ) {
254 } elseif ( $curChar ==
'=' ) {
256 } elseif ( $curChar ==
'<' ) {
258 } elseif ( $curChar ==
"\n" ) {
262 $found =
'line-start';
264 } elseif ( $curTwoChar == $currentClosing ) {
266 $curChar = $curTwoChar;
267 } elseif ( $curChar == $currentClosing ) {
269 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
270 $curChar = $curTwoChar;
272 $rule = $this->rules[$curChar];
273 } elseif ( isset( $this->rules[$curChar] ) ) {
275 $rule = $this->rules[$curChar];
277 # Some versions of PHP have a strcspn which stops on
278 # null characters; ignore these and continue.
279 # We also may get '-' and '}' characters here which
280 # don't match -{ or $currentClosing. Add these to
281 # output and continue.
282 if ( $curChar ==
'-' || $curChar ==
'}' ) {
291 if ( $found ==
'angle' ) {
294 if ( $enableOnlyinclude
295 && substr( $text, $i, strlen(
'</onlyinclude>' ) ) ==
'</onlyinclude>'
297 $findOnlyinclude =
true;
302 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
316 $endPos = strpos( $text,
'-->', $i + 4 );
317 if ( $endPos ===
false ) {
319 $inner = substr( $text, $i );
320 $accum[] = [
'comment', [ $inner ] ];
324 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
328 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
332 $comments = [ [ $wsStart, $wsEnd ] ];
333 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
334 $c = strpos( $text,
'-->', $wsEnd + 4 );
335 if ( $c ===
false ) {
338 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
339 $comments[] = [ $wsEnd + 1, $c ];
347 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
348 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
351 $wsLength = $i - $wsStart;
352 $endIndex = count( $accum ) - 1;
357 && is_string( $accum[$endIndex] )
358 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
360 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
364 foreach ( $comments as $j => $com ) {
366 $endPos = $com[1] + 1;
367 if ( $j == ( count( $comments ) - 1 ) ) {
370 $inner = substr( $text, $startPos, $endPos - $startPos );
371 $accum[] = [
'comment', [ $inner ] ];
375 $fakeLineStart =
true;
383 $part = $stack->top->getCurrentPart();
384 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
385 $part->visualEnd = $wsStart;
388 $part->commentEnd = $endPos;
391 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
392 $accum[] = [
'comment', [ $inner ] ];
397 $lowerName = strtolower( $name );
398 $attrStart = $i + strlen( $name ) + 1;
401 $tagEndPos = $noMoreGT ? false : strpos( $text,
'>', $attrStart );
402 if ( $tagEndPos ===
false ) {
412 if ( in_array( $lowerName, $ignoredTags ) ) {
413 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
419 if ( $text[$tagEndPos - 1] ==
'/' ) {
421 $attrEnd = $tagEndPos - 1;
426 $attrEnd = $tagEndPos;
429 !isset( $noMoreClosingTag[$name] ) &&
430 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
431 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
433 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
438 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
440 $inner = substr( $text, $tagEndPos + 1 );
447 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
449 $noMoreClosingTag[$name] =
true;
455 if ( in_array( $lowerName, $ignoredElements ) ) {
456 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
460 if ( $attrEnd <= $attrStart ) {
465 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
469 [
'name', [ $name ] ],
470 [
'attr', [ $attr ] ] ];
471 if ( $inner !==
null ) {
472 $children[] = [
'inner', [ $inner ] ];
474 if ( $close !==
null ) {
475 $children[] = [
'close', [ $close ] ];
477 $accum[] = [
'ext', $children ];
478 } elseif ( $found ==
'line-start' ) {
481 if ( $fakeLineStart ) {
482 $fakeLineStart =
false;
489 $count = strspn( $text,
'=', $i, min( strlen( $text ), 6 ) );
490 if ( $count == 1 && $findEquals ) {
495 } elseif ( $count > 0 ) {
499 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
502 $stack->push( $piece );
503 $accum =& $stack->getAccum();
504 $stackFlags = $stack->getFlags();
505 if ( isset( $stackFlags[
'findEquals'] ) ) {
506 $findEquals = $stackFlags[
'findEquals'];
508 if ( isset( $stackFlags[
'findPipe'] ) ) {
509 $findPipe = $stackFlags[
'findPipe'];
511 if ( isset( $stackFlags[
'inHeading'] ) ) {
512 $inHeading = $stackFlags[
'inHeading'];
516 } elseif ( $found ==
'line-end' ) {
517 $piece = $stack->top;
521 assert( $piece->open ===
"\n" );
522 $part = $piece->getCurrentPart();
526 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
527 $searchStart = $i - $wsLength;
528 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
531 $searchStart = $part->visualEnd;
532 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
534 $count = $piece->count;
535 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
536 if ( $equalsLength > 0 ) {
537 if ( $searchStart - $equalsLength == $piece->startPos ) {
541 $count = $equalsLength;
545 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
548 $count = min( $equalsLength, $count );
552 $element = [ [
'possible-h',
555 [
'@level', [ $count ] ],
556 [
'@i', [ $headingIndex++ ] ]
571 $accum =& $stack->getAccum();
572 $stackFlags = $stack->getFlags();
573 if ( isset( $stackFlags[
'findEquals'] ) ) {
574 $findEquals = $stackFlags[
'findEquals'];
576 if ( isset( $stackFlags[
'findPipe'] ) ) {
577 $findPipe = $stackFlags[
'findPipe'];
579 if ( isset( $stackFlags[
'inHeading'] ) ) {
580 $inHeading = $stackFlags[
'inHeading'];
584 array_splice( $accum, count( $accum ), 0, $element );
591 } elseif ( $found ==
'open' ) {
592 # count opening brace characters
593 $curLen = strlen( $curChar );
594 $count = ( $curLen > 1 ) ?
595 # allow the
final character to repeat
596 strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
597 strspn( $text, $curChar, $i );
600 $lineStart = ( $i > 0 && $text[$i - 1] ==
"\n" );
602 if ( $curChar ===
"-{" && $count > $curLen ) {
608 $rule = $this->rules[$curChar];
611 # we need to add to stack only if opening brace count is enough for one of the rules
612 if ( $count >= $rule[
'min'] ) {
613 # Add it to the stack
616 'close' => $rule[
'end'],
617 'savedPrefix' => $savedPrefix,
619 'lineStart' => $lineStart,
622 $stack->push( $piece );
623 $accum =& $stack->getAccum();
624 $stackFlags = $stack->getFlags();
625 if ( isset( $stackFlags[
'findEquals'] ) ) {
626 $findEquals = $stackFlags[
'findEquals'];
628 if ( isset( $stackFlags[
'findPipe'] ) ) {
629 $findPipe = $stackFlags[
'findPipe'];
631 if ( isset( $stackFlags[
'inHeading'] ) ) {
632 $inHeading = $stackFlags[
'inHeading'];
635 # Add literal brace(s)
639 } elseif ( $found ==
'close' ) {
641 $piece = $stack->top;
642 '@phan-var PPDStackElement_Hash $piece';
643 # lets check if there are enough characters for closing brace
644 $maxCount = $piece->count;
645 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
646 $maxCount--; # don
't try to match closing '-
' as a '}
'
648 $curLen = strlen( $curChar );
649 $count = ( $curLen > 1 ) ? $curLen :
650 strspn( $text, $curChar, $i, $maxCount );
652 # check for maximum matching characters (if there are 5 closing
653 # characters, we will probably need only 3 - depending on the rules)
654 $rule = $this->rules[$piece->open];
655 if ( $count > $rule['max
'] ) {
656 # The specified maximum exists in the callback array, unless the caller
658 $matchingCount = $rule['max
'];
660 # Count is less than the maximum
661 # Skip any gaps in the callback array to find the true largest match
662 # Need to use array_key_exists not isset because the callback can be null
663 $matchingCount = $count;
664 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
669 if ( $matchingCount <= 0 ) {
670 # No matching element found in callback array
671 # Output a literal closing brace and continue
672 $endText = substr( $text, $i, $count );
673 self::addLiteral( $accum, $endText );
677 $name = $rule['names
'][$matchingCount];
678 if ( $name === null ) {
679 // No element, just literal text
680 $endText = substr( $text, $i, $matchingCount );
681 $element = $piece->breakSyntax( $matchingCount );
682 self::addLiteral( $element, $endText );
685 $parts = $piece->parts;
686 $titleAccum = $parts[0]->out;
691 # The invocation is at the start of the line if lineStart is set in
692 # the stack, and all opening brackets are used up.
693 if ( $maxCount == $matchingCount &&
694 !empty( $piece->lineStart ) &&
695 strlen( $piece->savedPrefix ) == 0 ) {
696 $children[] = [ '@lineStart
', [ 1 ] ];
698 $titleNode = [ 'title
', $titleAccum ];
699 $children[] = $titleNode;
701 foreach ( $parts as $part ) {
702 if ( isset( $part->eqpos ) ) {
703 $equalsNode = $part->out[$part->eqpos];
704 $nameNode = [ 'name
', array_slice( $part->out, 0, $part->eqpos ) ];
705 $valueNode = [ 'value
', array_slice( $part->out, $part->eqpos + 1 ) ];
706 $partNode = [ 'part
', [ $nameNode, $equalsNode, $valueNode ] ];
707 $children[] = $partNode;
709 $nameNode = [ 'name
', [ [ '@index
', [ $argIndex++ ] ] ] ];
710 $valueNode = [ 'value
', $part->out ];
711 $partNode = [ 'part
', [ $nameNode, $valueNode ] ];
712 $children[] = $partNode;
715 $element = [ [ $name, $children ] ];
718 # Advance input pointer
719 $i += $matchingCount;
723 $accum =& $stack->getAccum();
725 # Re-add the old stack element if it still has unmatched opening characters remaining
726 if ( $matchingCount < $piece->count ) {
727 $piece->parts = [ new PPDPart_Hash ];
728 $piece->count -= $matchingCount;
729 # do we still qualify for any callback with remaining count?
730 $min = $this->rules[$piece->open]['min
'];
731 if ( $piece->count >= $min ) {
732 $stack->push( $piece );
733 $accum =& $stack->getAccum();
734 } elseif ( $piece->count == 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
735 $piece->savedPrefix = '';
738 $piece->close = $this->rules[$piece->open]['end
'];
739 $stack->push( $piece );
740 $accum =& $stack->getAccum();
742 $s = substr( $piece->open, 0, -1 );
744 substr( $piece->open, -1 ),
745 $piece->count - strlen( $s )
747 self::addLiteral( $accum, $piece->savedPrefix . $s );
749 } elseif ( $piece->savedPrefix !== '' ) {
750 self::addLiteral( $accum, $piece->savedPrefix );
753 $stackFlags = $stack->getFlags();
754 if ( isset( $stackFlags['findEquals
'] ) ) {
755 $findEquals = $stackFlags['findEquals
'];
757 if ( isset( $stackFlags['findPipe
'] ) ) {
758 $findPipe = $stackFlags['findPipe
'];
760 if ( isset( $stackFlags['inHeading
'] ) ) {
761 $inHeading = $stackFlags['inHeading
'];
764 # Add XML element to the enclosing accumulator
765 array_splice( $accum, count( $accum ), 0, $element );
766 } elseif ( $found == 'pipe
' ) {
767 $findEquals = true; // shortcut for getFlags()
769 $accum =& $stack->getAccum();
771 } elseif ( $found == 'equals
' ) {
772 $findEquals = false; // shortcut for getFlags()
773 $accum[] = [ 'equals
', [ '=
' ] ];
774 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
779 # Output any remaining unclosed brackets
780 foreach ( $stack->stack as $piece ) {
781 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
784 # Enable top-level headings
785 foreach ( $stack->rootAccum as &$node ) {
786 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
787 $node[PPNode_Hash_Tree::NAME] = 'h
';
791 return [ [ 'root
', $stack->rootAccum ] ];
794 private static function addLiteral( array &$accum, $text ) {
795 $n = count( $accum );
796 if ( $n && is_string( $accum[$n - 1] ) ) {
797 $accum[$n - 1] .= $text;