Merge "docs: Fix typo"
[mediawiki.git] / includes / parser / Preprocessor_Hash.php
blobe37b8a78c5d5eeba7a8820971723fef26180e200
1 <?php
2 /**
3 * Preprocessor using PHP arrays
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Parser
24 namespace MediaWiki\Parser;
26 use Wikimedia\ObjectCache\WANObjectCache;
28 /**
29 * Differences from DOM schema:
30 * * attribute nodes are children
31 * * "<h>" nodes that aren't at the top are replaced with <possible-h>
33 * Nodes are stored in a recursive array data structure. A node store is an
34 * array where each element may be either a scalar (representing a text node)
35 * or a "descriptor", which is a two-element array where the first element is
36 * the node name and the second element is the node store for the children.
38 * Attributes are represented as children that have a node name starting with
39 * "@", and a single text node child.
41 * @todo: Consider replacing descriptor arrays with objects of a new class.
42 * Benchmark and measure resulting memory impact.
44 * @ingroup Parser
46 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
47 class Preprocessor_Hash extends Preprocessor {
48 /** Cache format version */
49 protected const CACHE_VERSION = 4;
51 /** @var int|false Min wikitext size for which to cache DOM tree */
52 protected $cacheThreshold;
54 /**
55 * @see Preprocessor::__construct()
56 * @param Parser $parser
57 * @param WANObjectCache|null $wanCache
58 * @param array $options Additional options include:
59 * - cacheThreshold: min text size for which to cache DOMs. [Default: false]
61 public function __construct(
62 Parser $parser,
63 ?WANObjectCache $wanCache = null,
64 array $options = []
65 ) {
66 parent::__construct( $parser, $wanCache, $options );
68 $this->cacheThreshold = $options['cacheThreshold'] ?? false;
71 /**
72 * @return PPFrame_Hash
74 public function newFrame() {
75 return new PPFrame_Hash( $this );
78 /**
79 * @param array $args
80 * @return PPCustomFrame_Hash
82 public function newCustomFrame( $args ) {
83 return new PPCustomFrame_Hash( $this, $args );
86 /**
87 * @param array $values
88 * @return PPNode_Hash_Array
90 public function newPartNodeArray( $values ) {
91 $list = [];
93 foreach ( $values as $k => $val ) {
94 if ( is_int( $k ) ) {
95 $store = [ [ 'part', [
96 [ 'name', [ [ '@index', [ $k ] ] ] ],
97 [ 'value', [ strval( $val ) ] ],
98 ] ] ];
99 } else {
100 $store = [ [ 'part', [
101 [ 'name', [ strval( $k ) ] ],
102 '=',
103 [ 'value', [ strval( $val ) ] ],
104 ] ] ];
107 $list[] = new PPNode_Hash_Tree( $store, 0 );
110 return new PPNode_Hash_Array( $list );
113 public function preprocessToObj( $text, $flags = 0 ) {
114 if ( $this->disableLangConversion ) {
115 // Language conversions are globally disabled; implicitly set flag
116 $flags |= self::DOM_LANG_CONVERSION_DISABLED;
119 $domTreeArray = null;
120 if (
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 );
131 return json_encode(
132 $domTreeArray,
133 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
136 [ 'version' => self::CACHE_VERSION, 'segmentable' => true ]
138 $domTreeArray ??= json_decode( $domTreeJson );
141 $domTreeArray ??= $this->buildDomTreeArrayFromText( $text, $flags );
143 return new PPNode_Hash_Tree( $domTreeArray, 0 );
147 * @param string $text Wikitext
148 * @param int $flags Bit field of Preprocessor::DOM_* flags
149 * @return array JSON-serializable document object model array
151 private function buildDomTreeArrayFromText( $text, $flags ) {
152 $textStartsInSOLState = $flags & self::START_IN_SOL_STATE;
153 $forInclusion = ( $flags & self::DOM_FOR_INCLUSION );
154 $langConversionDisabled = ( $flags & self::DOM_LANG_CONVERSION_DISABLED );
156 $xmlishElements = $this->parser->getStripList();
157 $xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ];
158 $enableOnlyinclude = false;
159 if ( $forInclusion ) {
160 $ignoredTags = [ 'includeonly', '/includeonly' ];
161 $ignoredElements = [ 'noinclude' ];
162 $xmlishElements[] = 'noinclude';
163 if ( str_contains( $text, '<onlyinclude>' )
164 && str_contains( $text, '</onlyinclude>' )
166 $enableOnlyinclude = true;
168 } else {
169 $ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ];
170 $ignoredElements = [ 'includeonly' ];
171 $xmlishElements[] = 'includeonly';
173 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
175 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
176 $elementsRegex = "~(?:$xmlishRegex)(?=\s|\/>|>)|!--~iA";
178 $stack = new PPDStack_Hash;
180 $searchBase = "[{<\n";
181 if ( !$langConversionDisabled ) {
182 $searchBase .= '-';
185 // For fast reverse searches
186 $revText = strrev( $text );
187 $lengthText = strlen( $text );
189 // Input pointer, starts out pointing to a pseudo-newline before the start
190 $i = 0;
191 // Current accumulator. See the doc comment for Preprocessor_Hash for the format.
192 $accum =& $stack->getAccum();
193 // True to find equals signs in arguments
194 $findEquals = false;
195 // True to take notice of pipe characters
196 $findPipe = false;
197 $headingIndex = 1;
198 // True if $i is inside a possible heading
199 $inHeading = false;
200 // True if there are no more greater-than (>) signs right of $i
201 $noMoreGT = false;
202 // Map of tag name => true if there are no more closing tags of given type right of $i
203 $noMoreClosingTag = [];
204 // True to ignore all input up to the next <onlyinclude>
205 $findOnlyinclude = $enableOnlyinclude;
206 // Do a line-start run without outputting an LF character
207 $fakeLineStart = true;
209 while ( true ) {
210 if ( $findOnlyinclude ) {
211 // Ignore all input up to the next <onlyinclude>
212 $startPos = strpos( $text, '<onlyinclude>', $i );
213 if ( $startPos === false ) {
214 // Ignored section runs to the end
215 $accum[] = [ 'ignore', [ substr( $text, $i ) ] ];
216 break;
218 $tagEndPos = $startPos + 13; // past-the-end of <onlyinclude>
219 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
220 $i = $tagEndPos;
221 $findOnlyinclude = false;
224 if ( $fakeLineStart ) {
225 $found = 'line-start';
226 $curChar = '';
227 } else {
228 # Find next opening brace, closing brace or pipe
229 $search = $searchBase;
230 if ( $stack->top === false ) {
231 $currentClosing = '';
232 } else {
233 $currentClosing = $stack->top->close;
234 $search .= $currentClosing;
236 if ( $findPipe ) {
237 $search .= '|';
239 if ( $findEquals ) {
240 // First equals will be for the template
241 $search .= '=';
243 $rule = null;
244 # Output literal section, advance input counter
245 $literalLength = strcspn( $text, $search, $i );
246 if ( $literalLength > 0 ) {
247 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
248 $i += $literalLength;
250 if ( $i >= $lengthText ) {
251 if ( $currentClosing === "\n" ) {
252 // Do a past-the-end run to finish off the heading
253 $curChar = '';
254 $found = 'line-end';
255 } else {
256 # All done
257 break;
259 } else {
260 $curChar = $curTwoChar = $text[$i];
261 if ( $i + 1 < $lengthText ) {
262 $curTwoChar .= $text[$i + 1];
264 if ( $curChar === '|' ) {
265 $found = 'pipe';
266 } elseif ( $curChar === '=' ) {
267 $found = 'equals';
268 } elseif ( $curChar === '<' ) {
269 $found = 'angle';
270 } elseif ( $curChar === "\n" ) {
271 if ( $inHeading ) {
272 $found = 'line-end';
273 } else {
274 $found = 'line-start';
276 } elseif ( $curTwoChar === $currentClosing ) {
277 $found = 'close';
278 $curChar = $curTwoChar;
279 } elseif ( $curChar === $currentClosing ) {
280 $found = 'close';
281 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
282 $curChar = $curTwoChar;
283 $found = 'open';
284 $rule = $this->rules[$curChar];
285 } elseif ( isset( $this->rules[$curChar] ) ) {
286 $found = 'open';
287 $rule = $this->rules[$curChar];
288 } else {
289 # Some versions of PHP have a strcspn which stops on
290 # null characters; ignore these and continue.
291 # We also may get '-' and '}' characters here which
292 # don't match -{ or $currentClosing. Add these to
293 # output and continue.
294 if ( $curChar === '-' || $curChar === '}' ) {
295 self::addLiteral( $accum, $curChar );
297 ++$i;
298 continue;
303 if ( $found === 'angle' ) {
304 // Handle </onlyinclude>
305 if ( $enableOnlyinclude
306 && substr_compare( $text, '</onlyinclude>', $i, 14 ) === 0
308 $findOnlyinclude = true;
309 continue;
312 // Determine element name
313 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
314 // Element name missing or not listed
315 self::addLiteral( $accum, '<' );
316 ++$i;
317 continue;
319 $name = $matches[0];
320 // Handle comments
321 if ( $name === '!--' ) {
322 // To avoid leaving blank lines, when a sequence of
323 // space-separated comments is both preceded and followed by
324 // a newline (ignoring spaces), then
325 // trim leading and trailing spaces and the trailing newline.
327 // Find the end
328 $endPos = strpos( $text, '-->', $i + 4 );
329 if ( $endPos === false ) {
330 // Unclosed comment in input, runs to end
331 $inner = substr( $text, $i );
332 $accum[] = [ 'comment', [ $inner ] ];
333 $i = $lengthText;
334 } else {
335 // Search backwards for leading whitespace
336 // $wsStart is the first char of the comment (first of the leading space or '<')
337 $wsStart = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
339 // $wsEnd will be the char *after* the comment (after last space or the '>' if there's no space)
340 $wsEnd = $endPos + 3; // add length of -->
341 // Search forwards for trailing whitespace
342 $wsEnd += strspn( $text, " \t", $wsEnd );
344 // Keep looking forward as long as we're finding more comments on the line
345 $comments = [ [ $wsStart, $wsEnd ] ];
346 while ( substr_compare( $text, '<!--', $wsEnd, 4 ) === 0 ) {
347 $c = strpos( $text, '-->', $wsEnd + 4 );
348 if ( $c === false ) {
349 break;
351 $c += 3; // add length of -->
352 // Search forwards for trailing whitespace
353 $c += strspn( $text, " \t", $c );
354 $comments[] = [ $wsEnd, $c ];
355 $wsEnd = $c;
358 // Eat the line if possible
359 // TODO: This could theoretically be done if $wsStart === 0, i.e. for comments at
360 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
361 // it's a possible beneficial b/c break.
362 if ( $wsStart > 0 && substr_compare( $text, "\n", $wsStart - 1, 1 ) === 0
363 && substr_compare( $text, "\n", $wsEnd, 1 ) === 0
365 // Remove leading whitespace from the end of the accumulator
366 $wsLength = $i - $wsStart;
367 $endIndex = count( $accum ) - 1;
369 if ( $wsLength > 0
370 && $endIndex >= 0
371 && is_string( $accum[$endIndex] )
372 && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
374 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
377 // Dump all but the last comment to the accumulator
378 // $endPos includes the newline from the if above, want also eat that
379 [ $startPos, $endPos ] = array_pop( $comments );
380 foreach ( $comments as [ $cStartPos, $cEndPos ] ) {
381 // $cEndPos is the next char, no +1 needed to get correct length between start/end
382 $inner = substr( $text, $cStartPos, $cEndPos - $cStartPos );
383 $accum[] = [ 'comment', [ $inner ] ];
386 // Do a line-start run next time to look for headings after the comment
387 $fakeLineStart = true;
388 } else {
389 // No line to eat, just take the comment itself
390 $startPos = $i;
391 $endPos += 2;
394 if ( $stack->top ) {
395 $part = $stack->top->getCurrentPart();
396 if ( $part->commentEnd !== $wsStart - 1 ) {
397 $part->visualEnd = $wsStart;
399 // Else comments abutting, no change in visual end
400 $part->commentEnd = $endPos;
402 $i = $endPos + 1;
403 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
404 $accum[] = [ 'comment', [ $inner ] ];
406 continue;
408 $attrStart = $i + strlen( $name ) + 1;
410 // Find end of tag
411 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
412 if ( $tagEndPos === false ) {
413 // Infinite backtrack
414 // Disable tag search to prevent worst-case O(N^2) performance
415 $noMoreGT = true;
416 self::addLiteral( $accum, '<' );
417 ++$i;
418 continue;
421 $lowerName = strtolower( $name );
422 // Handle ignored tags
423 if ( in_array( $lowerName, $ignoredTags ) ) {
424 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
425 $i = $tagEndPos + 1;
426 continue;
429 $tagStartPos = $i;
430 if ( $text[$tagEndPos - 1] === '/' ) {
431 // Short end tag
432 $attrEnd = $tagEndPos - 1;
433 $inner = null;
434 $i = $tagEndPos + 1;
435 $close = null;
436 } else {
437 $attrEnd = $tagEndPos;
438 // Find closing tag
439 if (
440 !isset( $noMoreClosingTag[$lowerName] ) &&
441 preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
442 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
444 [ $close, $closeTagStartPos ] = $matches[0];
445 $inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 );
446 $i = $closeTagStartPos + strlen( $close );
447 } else {
448 // No end tag
449 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
450 // Let it run out to the end of the text.
451 $inner = substr( $text, $tagEndPos + 1 );
452 $i = $lengthText;
453 $close = null;
454 } else {
455 // Don't match the tag, treat opening tag as literal and resume parsing.
456 $i = $tagEndPos + 1;
457 self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
458 // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
459 $noMoreClosingTag[$lowerName] = true;
460 continue;
464 // <includeonly> and <noinclude> just become <ignore> tags
465 if ( in_array( $lowerName, $ignoredElements ) ) {
466 $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
467 continue;
470 if ( $attrEnd <= $attrStart ) {
471 $attr = '';
472 } else {
473 // Note that the attr element contains the whitespace between name and attribute,
474 // this is necessary for precise reconstruction during pre-save transform.
475 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
478 $children = [
479 [ 'name', [ $name ] ],
480 [ 'attr', [ $attr ] ],
482 if ( $inner !== null ) {
483 $children[] = [ 'inner', [ $inner ] ];
485 if ( $close !== null ) {
486 $children[] = [ 'close', [ $close ] ];
488 $accum[] = [ 'ext', $children ];
489 } elseif ( $found === 'line-start' ) {
490 // Is this the start of a heading?
491 // Line break belongs before the heading element in any case
492 if ( $fakeLineStart ) {
493 $fakeLineStart = false;
494 } else {
495 self::addLiteral( $accum, $curChar );
496 $i++;
499 // Examine upto 6 characters
500 $count = strspn( $text, '=', $i, min( $lengthText, 6 ) );
501 if ( $count === 1 && $findEquals ) {
502 // DWIM: This looks kind of like a name/value separator.
503 // Let's let the equals handler have it and break the potential
504 // heading. This is heuristic, but AFAICT the methods for
505 // completely correct disambiguation are very complex.
506 } elseif ( $count > 0 ) {
507 $piece = [
508 'open' => "\n",
509 'close' => "\n",
510 'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ],
511 'startPos' => $i,
512 'count' => $count,
514 $stack->push( $piece );
515 $accum =& $stack->getAccum();
516 $stackFlags = $stack->getFlags();
517 if ( isset( $stackFlags['findEquals'] ) ) {
518 $findEquals = $stackFlags['findEquals'];
520 if ( isset( $stackFlags['findPipe'] ) ) {
521 $findPipe = $stackFlags['findPipe'];
523 if ( isset( $stackFlags['inHeading'] ) ) {
524 $inHeading = $stackFlags['inHeading'];
526 $i += $count;
528 } elseif ( $found === 'line-end' ) {
529 $piece = $stack->top;
530 // A heading must be open, otherwise \n wouldn't have been in the search list
531 // FIXME: Don't use assert()
532 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
533 assert( $piece->open === "\n" );
534 $part = $piece->getCurrentPart();
535 // Search back through the input to see if it has a proper close.
536 // Do this using the reversed string since the other solutions
537 // (end anchor, etc.) are inefficient.
538 $wsLength = strspn( $revText, " \t", $lengthText - $i );
539 $searchStart = $i - $wsLength;
540 if ( $part->commentEnd === $searchStart - 1 ) {
541 // Comment found at line end
542 // Search for equals signs before the comment
543 $searchStart = $part->visualEnd;
544 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
546 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
547 if ( $equalsLength > 0 ) {
548 if ( $searchStart - $equalsLength === $piece->startPos ) {
549 // This is just a single string of equals signs on its own line
550 // Replicate the doHeadings behavior /={count}(.+)={count}/
551 // First find out how many equals signs there really are (don't stop at 6)
552 if ( $equalsLength < 3 ) {
553 $count = 0;
554 } else {
555 $count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) );
557 } else {
558 $count = min( $equalsLength, $piece->count );
560 if ( $count > 0 ) {
561 // Normal match, output <h>
562 $element = [ [ 'possible-h',
563 array_merge(
565 [ '@level', [ $count ] ],
566 [ '@i', [ $headingIndex++ ] ]
568 $accum
570 ] ];
571 } else {
572 // Single equals sign on its own line, count=0
573 $element = $accum;
575 } else {
576 // No match, no <h>, just pass down the inner text
577 $element = $accum;
579 // Unwind the stack
580 $stack->pop();
581 $accum =& $stack->getAccum();
582 $stackFlags = $stack->getFlags();
583 if ( isset( $stackFlags['findEquals'] ) ) {
584 $findEquals = $stackFlags['findEquals'];
586 if ( isset( $stackFlags['findPipe'] ) ) {
587 $findPipe = $stackFlags['findPipe'];
589 if ( isset( $stackFlags['inHeading'] ) ) {
590 $inHeading = $stackFlags['inHeading'];
593 // Append the result to the enclosing accumulator
594 array_splice( $accum, count( $accum ), 0, $element );
596 // Note that we do NOT increment the input pointer.
597 // This is because the closing linebreak could be the opening linebreak of
598 // another heading. Infinite loops are avoided because the next iteration MUST
599 // hit the heading open case above, which unconditionally increments the
600 // input pointer.
601 } elseif ( $found === 'open' ) {
602 # count opening brace characters
603 $curLen = strlen( $curChar );
604 $count = $curLen > 1
605 # allow the final character to repeat
606 ? strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1
607 : strspn( $text, $curChar, $i );
609 $savedPrefix = '';
610 $lineStart = ( $i === 0 ) ? $textStartsInSOLState : ( $text[$i - 1] === "\n" );
612 if ( $curChar === "-{" && $count > $curLen ) {
613 // -{ => {{ transition because rightmost wins
614 $savedPrefix = '-';
615 $i++;
616 $curChar = '{';
617 $count--;
618 $rule = $this->rules[$curChar];
621 # we need to add to stack only if opening brace count is enough for one of the rules
622 if ( $count >= $rule['min'] ) {
623 # Add it to the stack
624 $piece = [
625 'open' => $curChar,
626 'close' => $rule['end'],
627 'savedPrefix' => $savedPrefix,
628 'count' => $count,
629 'lineStart' => $lineStart,
632 $stack->push( $piece );
633 $accum =& $stack->getAccum();
634 $stackFlags = $stack->getFlags();
635 if ( isset( $stackFlags['findEquals'] ) ) {
636 $findEquals = $stackFlags['findEquals'];
638 if ( isset( $stackFlags['findPipe'] ) ) {
639 $findPipe = $stackFlags['findPipe'];
641 if ( isset( $stackFlags['inHeading'] ) ) {
642 $inHeading = $stackFlags['inHeading'];
644 } else {
645 # Add literal brace(s)
646 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
648 $i += $count;
649 } elseif ( $found === 'close' ) {
650 /** @var PPDStackElement_Hash $piece */
651 $piece = $stack->top;
652 '@phan-var PPDStackElement_Hash $piece';
653 # lets check if there are enough characters for closing brace
654 $maxCount = $piece->count;
655 if ( $piece->close === '}-' && $curChar === '}' ) {
656 $maxCount--; # don't try to match closing '-' as a '}'
658 $curLen = strlen( $curChar );
659 $count = $curLen > 1
660 ? $curLen
661 : strspn( $text, $curChar, $i, $maxCount );
663 # check for maximum matching characters (if there are 5 closing
664 # characters, we will probably need only 3 - depending on the rules)
665 $rule = $this->rules[$piece->open];
666 if ( $count > $rule['max'] ) {
667 # The specified maximum exists in the callback array, unless the caller
668 # has made an error
669 $matchingCount = $rule['max'];
670 } else {
671 # Count is less than the maximum
672 # Skip any gaps in the callback array to find the true largest match
673 # Need to use array_key_exists not isset because the callback can be null
674 $matchingCount = $count;
675 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
676 --$matchingCount;
680 if ( $matchingCount <= 0 ) {
681 # No matching element found in callback array
682 # Output a literal closing brace and continue
683 $endText = substr( $text, $i, $count );
684 self::addLiteral( $accum, $endText );
685 $i += $count;
686 continue;
688 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
689 $name = $rule['names'][$matchingCount];
690 if ( $name === null ) {
691 // No element, just literal text
692 $endText = substr( $text, $i, $matchingCount );
693 $element = $piece->breakSyntax( $matchingCount );
694 self::addLiteral( $element, $endText );
695 } else {
696 # Create XML element
697 $parts = $piece->parts;
698 $titleAccum = $parts[0]->out;
699 unset( $parts[0] );
701 $children = [];
703 # The invocation is at the start of the line if lineStart is set in
704 # the stack, and all opening brackets are used up.
705 if ( $maxCount === $matchingCount &&
706 $piece->lineStart &&
707 $piece->savedPrefix === ''
709 $children[] = [ '@lineStart', [ 1 ] ];
711 $titleNode = [ 'title', $titleAccum ];
712 $children[] = $titleNode;
713 $argIndex = 1;
714 foreach ( $parts as $part ) {
715 if ( $part->eqpos !== null ) {
716 $equalsNode = $part->out[$part->eqpos];
717 $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
718 $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
719 $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
720 $children[] = $partNode;
721 } else {
722 $nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ];
723 $valueNode = [ 'value', $part->out ];
724 $partNode = [ 'part', [ $nameNode, $valueNode ] ];
725 $children[] = $partNode;
728 $element = [ [ $name, $children ] ];
731 # Advance input pointer
732 $i += $matchingCount;
734 # Unwind the stack
735 $stack->pop();
736 $accum =& $stack->getAccum();
738 # Re-add the old stack element if it still has unmatched opening characters remaining
739 if ( $matchingCount < $piece->count ) {
740 $piece->parts = [ new PPDPart_Hash ];
741 $piece->count -= $matchingCount;
742 # do we still qualify for any callback with remaining count?
743 $min = $this->rules[$piece->open]['min'];
744 if ( $piece->count >= $min ) {
745 $stack->push( $piece );
746 $accum =& $stack->getAccum();
747 } elseif ( $piece->count === 1 && $piece->open === '{' && $piece->savedPrefix === '-' ) {
748 $piece->savedPrefix = '';
749 $piece->open = '-{';
750 $piece->count = 2;
751 $piece->close = $this->rules[$piece->open]['end'];
752 $stack->push( $piece );
753 $accum =& $stack->getAccum();
754 } else {
755 $s = substr( $piece->open, 0, -1 );
756 $s .= str_repeat(
757 substr( $piece->open, -1 ),
758 $piece->count - strlen( $s )
760 self::addLiteral( $accum, $piece->savedPrefix . $s );
762 } elseif ( $piece->savedPrefix !== '' ) {
763 self::addLiteral( $accum, $piece->savedPrefix );
766 $stackFlags = $stack->getFlags();
767 if ( isset( $stackFlags['findEquals'] ) ) {
768 $findEquals = $stackFlags['findEquals'];
770 if ( isset( $stackFlags['findPipe'] ) ) {
771 $findPipe = $stackFlags['findPipe'];
773 if ( isset( $stackFlags['inHeading'] ) ) {
774 $inHeading = $stackFlags['inHeading'];
777 # Add XML element to the enclosing accumulator
778 array_splice( $accum, count( $accum ), 0, $element );
779 } elseif ( $found === 'pipe' ) {
780 $findEquals = true; // shortcut for getFlags()
781 $stack->addPart();
782 $accum =& $stack->getAccum();
783 ++$i;
784 } elseif ( $found === 'equals' ) {
785 $findEquals = false; // shortcut for getFlags()
786 $accum[] = [ 'equals', [ '=' ] ];
787 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
788 ++$i;
792 # Output any remaining unclosed brackets
793 foreach ( $stack->stack as $piece ) {
794 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
797 # Enable top-level headings
798 foreach ( $stack->rootAccum as &$node ) {
799 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
800 $node[PPNode_Hash_Tree::NAME] = 'h';
804 return [ [ 'root', $stack->rootAccum ] ];
807 private static function addLiteral( array &$accum, $text ) {
808 $n = count( $accum );
809 if ( $n && is_string( $accum[$n - 1] ) ) {
810 $accum[$n - 1] .= $text;
811 } else {
812 $accum[] = $text;
817 /** @deprecated class alias since 1.43 */
818 class_alias( Preprocessor_Hash::class, 'Preprocessor_Hash' );