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 $com ) {
376 [ $cStartPos, $cEndPos ] = $com;
378 $inner = substr( $text, $cStartPos, $cEndPos - $cStartPos );
379 $accum[] = [
'comment', [ $inner ] ];
383 $fakeLineStart =
true;
391 $part = $stack->top->getCurrentPart();
392 if ( $part->commentEnd !== $wsStart - 1 ) {
393 $part->visualEnd = $wsStart;
396 $part->commentEnd = $endPos;
399 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
400 $accum[] = [
'comment', [ $inner ] ];
404 $attrStart = $i + strlen( $name ) + 1;
407 $tagEndPos = $noMoreGT ? false : strpos( $text,
'>', $attrStart );
408 if ( $tagEndPos ===
false ) {
412 self::addLiteral( $accum,
'<' );
417 $lowerName = strtolower( $name );
419 if ( in_array( $lowerName, $ignoredTags ) ) {
420 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
426 if ( $text[$tagEndPos - 1] ===
'/' ) {
428 $attrEnd = $tagEndPos - 1;
433 $attrEnd = $tagEndPos;
436 !isset( $noMoreClosingTag[$lowerName] ) &&
437 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
438 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
440 [ $close, $closeTagStartPos ] =
$matches[0];
441 $inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 );
442 $i = $closeTagStartPos + strlen( $close );
445 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
447 $inner = substr( $text, $tagEndPos + 1 );
453 self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
455 $noMoreClosingTag[$lowerName] =
true;
461 if ( in_array( $lowerName, $ignoredElements ) ) {
462 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
466 if ( $attrEnd <= $attrStart ) {
471 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
475 [
'name', [ $name ] ],
476 [
'attr', [ $attr ] ],
478 if ( $inner !==
null ) {
479 $children[] = [
'inner', [ $inner ] ];
481 if ( $close !==
null ) {
482 $children[] = [
'close', [ $close ] ];
484 $accum[] = [
'ext', $children ];
485 } elseif ( $found ===
'line-start' ) {
488 if ( $fakeLineStart ) {
489 $fakeLineStart =
false;
491 self::addLiteral( $accum, $curChar );
496 $count = strspn( $text,
'=', $i, min( $lengthText, 6 ) );
497 if ( $count === 1 && $findEquals ) {
502 } elseif ( $count > 0 ) {
506 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
510 $stack->push( $piece );
511 $accum =& $stack->getAccum();
512 $stackFlags = $stack->getFlags();
513 if ( isset( $stackFlags[
'findEquals'] ) ) {
514 $findEquals = $stackFlags[
'findEquals'];
516 if ( isset( $stackFlags[
'findPipe'] ) ) {
517 $findPipe = $stackFlags[
'findPipe'];
519 if ( isset( $stackFlags[
'inHeading'] ) ) {
520 $inHeading = $stackFlags[
'inHeading'];
524 } elseif ( $found ===
'line-end' ) {
525 $piece = $stack->top;
529 assert( $piece->open ===
"\n" );
530 $part = $piece->getCurrentPart();
534 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
535 $searchStart = $i - $wsLength;
536 if ( $part->commentEnd === $searchStart - 1 ) {
539 $searchStart = $part->visualEnd;
540 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
542 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
543 if ( $equalsLength > 0 ) {
544 if ( $searchStart - $equalsLength === $piece->startPos ) {
548 if ( $equalsLength < 3 ) {
551 $count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) );
554 $count = min( $equalsLength, $piece->count );
558 $element = [ [
'possible-h',
561 [
'@level', [ $count ] ],
562 [
'@i', [ $headingIndex++ ] ]
577 $accum =& $stack->getAccum();
578 $stackFlags = $stack->getFlags();
579 if ( isset( $stackFlags[
'findEquals'] ) ) {
580 $findEquals = $stackFlags[
'findEquals'];
582 if ( isset( $stackFlags[
'findPipe'] ) ) {
583 $findPipe = $stackFlags[
'findPipe'];
585 if ( isset( $stackFlags[
'inHeading'] ) ) {
586 $inHeading = $stackFlags[
'inHeading'];
590 array_splice( $accum, count( $accum ), 0, $element );
597 } elseif ( $found ===
'open' ) {
598 # count opening brace characters
599 $curLen = strlen( $curChar );
601 # allow the final character to repeat
602 ? strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1
603 : strspn( $text, $curChar, $i );
606 $lineStart = $i > 0 && $text[$i - 1] ===
"\n";
608 if ( $curChar ===
"-{" && $count > $curLen ) {
614 $rule = $this->rules[$curChar];
617 # we need to add to stack only if opening brace count is enough for one of the rules
618 if ( $count >= $rule[
'min'] ) {
619 # Add it to the stack
622 'close' => $rule[
'end'],
623 'savedPrefix' => $savedPrefix,
625 'lineStart' => $lineStart,
628 $stack->push( $piece );
629 $accum =& $stack->getAccum();
630 $stackFlags = $stack->getFlags();
631 if ( isset( $stackFlags[
'findEquals'] ) ) {
632 $findEquals = $stackFlags[
'findEquals'];
634 if ( isset( $stackFlags[
'findPipe'] ) ) {
635 $findPipe = $stackFlags[
'findPipe'];
637 if ( isset( $stackFlags[
'inHeading'] ) ) {
638 $inHeading = $stackFlags[
'inHeading'];
641 # Add literal brace(s)
642 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
645 } elseif ( $found ===
'close' ) {
647 $piece = $stack->top;
648 '@phan-var PPDStackElement_Hash $piece';
649 # lets check if there are enough characters for closing brace
650 $maxCount = $piece->count;
651 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
652 $maxCount--; # don
't try to match closing '-
' as a '}
'
654 $curLen = strlen( $curChar );
657 : strspn( $text, $curChar, $i, $maxCount );
659 # check for maximum matching characters (if there are 5 closing
660 # characters, we will probably need only 3 - depending on the rules)
661 $rule = $this->rules[$piece->open];
662 if ( $count > $rule['max
'] ) {
663 # The specified maximum exists in the callback array, unless the caller
665 $matchingCount = $rule['max
'];
667 # Count is less than the maximum
668 # Skip any gaps in the callback array to find the true largest match
669 # Need to use array_key_exists not isset because the callback can be null
670 $matchingCount = $count;
671 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
676 if ( $matchingCount <= 0 ) {
677 # No matching element found in callback array
678 # Output a literal closing brace and continue
679 $endText = substr( $text, $i, $count );
680 self::addLiteral( $accum, $endText );
684 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
685 $name = $rule['names
'][$matchingCount];
686 if ( $name === null ) {
687 // No element, just literal text
688 $endText = substr( $text, $i, $matchingCount );
689 $element = $piece->breakSyntax( $matchingCount );
690 self::addLiteral( $element, $endText );
693 $parts = $piece->parts;
694 $titleAccum = $parts[0]->out;
699 # The invocation is at the start of the line if lineStart is set in
700 # the stack, and all opening brackets are used up.
701 if ( $maxCount === $matchingCount &&
703 $piece->savedPrefix === ''
705 $children[] = [ '@lineStart
', [ 1 ] ];
707 $titleNode = [ 'title
', $titleAccum ];
708 $children[] = $titleNode;
710 foreach ( $parts as $part ) {
711 if ( isset( $part->eqpos ) ) {
712 $equalsNode = $part->out[$part->eqpos];
713 $nameNode = [ 'name
', array_slice( $part->out, 0, $part->eqpos ) ];
714 $valueNode = [ 'value
', array_slice( $part->out, $part->eqpos + 1 ) ];
715 $partNode = [ 'part
', [ $nameNode, $equalsNode, $valueNode ] ];
716 $children[] = $partNode;
718 $nameNode = [ 'name
', [ [ '@index
', [ $argIndex++ ] ] ] ];
719 $valueNode = [ 'value
', $part->out ];
720 $partNode = [ 'part
', [ $nameNode, $valueNode ] ];
721 $children[] = $partNode;
724 $element = [ [ $name, $children ] ];
727 # Advance input pointer
728 $i += $matchingCount;
732 $accum =& $stack->getAccum();
734 # Re-add the old stack element if it still has unmatched opening characters remaining
735 if ( $matchingCount < $piece->count ) {
736 $piece->parts = [ new PPDPart_Hash ];
737 $piece->count -= $matchingCount;
738 # do we still qualify for any callback with remaining count?
739 $min = $this->rules[$piece->open]['min
'];
740 if ( $piece->count >= $min ) {
741 $stack->push( $piece );
742 $accum =& $stack->getAccum();
743 } elseif ( $piece->count === 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
744 $piece->savedPrefix = '';
747 $piece->close = $this->rules[$piece->open]['end
'];
748 $stack->push( $piece );
749 $accum =& $stack->getAccum();
751 $s = substr( $piece->open, 0, -1 );
753 substr( $piece->open, -1 ),
754 $piece->count - strlen( $s )
756 self::addLiteral( $accum, $piece->savedPrefix . $s );
758 } elseif ( $piece->savedPrefix !== '' ) {
759 self::addLiteral( $accum, $piece->savedPrefix );
762 $stackFlags = $stack->getFlags();
763 if ( isset( $stackFlags['findEquals
'] ) ) {
764 $findEquals = $stackFlags['findEquals
'];
766 if ( isset( $stackFlags['findPipe
'] ) ) {
767 $findPipe = $stackFlags['findPipe
'];
769 if ( isset( $stackFlags['inHeading
'] ) ) {
770 $inHeading = $stackFlags['inHeading
'];
773 # Add XML element to the enclosing accumulator
774 array_splice( $accum, count( $accum ), 0, $element );
775 } elseif ( $found === 'pipe
' ) {
776 $findEquals = true; // shortcut for getFlags()
778 $accum =& $stack->getAccum();
780 } elseif ( $found === 'equals
' ) {
781 $findEquals = false; // shortcut for getFlags()
782 $accum[] = [ 'equals
', [ '=
' ] ];
783 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
788 # Output any remaining unclosed brackets
789 foreach ( $stack->stack as $piece ) {
790 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
793 # Enable top-level headings
794 foreach ( $stack->rootAccum as &$node ) {
795 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
796 $node[PPNode_Hash_Tree::NAME] = 'h
';
800 return [ [ 'root
', $stack->rootAccum ] ];
803 private static function addLiteral( array &$accum, $text ) {
804 $n = count( $accum );
805 if ( $n && is_string( $accum[$n - 1] ) ) {
806 $accum[$n - 1] .= $text;