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 $com ) {
376  [ $cStartPos, $cEndPos ] = $com;
377  // $cEndPos is the next char, no +1 needed to get correct length between start/end
378  $inner = substr( $text, $cStartPos, $cEndPos - $cStartPos );
379  $accum[] = [ 'comment', [ $inner ] ];
380  }
381 
382  // Do a line-start run next time to look for headings after the comment
383  $fakeLineStart = true;
384  } else {
385  // No line to eat, just take the comment itself
386  $startPos = $i;
387  $endPos += 2;
388  }
389 
390  if ( $stack->top ) {
391  $part = $stack->top->getCurrentPart();
392  if ( $part->commentEnd !== $wsStart - 1 ) {
393  $part->visualEnd = $wsStart;
394  }
395  // Else comments abutting, no change in visual end
396  $part->commentEnd = $endPos;
397  }
398  $i = $endPos + 1;
399  $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
400  $accum[] = [ 'comment', [ $inner ] ];
401  }
402  continue;
403  }
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  $lowerName = strtolower( $name );
418  // Handle ignored tags
419  if ( in_array( $lowerName, $ignoredTags ) ) {
420  $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
421  $i = $tagEndPos + 1;
422  continue;
423  }
424 
425  $tagStartPos = $i;
426  if ( $text[$tagEndPos - 1] === '/' ) {
427  // Short end tag
428  $attrEnd = $tagEndPos - 1;
429  $inner = null;
430  $i = $tagEndPos + 1;
431  $close = null;
432  } else {
433  $attrEnd = $tagEndPos;
434  // Find closing tag
435  if (
436  !isset( $noMoreClosingTag[$lowerName] ) &&
437  preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
438  $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
439  ) {
440  [ $close, $closeTagStartPos ] = $matches[0];
441  $inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 );
442  $i = $closeTagStartPos + strlen( $close );
443  } else {
444  // No end tag
445  if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
446  // Let it run out to the end of the text.
447  $inner = substr( $text, $tagEndPos + 1 );
448  $i = $lengthText;
449  $close = null;
450  } else {
451  // Don't match the tag, treat opening tag as literal and resume parsing.
452  $i = $tagEndPos + 1;
453  self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
454  // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
455  $noMoreClosingTag[$lowerName] = 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  ];
478  if ( $inner !== null ) {
479  $children[] = [ 'inner', [ $inner ] ];
480  }
481  if ( $close !== null ) {
482  $children[] = [ 'close', [ $close ] ];
483  }
484  $accum[] = [ 'ext', $children ];
485  } elseif ( $found === 'line-start' ) {
486  // Is this the start of a heading?
487  // Line break belongs before the heading element in any case
488  if ( $fakeLineStart ) {
489  $fakeLineStart = false;
490  } else {
491  self::addLiteral( $accum, $curChar );
492  $i++;
493  }
494 
495  // Examine upto 6 characters
496  $count = strspn( $text, '=', $i, min( $lengthText, 6 ) );
497  if ( $count === 1 && $findEquals ) {
498  // DWIM: This looks kind of like a name/value separator.
499  // Let's let the equals handler have it and break the potential
500  // heading. This is heuristic, but AFAICT the methods for
501  // completely correct disambiguation are very complex.
502  } elseif ( $count > 0 ) {
503  $piece = [
504  'open' => "\n",
505  'close' => "\n",
506  'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ],
507  'startPos' => $i,
508  'count' => $count,
509  ];
510  $stack->push( $piece );
511  $accum =& $stack->getAccum();
512  $stackFlags = $stack->getFlags();
513  if ( isset( $stackFlags['findEquals'] ) ) {
514  $findEquals = $stackFlags['findEquals'];
515  }
516  if ( isset( $stackFlags['findPipe'] ) ) {
517  $findPipe = $stackFlags['findPipe'];
518  }
519  if ( isset( $stackFlags['inHeading'] ) ) {
520  $inHeading = $stackFlags['inHeading'];
521  }
522  $i += $count;
523  }
524  } elseif ( $found === 'line-end' ) {
525  $piece = $stack->top;
526  // A heading must be open, otherwise \n wouldn't have been in the search list
527  // FIXME: Don't use assert()
528  // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
529  assert( $piece->open === "\n" );
530  $part = $piece->getCurrentPart();
531  // Search back through the input to see if it has a proper close.
532  // Do this using the reversed string since the other solutions
533  // (end anchor, etc.) are inefficient.
534  $wsLength = strspn( $revText, " \t", $lengthText - $i );
535  $searchStart = $i - $wsLength;
536  if ( $part->commentEnd === $searchStart - 1 ) {
537  // Comment found at line end
538  // Search for equals signs before the comment
539  $searchStart = $part->visualEnd;
540  $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
541  }
542  $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
543  if ( $equalsLength > 0 ) {
544  if ( $searchStart - $equalsLength === $piece->startPos ) {
545  // This is just a single string of equals signs on its own line
546  // Replicate the doHeadings behavior /={count}(.+)={count}/
547  // First find out how many equals signs there really are (don't stop at 6)
548  if ( $equalsLength < 3 ) {
549  $count = 0;
550  } else {
551  $count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) );
552  }
553  } else {
554  $count = min( $equalsLength, $piece->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
656  ? $curLen
657  : strspn( $text, $curChar, $i, $maxCount );
658 
659  # check for maximum matching characters (if there are 5 closing
660  # characters, we will probably need only 3 - depending on the rules)
661  $rule = $this->rules[$piece->open];
662  if ( $count > $rule['max'] ) {
663  # The specified maximum exists in the callback array, unless the caller
664  # has made an error
665  $matchingCount = $rule['max'];
666  } else {
667  # Count is less than the maximum
668  # Skip any gaps in the callback array to find the true largest match
669  # Need to use array_key_exists not isset because the callback can be null
670  $matchingCount = $count;
671  while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
672  --$matchingCount;
673  }
674  }
675 
676  if ( $matchingCount <= 0 ) {
677  # No matching element found in callback array
678  # Output a literal closing brace and continue
679  $endText = substr( $text, $i, $count );
680  self::addLiteral( $accum, $endText );
681  $i += $count;
682  continue;
683  }
684  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
685  $name = $rule['names'][$matchingCount];
686  if ( $name === null ) {
687  // No element, just literal text
688  $endText = substr( $text, $i, $matchingCount );
689  $element = $piece->breakSyntax( $matchingCount );
690  self::addLiteral( $element, $endText );
691  } else {
692  # Create XML element
693  $parts = $piece->parts;
694  $titleAccum = $parts[0]->out;
695  unset( $parts[0] );
696 
697  $children = [];
698 
699  # The invocation is at the start of the line if lineStart is set in
700  # the stack, and all opening brackets are used up.
701  if ( $maxCount === $matchingCount &&
702  $piece->lineStart &&
703  $piece->savedPrefix === ''
704  ) {
705  $children[] = [ '@lineStart', [ 1 ] ];
706  }
707  $titleNode = [ 'title', $titleAccum ];
708  $children[] = $titleNode;
709  $argIndex = 1;
710  foreach ( $parts as $part ) {
711  if ( isset( $part->eqpos ) ) {
712  $equalsNode = $part->out[$part->eqpos];
713  $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
714  $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
715  $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
716  $children[] = $partNode;
717  } else {
718  $nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ];
719  $valueNode = [ 'value', $part->out ];
720  $partNode = [ 'part', [ $nameNode, $valueNode ] ];
721  $children[] = $partNode;
722  }
723  }
724  $element = [ [ $name, $children ] ];
725  }
726 
727  # Advance input pointer
728  $i += $matchingCount;
729 
730  # Unwind the stack
731  $stack->pop();
732  $accum =& $stack->getAccum();
733 
734  # Re-add the old stack element if it still has unmatched opening characters remaining
735  if ( $matchingCount < $piece->count ) {
736  $piece->parts = [ new PPDPart_Hash ];
737  $piece->count -= $matchingCount;
738  # do we still qualify for any callback with remaining count?
739  $min = $this->rules[$piece->open]['min'];
740  if ( $piece->count >= $min ) {
741  $stack->push( $piece );
742  $accum =& $stack->getAccum();
743  } elseif ( $piece->count === 1 && $piece->open === '{' && $piece->savedPrefix === '-' ) {
744  $piece->savedPrefix = '';
745  $piece->open = '-{';
746  $piece->count = 2;
747  $piece->close = $this->rules[$piece->open]['end'];
748  $stack->push( $piece );
749  $accum =& $stack->getAccum();
750  } else {
751  $s = substr( $piece->open, 0, -1 );
752  $s .= str_repeat(
753  substr( $piece->open, -1 ),
754  $piece->count - strlen( $s )
755  );
756  self::addLiteral( $accum, $piece->savedPrefix . $s );
757  }
758  } elseif ( $piece->savedPrefix !== '' ) {
759  self::addLiteral( $accum, $piece->savedPrefix );
760  }
761 
762  $stackFlags = $stack->getFlags();
763  if ( isset( $stackFlags['findEquals'] ) ) {
764  $findEquals = $stackFlags['findEquals'];
765  }
766  if ( isset( $stackFlags['findPipe'] ) ) {
767  $findPipe = $stackFlags['findPipe'];
768  }
769  if ( isset( $stackFlags['inHeading'] ) ) {
770  $inHeading = $stackFlags['inHeading'];
771  }
772 
773  # Add XML element to the enclosing accumulator
774  array_splice( $accum, count( $accum ), 0, $element );
775  } elseif ( $found === 'pipe' ) {
776  $findEquals = true; // shortcut for getFlags()
777  $stack->addPart();
778  $accum =& $stack->getAccum();
779  ++$i;
780  } elseif ( $found === 'equals' ) {
781  $findEquals = false; // shortcut for getFlags()
782  $accum[] = [ 'equals', [ '=' ] ];
783  $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
784  ++$i;
785  }
786  }
787 
788  # Output any remaining unclosed brackets
789  foreach ( $stack->stack as $piece ) {
790  array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
791  }
792 
793  # Enable top-level headings
794  foreach ( $stack->rootAccum as &$node ) {
795  if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
796  $node[PPNode_Hash_Tree::NAME] = 'h';
797  }
798  }
799 
800  return [ [ 'root', $stack->rootAccum ] ];
801  }
802 
803  private static function addLiteral( array &$accum, $text ) {
804  $n = count( $accum );
805  if ( $n && is_string( $accum[$n - 1] ) ) {
806  $accum[$n - 1] .= $text;
807  } else {
808  $accum[] = $text;
809  }
810  }
811 }
$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:107
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.
return true
Definition: router.php:90