64 parent::__construct( $parser,
$wanCache, $options );
66 $this->cacheThreshold = $options[
'cacheThreshold'] ??
false;
91 foreach ( $values as $k => $val ) {
93 $store = [ [
'part', [
94 [
'name', [ [
'@index', [ $k ] ] ] ],
95 [
'value', [ strval( $val ) ] ],
98 $store = [ [
'part', [
99 [
'name', [ strval( $k ) ] ],
101 [
'value', [ strval( $val ) ] ],
112 if ( $this->disableLangConversion ) {
114 $flags |= self::DOM_LANG_CONVERSION_DISABLED;
117 $domTreeArray =
null;
119 $this->cacheThreshold !==
false &&
120 strlen( $text ) >= $this->cacheThreshold &&
121 ( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED
123 $domTreeJson = $this->wanCache->getWithSetCallback(
124 $this->wanCache->makeKey(
'preprocess-hash', sha1( $text ), $flags ),
125 $this->wanCache::TTL_DAY,
126 function () use ( $text, $flags, &$domTreeArray ) {
127 $domTreeArray = $this->buildDomTreeArrayFromText( $text, $flags );
131 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
134 [
'version' => self::CACHE_VERSION,
'segmentable' => true ]
136 $domTreeArray ??= json_decode( $domTreeJson );
139 $domTreeArray ??= $this->buildDomTreeArrayFromText( $text, $flags );
149 private function buildDomTreeArrayFromText( $text, $flags ) {
150 $forInclusion = ( $flags & self::DOM_FOR_INCLUSION );
151 $langConversionDisabled = ( $flags & self::DOM_LANG_CONVERSION_DISABLED );
153 $xmlishElements = $this->parser->getStripList();
154 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
155 $enableOnlyinclude =
false;
156 if ( $forInclusion ) {
157 $ignoredTags = [
'includeonly',
'/includeonly' ];
158 $ignoredElements = [
'noinclude' ];
159 $xmlishElements[] =
'noinclude';
160 if ( str_contains( $text,
'<onlyinclude>' )
161 && str_contains( $text,
'</onlyinclude>' )
163 $enableOnlyinclude =
true;
166 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
167 $ignoredElements = [
'includeonly' ];
168 $xmlishElements[] =
'includeonly';
170 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
173 $elementsRegex =
"~(?:$xmlishRegex)(?=\s|\/>|>)|!--~iA";
177 $searchBase =
"[{<\n";
178 if ( !$langConversionDisabled ) {
183 $revText = strrev( $text );
184 $lengthText = strlen( $text );
189 $accum =& $stack->getAccum();
200 $noMoreClosingTag = [];
202 $findOnlyinclude = $enableOnlyinclude;
204 $fakeLineStart =
true;
207 if ( $findOnlyinclude ) {
209 $startPos = strpos( $text,
'<onlyinclude>', $i );
210 if ( $startPos ===
false ) {
212 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
215 $tagEndPos = $startPos + 13;
216 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
218 $findOnlyinclude =
false;
221 if ( $fakeLineStart ) {
222 $found =
'line-start';
225 # Find next opening brace, closing brace or pipe
226 $search = $searchBase;
227 if ( $stack->top ===
false ) {
228 $currentClosing =
'';
230 $currentClosing = $stack->top->close;
231 $search .= $currentClosing;
241 # Output literal section, advance input counter
242 $literalLength = strcspn( $text, $search, $i );
243 if ( $literalLength > 0 ) {
244 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
245 $i += $literalLength;
247 if ( $i >= $lengthText ) {
248 if ( $currentClosing ===
"\n" ) {
257 $curChar = $curTwoChar = $text[$i];
258 if ( $i + 1 < $lengthText ) {
259 $curTwoChar .= $text[$i + 1];
261 if ( $curChar ===
'|' ) {
263 } elseif ( $curChar ===
'=' ) {
265 } elseif ( $curChar ===
'<' ) {
267 } elseif ( $curChar ===
"\n" ) {
271 $found =
'line-start';
273 } elseif ( $curTwoChar === $currentClosing ) {
275 $curChar = $curTwoChar;
276 } elseif ( $curChar === $currentClosing ) {
278 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
279 $curChar = $curTwoChar;
281 $rule = $this->rules[$curChar];
282 } elseif ( isset( $this->rules[$curChar] ) ) {
284 $rule = $this->rules[$curChar];
286 # Some versions of PHP have a strcspn which stops on
287 # null characters; ignore these and continue.
288 # We also may get '-' and '}' characters here which
289 # don't match -{ or $currentClosing. Add these to
290 # output and continue.
291 if ( $curChar ===
'-' || $curChar ===
'}' ) {
292 self::addLiteral( $accum, $curChar );
300 if ( $found ===
'angle' ) {
302 if ( $enableOnlyinclude
303 && substr_compare( $text,
'</onlyinclude>', $i, 14 ) === 0
305 $findOnlyinclude =
true;
310 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
312 self::addLiteral( $accum,
'<' );
318 if ( $name ===
'!--' ) {
325 $endPos = strpos( $text,
'-->', $i + 4 );
326 if ( $endPos ===
false ) {
328 $inner = substr( $text, $i );
329 $accum[] = [
'comment', [ $inner ] ];
334 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
337 $wsEnd = $endPos + 3;
339 $wsEnd += strspn( $text,
" \t", $wsEnd );
342 $comments = [ [ $wsStart, $wsEnd ] ];
343 while ( substr_compare( $text,
'<!--', $wsEnd, 4 ) === 0 ) {
344 $c = strpos( $text,
'-->', $wsEnd + 4 );
345 if ( $c ===
false ) {
350 $c += strspn( $text,
" \t", $c );
351 $comments[] = [ $wsEnd, $c ];
359 if ( $wsStart > 0 && substr_compare( $text,
"\n", $wsStart - 1, 1 ) === 0
360 && substr_compare( $text,
"\n", $wsEnd, 1 ) === 0
363 $wsLength = $i - $wsStart;
364 $endIndex = count( $accum ) - 1;
368 && is_string( $accum[$endIndex] )
369 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
371 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
376 [ $startPos, $endPos ] = array_pop( $comments );
377 foreach ( $comments as [ $cStartPos, $cEndPos ] ) {
379 $inner = substr( $text, $cStartPos, $cEndPos - $cStartPos );
380 $accum[] = [
'comment', [ $inner ] ];
384 $fakeLineStart =
true;
392 $part = $stack->top->getCurrentPart();
393 if ( $part->commentEnd !== $wsStart - 1 ) {
394 $part->visualEnd = $wsStart;
397 $part->commentEnd = $endPos;
400 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
401 $accum[] = [
'comment', [ $inner ] ];
405 $attrStart = $i + strlen( $name ) + 1;
408 $tagEndPos = $noMoreGT ? false : strpos( $text,
'>', $attrStart );
409 if ( $tagEndPos ===
false ) {
413 self::addLiteral( $accum,
'<' );
418 $lowerName = strtolower( $name );
420 if ( in_array( $lowerName, $ignoredTags ) ) {
421 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
427 if ( $text[$tagEndPos - 1] ===
'/' ) {
429 $attrEnd = $tagEndPos - 1;
434 $attrEnd = $tagEndPos;
437 !isset( $noMoreClosingTag[$lowerName] ) &&
438 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
439 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
441 [ $close, $closeTagStartPos ] =
$matches[0];
442 $inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 );
443 $i = $closeTagStartPos + strlen( $close );
446 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
448 $inner = substr( $text, $tagEndPos + 1 );
454 self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
456 $noMoreClosingTag[$lowerName] =
true;
462 if ( in_array( $lowerName, $ignoredElements ) ) {
463 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
467 if ( $attrEnd <= $attrStart ) {
472 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
476 [
'name', [ $name ] ],
477 [
'attr', [ $attr ] ],
479 if ( $inner !==
null ) {
480 $children[] = [
'inner', [ $inner ] ];
482 if ( $close !==
null ) {
483 $children[] = [
'close', [ $close ] ];
485 $accum[] = [
'ext', $children ];
486 } elseif ( $found ===
'line-start' ) {
489 if ( $fakeLineStart ) {
490 $fakeLineStart =
false;
492 self::addLiteral( $accum, $curChar );
497 $count = strspn( $text,
'=', $i, min( $lengthText, 6 ) );
498 if ( $count === 1 && $findEquals ) {
503 } elseif ( $count > 0 ) {
507 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
511 $stack->push( $piece );
512 $accum =& $stack->getAccum();
513 $stackFlags = $stack->getFlags();
514 if ( isset( $stackFlags[
'findEquals'] ) ) {
515 $findEquals = $stackFlags[
'findEquals'];
517 if ( isset( $stackFlags[
'findPipe'] ) ) {
518 $findPipe = $stackFlags[
'findPipe'];
520 if ( isset( $stackFlags[
'inHeading'] ) ) {
521 $inHeading = $stackFlags[
'inHeading'];
525 } elseif ( $found ===
'line-end' ) {
526 $piece = $stack->top;
530 assert( $piece->open ===
"\n" );
531 $part = $piece->getCurrentPart();
535 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
536 $searchStart = $i - $wsLength;
537 if ( $part->commentEnd === $searchStart - 1 ) {
540 $searchStart = $part->visualEnd;
541 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
543 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
544 if ( $equalsLength > 0 ) {
545 if ( $searchStart - $equalsLength === $piece->startPos ) {
549 if ( $equalsLength < 3 ) {
552 $count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) );
555 $count = min( $equalsLength, $piece->count );
559 $element = [ [
'possible-h',
562 [
'@level', [ $count ] ],
563 [
'@i', [ $headingIndex++ ] ]
578 $accum =& $stack->getAccum();
579 $stackFlags = $stack->getFlags();
580 if ( isset( $stackFlags[
'findEquals'] ) ) {
581 $findEquals = $stackFlags[
'findEquals'];
583 if ( isset( $stackFlags[
'findPipe'] ) ) {
584 $findPipe = $stackFlags[
'findPipe'];
586 if ( isset( $stackFlags[
'inHeading'] ) ) {
587 $inHeading = $stackFlags[
'inHeading'];
591 array_splice( $accum, count( $accum ), 0, $element );
598 } elseif ( $found ===
'open' ) {
599 # count opening brace characters
600 $curLen = strlen( $curChar );
602 # allow the final character to repeat
603 ? strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1
604 : strspn( $text, $curChar, $i );
607 $lineStart = $i > 0 && $text[$i - 1] ===
"\n";
609 if ( $curChar ===
"-{" && $count > $curLen ) {
615 $rule = $this->rules[$curChar];
618 # we need to add to stack only if opening brace count is enough for one of the rules
619 if ( $count >= $rule[
'min'] ) {
620 # Add it to the stack
623 'close' => $rule[
'end'],
624 'savedPrefix' => $savedPrefix,
626 'lineStart' => $lineStart,
629 $stack->push( $piece );
630 $accum =& $stack->getAccum();
631 $stackFlags = $stack->getFlags();
632 if ( isset( $stackFlags[
'findEquals'] ) ) {
633 $findEquals = $stackFlags[
'findEquals'];
635 if ( isset( $stackFlags[
'findPipe'] ) ) {
636 $findPipe = $stackFlags[
'findPipe'];
638 if ( isset( $stackFlags[
'inHeading'] ) ) {
639 $inHeading = $stackFlags[
'inHeading'];
642 # Add literal brace(s)
643 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
646 } elseif ( $found ===
'close' ) {
648 $piece = $stack->top;
649 '@phan-var PPDStackElement_Hash $piece';
650 # lets check if there are enough characters for closing brace
651 $maxCount = $piece->count;
652 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
653 $maxCount--; # don
't try to match closing '-
' as a '}
'
655 $curLen = strlen( $curChar );
658 : strspn( $text, $curChar, $i, $maxCount );
660 # check for maximum matching characters (if there are 5 closing
661 # characters, we will probably need only 3 - depending on the rules)
662 $rule = $this->rules[$piece->open];
663 if ( $count > $rule['max
'] ) {
664 # The specified maximum exists in the callback array, unless the caller
666 $matchingCount = $rule['max
'];
668 # Count is less than the maximum
669 # Skip any gaps in the callback array to find the true largest match
670 # Need to use array_key_exists not isset because the callback can be null
671 $matchingCount = $count;
672 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
677 if ( $matchingCount <= 0 ) {
678 # No matching element found in callback array
679 # Output a literal closing brace and continue
680 $endText = substr( $text, $i, $count );
681 self::addLiteral( $accum, $endText );
685 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
686 $name = $rule['names
'][$matchingCount];
687 if ( $name === null ) {
688 // No element, just literal text
689 $endText = substr( $text, $i, $matchingCount );
690 $element = $piece->breakSyntax( $matchingCount );
691 self::addLiteral( $element, $endText );
694 $parts = $piece->parts;
695 $titleAccum = $parts[0]->out;
700 # The invocation is at the start of the line if lineStart is set in
701 # the stack, and all opening brackets are used up.
702 if ( $maxCount === $matchingCount &&
704 $piece->savedPrefix === ''
706 $children[] = [ '@lineStart
', [ 1 ] ];
708 $titleNode = [ 'title
', $titleAccum ];
709 $children[] = $titleNode;
711 foreach ( $parts as $part ) {
712 if ( isset( $part->eqpos ) ) {
713 $equalsNode = $part->out[$part->eqpos];
714 $nameNode = [ 'name
', array_slice( $part->out, 0, $part->eqpos ) ];
715 $valueNode = [ 'value
', array_slice( $part->out, $part->eqpos + 1 ) ];
716 $partNode = [ 'part
', [ $nameNode, $equalsNode, $valueNode ] ];
717 $children[] = $partNode;
719 $nameNode = [ 'name
', [ [ '@index
', [ $argIndex++ ] ] ] ];
720 $valueNode = [ 'value
', $part->out ];
721 $partNode = [ 'part
', [ $nameNode, $valueNode ] ];
722 $children[] = $partNode;
725 $element = [ [ $name, $children ] ];
728 # Advance input pointer
729 $i += $matchingCount;
733 $accum =& $stack->getAccum();
735 # Re-add the old stack element if it still has unmatched opening characters remaining
736 if ( $matchingCount < $piece->count ) {
737 $piece->parts = [ new PPDPart_Hash ];
738 $piece->count -= $matchingCount;
739 # do we still qualify for any callback with remaining count?
740 $min = $this->rules[$piece->open]['min
'];
741 if ( $piece->count >= $min ) {
742 $stack->push( $piece );
743 $accum =& $stack->getAccum();
744 } elseif ( $piece->count === 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
745 $piece->savedPrefix = '';
748 $piece->close = $this->rules[$piece->open]['end
'];
749 $stack->push( $piece );
750 $accum =& $stack->getAccum();
752 $s = substr( $piece->open, 0, -1 );
754 substr( $piece->open, -1 ),
755 $piece->count - strlen( $s )
757 self::addLiteral( $accum, $piece->savedPrefix . $s );
759 } elseif ( $piece->savedPrefix !== '' ) {
760 self::addLiteral( $accum, $piece->savedPrefix );
763 $stackFlags = $stack->getFlags();
764 if ( isset( $stackFlags['findEquals
'] ) ) {
765 $findEquals = $stackFlags['findEquals
'];
767 if ( isset( $stackFlags['findPipe
'] ) ) {
768 $findPipe = $stackFlags['findPipe
'];
770 if ( isset( $stackFlags['inHeading
'] ) ) {
771 $inHeading = $stackFlags['inHeading
'];
774 # Add XML element to the enclosing accumulator
775 array_splice( $accum, count( $accum ), 0, $element );
776 } elseif ( $found === 'pipe
' ) {
777 $findEquals = true; // shortcut for getFlags()
779 $accum =& $stack->getAccum();
781 } elseif ( $found === 'equals
' ) {
782 $findEquals = false; // shortcut for getFlags()
783 $accum[] = [ 'equals
', [ '=
' ] ];
784 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
789 # Output any remaining unclosed brackets
790 foreach ( $stack->stack as $piece ) {
791 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
794 # Enable top-level headings
795 foreach ( $stack->rootAccum as &$node ) {
796 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
797 $node[PPNode_Hash_Tree::NAME] = 'h
';
801 return [ [ 'root
', $stack->rootAccum ] ];
804 private static function addLiteral( array &$accum, $text ) {
805 $n = count( $accum );
806 if ( $n && is_string( $accum[$n - 1] ) ) {
807 $accum[$n - 1] .= $text;