68 $this->cacheThreshold = $options[
'cacheThreshold'] ??
false;
93 foreach ( $values as $k => $val ) {
95 $store = [ [
'part', [
96 [
'name', [ [
'@index', [ $k ] ] ] ],
97 [
'value', [ strval( $val ) ] ],
100 $store = [ [
'part', [
101 [
'name', [ strval( $k ) ] ],
103 [
'value', [ strval( $val ) ] ],
114 if ( $this->disableLangConversion ) {
119 $domTreeArray =
null;
121 $this->cacheThreshold !==
false &&
122 strlen( $text ) >= $this->cacheThreshold &&
123 ( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED
125 $domTreeJson = $this->wanCache->getWithSetCallback(
126 $this->wanCache->makeKey(
'preprocess-hash', sha1( $text ), $flags ),
127 $this->wanCache::TTL_DAY,
128 function () use ( $text, $flags, &$domTreeArray ) {
129 $domTreeArray = $this->buildDomTreeArrayFromText( $text, $flags );
133 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
138 $domTreeArray ??= json_decode( $domTreeJson );
141 $domTreeArray ??= $this->buildDomTreeArrayFromText( $text, $flags );
151 private function buildDomTreeArrayFromText( $text, $flags ) {
155 $xmlishElements = $this->parser->getStripList();
156 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
157 $enableOnlyinclude =
false;
158 if ( $forInclusion ) {
159 $ignoredTags = [
'includeonly',
'/includeonly' ];
160 $ignoredElements = [
'noinclude' ];
161 $xmlishElements[] =
'noinclude';
162 if ( str_contains( $text,
'<onlyinclude>' )
163 && str_contains( $text,
'</onlyinclude>' )
165 $enableOnlyinclude =
true;
168 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
169 $ignoredElements = [
'includeonly' ];
170 $xmlishElements[] =
'includeonly';
172 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
175 $elementsRegex =
"~(?:$xmlishRegex)(?=\s|\/>|>)|!--~iA";
177 $stack =
new PPDStack_Hash;
179 $searchBase =
"[{<\n";
180 if ( !$langConversionDisabled ) {
185 $revText = strrev( $text );
186 $lengthText = strlen( $text );
191 $accum =& $stack->getAccum();
202 $noMoreClosingTag = [];
204 $findOnlyinclude = $enableOnlyinclude;
206 $fakeLineStart =
true;
209 if ( $findOnlyinclude ) {
211 $startPos = strpos( $text,
'<onlyinclude>', $i );
212 if ( $startPos ===
false ) {
214 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
217 $tagEndPos = $startPos + 13;
218 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
220 $findOnlyinclude =
false;
223 if ( $fakeLineStart ) {
224 $found =
'line-start';
227 # Find next opening brace, closing brace or pipe
228 $search = $searchBase;
229 if ( $stack->top ===
false ) {
230 $currentClosing =
'';
232 $currentClosing = $stack->top->close;
233 $search .= $currentClosing;
243 # Output literal section, advance input counter
244 $literalLength = strcspn( $text, $search, $i );
245 if ( $literalLength > 0 ) {
246 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
247 $i += $literalLength;
249 if ( $i >= $lengthText ) {
250 if ( $currentClosing ===
"\n" ) {
259 $curChar = $curTwoChar = $text[$i];
260 if ( $i + 1 < $lengthText ) {
261 $curTwoChar .= $text[$i + 1];
263 if ( $curChar ===
'|' ) {
265 } elseif ( $curChar ===
'=' ) {
267 } elseif ( $curChar ===
'<' ) {
269 } elseif ( $curChar ===
"\n" ) {
273 $found =
'line-start';
275 } elseif ( $curTwoChar === $currentClosing ) {
277 $curChar = $curTwoChar;
278 } elseif ( $curChar === $currentClosing ) {
280 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
281 $curChar = $curTwoChar;
283 $rule = $this->rules[$curChar];
284 } elseif ( isset( $this->rules[$curChar] ) ) {
286 $rule = $this->rules[$curChar];
288 # Some versions of PHP have a strcspn which stops on
289 # null characters; ignore these and continue.
290 # We also may get '-' and '}' characters here which
291 # don't match -{ or $currentClosing. Add these to
292 # output and continue.
293 if ( $curChar ===
'-' || $curChar ===
'}' ) {
294 self::addLiteral( $accum, $curChar );
302 if ( $found ===
'angle' ) {
304 if ( $enableOnlyinclude
305 && substr_compare( $text,
'</onlyinclude>', $i, 14 ) === 0
307 $findOnlyinclude =
true;
312 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
314 self::addLiteral( $accum,
'<' );
320 if ( $name ===
'!--' ) {
327 $endPos = strpos( $text,
'-->', $i + 4 );
328 if ( $endPos ===
false ) {
330 $inner = substr( $text, $i );
331 $accum[] = [
'comment', [ $inner ] ];
336 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
339 $wsEnd = $endPos + 3;
341 $wsEnd += strspn( $text,
" \t", $wsEnd );
344 $comments = [ [ $wsStart, $wsEnd ] ];
345 while ( substr_compare( $text,
'<!--', $wsEnd, 4 ) === 0 ) {
346 $c = strpos( $text,
'-->', $wsEnd + 4 );
347 if ( $c ===
false ) {
352 $c += strspn( $text,
" \t", $c );
353 $comments[] = [ $wsEnd, $c ];
361 if ( $wsStart > 0 && substr_compare( $text,
"\n", $wsStart - 1, 1 ) === 0
362 && substr_compare( $text,
"\n", $wsEnd, 1 ) === 0
365 $wsLength = $i - $wsStart;
366 $endIndex = count( $accum ) - 1;
370 && is_string( $accum[$endIndex] )
371 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
373 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
378 [ $startPos, $endPos ] = array_pop( $comments );
379 foreach ( $comments as [ $cStartPos, $cEndPos ] ) {
381 $inner = substr( $text, $cStartPos, $cEndPos - $cStartPos );
382 $accum[] = [
'comment', [ $inner ] ];
386 $fakeLineStart =
true;
394 $part = $stack->top->getCurrentPart();
395 if ( $part->commentEnd !== $wsStart - 1 ) {
396 $part->visualEnd = $wsStart;
399 $part->commentEnd = $endPos;
402 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
403 $accum[] = [
'comment', [ $inner ] ];
407 $attrStart = $i + strlen( $name ) + 1;
410 $tagEndPos = $noMoreGT ? false : strpos( $text,
'>', $attrStart );
411 if ( $tagEndPos ===
false ) {
415 self::addLiteral( $accum,
'<' );
420 $lowerName = strtolower( $name );
422 if ( in_array( $lowerName, $ignoredTags ) ) {
423 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
429 if ( $text[$tagEndPos - 1] ===
'/' ) {
431 $attrEnd = $tagEndPos - 1;
436 $attrEnd = $tagEndPos;
439 !isset( $noMoreClosingTag[$lowerName] ) &&
440 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
441 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
443 [ $close, $closeTagStartPos ] =
$matches[0];
444 $inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 );
445 $i = $closeTagStartPos + strlen( $close );
448 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
450 $inner = substr( $text, $tagEndPos + 1 );
456 self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
458 $noMoreClosingTag[$lowerName] =
true;
464 if ( in_array( $lowerName, $ignoredElements ) ) {
465 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
469 if ( $attrEnd <= $attrStart ) {
474 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
478 [
'name', [ $name ] ],
479 [
'attr', [ $attr ] ],
481 if ( $inner !==
null ) {
482 $children[] = [
'inner', [ $inner ] ];
484 if ( $close !==
null ) {
485 $children[] = [
'close', [ $close ] ];
487 $accum[] = [
'ext', $children ];
488 } elseif ( $found ===
'line-start' ) {
491 if ( $fakeLineStart ) {
492 $fakeLineStart =
false;
494 self::addLiteral( $accum, $curChar );
499 $count = strspn( $text,
'=', $i, min( $lengthText, 6 ) );
500 if ( $count === 1 && $findEquals ) {
505 } elseif ( $count > 0 ) {
509 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
513 $stack->push( $piece );
514 $accum =& $stack->getAccum();
515 $stackFlags = $stack->getFlags();
516 if ( isset( $stackFlags[
'findEquals'] ) ) {
517 $findEquals = $stackFlags[
'findEquals'];
519 if ( isset( $stackFlags[
'findPipe'] ) ) {
520 $findPipe = $stackFlags[
'findPipe'];
522 if ( isset( $stackFlags[
'inHeading'] ) ) {
523 $inHeading = $stackFlags[
'inHeading'];
527 } elseif ( $found ===
'line-end' ) {
528 $piece = $stack->top;
532 assert( $piece->open ===
"\n" );
533 $part = $piece->getCurrentPart();
537 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
538 $searchStart = $i - $wsLength;
539 if ( $part->commentEnd === $searchStart - 1 ) {
542 $searchStart = $part->visualEnd;
543 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
545 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
546 if ( $equalsLength > 0 ) {
547 if ( $searchStart - $equalsLength === $piece->startPos ) {
551 if ( $equalsLength < 3 ) {
554 $count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) );
557 $count = min( $equalsLength, $piece->count );
561 $element = [ [
'possible-h',
564 [
'@level', [ $count ] ],
565 [
'@i', [ $headingIndex++ ] ]
580 $accum =& $stack->getAccum();
581 $stackFlags = $stack->getFlags();
582 if ( isset( $stackFlags[
'findEquals'] ) ) {
583 $findEquals = $stackFlags[
'findEquals'];
585 if ( isset( $stackFlags[
'findPipe'] ) ) {
586 $findPipe = $stackFlags[
'findPipe'];
588 if ( isset( $stackFlags[
'inHeading'] ) ) {
589 $inHeading = $stackFlags[
'inHeading'];
593 array_splice( $accum, count( $accum ), 0, $element );
600 } elseif ( $found ===
'open' ) {
601 # count opening brace characters
602 $curLen = strlen( $curChar );
604 # allow the final character to repeat
605 ? strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1
606 : strspn( $text, $curChar, $i );
609 $lineStart = $i > 0 && $text[$i - 1] ===
"\n";
611 if ( $curChar ===
"-{" && $count > $curLen ) {
617 $rule = $this->rules[$curChar];
620 # we need to add to stack only if opening brace count is enough for one of the rules
621 if ( $count >= $rule[
'min'] ) {
622 # Add it to the stack
625 'close' => $rule[
'end'],
626 'savedPrefix' => $savedPrefix,
628 'lineStart' => $lineStart,
631 $stack->push( $piece );
632 $accum =& $stack->getAccum();
633 $stackFlags = $stack->getFlags();
634 if ( isset( $stackFlags[
'findEquals'] ) ) {
635 $findEquals = $stackFlags[
'findEquals'];
637 if ( isset( $stackFlags[
'findPipe'] ) ) {
638 $findPipe = $stackFlags[
'findPipe'];
640 if ( isset( $stackFlags[
'inHeading'] ) ) {
641 $inHeading = $stackFlags[
'inHeading'];
644 # Add literal brace(s)
645 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
648 } elseif ( $found ===
'close' ) {
650 $piece = $stack->top;
651 '@phan-var PPDStackElement_Hash $piece';
652 # lets check if there are enough characters for closing brace
653 $maxCount = $piece->count;
654 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
655 $maxCount--; # don
't try to match closing '-
' as a '}
'
657 $curLen = strlen( $curChar );
660 : strspn( $text, $curChar, $i, $maxCount );
662 # check for maximum matching characters (if there are 5 closing
663 # characters, we will probably need only 3 - depending on the rules)
664 $rule = $this->rules[$piece->open];
665 if ( $count > $rule['max
'] ) {
666 # The specified maximum exists in the callback array, unless the caller
668 $matchingCount = $rule['max
'];
670 # Count is less than the maximum
671 # Skip any gaps in the callback array to find the true largest match
672 # Need to use array_key_exists not isset because the callback can be null
673 $matchingCount = $count;
674 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
679 if ( $matchingCount <= 0 ) {
680 # No matching element found in callback array
681 # Output a literal closing brace and continue
682 $endText = substr( $text, $i, $count );
683 self::addLiteral( $accum, $endText );
687 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
688 $name = $rule['names
'][$matchingCount];
689 if ( $name === null ) {
690 // No element, just literal text
691 $endText = substr( $text, $i, $matchingCount );
692 $element = $piece->breakSyntax( $matchingCount );
693 self::addLiteral( $element, $endText );
696 $parts = $piece->parts;
697 $titleAccum = $parts[0]->out;
702 # The invocation is at the start of the line if lineStart is set in
703 # the stack, and all opening brackets are used up.
704 if ( $maxCount === $matchingCount &&
706 $piece->savedPrefix === ''
708 $children[] = [ '@lineStart
', [ 1 ] ];
710 $titleNode = [ 'title
', $titleAccum ];
711 $children[] = $titleNode;
713 foreach ( $parts as $part ) {
714 if ( isset( $part->eqpos ) ) {
715 $equalsNode = $part->out[$part->eqpos];
716 $nameNode = [ 'name
', array_slice( $part->out, 0, $part->eqpos ) ];
717 $valueNode = [ 'value
', array_slice( $part->out, $part->eqpos + 1 ) ];
718 $partNode = [ 'part
', [ $nameNode, $equalsNode, $valueNode ] ];
719 $children[] = $partNode;
721 $nameNode = [ 'name
', [ [ '@index
', [ $argIndex++ ] ] ] ];
722 $valueNode = [ 'value
', $part->out ];
723 $partNode = [ 'part
', [ $nameNode, $valueNode ] ];
724 $children[] = $partNode;
727 $element = [ [ $name, $children ] ];
730 # Advance input pointer
731 $i += $matchingCount;
735 $accum =& $stack->getAccum();
737 # Re-add the old stack element if it still has unmatched opening characters remaining
738 if ( $matchingCount < $piece->count ) {
739 $piece->parts = [ new PPDPart_Hash ];
740 $piece->count -= $matchingCount;
741 # do we still qualify for any callback with remaining count?
742 $min = $this->rules[$piece->open]['min
'];
743 if ( $piece->count >= $min ) {
744 $stack->push( $piece );
745 $accum =& $stack->getAccum();
746 } elseif ( $piece->count === 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
747 $piece->savedPrefix = '';
750 $piece->close = $this->rules[$piece->open]['end
'];
751 $stack->push( $piece );
752 $accum =& $stack->getAccum();
754 $s = substr( $piece->open, 0, -1 );
756 substr( $piece->open, -1 ),
757 $piece->count - strlen( $s )
759 self::addLiteral( $accum, $piece->savedPrefix . $s );
761 } elseif ( $piece->savedPrefix !== '' ) {
762 self::addLiteral( $accum, $piece->savedPrefix );
765 $stackFlags = $stack->getFlags();
766 if ( isset( $stackFlags['findEquals
'] ) ) {
767 $findEquals = $stackFlags['findEquals
'];
769 if ( isset( $stackFlags['findPipe
'] ) ) {
770 $findPipe = $stackFlags['findPipe
'];
772 if ( isset( $stackFlags['inHeading
'] ) ) {
773 $inHeading = $stackFlags['inHeading
'];
776 # Add XML element to the enclosing accumulator
777 array_splice( $accum, count( $accum ), 0, $element );
778 } elseif ( $found === 'pipe
' ) {
779 $findEquals = true; // shortcut for getFlags()
781 $accum =& $stack->getAccum();
783 } elseif ( $found === 'equals
' ) {
784 $findEquals = false; // shortcut for getFlags()
785 $accum[] = [ 'equals
', [ '=
' ] ];
786 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
791 # Output any remaining unclosed brackets
792 foreach ( $stack->stack as $piece ) {
793 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
796 # Enable top-level headings
797 foreach ( $stack->rootAccum as &$node ) {
798 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
799 $node[PPNode_Hash_Tree::NAME] = 'h
';
803 return [ [ 'root
', $stack->rootAccum ] ];
806 private static function addLiteral( array &$accum, $text ) {
807 $n = count( $accum );
808 if ( $n && is_string( $accum[$n - 1] ) ) {
809 $accum[$n - 1] .= $text;