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  && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</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  // Sanity check
355  if ( $wsLength > 0
356  && $endIndex >= 0
357  && is_string( $accum[$endIndex] )
358  && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
359  ) {
360  $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
361  }
362 
363  // Dump all but the last comment to the accumulator
364  foreach ( $comments as $j => $com ) {
365  $startPos = $com[0];
366  $endPos = $com[1] + 1;
367  if ( $j == ( count( $comments ) - 1 ) ) {
368  break;
369  }
370  $inner = substr( $text, $startPos, $endPos - $startPos );
371  $accum[] = [ 'comment', [ $inner ] ];
372  }
373 
374  // Do a line-start run next time to look for headings after the comment
375  $fakeLineStart = true;
376  } else {
377  // No line to eat, just take the comment itself
378  $startPos = $i;
379  $endPos += 2;
380  }
381 
382  if ( $stack->top ) {
383  $part = $stack->top->getCurrentPart();
384  if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
385  $part->visualEnd = $wsStart;
386  }
387  // Else comments abutting, no change in visual end
388  $part->commentEnd = $endPos;
389  }
390  $i = $endPos + 1;
391  $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
392  $accum[] = [ 'comment', [ $inner ] ];
393  }
394  continue;
395  }
396  $name = $matches[1];
397  $lowerName = strtolower( $name );
398  $attrStart = $i + strlen( $name ) + 1;
399 
400  // Find end of tag
401  $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
402  if ( $tagEndPos === false ) {
403  // Infinite backtrack
404  // Disable tag search to prevent worst-case O(N^2) performance
405  $noMoreGT = true;
406  self::addLiteral( $accum, '<' );
407  ++$i;
408  continue;
409  }
410 
411  // Handle ignored tags
412  if ( in_array( $lowerName, $ignoredTags ) ) {
413  $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
414  $i = $tagEndPos + 1;
415  continue;
416  }
417 
418  $tagStartPos = $i;
419  if ( $text[$tagEndPos - 1] == '/' ) {
420  // Short end tag
421  $attrEnd = $tagEndPos - 1;
422  $inner = null;
423  $i = $tagEndPos + 1;
424  $close = null;
425  } else {
426  $attrEnd = $tagEndPos;
427  // Find closing tag
428  if (
429  !isset( $noMoreClosingTag[$name] ) &&
430  preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
431  $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
432  ) {
433  $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
434  $i = $matches[0][1] + strlen( $matches[0][0] );
435  $close = $matches[0][0];
436  } else {
437  // No end tag
438  if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
439  // Let it run out to the end of the text.
440  $inner = substr( $text, $tagEndPos + 1 );
441  $i = $lengthText;
442  $close = null;
443  } else {
444  // Don't match the tag, treat opening tag as literal and resume parsing.
445  $i = $tagEndPos + 1;
446  self::addLiteral( $accum,
447  substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
448  // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
449  $noMoreClosingTag[$name] = true;
450  continue;
451  }
452  }
453  }
454  // <includeonly> and <noinclude> just become <ignore> tags
455  if ( in_array( $lowerName, $ignoredElements ) ) {
456  $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
457  continue;
458  }
459 
460  if ( $attrEnd <= $attrStart ) {
461  $attr = '';
462  } else {
463  // Note that the attr element contains the whitespace between name and attribute,
464  // this is necessary for precise reconstruction during pre-save transform.
465  $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
466  }
467 
468  $children = [
469  [ 'name', [ $name ] ],
470  [ 'attr', [ $attr ] ] ];
471  if ( $inner !== null ) {
472  $children[] = [ 'inner', [ $inner ] ];
473  }
474  if ( $close !== null ) {
475  $children[] = [ 'close', [ $close ] ];
476  }
477  $accum[] = [ 'ext', $children ];
478  } elseif ( $found == 'line-start' ) {
479  // Is this the start of a heading?
480  // Line break belongs before the heading element in any case
481  if ( $fakeLineStart ) {
482  $fakeLineStart = false;
483  } else {
484  self::addLiteral( $accum, $curChar );
485  $i++;
486  }
487 
488  // Examine upto 6 characters
489  $count = strspn( $text, '=', $i, min( strlen( $text ), 6 ) );
490  if ( $count == 1 && $findEquals ) {
491  // DWIM: This looks kind of like a name/value separator.
492  // Let's let the equals handler have it and break the potential
493  // heading. This is heuristic, but AFAICT the methods for
494  // completely correct disambiguation are very complex.
495  } elseif ( $count > 0 ) {
496  $piece = [
497  'open' => "\n",
498  'close' => "\n",
499  'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ],
500  'startPos' => $i,
501  'count' => $count ];
502  $stack->push( $piece );
503  $accum =& $stack->getAccum();
504  $stackFlags = $stack->getFlags();
505  if ( isset( $stackFlags['findEquals'] ) ) {
506  $findEquals = $stackFlags['findEquals'];
507  }
508  if ( isset( $stackFlags['findPipe'] ) ) {
509  $findPipe = $stackFlags['findPipe'];
510  }
511  if ( isset( $stackFlags['inHeading'] ) ) {
512  $inHeading = $stackFlags['inHeading'];
513  }
514  $i += $count;
515  }
516  } elseif ( $found == 'line-end' ) {
517  $piece = $stack->top;
518  // A heading must be open, otherwise \n wouldn't have been in the search list
519  // FIXME: Don't use assert()
520  // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.assert
521  assert( $piece->open === "\n" );
522  $part = $piece->getCurrentPart();
523  // Search back through the input to see if it has a proper close.
524  // Do this using the reversed string since the other solutions
525  // (end anchor, etc.) are inefficient.
526  $wsLength = strspn( $revText, " \t", $lengthText - $i );
527  $searchStart = $i - $wsLength;
528  if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
529  // Comment found at line end
530  // Search for equals signs before the comment
531  $searchStart = $part->visualEnd;
532  $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
533  }
534  $count = $piece->count;
535  $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
536  if ( $equalsLength > 0 ) {
537  if ( $searchStart - $equalsLength == $piece->startPos ) {
538  // This is just a single string of equals signs on its own line
539  // Replicate the doHeadings behavior /={count}(.+)={count}/
540  // First find out how many equals signs there really are (don't stop at 6)
541  $count = $equalsLength;
542  if ( $count < 3 ) {
543  $count = 0;
544  } else {
545  $count = min( 6, intval( ( $count - 1 ) / 2 ) );
546  }
547  } else {
548  $count = min( $equalsLength, $count );
549  }
550  if ( $count > 0 ) {
551  // Normal match, output <h>
552  $element = [ [ 'possible-h',
553  array_merge(
554  [
555  [ '@level', [ $count ] ],
556  [ '@i', [ $headingIndex++ ] ]
557  ],
558  $accum
559  )
560  ] ];
561  } else {
562  // Single equals sign on its own line, count=0
563  $element = $accum;
564  }
565  } else {
566  // No match, no <h>, just pass down the inner text
567  $element = $accum;
568  }
569  // Unwind the stack
570  $stack->pop();
571  $accum =& $stack->getAccum();
572  $stackFlags = $stack->getFlags();
573  if ( isset( $stackFlags['findEquals'] ) ) {
574  $findEquals = $stackFlags['findEquals'];
575  }
576  if ( isset( $stackFlags['findPipe'] ) ) {
577  $findPipe = $stackFlags['findPipe'];
578  }
579  if ( isset( $stackFlags['inHeading'] ) ) {
580  $inHeading = $stackFlags['inHeading'];
581  }
582 
583  // Append the result to the enclosing accumulator
584  array_splice( $accum, count( $accum ), 0, $element );
585 
586  // Note that we do NOT increment the input pointer.
587  // This is because the closing linebreak could be the opening linebreak of
588  // another heading. Infinite loops are avoided because the next iteration MUST
589  // hit the heading open case above, which unconditionally increments the
590  // input pointer.
591  } elseif ( $found == 'open' ) {
592  # count opening brace characters
593  $curLen = strlen( $curChar );
594  $count = ( $curLen > 1 ) ?
595  # allow the final character to repeat
596  strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
597  strspn( $text, $curChar, $i );
598 
599  $savedPrefix = '';
600  $lineStart = ( $i > 0 && $text[$i - 1] == "\n" );
601 
602  if ( $curChar === "-{" && $count > $curLen ) {
603  // -{ => {{ transition because rightmost wins
604  $savedPrefix = '-';
605  $i++;
606  $curChar = '{';
607  $count--;
608  $rule = $this->rules[$curChar];
609  }
610 
611  # we need to add to stack only if opening brace count is enough for one of the rules
612  if ( $count >= $rule['min'] ) {
613  # Add it to the stack
614  $piece = [
615  'open' => $curChar,
616  'close' => $rule['end'],
617  'savedPrefix' => $savedPrefix,
618  'count' => $count,
619  'lineStart' => $lineStart,
620  ];
621 
622  $stack->push( $piece );
623  $accum =& $stack->getAccum();
624  $stackFlags = $stack->getFlags();
625  if ( isset( $stackFlags['findEquals'] ) ) {
626  $findEquals = $stackFlags['findEquals'];
627  }
628  if ( isset( $stackFlags['findPipe'] ) ) {
629  $findPipe = $stackFlags['findPipe'];
630  }
631  if ( isset( $stackFlags['inHeading'] ) ) {
632  $inHeading = $stackFlags['inHeading'];
633  }
634  } else {
635  # Add literal brace(s)
636  self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) );
637  }
638  $i += $count;
639  } elseif ( $found == 'close' ) {
641  $piece = $stack->top;
642  '@phan-var PPDStackElement_Hash $piece';
643  # lets check if there are enough characters for closing brace
644  $maxCount = $piece->count;
645  if ( $piece->close === '}-' && $curChar === '}' ) {
646  $maxCount--; # don't try to match closing '-' as a '}'
647  }
648  $curLen = strlen( $curChar );
649  $count = ( $curLen > 1 ) ? $curLen :
650  strspn( $text, $curChar, $i, $maxCount );
651 
652  # check for maximum matching characters (if there are 5 closing
653  # characters, we will probably need only 3 - depending on the rules)
654  $rule = $this->rules[$piece->open];
655  if ( $count > $rule['max'] ) {
656  # The specified maximum exists in the callback array, unless the caller
657  # has made an error
658  $matchingCount = $rule['max'];
659  } else {
660  # Count is less than the maximum
661  # Skip any gaps in the callback array to find the true largest match
662  # Need to use array_key_exists not isset because the callback can be null
663  $matchingCount = $count;
664  while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
665  --$matchingCount;
666  }
667  }
668 
669  if ( $matchingCount <= 0 ) {
670  # No matching element found in callback array
671  # Output a literal closing brace and continue
672  $endText = substr( $text, $i, $count );
673  self::addLiteral( $accum, $endText );
674  $i += $count;
675  continue;
676  }
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 }
Preprocessor\DOM_FOR_INCLUSION
const DOM_FOR_INCLUSION
Transclusion mode flag for Preprocessor::preprocessToObj()
Definition: Preprocessor.php:29
Preprocessor_Hash\$cacheThreshold
int bool $cacheThreshold
Min wikitext size for which to cache DOM tree.
Definition: Preprocessor_Hash.php:48
PPDPart_Hash
Definition: PPDPart_Hash.php:27
PPFrame_Hash
An expansion frame, used as a context to expand the result of preprocessToObj()
Definition: PPFrame_Hash.php:27
Preprocessor\$wanCache
WANObjectCache $wanCache
Definition: Preprocessor.php:39
Preprocessor\$parser
Parser $parser
Definition: Preprocessor.php:36
Preprocessor_Hash\newFrame
newFrame()
Definition: Preprocessor_Hash.php:70
Preprocessor_Hash
Differences from DOM schema:
Definition: Preprocessor_Hash.php:43
Preprocessor_Hash\buildDomTreeArrayFromText
buildDomTreeArrayFromText( $text, $flags)
Definition: Preprocessor_Hash.php:140
Preprocessor_Hash\__construct
__construct(Parser $parser, WANObjectCache $wanCache=null, array $options=[])
Definition: Preprocessor_Hash.php:57
Preprocessor
Definition: Preprocessor.php:27
Preprocessor_Hash\newCustomFrame
newCustomFrame( $args)
Definition: Preprocessor_Hash.php:78
Preprocessor\DOM_LANG_CONVERSION_DISABLED
const DOM_LANG_CONVERSION_DISABLED
Language conversion construct omission flag for Preprocessor::preprocessToObj()
Definition: Preprocessor.php:31
Preprocessor_Hash\newPartNodeArray
newPartNodeArray( $values)
Definition: Preprocessor_Hash.php:86
$matches
$matches
Definition: NoLocalSettings.php:24
Preprocessor_Hash\CACHE_VERSION
const CACHE_VERSION
Cache format version.
Definition: Preprocessor_Hash.php:45
$args
if( $line===false) $args
Definition: mcc.php:124
PPNode_Hash_Array
Definition: PPNode_Hash_Array.php:26
Preprocessor_Hash\preprocessToObj
preprocessToObj( $text, $flags=0)
Get the document object model for the given wikitext.
Definition: Preprocessor_Hash.php:109
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:125
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:84
PPDStack_Hash
Stack class to help Preprocessor::preprocessToObj()
Definition: PPDStack_Hash.php:27
Preprocessor_Hash\addLiteral
static addLiteral(array &$accum, $text)
Definition: Preprocessor_Hash.php:794
PPCustomFrame_Hash
Expansion frame with custom arguments.
Definition: PPCustomFrame_Hash.php:27
PPNode_Hash_Tree
Definition: PPNode_Hash_Tree.php:26