40 private ?
string $configurationHash =
null;
56 $this->cacheThreshold = $options[
'cacheThreshold'] ??
false;
81 foreach ( $values as $k => $val ) {
83 $store = [ [
'part', [
84 [
'name', [ [
'@index', [ $k ] ] ] ],
85 [
'value', [ strval( $val ) ] ],
88 $store = [ [
'part', [
89 [
'name', [ strval( $k ) ] ],
91 [
'value', [ strval( $val ) ] ],
101 private function getConfigurationHash(): string {
102 if ( $this->configurationHash ) {
103 return $this->configurationHash;
107 'tags' => $this->parser->getTags(),
109 sort( $configs[
'tags'] );
110 $this->configurationHash = sha1( json_encode( $configs ) );
112 return $this->configurationHash;
117 if ( $this->disableLangConversion ) {
119 $flags |= self::DOM_LANG_CONVERSION_DISABLED;
122 $domTreeArray =
null;
124 $this->cacheThreshold !==
false &&
125 strlen( $text ) >= $this->cacheThreshold &&
126 ( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED
128 $domTreeJson = $this->wanCache->getWithSetCallback(
129 $this->wanCache->makeKey(
130 'preprocess-hash', $this->getConfigurationHash(), sha1( $text ), $flags ),
131 $this->wanCache::TTL_DAY,
132 function () use ( $text, $flags, &$domTreeArray ) {
133 $domTreeArray = $this->buildDomTreeArrayFromText( $text, $flags );
137 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
140 [
'version' => self::CACHE_VERSION,
'segmentable' => true ]
142 $domTreeArray ??= json_decode( $domTreeJson );
145 $domTreeArray ??= $this->buildDomTreeArrayFromText( $text, $flags );
155 private function buildDomTreeArrayFromText( $text, $flags ) {
156 $textStartsInSOLState = $flags & self::START_IN_SOL_STATE;
157 $forInclusion = ( $flags & self::DOM_FOR_INCLUSION );
158 $langConversionDisabled = ( $flags & self::DOM_LANG_CONVERSION_DISABLED );
160 $xmlishElements = $this->parser->getStripList();
161 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
162 $enableOnlyinclude =
false;
163 if ( $forInclusion ) {
164 $ignoredTags = [
'includeonly',
'/includeonly' ];
165 $ignoredElements = [
'noinclude' ];
166 $xmlishElements[] =
'noinclude';
167 if ( str_contains( $text,
'<onlyinclude>' )
168 && str_contains( $text,
'</onlyinclude>' )
170 $enableOnlyinclude =
true;
173 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
174 $ignoredElements = [
'includeonly' ];
175 $xmlishElements[] =
'includeonly';
177 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
180 $elementsRegex =
"~(?:$xmlishRegex)(?=\s|\/>|>)|!--~iA";
182 $stack =
new PPDStack_Hash;
184 $searchBase =
"[{<\n";
185 if ( !$langConversionDisabled ) {
190 $revText = strrev( $text );
191 $lengthText = strlen( $text );
196 $accum =& $stack->getAccum();
207 $noMoreClosingTag = [];
209 $findOnlyinclude = $enableOnlyinclude;
211 $fakeLineStart =
true;
214 if ( $findOnlyinclude ) {
216 $startPos = strpos( $text,
'<onlyinclude>', $i );
217 if ( $startPos ===
false ) {
219 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
222 $tagEndPos = $startPos + 13;
223 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
225 $findOnlyinclude =
false;
228 if ( $fakeLineStart ) {
229 $found =
'line-start';
232 # Find next opening brace, closing brace or pipe
233 $search = $searchBase;
234 if ( $stack->top ===
false ) {
235 $currentClosing =
'';
237 $currentClosing = $stack->top->close;
238 $search .= $currentClosing;
248 # Output literal section, advance input counter
249 $literalLength = strcspn( $text, $search, $i );
250 if ( $literalLength > 0 ) {
251 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
252 $i += $literalLength;
254 if ( $i >= $lengthText ) {
255 if ( $currentClosing ===
"\n" ) {
264 $curChar = $curTwoChar = $text[$i];
265 if ( $i + 1 < $lengthText ) {
266 $curTwoChar .= $text[$i + 1];
268 if ( $curChar ===
'|' ) {
270 } elseif ( $curChar ===
'=' ) {
272 } elseif ( $curChar ===
'<' ) {
274 } elseif ( $curChar ===
"\n" ) {
278 $found =
'line-start';
280 } elseif ( $curTwoChar === $currentClosing ) {
282 $curChar = $curTwoChar;
283 } elseif ( $curChar === $currentClosing ) {
285 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
286 $curChar = $curTwoChar;
288 $rule = $this->rules[$curChar];
289 } elseif ( isset( $this->rules[$curChar] ) ) {
291 $rule = $this->rules[$curChar];
293 # Some versions of PHP have a strcspn which stops on
294 # null characters; ignore these and continue.
295 # We also may get '-' and '}' characters here which
296 # don't match -{ or $currentClosing. Add these to
297 # output and continue.
298 if ( $curChar ===
'-' || $curChar ===
'}' ) {
299 self::addLiteral( $accum, $curChar );
307 if ( $found ===
'angle' ) {
309 if ( $enableOnlyinclude
310 && substr_compare( $text,
'</onlyinclude>', $i, 14 ) === 0
312 $findOnlyinclude =
true;
317 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
319 self::addLiteral( $accum,
'<' );
325 if ( $name ===
'!--' ) {
332 $endPos = strpos( $text,
'-->', $i + 4 );
333 if ( $endPos ===
false ) {
335 $inner = substr( $text, $i );
336 $accum[] = [
'comment', [ $inner ] ];
341 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
344 $wsEnd = $endPos + 3;
346 $wsEnd += strspn( $text,
" \t", $wsEnd );
349 $comments = [ [ $wsStart, $wsEnd ] ];
350 while ( substr_compare( $text,
'<!--', $wsEnd, 4 ) === 0 ) {
351 $c = strpos( $text,
'-->', $wsEnd + 4 );
352 if ( $c ===
false ) {
357 $c += strspn( $text,
" \t", $c );
358 $comments[] = [ $wsEnd, $c ];
366 if ( $wsStart > 0 && substr_compare( $text,
"\n", $wsStart - 1, 1 ) === 0
367 && substr_compare( $text,
"\n", $wsEnd, 1 ) === 0
370 $wsLength = $i - $wsStart;
371 $endIndex = count( $accum ) - 1;
375 && is_string( $accum[$endIndex] )
376 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
378 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
383 [ $startPos, $endPos ] = array_pop( $comments );
384 foreach ( $comments as [ $cStartPos, $cEndPos ] ) {
386 $inner = substr( $text, $cStartPos, $cEndPos - $cStartPos );
387 $accum[] = [
'comment', [ $inner ] ];
391 $fakeLineStart =
true;
399 $part = $stack->top->getCurrentPart();
400 if ( $part->commentEnd !== $wsStart - 1 ) {
401 $part->visualEnd = $wsStart;
404 $part->commentEnd = $endPos;
407 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
408 $accum[] = [
'comment', [ $inner ] ];
412 $attrStart = $i + strlen( $name ) + 1;
415 $tagEndPos = $noMoreGT ? false : strpos( $text,
'>', $attrStart );
416 if ( $tagEndPos ===
false ) {
420 self::addLiteral( $accum,
'<' );
425 $lowerName = strtolower( $name );
427 if ( in_array( $lowerName, $ignoredTags ) ) {
428 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
434 if ( $text[$tagEndPos - 1] ===
'/' ) {
436 $attrEnd = $tagEndPos - 1;
441 $attrEnd = $tagEndPos;
444 !isset( $noMoreClosingTag[$lowerName] ) &&
445 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
446 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
448 [ $close, $closeTagStartPos ] =
$matches[0];
449 $inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 );
450 $i = $closeTagStartPos + strlen( $close );
453 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
455 $inner = substr( $text, $tagEndPos + 1 );
461 self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
463 $noMoreClosingTag[$lowerName] =
true;
469 if ( in_array( $lowerName, $ignoredElements ) ) {
470 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
474 if ( $attrEnd <= $attrStart ) {
479 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
483 [
'name', [ $name ] ],
484 [
'attr', [ $attr ] ],
486 if ( $inner !==
null ) {
487 $children[] = [
'inner', [ $inner ] ];
489 if ( $close !==
null ) {
490 $children[] = [
'close', [ $close ] ];
492 $accum[] = [
'ext', $children ];
493 } elseif ( $found ===
'line-start' ) {
496 if ( $fakeLineStart ) {
497 $fakeLineStart =
false;
499 self::addLiteral( $accum, $curChar );
504 $count = strspn( $text,
'=', $i, min( $lengthText, 6 ) );
505 if ( $count === 1 && $findEquals ) {
510 } elseif ( $count > 0 ) {
514 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
518 $stack->push( $piece );
519 $accum =& $stack->getAccum();
520 $stackFlags = $stack->getFlags();
521 if ( isset( $stackFlags[
'findEquals'] ) ) {
522 $findEquals = $stackFlags[
'findEquals'];
524 if ( isset( $stackFlags[
'findPipe'] ) ) {
525 $findPipe = $stackFlags[
'findPipe'];
527 if ( isset( $stackFlags[
'inHeading'] ) ) {
528 $inHeading = $stackFlags[
'inHeading'];
532 } elseif ( $found ===
'line-end' ) {
533 $piece = $stack->top;
537 assert( $piece->open ===
"\n" );
538 $part = $piece->getCurrentPart();
542 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
543 $searchStart = $i - $wsLength;
544 if ( $part->commentEnd === $searchStart - 1 ) {
547 $searchStart = $part->visualEnd;
548 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
550 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
551 if ( $equalsLength > 0 ) {
552 if ( $searchStart - $equalsLength === $piece->startPos ) {
556 if ( $equalsLength < 3 ) {
559 $count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) );
562 $count = min( $equalsLength, $piece->count );
566 $element = [ [
'possible-h',
569 [
'@level', [ $count ] ],
570 [
'@i', [ $headingIndex++ ] ]
585 $accum =& $stack->getAccum();
586 $stackFlags = $stack->getFlags();
587 if ( isset( $stackFlags[
'findEquals'] ) ) {
588 $findEquals = $stackFlags[
'findEquals'];
590 if ( isset( $stackFlags[
'findPipe'] ) ) {
591 $findPipe = $stackFlags[
'findPipe'];
593 if ( isset( $stackFlags[
'inHeading'] ) ) {
594 $inHeading = $stackFlags[
'inHeading'];
598 array_splice( $accum, count( $accum ), 0, $element );
605 } elseif ( $found ===
'open' ) {
606 # count opening brace characters
607 $curLen = strlen( $curChar );
609 # allow the final character to repeat
610 ? strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1
611 : strspn( $text, $curChar, $i );
614 $lineStart = ( $i === 0 ) ? $textStartsInSOLState : ( $text[$i - 1] ===
"\n" );
616 if ( $curChar ===
"-{" && $count > $curLen ) {
622 $rule = $this->rules[$curChar];
625 # we need to add to stack only if opening brace count is enough for one of the rules
626 if ( $count >= $rule[
'min'] ) {
627 # Add it to the stack
630 'close' => $rule[
'end'],
631 'savedPrefix' => $savedPrefix,
633 'lineStart' => $lineStart,
636 $stack->push( $piece );
637 $accum =& $stack->getAccum();
638 $stackFlags = $stack->getFlags();
639 if ( isset( $stackFlags[
'findEquals'] ) ) {
640 $findEquals = $stackFlags[
'findEquals'];
642 if ( isset( $stackFlags[
'findPipe'] ) ) {
643 $findPipe = $stackFlags[
'findPipe'];
645 if ( isset( $stackFlags[
'inHeading'] ) ) {
646 $inHeading = $stackFlags[
'inHeading'];
649 # Add literal brace(s)
650 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
653 } elseif ( $found ===
'close' ) {
655 $piece = $stack->top;
656 '@phan-var PPDStackElement_Hash $piece';
657 # lets check if there are enough characters for closing brace
658 $maxCount = $piece->count;
659 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
660 $maxCount--; # don
't try to match closing '-
' as a '}
'
662 $curLen = strlen( $curChar );
665 : strspn( $text, $curChar, $i, $maxCount );
667 # check for maximum matching characters (if there are 5 closing
668 # characters, we will probably need only 3 - depending on the rules)
669 $rule = $this->rules[$piece->open];
670 if ( $count > $rule['max
'] ) {
671 # The specified maximum exists in the callback array, unless the caller
673 $matchingCount = $rule['max
'];
675 # Count is less than the maximum
676 # Skip any gaps in the callback array to find the true largest match
677 # Need to use array_key_exists not isset because the callback can be null
678 $matchingCount = $count;
679 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
684 if ( $matchingCount <= 0 ) {
685 # No matching element found in callback array
686 # Output a literal closing brace and continue
687 $endText = substr( $text, $i, $count );
688 self::addLiteral( $accum, $endText );
692 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
693 $name = $rule['names
'][$matchingCount];
694 if ( $name === null ) {
695 // No element, just literal text
696 $endText = substr( $text, $i, $matchingCount );
697 $element = $piece->breakSyntax( $matchingCount );
698 self::addLiteral( $element, $endText );
701 $parts = $piece->parts;
702 $titleAccum = $parts[0]->out;
707 # The invocation is at the start of the line if lineStart is set in
708 # the stack, and all opening brackets are used up.
709 if ( $maxCount === $matchingCount &&
711 $piece->savedPrefix === ''
713 $children[] = [ '@lineStart
', [ 1 ] ];
715 $titleNode = [ 'title
', $titleAccum ];
716 $children[] = $titleNode;
718 foreach ( $parts as $part ) {
719 if ( $part->eqpos !== null ) {
720 $equalsNode = $part->out[$part->eqpos];
721 $nameNode = [ 'name
', array_slice( $part->out, 0, $part->eqpos ) ];
722 $valueNode = [ 'value
', array_slice( $part->out, $part->eqpos + 1 ) ];
723 $partNode = [ 'part
', [ $nameNode, $equalsNode, $valueNode ] ];
724 $children[] = $partNode;
726 $nameNode = [ 'name
', [ [ '@index
', [ $argIndex++ ] ] ] ];
727 $valueNode = [ 'value
', $part->out ];
728 $partNode = [ 'part
', [ $nameNode, $valueNode ] ];
729 $children[] = $partNode;
732 $element = [ [ $name, $children ] ];
735 # Advance input pointer
736 $i += $matchingCount;
740 $accum =& $stack->getAccum();
742 # Re-add the old stack element if it still has unmatched opening characters remaining
743 if ( $matchingCount < $piece->count ) {
744 $piece->parts = [ new PPDPart_Hash ];
745 $piece->count -= $matchingCount;
746 # do we still qualify for any callback with remaining count?
747 $min = $this->rules[$piece->open]['min
'];
748 if ( $piece->count >= $min ) {
749 $stack->push( $piece );
750 $accum =& $stack->getAccum();
751 } elseif ( $piece->count === 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
752 $piece->savedPrefix = '';
755 $piece->close = $this->rules[$piece->open]['end
'];
756 $stack->push( $piece );
757 $accum =& $stack->getAccum();
759 $s = substr( $piece->open, 0, -1 );
761 substr( $piece->open, -1 ),
762 $piece->count - strlen( $s )
764 self::addLiteral( $accum, $piece->savedPrefix . $s );
766 } elseif ( $piece->savedPrefix !== '' ) {
767 self::addLiteral( $accum, $piece->savedPrefix );
770 $stackFlags = $stack->getFlags();
771 if ( isset( $stackFlags['findEquals
'] ) ) {
772 $findEquals = $stackFlags['findEquals
'];
774 if ( isset( $stackFlags['findPipe
'] ) ) {
775 $findPipe = $stackFlags['findPipe
'];
777 if ( isset( $stackFlags['inHeading
'] ) ) {
778 $inHeading = $stackFlags['inHeading
'];
781 # Add XML element to the enclosing accumulator
782 array_splice( $accum, count( $accum ), 0, $element );
783 } elseif ( $found === 'pipe
' ) {
784 $findEquals = true; // shortcut for getFlags()
786 $accum =& $stack->getAccum();
788 } elseif ( $found === 'equals
' ) {
789 $findEquals = false; // shortcut for getFlags()
790 $accum[] = [ 'equals
', [ '=
' ] ];
791 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
796 # Output any remaining unclosed brackets
797 foreach ( $stack->stack as $piece ) {
798 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
801 # Enable top-level headings
802 foreach ( $stack->rootAccum as &$node ) {
803 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
804 $node[PPNode_Hash_Tree::NAME] = 'h
';
808 return [ [ 'root
', $stack->rootAccum ] ];
811 private static function addLiteral( array &$accum, string $text ) {
812 $n = count( $accum );
813 if ( $n && is_string( $accum[$n - 1] ) ) {
814 $accum[$n - 1] .= $text;