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 ) {
116 $this->cacheThreshold !==
false &&
117 strlen( $text ) >= $this->cacheThreshold &&
118 ( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED
120 $domTreeArray = $this->wanCache->getWithSetCallback(
121 $this->wanCache->makeKey(
'preprocess-hash', sha1( $text ), $flags ),
122 $this->wanCache::TTL_DAY,
123 function () use ( $text, $flags ) {
124 return $this->buildDomTreeArrayFromText( $text, $flags );
129 $domTreeArray = $this->buildDomTreeArrayFromText( $text, $flags );
140 private function buildDomTreeArrayFromText( $text, $flags ) {
144 $xmlishElements = $this->parser->getStripList();
145 $xmlishAllowMissingEndTag = [
'includeonly',
'noinclude',
'onlyinclude' ];
146 $enableOnlyinclude =
false;
147 if ( $forInclusion ) {
148 $ignoredTags = [
'includeonly',
'/includeonly' ];
149 $ignoredElements = [
'noinclude' ];
150 $xmlishElements[] =
'noinclude';
151 if ( strpos( $text,
'<onlyinclude>' ) !==
false
152 && strpos( $text,
'</onlyinclude>' ) !== false
154 $enableOnlyinclude =
true;
157 $ignoredTags = [
'noinclude',
'/noinclude',
'onlyinclude',
'/onlyinclude' ];
158 $ignoredElements = [
'includeonly' ];
159 $xmlishElements[] =
'includeonly';
161 $xmlishRegex = implode(
'|', array_merge( $xmlishElements, $ignoredTags ) );
164 $elementsRegex =
"~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
168 $searchBase =
"[{<\n";
169 if ( !$langConversionDisabled ) {
174 $revText = strrev( $text );
175 $lengthText = strlen( $text );
180 $accum =& $stack->getAccum();
191 $noMoreClosingTag = [];
193 $findOnlyinclude = $enableOnlyinclude;
195 $fakeLineStart =
true;
198 if ( $findOnlyinclude ) {
200 $startPos = strpos( $text,
'<onlyinclude>', $i );
201 if ( $startPos ===
false ) {
203 $accum[] = [
'ignore', [ substr( $text, $i ) ] ];
206 $tagEndPos = $startPos + strlen(
'<onlyinclude>' );
207 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
209 $findOnlyinclude =
false;
212 if ( $fakeLineStart ) {
213 $found =
'line-start';
216 # Find next opening brace, closing brace or pipe
217 $search = $searchBase;
218 if ( $stack->top ===
false ) {
219 $currentClosing =
'';
221 $currentClosing = $stack->top->close;
222 $search .= $currentClosing;
232 # Output literal section, advance input counter
233 $literalLength = strcspn( $text, $search, $i );
234 if ( $literalLength > 0 ) {
235 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
236 $i += $literalLength;
238 if ( $i >= $lengthText ) {
239 if ( $currentClosing ==
"\n" ) {
248 $curChar = $curTwoChar = $text[$i];
249 if ( ( $i + 1 ) < $lengthText ) {
250 $curTwoChar .= $text[$i + 1];
252 if ( $curChar ==
'|' ) {
254 } elseif ( $curChar ==
'=' ) {
256 } elseif ( $curChar ==
'<' ) {
258 } elseif ( $curChar ==
"\n" ) {
262 $found =
'line-start';
264 } elseif ( $curTwoChar == $currentClosing ) {
266 $curChar = $curTwoChar;
267 } elseif ( $curChar == $currentClosing ) {
269 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
270 $curChar = $curTwoChar;
272 $rule = $this->rules[$curChar];
273 } elseif ( isset( $this->rules[$curChar] ) ) {
275 $rule = $this->rules[$curChar];
277 # Some versions of PHP have a strcspn which stops on
278 # null characters; ignore these and continue.
279 # We also may get '-' and '}' characters here which
280 # don't match -{ or $currentClosing. Add these to
281 # output and continue.
282 if ( $curChar ==
'-' || $curChar ==
'}' ) {
283 self::addLiteral( $accum, $curChar );
291 if ( $found ==
'angle' ) {
294 if ( $enableOnlyinclude
295 && str_starts_with( substr( $text, $i ),
'</onlyinclude>' )
297 $findOnlyinclude =
true;
302 if ( !preg_match( $elementsRegex, $text,
$matches, 0, $i + 1 ) ) {
304 self::addLiteral( $accum,
'<' );
316 $endPos = strpos( $text,
'-->', $i + 4 );
317 if ( $endPos ===
false ) {
319 $inner = substr( $text, $i );
320 $accum[] = [
'comment', [ $inner ] ];
324 $wsStart = $i ? ( $i - strspn( $revText,
" \t", $lengthText - $i ) ) : 0;
328 $wsEnd = $endPos + 2 + strspn( $text,
" \t", $endPos + 3 );
332 $comments = [ [ $wsStart, $wsEnd ] ];
333 while ( substr( $text, $wsEnd + 1, 4 ) ==
'<!--' ) {
334 $c = strpos( $text,
'-->', $wsEnd + 4 );
335 if ( $c ===
false ) {
338 $c = $c + 2 + strspn( $text,
" \t", $c + 3 );
339 $comments[] = [ $wsEnd + 1, $c ];
347 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) ==
"\n"
348 && substr( $text, $wsEnd + 1, 1 ) ==
"\n"
351 $wsLength = $i - $wsStart;
352 $endIndex = count( $accum ) - 1;
356 && is_string( $accum[$endIndex] )
357 && strspn( $accum[$endIndex],
" \t", -$wsLength ) === $wsLength
359 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
363 foreach ( $comments as $j => $com ) {
365 $endPos = $com[1] + 1;
366 if ( $j == ( count( $comments ) - 1 ) ) {
369 $inner = substr( $text, $startPos, $endPos - $startPos );
370 $accum[] = [
'comment', [ $inner ] ];
374 $fakeLineStart =
true;
382 $part = $stack->top->getCurrentPart();
383 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
384 $part->visualEnd = $wsStart;
387 $part->commentEnd = $endPos;
390 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
391 $accum[] = [
'comment', [ $inner ] ];
396 $lowerName = strtolower( $name );
397 $attrStart = $i + strlen( $name ) + 1;
400 $tagEndPos = $noMoreGT ? false : strpos( $text,
'>', $attrStart );
401 if ( $tagEndPos ===
false ) {
405 self::addLiteral( $accum,
'<' );
411 if ( in_array( $lowerName, $ignoredTags ) ) {
412 $accum[] = [
'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
418 if ( $text[$tagEndPos - 1] ==
'/' ) {
420 $attrEnd = $tagEndPos - 1;
425 $attrEnd = $tagEndPos;
428 !isset( $noMoreClosingTag[$name] ) &&
429 preg_match(
"/<\/" . preg_quote( $name,
'/' ) .
"\s*>/i",
430 $text,
$matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
432 $inner = substr( $text, $tagEndPos + 1,
$matches[0][1] - $tagEndPos - 1 );
437 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
439 $inner = substr( $text, $tagEndPos + 1 );
445 self::addLiteral( $accum,
446 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
448 $noMoreClosingTag[$name] =
true;
454 if ( in_array( $lowerName, $ignoredElements ) ) {
455 $accum[] = [
'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
459 if ( $attrEnd <= $attrStart ) {
464 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
468 [
'name', [ $name ] ],
469 [
'attr', [ $attr ] ] ];
470 if ( $inner !==
null ) {
471 $children[] = [
'inner', [ $inner ] ];
473 if ( $close !==
null ) {
474 $children[] = [
'close', [ $close ] ];
476 $accum[] = [
'ext', $children ];
477 } elseif ( $found ==
'line-start' ) {
480 if ( $fakeLineStart ) {
481 $fakeLineStart =
false;
483 self::addLiteral( $accum, $curChar );
488 $count = strspn( $text,
'=', $i, min( strlen( $text ), 6 ) );
489 if ( $count == 1 && $findEquals ) {
494 } elseif ( $count > 0 ) {
498 'parts' => [
new PPDPart_Hash( str_repeat(
'=', $count ) ) ],
501 $stack->push( $piece );
502 $accum =& $stack->getAccum();
503 $stackFlags = $stack->getFlags();
504 if ( isset( $stackFlags[
'findEquals'] ) ) {
505 $findEquals = $stackFlags[
'findEquals'];
507 if ( isset( $stackFlags[
'findPipe'] ) ) {
508 $findPipe = $stackFlags[
'findPipe'];
510 if ( isset( $stackFlags[
'inHeading'] ) ) {
511 $inHeading = $stackFlags[
'inHeading'];
515 } elseif ( $found ==
'line-end' ) {
516 $piece = $stack->top;
520 assert( $piece->open ===
"\n" );
521 $part = $piece->getCurrentPart();
525 $wsLength = strspn( $revText,
" \t", $lengthText - $i );
526 $searchStart = $i - $wsLength;
527 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
530 $searchStart = $part->visualEnd;
531 $searchStart -= strspn( $revText,
" \t", $lengthText - $searchStart );
533 $count = $piece->count;
534 $equalsLength = strspn( $revText,
'=', $lengthText - $searchStart );
535 if ( $equalsLength > 0 ) {
536 if ( $searchStart - $equalsLength == $piece->startPos ) {
540 $count = $equalsLength;
544 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
547 $count = min( $equalsLength, $count );
551 $element = [ [
'possible-h',
554 [
'@level', [ $count ] ],
555 [
'@i', [ $headingIndex++ ] ]
570 $accum =& $stack->getAccum();
571 $stackFlags = $stack->getFlags();
572 if ( isset( $stackFlags[
'findEquals'] ) ) {
573 $findEquals = $stackFlags[
'findEquals'];
575 if ( isset( $stackFlags[
'findPipe'] ) ) {
576 $findPipe = $stackFlags[
'findPipe'];
578 if ( isset( $stackFlags[
'inHeading'] ) ) {
579 $inHeading = $stackFlags[
'inHeading'];
583 array_splice( $accum, count( $accum ), 0, $element );
590 } elseif ( $found ==
'open' ) {
591 # count opening brace characters
592 $curLen = strlen( $curChar );
593 $count = ( $curLen > 1 ) ?
594 # allow the
final character to repeat
595 strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
596 strspn( $text, $curChar, $i );
599 $lineStart = ( $i > 0 && $text[$i - 1] ==
"\n" );
601 if ( $curChar ===
"-{" && $count > $curLen ) {
607 $rule = $this->rules[$curChar];
610 # we need to add to stack only if opening brace count is enough for one of the rules
611 if ( $count >= $rule[
'min'] ) {
612 # Add it to the stack
615 'close' => $rule[
'end'],
616 'savedPrefix' => $savedPrefix,
618 'lineStart' => $lineStart,
621 $stack->push( $piece );
622 $accum =& $stack->getAccum();
623 $stackFlags = $stack->getFlags();
624 if ( isset( $stackFlags[
'findEquals'] ) ) {
625 $findEquals = $stackFlags[
'findEquals'];
627 if ( isset( $stackFlags[
'findPipe'] ) ) {
628 $findPipe = $stackFlags[
'findPipe'];
630 if ( isset( $stackFlags[
'inHeading'] ) ) {
631 $inHeading = $stackFlags[
'inHeading'];
634 # Add literal brace(s)
635 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
638 } elseif ( $found ==
'close' ) {
640 $piece = $stack->top;
641 '@phan-var PPDStackElement_Hash $piece';
642 # lets check if there are enough characters for closing brace
643 $maxCount = $piece->count;
644 if ( $piece->close ===
'}-' && $curChar ===
'}' ) {
645 $maxCount--; # don
't try to match closing '-
' as a '}
'
647 $curLen = strlen( $curChar );
648 $count = ( $curLen > 1 ) ? $curLen :
649 strspn( $text, $curChar, $i, $maxCount );
651 # check for maximum matching characters (if there are 5 closing
652 # characters, we will probably need only 3 - depending on the rules)
653 $rule = $this->rules[$piece->open];
654 if ( $count > $rule['max
'] ) {
655 # The specified maximum exists in the callback array, unless the caller
657 $matchingCount = $rule['max
'];
659 # Count is less than the maximum
660 # Skip any gaps in the callback array to find the true largest match
661 # Need to use array_key_exists not isset because the callback can be null
662 $matchingCount = $count;
663 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names
'] ) ) {
668 if ( $matchingCount <= 0 ) {
669 # No matching element found in callback array
670 # Output a literal closing brace and continue
671 $endText = substr( $text, $i, $count );
672 self::addLiteral( $accum, $endText );
676 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
677 $name = $rule['names
'][$matchingCount];
678 if ( $name === null ) {
679 // No element, just literal text
680 $endText = substr( $text, $i, $matchingCount );
681 $element = $piece->breakSyntax( $matchingCount );
682 self::addLiteral( $element, $endText );
685 $parts = $piece->parts;
686 $titleAccum = $parts[0]->out;
691 # The invocation is at the start of the line if lineStart is set in
692 # the stack, and all opening brackets are used up.
693 if ( $maxCount == $matchingCount &&
694 !empty( $piece->lineStart ) &&
695 strlen( $piece->savedPrefix ) == 0 ) {
696 $children[] = [ '@lineStart
', [ 1 ] ];
698 $titleNode = [ 'title
', $titleAccum ];
699 $children[] = $titleNode;
701 foreach ( $parts as $part ) {
702 if ( isset( $part->eqpos ) ) {
703 $equalsNode = $part->out[$part->eqpos];
704 $nameNode = [ 'name
', array_slice( $part->out, 0, $part->eqpos ) ];
705 $valueNode = [ 'value
', array_slice( $part->out, $part->eqpos + 1 ) ];
706 $partNode = [ 'part
', [ $nameNode, $equalsNode, $valueNode ] ];
707 $children[] = $partNode;
709 $nameNode = [ 'name
', [ [ '@index
', [ $argIndex++ ] ] ] ];
710 $valueNode = [ 'value
', $part->out ];
711 $partNode = [ 'part
', [ $nameNode, $valueNode ] ];
712 $children[] = $partNode;
715 $element = [ [ $name, $children ] ];
718 # Advance input pointer
719 $i += $matchingCount;
723 $accum =& $stack->getAccum();
725 # Re-add the old stack element if it still has unmatched opening characters remaining
726 if ( $matchingCount < $piece->count ) {
727 $piece->parts = [ new PPDPart_Hash ];
728 $piece->count -= $matchingCount;
729 # do we still qualify for any callback with remaining count?
730 $min = $this->rules[$piece->open]['min
'];
731 if ( $piece->count >= $min ) {
732 $stack->push( $piece );
733 $accum =& $stack->getAccum();
734 } elseif ( $piece->count == 1 && $piece->open === '{
' && $piece->savedPrefix === '-
' ) {
735 $piece->savedPrefix = '';
738 $piece->close = $this->rules[$piece->open]['end
'];
739 $stack->push( $piece );
740 $accum =& $stack->getAccum();
742 $s = substr( $piece->open, 0, -1 );
744 substr( $piece->open, -1 ),
745 $piece->count - strlen( $s )
747 self::addLiteral( $accum, $piece->savedPrefix . $s );
749 } elseif ( $piece->savedPrefix !== '' ) {
750 self::addLiteral( $accum, $piece->savedPrefix );
753 $stackFlags = $stack->getFlags();
754 if ( isset( $stackFlags['findEquals
'] ) ) {
755 $findEquals = $stackFlags['findEquals
'];
757 if ( isset( $stackFlags['findPipe
'] ) ) {
758 $findPipe = $stackFlags['findPipe
'];
760 if ( isset( $stackFlags['inHeading
'] ) ) {
761 $inHeading = $stackFlags['inHeading
'];
764 # Add XML element to the enclosing accumulator
765 array_splice( $accum, count( $accum ), 0, $element );
766 } elseif ( $found == 'pipe
' ) {
767 $findEquals = true; // shortcut for getFlags()
769 $accum =& $stack->getAccum();
771 } elseif ( $found == 'equals
' ) {
772 $findEquals = false; // shortcut for getFlags()
773 $accum[] = [ 'equals
', [ '=
' ] ];
774 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
779 # Output any remaining unclosed brackets
780 foreach ( $stack->stack as $piece ) {
781 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
784 # Enable top-level headings
785 foreach ( $stack->rootAccum as &$node ) {
786 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h
' ) {
787 $node[PPNode_Hash_Tree::NAME] = 'h
';
791 return [ [ 'root
', $stack->rootAccum ] ];
794 private static function addLiteral( array &$accum, $text ) {
795 $n = count( $accum );
796 if ( $n && is_string( $accum[$n - 1] ) ) {
797 $accum[$n - 1] .= $text;