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 ( strpos( $text, '<onlyinclude>' ) !== false
159  && strpos( $text, '</onlyinclude>' ) !== false
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 + strlen( '<onlyinclude>' ); // past-the-end
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  $matches = false;
300  // Handle </onlyinclude>
301  if ( $enableOnlyinclude
302  && str_starts_with( substr( $text, $i ), '</onlyinclude>' )
303  ) {
304  $findOnlyinclude = true;
305  continue;
306  }
307 
308  // Determine element name
309  if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
310  // Element name missing or not listed
311  self::addLiteral( $accum, '<' );
312  ++$i;
313  continue;
314  }
315  // Handle comments
316  if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
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 = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
332 
333  // Search forwards for trailing whitespace
334  // $wsEnd will be the position of the last space (or the '>' if there's none)
335  $wsEnd = $endPos + 2 + strspn( $text, " \t", $endPos + 3 );
336 
337  // Keep looking forward as long as we're finding more
338  // comments.
339  $comments = [ [ $wsStart, $wsEnd ] ];
340  while ( substr( $text, $wsEnd + 1, 4 ) == '<!--' ) {
341  $c = strpos( $text, '-->', $wsEnd + 5 );
342  if ( $c === false ) {
343  break;
344  }
345  $c = $c + 2 + strspn( $text, " \t", $c + 3 );
346  $comments[] = [ $wsEnd + 1, $c ];
347  $wsEnd = $c;
348  }
349 
350  // Eat the line if possible
351  // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
352  // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
353  // it's a possible beneficial b/c break.
354  if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
355  && substr( $text, $wsEnd + 1, 1 ) == "\n"
356  ) {
357  // Remove leading whitespace from the end of the accumulator
358  $wsLength = $i - $wsStart;
359  $endIndex = count( $accum ) - 1;
360 
361  if ( $wsLength > 0
362  && $endIndex >= 0
363  && is_string( $accum[$endIndex] )
364  && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
365  ) {
366  $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
367  }
368 
369  // Dump all but the last comment to the accumulator
370  foreach ( $comments as $j => $com ) {
371  $startPos = $com[0];
372  $endPos = $com[1] + 1;
373  if ( $j == ( count( $comments ) - 1 ) ) {
374  break;
375  }
376  $inner = substr( $text, $startPos, $endPos - $startPos );
377  $accum[] = [ 'comment', [ $inner ] ];
378  }
379 
380  // Do a line-start run next time to look for headings after the comment
381  $fakeLineStart = true;
382  } else {
383  // No line to eat, just take the comment itself
384  $startPos = $i;
385  $endPos += 2;
386  }
387 
388  if ( $stack->top ) {
389  $part = $stack->top->getCurrentPart();
390  if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
391  $part->visualEnd = $wsStart;
392  }
393  // Else comments abutting, no change in visual end
394  $part->commentEnd = $endPos;
395  }
396  $i = $endPos + 1;
397  $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
398  $accum[] = [ 'comment', [ $inner ] ];
399  }
400  continue;
401  }
402  $name = $matches[1];
403  $lowerName = strtolower( $name );
404  $attrStart = $i + strlen( $name ) + 1;
405 
406  // Find end of tag
407  $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
408  if ( $tagEndPos === false ) {
409  // Infinite backtrack
410  // Disable tag search to prevent worst-case O(N^2) performance
411  $noMoreGT = true;
412  self::addLiteral( $accum, '<' );
413  ++$i;
414  continue;
415  }
416 
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[$name] ) &&
436  preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
437  $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
438  ) {
439  $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
440  $i = $matches[0][1] + strlen( $matches[0][0] );
441  $close = $matches[0][0];
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,
453  substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
454  // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
455  $noMoreClosingTag[$name] = true;
456  continue;
457  }
458  }
459  }
460  // <includeonly> and <noinclude> just become <ignore> tags
461  if ( in_array( $lowerName, $ignoredElements ) ) {
462  $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
463  continue;
464  }
465 
466  if ( $attrEnd <= $attrStart ) {
467  $attr = '';
468  } else {
469  // Note that the attr element contains the whitespace between name and attribute,
470  // this is necessary for precise reconstruction during pre-save transform.
471  $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
472  }
473 
474  $children = [
475  [ 'name', [ $name ] ],
476  [ 'attr', [ $attr ] ] ];
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( strlen( $text ), 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  $stack->push( $piece );
509  $accum =& $stack->getAccum();
510  $stackFlags = $stack->getFlags();
511  if ( isset( $stackFlags['findEquals'] ) ) {
512  $findEquals = $stackFlags['findEquals'];
513  }
514  if ( isset( $stackFlags['findPipe'] ) ) {
515  $findPipe = $stackFlags['findPipe'];
516  }
517  if ( isset( $stackFlags['inHeading'] ) ) {
518  $inHeading = $stackFlags['inHeading'];
519  }
520  $i += $count;
521  }
522  } elseif ( $found == 'line-end' ) {
523  $piece = $stack->top;
524  // A heading must be open, otherwise \n wouldn't have been in the search list
525  // FIXME: Don't use assert()
526  // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
527  assert( $piece->open === "\n" );
528  $part = $piece->getCurrentPart();
529  // Search back through the input to see if it has a proper close.
530  // Do this using the reversed string since the other solutions
531  // (end anchor, etc.) are inefficient.
532  $wsLength = strspn( $revText, " \t", $lengthText - $i );
533  $searchStart = $i - $wsLength;
534  if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
535  // Comment found at line end
536  // Search for equals signs before the comment
537  $searchStart = $part->visualEnd;
538  $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
539  }
540  $count = $piece->count;
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  $count = $equalsLength;
548  if ( $count < 3 ) {
549  $count = 0;
550  } else {
551  $count = min( 6, intval( ( $count - 1 ) / 2 ) );
552  }
553  } else {
554  $count = min( $equalsLength, $count );
555  }
556  if ( $count > 0 ) {
557  // Normal match, output <h>
558  $element = [ [ 'possible-h',
559  array_merge(
560  [
561  [ '@level', [ $count ] ],
562  [ '@i', [ $headingIndex++ ] ]
563  ],
564  $accum
565  )
566  ] ];
567  } else {
568  // Single equals sign on its own line, count=0
569  $element = $accum;
570  }
571  } else {
572  // No match, no <h>, just pass down the inner text
573  $element = $accum;
574  }
575  // Unwind the stack
576  $stack->pop();
577  $accum =& $stack->getAccum();
578  $stackFlags = $stack->getFlags();
579  if ( isset( $stackFlags['findEquals'] ) ) {
580  $findEquals = $stackFlags['findEquals'];
581  }
582  if ( isset( $stackFlags['findPipe'] ) ) {
583  $findPipe = $stackFlags['findPipe'];
584  }
585  if ( isset( $stackFlags['inHeading'] ) ) {
586  $inHeading = $stackFlags['inHeading'];
587  }
588 
589  // Append the result to the enclosing accumulator
590  array_splice( $accum, count( $accum ), 0, $element );
591 
592  // Note that we do NOT increment the input pointer.
593  // This is because the closing linebreak could be the opening linebreak of
594  // another heading. Infinite loops are avoided because the next iteration MUST
595  // hit the heading open case above, which unconditionally increments the
596  // input pointer.
597  } elseif ( $found == 'open' ) {
598  # count opening brace characters
599  $curLen = strlen( $curChar );
600  $count = ( $curLen > 1 ) ?
601  # allow the final character to repeat
602  strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
603  strspn( $text, $curChar, $i );
604 
605  $savedPrefix = '';
606  $lineStart = ( $i > 0 && $text[$i - 1] == "\n" );
607 
608  if ( $curChar === "-{" && $count > $curLen ) {
609  // -{ => {{ transition because rightmost wins
610  $savedPrefix = '-';
611  $i++;
612  $curChar = '{';
613  $count--;
614  $rule = $this->rules[$curChar];
615  }
616 
617  # we need to add to stack only if opening brace count is enough for one of the rules
618  if ( $count >= $rule['min'] ) {
619  # Add it to the stack
620  $piece = [
621  'open' => $curChar,
622  'close' => $rule['end'],
623  'savedPrefix' => $savedPrefix,
624  'count' => $count,
625  'lineStart' => $lineStart,
626  ];
627 
628  $stack->push( $piece );
629  $accum =& $stack->getAccum();
630  $stackFlags = $stack->getFlags();
631  if ( isset( $stackFlags['findEquals'] ) ) {
632  $findEquals = $stackFlags['findEquals'];
633  }
634  if ( isset( $stackFlags['findPipe'] ) ) {
635  $findPipe = $stackFlags['findPipe'];
636  }
637  if ( isset( $stackFlags['inHeading'] ) ) {
638  $inHeading = $stackFlags['inHeading'];
639  }
640  } else {
641  # Add literal brace(s)
642  self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
643  }
644  $i += $count;
645  } elseif ( $found == 'close' ) {
647  $piece = $stack->top;
648  '@phan-var PPDStackElement_Hash $piece';
649  # lets check if there are enough characters for closing brace
650  $maxCount = $piece->count;
651  if ( $piece->close === '}-' && $curChar === '}' ) {
652  $maxCount--; # don't try to match closing '-' as a '}'
653  }
654  $curLen = strlen( $curChar );
655  $count = ( $curLen > 1 ) ? $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  !empty( $piece->lineStart ) &&
702  strlen( $piece->savedPrefix ) == 0 ) {
703  $children[] = [ '@lineStart', [ 1 ] ];
704  }
705  $titleNode = [ 'title', $titleAccum ];
706  $children[] = $titleNode;
707  $argIndex = 1;
708  foreach ( $parts as $part ) {
709  if ( isset( $part->eqpos ) ) {
710  $equalsNode = $part->out[$part->eqpos];
711  $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
712  $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
713  $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
714  $children[] = $partNode;
715  } else {
716  $nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ];
717  $valueNode = [ 'value', $part->out ];
718  $partNode = [ 'part', [ $nameNode, $valueNode ] ];
719  $children[] = $partNode;
720  }
721  }
722  $element = [ [ $name, $children ] ];
723  }
724 
725  # Advance input pointer
726  $i += $matchingCount;
727 
728  # Unwind the stack
729  $stack->pop();
730  $accum =& $stack->getAccum();
731 
732  # Re-add the old stack element if it still has unmatched opening characters remaining
733  if ( $matchingCount < $piece->count ) {
734  $piece->parts = [ new PPDPart_Hash ];
735  $piece->count -= $matchingCount;
736  # do we still qualify for any callback with remaining count?
737  $min = $this->rules[$piece->open]['min'];
738  if ( $piece->count >= $min ) {
739  $stack->push( $piece );
740  $accum =& $stack->getAccum();
741  } elseif ( $piece->count == 1 && $piece->open === '{' && $piece->savedPrefix === '-' ) {
742  $piece->savedPrefix = '';
743  $piece->open = '-{';
744  $piece->count = 2;
745  $piece->close = $this->rules[$piece->open]['end'];
746  $stack->push( $piece );
747  $accum =& $stack->getAccum();
748  } else {
749  $s = substr( $piece->open, 0, -1 );
750  $s .= str_repeat(
751  substr( $piece->open, -1 ),
752  $piece->count - strlen( $s )
753  );
754  self::addLiteral( $accum, $piece->savedPrefix . $s );
755  }
756  } elseif ( $piece->savedPrefix !== '' ) {
757  self::addLiteral( $accum, $piece->savedPrefix );
758  }
759 
760  $stackFlags = $stack->getFlags();
761  if ( isset( $stackFlags['findEquals'] ) ) {
762  $findEquals = $stackFlags['findEquals'];
763  }
764  if ( isset( $stackFlags['findPipe'] ) ) {
765  $findPipe = $stackFlags['findPipe'];
766  }
767  if ( isset( $stackFlags['inHeading'] ) ) {
768  $inHeading = $stackFlags['inHeading'];
769  }
770 
771  # Add XML element to the enclosing accumulator
772  array_splice( $accum, count( $accum ), 0, $element );
773  } elseif ( $found == 'pipe' ) {
774  $findEquals = true; // shortcut for getFlags()
775  $stack->addPart();
776  $accum =& $stack->getAccum();
777  ++$i;
778  } elseif ( $found == 'equals' ) {
779  $findEquals = false; // shortcut for getFlags()
780  $accum[] = [ 'equals', [ '=' ] ];
781  $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
782  ++$i;
783  }
784  }
785 
786  # Output any remaining unclosed brackets
787  foreach ( $stack->stack as $piece ) {
788  array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
789  }
790 
791  # Enable top-level headings
792  foreach ( $stack->rootAccum as &$node ) {
793  if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
794  $node[PPNode_Hash_Tree::NAME] = 'h';
795  }
796  }
797 
798  return [ [ 'root', $stack->rootAccum ] ];
799  }
800 
801  private static function addLiteral( array &$accum, $text ) {
802  $n = count( $accum );
803  if ( $n && is_string( $accum[$n - 1] ) ) {
804  $accum[$n - 1] .= $text;
805  } else {
806  $accum[] = $text;
807  }
808  }
809 }
$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:97
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.
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
return true
Definition: router.php:90