MediaWiki master
Preprocessor_Hash.php
Go to the documentation of this file.
1<?php
42// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
45 protected const CACHE_VERSION = 4;
46
48 protected $cacheThreshold;
49
57 public function __construct(
60 array $options = []
61 ) {
62 parent::__construct( $parser, $wanCache, $options );
63
64 $this->cacheThreshold = $options['cacheThreshold'] ?? false;
65 }
66
70 public function newFrame() {
71 return new PPFrame_Hash( $this );
72 }
73
78 public function newCustomFrame( $args ) {
79 return new PPCustomFrame_Hash( $this, $args );
80 }
81
86 public function newPartNodeArray( $values ) {
87 $list = [];
88
89 foreach ( $values as $k => $val ) {
90 if ( is_int( $k ) ) {
91 $store = [ [ 'part', [
92 [ 'name', [ [ '@index', [ $k ] ] ] ],
93 [ 'value', [ strval( $val ) ] ],
94 ] ] ];
95 } else {
96 $store = [ [ 'part', [
97 [ 'name', [ strval( $k ) ] ],
98 '=',
99 [ 'value', [ strval( $val ) ] ],
100 ] ] ];
101 }
102
103 $list[] = new PPNode_Hash_Tree( $store, 0 );
104 }
105
106 return new PPNode_Hash_Array( $list );
107 }
108
109 public function preprocessToObj( $text, $flags = 0 ) {
110 if ( $this->disableLangConversion ) {
111 // Language conversions are globally disabled; implicitly set flag
113 }
114
115 $domTreeArray = null;
116 if (
117 $this->cacheThreshold !== false &&
118 strlen( $text ) >= $this->cacheThreshold &&
119 ( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED
120 ) {
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 );
126
127 return json_encode(
128 $domTreeArray,
129 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
130 );
131 },
132 [ 'version' => self::CACHE_VERSION, 'segmentable' => true ]
133 );
134 $domTreeArray ??= json_decode( $domTreeJson );
135 }
136
137 $domTreeArray ??= $this->buildDomTreeArrayFromText( $text, $flags );
138
139 return new PPNode_Hash_Tree( $domTreeArray, 0 );
140 }
141
147 private function buildDomTreeArrayFromText( $text, $flags ) {
148 $forInclusion = ( $flags & self::DOM_FOR_INCLUSION );
149 $langConversionDisabled = ( $flags & self::DOM_LANG_CONVERSION_DISABLED );
150
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>' )
160 ) {
161 $enableOnlyinclude = true;
162 }
163 } else {
164 $ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ];
165 $ignoredElements = [ 'includeonly' ];
166 $xmlishElements[] = 'includeonly';
167 }
168 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
169
170 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
171 $elementsRegex = "~(?:$xmlishRegex)(?=\s|\/>|>)|!--~iA";
172
173 $stack = new PPDStack_Hash;
174
175 $searchBase = "[{<\n";
176 if ( !$langConversionDisabled ) {
177 $searchBase .= '-';
178 }
179
180 // For fast reverse searches
181 $revText = strrev( $text );
182 $lengthText = strlen( $text );
183
184 // Input pointer, starts out pointing to a pseudo-newline before the start
185 $i = 0;
186 // Current accumulator. See the doc comment for Preprocessor_Hash for the format.
187 $accum =& $stack->getAccum();
188 // True to find equals signs in arguments
189 $findEquals = false;
190 // True to take notice of pipe characters
191 $findPipe = false;
192 $headingIndex = 1;
193 // True if $i is inside a possible heading
194 $inHeading = false;
195 // True if there are no more greater-than (>) signs right of $i
196 $noMoreGT = false;
197 // Map of tag name => true if there are no more closing tags of given type right of $i
198 $noMoreClosingTag = [];
199 // True to ignore all input up to the next <onlyinclude>
200 $findOnlyinclude = $enableOnlyinclude;
201 // Do a line-start run without outputting an LF character
202 $fakeLineStart = true;
203
204 while ( true ) {
205 if ( $findOnlyinclude ) {
206 // Ignore all input up to the next <onlyinclude>
207 $startPos = strpos( $text, '<onlyinclude>', $i );
208 if ( $startPos === false ) {
209 // Ignored section runs to the end
210 $accum[] = [ 'ignore', [ substr( $text, $i ) ] ];
211 break;
212 }
213 $tagEndPos = $startPos + 13; // past-the-end of <onlyinclude>
214 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
215 $i = $tagEndPos;
216 $findOnlyinclude = false;
217 }
218
219 if ( $fakeLineStart ) {
220 $found = 'line-start';
221 $curChar = '';
222 } else {
223 # Find next opening brace, closing brace or pipe
224 $search = $searchBase;
225 if ( $stack->top === false ) {
226 $currentClosing = '';
227 } else {
228 $currentClosing = $stack->top->close;
229 $search .= $currentClosing;
230 }
231 if ( $findPipe ) {
232 $search .= '|';
233 }
234 if ( $findEquals ) {
235 // First equals will be for the template
236 $search .= '=';
237 }
238 $rule = null;
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;
244 }
245 if ( $i >= $lengthText ) {
246 if ( $currentClosing === "\n" ) {
247 // Do a past-the-end run to finish off the heading
248 $curChar = '';
249 $found = 'line-end';
250 } else {
251 # All done
252 break;
253 }
254 } else {
255 $curChar = $curTwoChar = $text[$i];
256 if ( $i + 1 < $lengthText ) {
257 $curTwoChar .= $text[$i + 1];
258 }
259 if ( $curChar === '|' ) {
260 $found = 'pipe';
261 } elseif ( $curChar === '=' ) {
262 $found = 'equals';
263 } elseif ( $curChar === '<' ) {
264 $found = 'angle';
265 } elseif ( $curChar === "\n" ) {
266 if ( $inHeading ) {
267 $found = 'line-end';
268 } else {
269 $found = 'line-start';
270 }
271 } elseif ( $curTwoChar === $currentClosing ) {
272 $found = 'close';
273 $curChar = $curTwoChar;
274 } elseif ( $curChar === $currentClosing ) {
275 $found = 'close';
276 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
277 $curChar = $curTwoChar;
278 $found = 'open';
279 $rule = $this->rules[$curChar];
280 } elseif ( isset( $this->rules[$curChar] ) ) {
281 $found = 'open';
282 $rule = $this->rules[$curChar];
283 } else {
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 );
291 }
292 ++$i;
293 continue;
294 }
295 }
296 }
297
298 if ( $found === 'angle' ) {
299 // Handle </onlyinclude>
300 if ( $enableOnlyinclude
301 && substr_compare( $text, '</onlyinclude>', $i, 14 ) === 0
302 ) {
303 $findOnlyinclude = true;
304 continue;
305 }
306
307 // Determine element name
308 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
309 // Element name missing or not listed
310 self::addLiteral( $accum, '<' );
311 ++$i;
312 continue;
313 }
314 $name = $matches[0];
315 // Handle comments
316 if ( $name === '!--' ) {
317 // To avoid leaving blank lines, when a sequence of
318 // space-separated comments is both preceded and followed by
319 // a newline (ignoring spaces), then
320 // trim leading and trailing spaces and the trailing newline.
321
322 // Find the end
323 $endPos = strpos( $text, '-->', $i + 4 );
324 if ( $endPos === false ) {
325 // Unclosed comment in input, runs to end
326 $inner = substr( $text, $i );
327 $accum[] = [ 'comment', [ $inner ] ];
328 $i = $lengthText;
329 } else {
330 // Search backwards for leading whitespace
331 // $wsStart is the first char of the comment (first of the leading space or '<')
332 $wsStart = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
333
334 // $wsEnd will be the char *after* the comment (after last space or the '>' if there's no space)
335 $wsEnd = $endPos + 3; // add length of -->
336 // Search forwards for trailing whitespace
337 $wsEnd += strspn( $text, " \t", $wsEnd );
338
339 // Keep looking forward as long as we're finding more comments on the line
340 $comments = [ [ $wsStart, $wsEnd ] ];
341 while ( substr_compare( $text, '<!--', $wsEnd, 4 ) === 0 ) {
342 $c = strpos( $text, '-->', $wsEnd + 4 );
343 if ( $c === false ) {
344 break;
345 }
346 $c += 3; // add length of -->
347 // Search forwards for trailing whitespace
348 $c += strspn( $text, " \t", $c );
349 $comments[] = [ $wsEnd, $c ];
350 $wsEnd = $c;
351 }
352
353 // Eat the line if possible
354 // TODO: This could theoretically be done if $wsStart === 0, i.e. for comments at
355 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
356 // it's a possible beneficial b/c break.
357 if ( $wsStart > 0 && substr_compare( $text, "\n", $wsStart - 1, 1 ) === 0
358 && substr_compare( $text, "\n", $wsEnd, 1 ) === 0
359 ) {
360 // Remove leading whitespace from the end of the accumulator
361 $wsLength = $i - $wsStart;
362 $endIndex = count( $accum ) - 1;
363
364 if ( $wsLength > 0
365 && $endIndex >= 0
366 && is_string( $accum[$endIndex] )
367 && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
368 ) {
369 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
370 }
371
372 // Dump all but the last comment to the accumulator
373 // $endPos includes the newline from the if above, want also eat that
374 [ $startPos, $endPos ] = array_pop( $comments );
375 foreach ( $comments as [ $cStartPos, $cEndPos ] ) {
376 // $cEndPos is the next char, no +1 needed to get correct length between start/end
377 $inner = substr( $text, $cStartPos, $cEndPos - $cStartPos );
378 $accum[] = [ 'comment', [ $inner ] ];
379 }
380
381 // Do a line-start run next time to look for headings after the comment
382 $fakeLineStart = true;
383 } else {
384 // No line to eat, just take the comment itself
385 $startPos = $i;
386 $endPos += 2;
387 }
388
389 if ( $stack->top ) {
390 $part = $stack->top->getCurrentPart();
391 if ( $part->commentEnd !== $wsStart - 1 ) {
392 $part->visualEnd = $wsStart;
393 }
394 // Else comments abutting, no change in visual end
395 $part->commentEnd = $endPos;
396 }
397 $i = $endPos + 1;
398 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
399 $accum[] = [ 'comment', [ $inner ] ];
400 }
401 continue;
402 }
403 $attrStart = $i + strlen( $name ) + 1;
404
405 // Find end of tag
406 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
407 if ( $tagEndPos === false ) {
408 // Infinite backtrack
409 // Disable tag search to prevent worst-case O(N^2) performance
410 $noMoreGT = true;
411 self::addLiteral( $accum, '<' );
412 ++$i;
413 continue;
414 }
415
416 $lowerName = strtolower( $name );
417 // Handle ignored tags
418 if ( in_array( $lowerName, $ignoredTags ) ) {
419 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
420 $i = $tagEndPos + 1;
421 continue;
422 }
423
424 $tagStartPos = $i;
425 if ( $text[$tagEndPos - 1] === '/' ) {
426 // Short end tag
427 $attrEnd = $tagEndPos - 1;
428 $inner = null;
429 $i = $tagEndPos + 1;
430 $close = null;
431 } else {
432 $attrEnd = $tagEndPos;
433 // Find closing tag
434 if (
435 !isset( $noMoreClosingTag[$lowerName] ) &&
436 preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
437 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
438 ) {
439 [ $close, $closeTagStartPos ] = $matches[0];
440 $inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 );
441 $i = $closeTagStartPos + strlen( $close );
442 } else {
443 // No end tag
444 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
445 // Let it run out to the end of the text.
446 $inner = substr( $text, $tagEndPos + 1 );
447 $i = $lengthText;
448 $close = null;
449 } else {
450 // Don't match the tag, treat opening tag as literal and resume parsing.
451 $i = $tagEndPos + 1;
452 self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
453 // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
454 $noMoreClosingTag[$lowerName] = true;
455 continue;
456 }
457 }
458 }
459 // <includeonly> and <noinclude> just become <ignore> tags
460 if ( in_array( $lowerName, $ignoredElements ) ) {
461 $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
462 continue;
463 }
464
465 if ( $attrEnd <= $attrStart ) {
466 $attr = '';
467 } else {
468 // Note that the attr element contains the whitespace between name and attribute,
469 // this is necessary for precise reconstruction during pre-save transform.
470 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
471 }
472
473 $children = [
474 [ 'name', [ $name ] ],
475 [ 'attr', [ $attr ] ],
476 ];
477 if ( $inner !== null ) {
478 $children[] = [ 'inner', [ $inner ] ];
479 }
480 if ( $close !== null ) {
481 $children[] = [ 'close', [ $close ] ];
482 }
483 $accum[] = [ 'ext', $children ];
484 } elseif ( $found === 'line-start' ) {
485 // Is this the start of a heading?
486 // Line break belongs before the heading element in any case
487 if ( $fakeLineStart ) {
488 $fakeLineStart = false;
489 } else {
490 self::addLiteral( $accum, $curChar );
491 $i++;
492 }
493
494 // Examine upto 6 characters
495 $count = strspn( $text, '=', $i, min( $lengthText, 6 ) );
496 if ( $count === 1 && $findEquals ) {
497 // DWIM: This looks kind of like a name/value separator.
498 // Let's let the equals handler have it and break the potential
499 // heading. This is heuristic, but AFAICT the methods for
500 // completely correct disambiguation are very complex.
501 } elseif ( $count > 0 ) {
502 $piece = [
503 'open' => "\n",
504 'close' => "\n",
505 'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ],
506 'startPos' => $i,
507 'count' => $count,
508 ];
509 $stack->push( $piece );
510 $accum =& $stack->getAccum();
511 $stackFlags = $stack->getFlags();
512 if ( isset( $stackFlags['findEquals'] ) ) {
513 $findEquals = $stackFlags['findEquals'];
514 }
515 if ( isset( $stackFlags['findPipe'] ) ) {
516 $findPipe = $stackFlags['findPipe'];
517 }
518 if ( isset( $stackFlags['inHeading'] ) ) {
519 $inHeading = $stackFlags['inHeading'];
520 }
521 $i += $count;
522 }
523 } elseif ( $found === 'line-end' ) {
524 $piece = $stack->top;
525 // A heading must be open, otherwise \n wouldn't have been in the search list
526 // FIXME: Don't use assert()
527 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
528 assert( $piece->open === "\n" );
529 $part = $piece->getCurrentPart();
530 // Search back through the input to see if it has a proper close.
531 // Do this using the reversed string since the other solutions
532 // (end anchor, etc.) are inefficient.
533 $wsLength = strspn( $revText, " \t", $lengthText - $i );
534 $searchStart = $i - $wsLength;
535 if ( $part->commentEnd === $searchStart - 1 ) {
536 // Comment found at line end
537 // Search for equals signs before the comment
538 $searchStart = $part->visualEnd;
539 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
540 }
541 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
542 if ( $equalsLength > 0 ) {
543 if ( $searchStart - $equalsLength === $piece->startPos ) {
544 // This is just a single string of equals signs on its own line
545 // Replicate the doHeadings behavior /={count}(.+)={count}/
546 // First find out how many equals signs there really are (don't stop at 6)
547 if ( $equalsLength < 3 ) {
548 $count = 0;
549 } else {
550 $count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) );
551 }
552 } else {
553 $count = min( $equalsLength, $piece->count );
554 }
555 if ( $count > 0 ) {
556 // Normal match, output <h>
557 $element = [ [ 'possible-h',
558 array_merge(
559 [
560 [ '@level', [ $count ] ],
561 [ '@i', [ $headingIndex++ ] ]
562 ],
563 $accum
564 )
565 ] ];
566 } else {
567 // Single equals sign on its own line, count=0
568 $element = $accum;
569 }
570 } else {
571 // No match, no <h>, just pass down the inner text
572 $element = $accum;
573 }
574 // Unwind the stack
575 $stack->pop();
576 $accum =& $stack->getAccum();
577 $stackFlags = $stack->getFlags();
578 if ( isset( $stackFlags['findEquals'] ) ) {
579 $findEquals = $stackFlags['findEquals'];
580 }
581 if ( isset( $stackFlags['findPipe'] ) ) {
582 $findPipe = $stackFlags['findPipe'];
583 }
584 if ( isset( $stackFlags['inHeading'] ) ) {
585 $inHeading = $stackFlags['inHeading'];
586 }
587
588 // Append the result to the enclosing accumulator
589 array_splice( $accum, count( $accum ), 0, $element );
590
591 // Note that we do NOT increment the input pointer.
592 // This is because the closing linebreak could be the opening linebreak of
593 // another heading. Infinite loops are avoided because the next iteration MUST
594 // hit the heading open case above, which unconditionally increments the
595 // input pointer.
596 } elseif ( $found === 'open' ) {
597 # count opening brace characters
598 $curLen = strlen( $curChar );
599 $count = $curLen > 1
600 # allow the final character to repeat
601 ? strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1
602 : strspn( $text, $curChar, $i );
603
604 $savedPrefix = '';
605 $lineStart = $i > 0 && $text[$i - 1] === "\n";
606
607 if ( $curChar === "-{" && $count > $curLen ) {
608 // -{ => {{ transition because rightmost wins
609 $savedPrefix = '-';
610 $i++;
611 $curChar = '{';
612 $count--;
613 $rule = $this->rules[$curChar];
614 }
615
616 # we need to add to stack only if opening brace count is enough for one of the rules
617 if ( $count >= $rule['min'] ) {
618 # Add it to the stack
619 $piece = [
620 'open' => $curChar,
621 'close' => $rule['end'],
622 'savedPrefix' => $savedPrefix,
623 'count' => $count,
624 'lineStart' => $lineStart,
625 ];
626
627 $stack->push( $piece );
628 $accum =& $stack->getAccum();
629 $stackFlags = $stack->getFlags();
630 if ( isset( $stackFlags['findEquals'] ) ) {
631 $findEquals = $stackFlags['findEquals'];
632 }
633 if ( isset( $stackFlags['findPipe'] ) ) {
634 $findPipe = $stackFlags['findPipe'];
635 }
636 if ( isset( $stackFlags['inHeading'] ) ) {
637 $inHeading = $stackFlags['inHeading'];
638 }
639 } else {
640 # Add literal brace(s)
641 self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
642 }
643 $i += $count;
644 } elseif ( $found === 'close' ) {
646 $piece = $stack->top;
647 '@phan-var PPDStackElement_Hash $piece';
648 # lets check if there are enough characters for closing brace
649 $maxCount = $piece->count;
650 if ( $piece->close === '}-' && $curChar === '}' ) {
651 $maxCount--; # don't try to match closing '-' as a '}'
652 }
653 $curLen = strlen( $curChar );
654 $count = $curLen > 1
655 ? $curLen
656 : strspn( $text, $curChar, $i, $maxCount );
657
658 # check for maximum matching characters (if there are 5 closing
659 # characters, we will probably need only 3 - depending on the rules)
660 $rule = $this->rules[$piece->open];
661 if ( $count > $rule['max'] ) {
662 # The specified maximum exists in the callback array, unless the caller
663 # has made an error
664 $matchingCount = $rule['max'];
665 } else {
666 # Count is less than the maximum
667 # Skip any gaps in the callback array to find the true largest match
668 # Need to use array_key_exists not isset because the callback can be null
669 $matchingCount = $count;
670 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
671 --$matchingCount;
672 }
673 }
674
675 if ( $matchingCount <= 0 ) {
676 # No matching element found in callback array
677 # Output a literal closing brace and continue
678 $endText = substr( $text, $i, $count );
679 self::addLiteral( $accum, $endText );
680 $i += $count;
681 continue;
682 }
683 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
684 $name = $rule['names'][$matchingCount];
685 if ( $name === null ) {
686 // No element, just literal text
687 $endText = substr( $text, $i, $matchingCount );
688 $element = $piece->breakSyntax( $matchingCount );
689 self::addLiteral( $element, $endText );
690 } else {
691 # Create XML element
692 $parts = $piece->parts;
693 $titleAccum = $parts[0]->out;
694 unset( $parts[0] );
695
696 $children = [];
697
698 # The invocation is at the start of the line if lineStart is set in
699 # the stack, and all opening brackets are used up.
700 if ( $maxCount === $matchingCount &&
701 $piece->lineStart &&
702 $piece->savedPrefix === ''
703 ) {
704 $children[] = [ '@lineStart', [ 1 ] ];
705 }
706 $titleNode = [ 'title', $titleAccum ];
707 $children[] = $titleNode;
708 $argIndex = 1;
709 foreach ( $parts as $part ) {
710 if ( isset( $part->eqpos ) ) {
711 $equalsNode = $part->out[$part->eqpos];
712 $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
713 $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
714 $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
715 $children[] = $partNode;
716 } else {
717 $nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ];
718 $valueNode = [ 'value', $part->out ];
719 $partNode = [ 'part', [ $nameNode, $valueNode ] ];
720 $children[] = $partNode;
721 }
722 }
723 $element = [ [ $name, $children ] ];
724 }
725
726 # Advance input pointer
727 $i += $matchingCount;
728
729 # Unwind the stack
730 $stack->pop();
731 $accum =& $stack->getAccum();
732
733 # Re-add the old stack element if it still has unmatched opening characters remaining
734 if ( $matchingCount < $piece->count ) {
735 $piece->parts = [ new PPDPart_Hash ];
736 $piece->count -= $matchingCount;
737 # do we still qualify for any callback with remaining count?
738 $min = $this->rules[$piece->open]['min'];
739 if ( $piece->count >= $min ) {
740 $stack->push( $piece );
741 $accum =& $stack->getAccum();
742 } elseif ( $piece->count === 1 && $piece->open === '{' && $piece->savedPrefix === '-' ) {
743 $piece->savedPrefix = '';
744 $piece->open = '-{';
745 $piece->count = 2;
746 $piece->close = $this->rules[$piece->open]['end'];
747 $stack->push( $piece );
748 $accum =& $stack->getAccum();
749 } else {
750 $s = substr( $piece->open, 0, -1 );
751 $s .= str_repeat(
752 substr( $piece->open, -1 ),
753 $piece->count - strlen( $s )
754 );
755 self::addLiteral( $accum, $piece->savedPrefix . $s );
756 }
757 } elseif ( $piece->savedPrefix !== '' ) {
758 self::addLiteral( $accum, $piece->savedPrefix );
759 }
760
761 $stackFlags = $stack->getFlags();
762 if ( isset( $stackFlags['findEquals'] ) ) {
763 $findEquals = $stackFlags['findEquals'];
764 }
765 if ( isset( $stackFlags['findPipe'] ) ) {
766 $findPipe = $stackFlags['findPipe'];
767 }
768 if ( isset( $stackFlags['inHeading'] ) ) {
769 $inHeading = $stackFlags['inHeading'];
770 }
771
772 # Add XML element to the enclosing accumulator
773 array_splice( $accum, count( $accum ), 0, $element );
774 } elseif ( $found === 'pipe' ) {
775 $findEquals = true; // shortcut for getFlags()
776 $stack->addPart();
777 $accum =& $stack->getAccum();
778 ++$i;
779 } elseif ( $found === 'equals' ) {
780 $findEquals = false; // shortcut for getFlags()
781 $accum[] = [ 'equals', [ '=' ] ];
782 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
783 ++$i;
784 }
785 }
786
787 # Output any remaining unclosed brackets
788 foreach ( $stack->stack as $piece ) {
789 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
790 }
791
792 # Enable top-level headings
793 foreach ( $stack->rootAccum as &$node ) {
794 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
795 $node[PPNode_Hash_Tree::NAME] = 'h';
796 }
797 }
798
799 return [ [ 'root', $stack->rootAccum ] ];
800 }
801
802 private static function addLiteral( array &$accum, $text ) {
803 $n = count( $accum );
804 if ( $n && is_string( $accum[$n - 1] ) ) {
805 $accum[$n - 1] .= $text;
806 } else {
807 $accum[] = $text;
808 }
809 }
810}
Expansion frame with custom arguments.
Stack class to help Preprocessor::preprocessToObj()
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:118
Differences from DOM schema:
int false $cacheThreshold
Min wikitext size for which to cache DOM tree.
__construct(Parser $parser, WANObjectCache $wanCache=null, array $options=[])
preprocessToObj( $text, $flags=0)
Get the document object model for the given wikitext.
const CACHE_VERSION
Cache format version.
const DOM_LANG_CONVERSION_DISABLED
Language conversion construct omission flag for Preprocessor::preprocessToObj()
WANObjectCache $wanCache
const DOM_FOR_INCLUSION
Transclusion mode flag for Preprocessor::preprocessToObj()
Multi-datacenter aware caching interface.
return true
Definition router.php:92