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 ) {
115 $domTreeArray =
null;
117 $this->cacheThreshold !==
false &&
118 strlen( $text ) >= $this->cacheThreshold &&
119 ( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED
121 $domTreeJson = $this->wanCache->getWithSetCallback(
122 $this->wanCache->makeKey(
'preprocess-hash', sha1( $text ), $flags ),
123 $this->wanCache::TTL_DAY,
124 function () use ( $text, $flags, &$domTreeArray ) {
125 $domTreeArray = $this->buildDomTreeArrayFromText( $text, $flags );
129 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
134 $domTreeArray ??= json_decode( $domTreeJson );
137 $domTreeArray ??= $this->buildDomTreeArrayFromText( $text, $flags );
147 private function buildDomTreeArrayFromText( $text, $flags ) {
151 $xmlishElements = $this->parser->getStripList();
152 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
153 $enableOnlyinclude =
false;
154 if ( $forInclusion ) {
155 $ignoredTags = [
'includeonly',
'/includeonly' ];
156 $ignoredElements = [
'noinclude' ];
157 $xmlishElements[] =
'noinclude';
158 if ( str_contains( $text,
'<onlyinclude>' )
159 && str_contains( $text,
'</onlyinclude>' )
161 $enableOnlyinclude =
true;
164 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
165 $ignoredElements = [
'includeonly' ];
166 $xmlishElements[] =
'includeonly';
168 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
171 $elementsRegex =
"~(?:$xmlishRegex)(?=\s|\/>|>)|!--~iA";
175 $searchBase =
"[{<\n";
176 if ( !$langConversionDisabled ) {
181 $revText = strrev( $text );
182 $lengthText = strlen( $text );
187 $accum =& $stack->getAccum();
198 $noMoreClosingTag = [];
200 $findOnlyinclude = $enableOnlyinclude;
202 $fakeLineStart =
true;
205 if ( $findOnlyinclude ) {
207 $startPos = strpos( $text,
'<onlyinclude>', $i );
208 if ( $startPos ===
false ) {
210 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
213 $tagEndPos = $startPos + 13;
214 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
216 $findOnlyinclude =
false;
219 if ( $fakeLineStart ) {
220 $found =
'line-start';
223 # Find next opening brace, closing brace or pipe
224 $search = $searchBase;
225 if ( $stack->top ===
false ) {
226 $currentClosing =
'';
228 $currentClosing = $stack->top->close;
229 $search .= $currentClosing;
239 # Output literal section, advance input counter
240 $literalLength = strcspn( $text, $search, $i );
241 if ( $literalLength > 0 ) {
242 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
243 $i += $literalLength;
245 if ( $i >= $lengthText ) {
246 if ( $currentClosing ===
"\n" ) {
255 $curChar = $curTwoChar = $text[$i];
256 if ( $i + 1 < $lengthText ) {
257 $curTwoChar .= $text[$i + 1];
259 if ( $curChar ===
'|' ) {
261 } elseif ( $curChar ===
'=' ) {
263 } elseif ( $curChar ===
'<' ) {
265 } elseif ( $curChar ===
"\n" ) {
269 $found =
'line-start';
271 } elseif ( $curTwoChar === $currentClosing ) {
273 $curChar = $curTwoChar;
274 } elseif ( $curChar === $currentClosing ) {
276 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
277 $curChar = $curTwoChar;
279 $rule = $this->rules[$curChar];
280 } elseif ( isset( $this->rules[$curChar] ) ) {
282 $rule = $this->rules[$curChar];
284 # Some versions of PHP have a strcspn which stops on
285 # null characters; ignore these and continue.
286 # We also may get '-' and '}' characters here which
287 # don't match -{ or $currentClosing. Add these to
288 # output and continue.
289 if ( $curChar ===
'-' || $curChar ===
'}' ) {
290 self::addLiteral( $accum, $curChar );
298 if ( $found ===
'angle' ) {
300 if ( $enableOnlyinclude
301 && substr_compare( $text,
'</onlyinclude>', $i, 14 ) === 0
303 $findOnlyinclude =
true;
308 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
310 self::addLiteral( $accum,
'<' );
316 if ( $name ===
'!--' ) {
323 $endPos = strpos( $text,
'-->', $i + 4 );
324 if ( $endPos ===
false ) {
326 $inner = substr( $text, $i );
327 $accum[] = [
'comment', [ $inner ] ];
332 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
335 $wsEnd = $endPos + 3;
337 $wsEnd += strspn( $text,
" \t", $wsEnd );
340 $comments = [ [ $wsStart, $wsEnd ] ];
341 while ( substr_compare( $text,
'<!--', $wsEnd, 4 ) === 0 ) {
342 $c = strpos( $text,
'-->', $wsEnd + 4 );
343 if ( $c ===
false ) {
348 $c += strspn( $text,
" \t", $c );
349 $comments[] = [ $wsEnd, $c ];
357 if ( $wsStart > 0 && substr_compare( $text,
"\n", $wsStart - 1, 1 ) === 0
358 && substr_compare( $text,
"\n", $wsEnd, 1 ) === 0
361 $wsLength = $i - $wsStart;
362 $endIndex = count( $accum ) - 1;
366 && is_string( $accum[$endIndex] )
367 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
369 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
374 [ $startPos, $endPos ] = array_pop( $comments );
375 foreach ( $comments as [ $cStartPos, $cEndPos ] ) {
377 $inner = substr( $text, $cStartPos, $cEndPos - $cStartPos );
378 $accum[] = [
'comment', [ $inner ] ];
382 $fakeLineStart =
true;
390 $part = $stack->top->getCurrentPart();
391 if ( $part->commentEnd !== $wsStart - 1 ) {
392 $part->visualEnd = $wsStart;
395 $part->commentEnd = $endPos;
398 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
399 $accum[] = [
'comment', [ $inner ] ];
403 $attrStart = $i + strlen( $name ) + 1;
406 $tagEndPos = $noMoreGT ? false : strpos( $text,
'>', $attrStart );
407 if ( $tagEndPos ===
false ) {
411 self::addLiteral( $accum,
'<' );
416 $lowerName = strtolower( $name );
418 if ( in_array( $lowerName, $ignoredTags ) ) {
419 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
425 if ( $text[$tagEndPos - 1] ===
'/' ) {
427 $attrEnd = $tagEndPos - 1;
432 $attrEnd = $tagEndPos;
435 !isset( $noMoreClosingTag[$lowerName] ) &&
436 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
437 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
439 [ $close, $closeTagStartPos ] =
$matches[0];
440 $inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 );
441 $i = $closeTagStartPos + strlen( $close );
444 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
446 $inner = substr( $text, $tagEndPos + 1 );
452 self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
454 $noMoreClosingTag[$lowerName] =
true;
460 if ( in_array( $lowerName, $ignoredElements ) ) {
461 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
465 if ( $attrEnd <= $attrStart ) {
470 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
474 [
'name', [ $name ] ],
475 [
'attr', [ $attr ] ],
477 if ( $inner !==
null ) {
478 $children[] = [
'inner', [ $inner ] ];
480 if ( $close !==
null ) {
481 $children[] = [
'close', [ $close ] ];
483 $accum[] = [
'ext', $children ];
484 } elseif ( $found ===
'line-start' ) {
487 if ( $fakeLineStart ) {
488 $fakeLineStart =
false;
490 self::addLiteral( $accum, $curChar );
495 $count = strspn( $text,
'=', $i, min( $lengthText, 6 ) );
496 if ( $count === 1 && $findEquals ) {
501 } elseif ( $count > 0 ) {
505 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
509 $stack->push( $piece );
510 $accum =& $stack->getAccum();
511 $stackFlags = $stack->getFlags();
512 if ( isset( $stackFlags[
'findEquals'] ) ) {
513 $findEquals = $stackFlags[
'findEquals'];
515 if ( isset( $stackFlags[
'findPipe'] ) ) {
516 $findPipe = $stackFlags[
'findPipe'];
518 if ( isset( $stackFlags[
'inHeading'] ) ) {
519 $inHeading = $stackFlags[
'inHeading'];
523 } elseif ( $found ===
'line-end' ) {
524 $piece = $stack->top;
528 assert( $piece->open ===
"\n" );
529 $part = $piece->getCurrentPart();
533 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
534 $searchStart = $i - $wsLength;
535 if ( $part->commentEnd === $searchStart - 1 ) {
538 $searchStart = $part->visualEnd;
539 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
541 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
542 if ( $equalsLength > 0 ) {
543 if ( $searchStart - $equalsLength === $piece->startPos ) {
547 if ( $equalsLength < 3 ) {
550 $count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) );
553 $count = min( $equalsLength, $piece->count );
557 $element = [ [
'possible-h',
560 [
'@level', [ $count ] ],
561 [
'@i', [ $headingIndex++ ] ]
576 $accum =& $stack->getAccum();
577 $stackFlags = $stack->getFlags();
578 if ( isset( $stackFlags[
'findEquals'] ) ) {
579 $findEquals = $stackFlags[
'findEquals'];
581 if ( isset( $stackFlags[
'findPipe'] ) ) {
582 $findPipe = $stackFlags[
'findPipe'];
584 if ( isset( $stackFlags[
'inHeading'] ) ) {
585 $inHeading = $stackFlags[
'inHeading'];
589 array_splice( $accum, count( $accum ), 0, $element );
596 } elseif ( $found ===
'open' ) {
597 # count opening brace characters
598 $curLen = strlen( $curChar );
600 # allow the final character to repeat
601 ? strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1
602 : strspn( $text, $curChar, $i );
605 $lineStart = $i > 0 && $text[$i - 1] ===
"\n";
607 if ( $curChar ===
"-{" && $count > $curLen ) {
613 $rule = $this->rules[$curChar];
616 # we need to add to stack only if opening brace count is enough for one of the rules
617 if ( $count >= $rule[
'min'] ) {
618 # Add it to the stack
621 'close' => $rule[
'end'],
622 'savedPrefix' => $savedPrefix,
624 'lineStart' => $lineStart,
627 $stack->push( $piece );
628 $accum =& $stack->getAccum();
629 $stackFlags = $stack->getFlags();
630 if ( isset( $stackFlags[
'findEquals'] ) ) {
631 $findEquals = $stackFlags[
'findEquals'];
633 if ( isset( $stackFlags[
'findPipe'] ) ) {
634 $findPipe = $stackFlags[
'findPipe'];
636 if ( isset( $stackFlags[
'inHeading'] ) ) {
637 $inHeading = $stackFlags[
'inHeading'];
640 # Add literal brace(s)
641 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
644 } elseif ( $found ===
'close' ) {
646 $piece = $stack->top;
647 '@phan-var PPDStackElement_Hash $piece';
648 # lets check if there are enough characters for closing brace
649 $maxCount = $piece->count;
650 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
651 $maxCount--; # don
't try to match closing '-
' as a '}
'
653 $curLen = strlen( $curChar );
656 : strspn( $text, $curChar, $i, $maxCount );
658 # check for maximum matching characters (if there are 5 closing
659 # characters, we will probably need only 3 - depending on the rules)
660 $rule = $this->rules[$piece->open];
661 if ( $count > $rule['max
'] ) {
662 # The specified maximum exists in the callback array, unless the caller
664 $matchingCount = $rule['max
'];
666 # Count is less than the maximum
667 # Skip any gaps in the callback array to find the true largest match
668 # Need to use array_key_exists not isset because the callback can be null
669 $matchingCount = $count;
670 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
675 if ( $matchingCount <= 0 ) {
676 # No matching element found in callback array
677 # Output a literal closing brace and continue
678 $endText = substr( $text, $i, $count );
679 self::addLiteral( $accum, $endText );
683 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
684 $name = $rule['names
'][$matchingCount];
685 if ( $name === null ) {
686 // No element, just literal text
687 $endText = substr( $text, $i, $matchingCount );
688 $element = $piece->breakSyntax( $matchingCount );
689 self::addLiteral( $element, $endText );
692 $parts = $piece->parts;
693 $titleAccum = $parts[0]->out;
698 # The invocation is at the start of the line if lineStart is set in
699 # the stack, and all opening brackets are used up.
700 if ( $maxCount === $matchingCount &&
702 $piece->savedPrefix === ''
704 $children[] = [ '@lineStart
', [ 1 ] ];
706 $titleNode = [ 'title
', $titleAccum ];
707 $children[] = $titleNode;
709 foreach ( $parts as $part ) {
710 if ( isset( $part->eqpos ) ) {
711 $equalsNode = $part->out[$part->eqpos];
712 $nameNode = [ 'name
', array_slice( $part->out, 0, $part->eqpos ) ];
713 $valueNode = [ 'value
', array_slice( $part->out, $part->eqpos + 1 ) ];
714 $partNode = [ 'part
', [ $nameNode, $equalsNode, $valueNode ] ];
715 $children[] = $partNode;
717 $nameNode = [ 'name
', [ [ '@index
', [ $argIndex++ ] ] ] ];
718 $valueNode = [ 'value
', $part->out ];
719 $partNode = [ 'part
', [ $nameNode, $valueNode ] ];
720 $children[] = $partNode;
723 $element = [ [ $name, $children ] ];
726 # Advance input pointer
727 $i += $matchingCount;
731 $accum =& $stack->getAccum();
733 # Re-add the old stack element if it still has unmatched opening characters remaining
734 if ( $matchingCount < $piece->count ) {
735 $piece->parts = [ new PPDPart_Hash ];
736 $piece->count -= $matchingCount;
737 # do we still qualify for any callback with remaining count?
738 $min = $this->rules[$piece->open]['min
'];
739 if ( $piece->count >= $min ) {
740 $stack->push( $piece );
741 $accum =& $stack->getAccum();
742 } elseif ( $piece->count === 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
743 $piece->savedPrefix = '';
746 $piece->close = $this->rules[$piece->open]['end
'];
747 $stack->push( $piece );
748 $accum =& $stack->getAccum();
750 $s = substr( $piece->open, 0, -1 );
752 substr( $piece->open, -1 ),
753 $piece->count - strlen( $s )
755 self::addLiteral( $accum, $piece->savedPrefix . $s );
757 } elseif ( $piece->savedPrefix !== '' ) {
758 self::addLiteral( $accum, $piece->savedPrefix );
761 $stackFlags = $stack->getFlags();
762 if ( isset( $stackFlags['findEquals
'] ) ) {
763 $findEquals = $stackFlags['findEquals
'];
765 if ( isset( $stackFlags['findPipe
'] ) ) {
766 $findPipe = $stackFlags['findPipe
'];
768 if ( isset( $stackFlags['inHeading
'] ) ) {
769 $inHeading = $stackFlags['inHeading
'];
772 # Add XML element to the enclosing accumulator
773 array_splice( $accum, count( $accum ), 0, $element );
774 } elseif ( $found === 'pipe
' ) {
775 $findEquals = true; // shortcut for getFlags()
777 $accum =& $stack->getAccum();
779 } elseif ( $found === 'equals
' ) {
780 $findEquals = false; // shortcut for getFlags()
781 $accum[] = [ 'equals
', [ '=
' ] ];
782 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
787 # Output any remaining unclosed brackets
788 foreach ( $stack->stack as $piece ) {
789 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
792 # Enable top-level headings
793 foreach ( $stack->rootAccum as &$node ) {
794 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
795 $node[PPNode_Hash_Tree::NAME] = 'h
';
799 return [ [ 'root
', $stack->rootAccum ] ];
802 private static function addLiteral( array &$accum, $text ) {
803 $n = count( $accum );
804 if ( $n && is_string( $accum[$n - 1] ) ) {
805 $accum[$n - 1] .= $text;