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