MediaWiki master
Preprocessor_Hash.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Parser;
25
27
46// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
49 protected const CACHE_VERSION = 4;
50
52 protected $cacheThreshold;
53
61 public function __construct(
64 array $options = []
65 ) {
66 parent::__construct( $parser, $wanCache, $options );
67
68 $this->cacheThreshold = $options['cacheThreshold'] ?? false;
69 }
70
74 public function newFrame() {
75 return new PPFrame_Hash( $this );
76 }
77
82 public function newCustomFrame( $args ) {
83 return new PPCustomFrame_Hash( $this, $args );
84 }
85
90 public function newPartNodeArray( $values ) {
91 $list = [];
92
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 ] ] ];
105 }
106
107 $list[] = new PPNode_Hash_Tree( $store, 0 );
108 }
109
110 return new PPNode_Hash_Array( $list );
111 }
112
113 public function preprocessToObj( $text, $flags = 0 ) {
114 if ( $this->disableLangConversion ) {
115 // Language conversions are globally disabled; implicitly set flag
117 }
118
119 $domTreeArray = null;
120 if (
121 $this->cacheThreshold !== false &&
122 strlen( $text ) >= $this->cacheThreshold &&
123 ( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED
124 ) {
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 );
130
131 return json_encode(
132 $domTreeArray,
133 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
134 );
135 },
136 [ 'version' => self::CACHE_VERSION, 'segmentable' => true ]
137 );
138 $domTreeArray ??= json_decode( $domTreeJson );
139 }
140
141 $domTreeArray ??= $this->buildDomTreeArrayFromText( $text, $flags );
142
143 return new PPNode_Hash_Tree( $domTreeArray, 0 );
144 }
145
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 );
155
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>' )
165 ) {
166 $enableOnlyinclude = true;
167 }
168 } else {
169 $ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ];
170 $ignoredElements = [ 'includeonly' ];
171 $xmlishElements[] = 'includeonly';
172 }
173 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
174
175 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
176 $elementsRegex = "~(?:$xmlishRegex)(?=\s|\/>|>)|!--~iA";
177
178 $stack = new PPDStack_Hash;
179
180 $searchBase = "[{<\n";
181 if ( !$langConversionDisabled ) {
182 $searchBase .= '-';
183 }
184
185 // For fast reverse searches
186 $revText = strrev( $text );
187 $lengthText = strlen( $text );
188
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;
208
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;
217 }
218 $tagEndPos = $startPos + 13; // past-the-end of <onlyinclude>
219 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
220 $i = $tagEndPos;
221 $findOnlyinclude = false;
222 }
223
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;
235 }
236 if ( $findPipe ) {
237 $search .= '|';
238 }
239 if ( $findEquals ) {
240 // First equals will be for the template
241 $search .= '=';
242 }
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;
249 }
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;
258 }
259 } else {
260 $curChar = $curTwoChar = $text[$i];
261 if ( $i + 1 < $lengthText ) {
262 $curTwoChar .= $text[$i + 1];
263 }
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';
275 }
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 );
296 }
297 ++$i;
298 continue;
299 }
300 }
301 }
302
303 if ( $found === 'angle' ) {
304 // Handle </onlyinclude>
305 if ( $enableOnlyinclude
306 && substr_compare( $text, '</onlyinclude>', $i, 14 ) === 0
307 ) {
308 $findOnlyinclude = true;
309 continue;
310 }
311
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;
318 }
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.
326
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;
338
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 );
343
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;
350 }
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;
356 }
357
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
364 ) {
365 // Remove leading whitespace from the end of the accumulator
366 $wsLength = $i - $wsStart;
367 $endIndex = count( $accum ) - 1;
368
369 if ( $wsLength > 0
370 && $endIndex >= 0
371 && is_string( $accum[$endIndex] )
372 && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
373 ) {
374 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
375 }
376
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 ] ];
384 }
385
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;
392 }
393
394 if ( $stack->top ) {
395 $part = $stack->top->getCurrentPart();
396 if ( $part->commentEnd !== $wsStart - 1 ) {
397 $part->visualEnd = $wsStart;
398 }
399 // Else comments abutting, no change in visual end
400 $part->commentEnd = $endPos;
401 }
402 $i = $endPos + 1;
403 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
404 $accum[] = [ 'comment', [ $inner ] ];
405 }
406 continue;
407 }
408 $attrStart = $i + strlen( $name ) + 1;
409
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;
419 }
420
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;
427 }
428
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 )
443 ) {
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;
461 }
462 }
463 }
464 // <includeonly> and <noinclude> just become <ignore> tags
465 if ( in_array( $lowerName, $ignoredElements ) ) {
466 $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
467 continue;
468 }
469
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 );
476 }
477
478 $children = [
479 [ 'name', [ $name ] ],
480 [ 'attr', [ $attr ] ],
481 ];
482 if ( $inner !== null ) {
483 $children[] = [ 'inner', [ $inner ] ];
484 }
485 if ( $close !== null ) {
486 $children[] = [ 'close', [ $close ] ];
487 }
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++;
497 }
498
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,
513 ];
514 $stack->push( $piece );
515 $accum =& $stack->getAccum();
516 $stackFlags = $stack->getFlags();
517 if ( isset( $stackFlags['findEquals'] ) ) {
518 $findEquals = $stackFlags['findEquals'];
519 }
520 if ( isset( $stackFlags['findPipe'] ) ) {
521 $findPipe = $stackFlags['findPipe'];
522 }
523 if ( isset( $stackFlags['inHeading'] ) ) {
524 $inHeading = $stackFlags['inHeading'];
525 }
526 $i += $count;
527 }
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 );
545 }
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 ) );
556 }
557 } else {
558 $count = min( $equalsLength, $piece->count );
559 }
560 if ( $count > 0 ) {
561 // Normal match, output <h>
562 $element = [ [ 'possible-h',
563 array_merge(
564 [
565 [ '@level', [ $count ] ],
566 [ '@i', [ $headingIndex++ ] ]
567 ],
568 $accum
569 )
570 ] ];
571 } else {
572 // Single equals sign on its own line, count=0
573 $element = $accum;
574 }
575 } else {
576 // No match, no <h>, just pass down the inner text
577 $element = $accum;
578 }
579 // Unwind the stack
580 $stack->pop();
581 $accum =& $stack->getAccum();
582 $stackFlags = $stack->getFlags();
583 if ( isset( $stackFlags['findEquals'] ) ) {
584 $findEquals = $stackFlags['findEquals'];
585 }
586 if ( isset( $stackFlags['findPipe'] ) ) {
587 $findPipe = $stackFlags['findPipe'];
588 }
589 if ( isset( $stackFlags['inHeading'] ) ) {
590 $inHeading = $stackFlags['inHeading'];
591 }
592
593 // Append the result to the enclosing accumulator
594 array_splice( $accum, count( $accum ), 0, $element );
595
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 );
608
609 $savedPrefix = '';
610 $lineStart = ( $i === 0 ) ? $textStartsInSOLState : ( $text[$i - 1] === "\n" );
611
612 if ( $curChar === "-{" && $count > $curLen ) {
613 // -{ => {{ transition because rightmost wins
614 $savedPrefix = '-';
615 $i++;
616 $curChar = '{';
617 $count--;
618 $rule = $this->rules[$curChar];
619 }
620
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,
630 ];
631
632 $stack->push( $piece );
633 $accum =& $stack->getAccum();
634 $stackFlags = $stack->getFlags();
635 if ( isset( $stackFlags['findEquals'] ) ) {
636 $findEquals = $stackFlags['findEquals'];
637 }
638 if ( isset( $stackFlags['findPipe'] ) ) {
639 $findPipe = $stackFlags['findPipe'];
640 }
641 if ( isset( $stackFlags['inHeading'] ) ) {
642 $inHeading = $stackFlags['inHeading'];
643 }
644 } else {
645 # Add literal brace(s)
646 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
647 }
648 $i += $count;
649 } elseif ( $found === 'close' ) {
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 '}'
657 }
658 $curLen = strlen( $curChar );
659 $count = $curLen > 1
660 ? $curLen
661 : strspn( $text, $curChar, $i, $maxCount );
662
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;
677 }
678 }
679
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;
687 }
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] );
700
701 $children = [];
702
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 === ''
708 ) {
709 $children[] = [ '@lineStart', [ 1 ] ];
710 }
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;
726 }
727 }
728 $element = [ [ $name, $children ] ];
729 }
730
731 # Advance input pointer
732 $i += $matchingCount;
733
734 # Unwind the stack
735 $stack->pop();
736 $accum =& $stack->getAccum();
737
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 )
759 );
760 self::addLiteral( $accum, $piece->savedPrefix . $s );
761 }
762 } elseif ( $piece->savedPrefix !== '' ) {
763 self::addLiteral( $accum, $piece->savedPrefix );
764 }
765
766 $stackFlags = $stack->getFlags();
767 if ( isset( $stackFlags['findEquals'] ) ) {
768 $findEquals = $stackFlags['findEquals'];
769 }
770 if ( isset( $stackFlags['findPipe'] ) ) {
771 $findPipe = $stackFlags['findPipe'];
772 }
773 if ( isset( $stackFlags['inHeading'] ) ) {
774 $inHeading = $stackFlags['inHeading'];
775 }
776
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;
789 }
790 }
791
792 # Output any remaining unclosed brackets
793 foreach ( $stack->stack as $piece ) {
794 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
795 }
796
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';
801 }
802 }
803
804 return [ [ 'root', $stack->rootAccum ] ];
805 }
806
807 private static function addLiteral( array &$accum, string $text ) {
808 $n = count( $accum );
809 if ( $n && is_string( $accum[$n - 1] ) ) {
810 $accum[$n - 1] .= $text;
811 } else {
812 $accum[] = $text;
813 }
814 }
815}
816
818class_alias( Preprocessor_Hash::class, 'Preprocessor_Hash' );
Expansion frame with custom arguments.
An expansion frame, used as a context to expand the result of preprocessToObj()
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:147
Differences from DOM schema:
__construct(Parser $parser, ?WANObjectCache $wanCache=null, array $options=[])
const CACHE_VERSION
Cache format version.
int false $cacheThreshold
Min wikitext size for which to cache DOM tree.
preprocessToObj( $text, $flags=0)
Get the document object model for the given wikitext.
const DOM_LANG_CONVERSION_DISABLED
Language conversion construct omission flag for Preprocessor::preprocessToObj()
const DOM_FOR_INCLUSION
Transclusion mode flag for Preprocessor::preprocessToObj()
Multi-datacenter aware caching interface.