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