Merge "Fix Postgres support"
[lhc/web/wiklou.git] / includes / parser / Preprocessor_Hash.php
1 <?php
2 /**
3 * Preprocessor using PHP arrays
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * Differences from DOM schema:
26 * * attribute nodes are children
27 * * "<h>" nodes that aren't at the top are replaced with <possible-h>
28 *
29 * Nodes are stored in a recursive array data structure. A node store is an
30 * array where each element may be either a scalar (representing a text node)
31 * or a "descriptor", which is a two-element array where the first element is
32 * the node name and the second element is the node store for the children.
33 *
34 * Attributes are represented as children that have a node name starting with
35 * "@", and a single text node child.
36 *
37 * @todo: Consider replacing descriptor arrays with objects of a new class.
38 * Benchmark and measure resulting memory impact.
39 *
40 * @ingroup Parser
41 */
42 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
43 class Preprocessor_Hash extends Preprocessor {
44 // @codingStandardsIgnoreEnd
45
46 /**
47 * @var Parser
48 */
49 public $parser;
50
51 const CACHE_PREFIX = 'preprocess-hash';
52 const CACHE_VERSION = 2;
53
54 public function __construct( $parser ) {
55 $this->parser = $parser;
56 }
57
58 /**
59 * @return PPFrame_Hash
60 */
61 public function newFrame() {
62 return new PPFrame_Hash( $this );
63 }
64
65 /**
66 * @param array $args
67 * @return PPCustomFrame_Hash
68 */
69 public function newCustomFrame( $args ) {
70 return new PPCustomFrame_Hash( $this, $args );
71 }
72
73 /**
74 * @param array $values
75 * @return PPNode_Hash_Array
76 */
77 public function newPartNodeArray( $values ) {
78 $list = [];
79
80 foreach ( $values as $k => $val ) {
81 if ( is_int( $k ) ) {
82 $store = [ [ 'part', [
83 [ 'name', [ [ '@index', [ $k ] ] ] ],
84 [ 'value', [ strval( $val ) ] ],
85 ] ] ];
86 } else {
87 $store = [ [ 'part', [
88 [ 'name', [ strval( $k ) ] ],
89 '=',
90 [ 'value', [ strval( $val ) ] ],
91 ] ] ];
92 }
93
94 $list[] = new PPNode_Hash_Tree( $store, 0 );
95 }
96
97 $node = new PPNode_Hash_Array( $list );
98 return $node;
99 }
100
101 /**
102 * Preprocess some wikitext and return the document tree.
103 *
104 * @param string $text The text to parse
105 * @param int $flags Bitwise combination of:
106 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<includeonly>" as if the text is being
107 * included. Default is to assume a direct page view.
108 *
109 * The generated DOM tree must depend only on the input text and the flags.
110 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of T6899.
111 *
112 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
113 * change in the DOM tree for a given text, must be passed through the section identifier
114 * in the section edit link and thus back to extractSections().
115 *
116 * @throws MWException
117 * @return PPNode_Hash_Tree
118 */
119 public function preprocessToObj( $text, $flags = 0 ) {
120 global $wgDisableLangConversion;
121
122 $tree = $this->cacheGetTree( $text, $flags );
123 if ( $tree !== false ) {
124 $store = json_decode( $tree );
125 if ( is_array( $store ) ) {
126 return new PPNode_Hash_Tree( $store, 0 );
127 }
128 }
129
130 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
131
132 $xmlishElements = $this->parser->getStripList();
133 $xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ];
134 $enableOnlyinclude = false;
135 if ( $forInclusion ) {
136 $ignoredTags = [ 'includeonly', '/includeonly' ];
137 $ignoredElements = [ 'noinclude' ];
138 $xmlishElements[] = 'noinclude';
139 if ( strpos( $text, '<onlyinclude>' ) !== false
140 && strpos( $text, '</onlyinclude>' ) !== false
141 ) {
142 $enableOnlyinclude = true;
143 }
144 } else {
145 $ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ];
146 $ignoredElements = [ 'includeonly' ];
147 $xmlishElements[] = 'includeonly';
148 }
149 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
150
151 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
152 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
153
154 $stack = new PPDStack_Hash;
155
156 $searchBase = "[{<\n";
157 if ( !$wgDisableLangConversion ) {
158 $searchBase .= '-';
159 }
160
161 // For fast reverse searches
162 $revText = strrev( $text );
163 $lengthText = strlen( $text );
164
165 // Input pointer, starts out pointing to a pseudo-newline before the start
166 $i = 0;
167 // Current accumulator. See the doc comment for Preprocessor_Hash for the format.
168 $accum =& $stack->getAccum();
169 // True to find equals signs in arguments
170 $findEquals = false;
171 // True to take notice of pipe characters
172 $findPipe = false;
173 $headingIndex = 1;
174 // True if $i is inside a possible heading
175 $inHeading = false;
176 // True if there are no more greater-than (>) signs right of $i
177 $noMoreGT = false;
178 // Map of tag name => true if there are no more closing tags of given type right of $i
179 $noMoreClosingTag = [];
180 // True to ignore all input up to the next <onlyinclude>
181 $findOnlyinclude = $enableOnlyinclude;
182 // Do a line-start run without outputting an LF character
183 $fakeLineStart = true;
184
185 while ( true ) {
186 // $this->memCheck();
187
188 if ( $findOnlyinclude ) {
189 // Ignore all input up to the next <onlyinclude>
190 $startPos = strpos( $text, '<onlyinclude>', $i );
191 if ( $startPos === false ) {
192 // Ignored section runs to the end
193 $accum[] = [ 'ignore', [ substr( $text, $i ) ] ];
194 break;
195 }
196 $tagEndPos = $startPos + strlen( '<onlyinclude>' ); // past-the-end
197 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ];
198 $i = $tagEndPos;
199 $findOnlyinclude = false;
200 }
201
202 if ( $fakeLineStart ) {
203 $found = 'line-start';
204 $curChar = '';
205 } else {
206 # Find next opening brace, closing brace or pipe
207 $search = $searchBase;
208 if ( $stack->top === false ) {
209 $currentClosing = '';
210 } elseif (
211 $stack->top->close === '}-' &&
212 $stack->top->count > 2
213 ) {
214 # adjust closing for -{{{...{{
215 $currentClosing = '}';
216 $search .= $currentClosing;
217 } else {
218 $currentClosing = $stack->top->close;
219 $search .= $currentClosing;
220 }
221 if ( $findPipe ) {
222 $search .= '|';
223 }
224 if ( $findEquals ) {
225 // First equals will be for the template
226 $search .= '=';
227 }
228 $rule = null;
229 # Output literal section, advance input counter
230 $literalLength = strcspn( $text, $search, $i );
231 if ( $literalLength > 0 ) {
232 self::addLiteral( $accum, substr( $text, $i, $literalLength ) );
233 $i += $literalLength;
234 }
235 if ( $i >= $lengthText ) {
236 if ( $currentClosing == "\n" ) {
237 // Do a past-the-end run to finish off the heading
238 $curChar = '';
239 $found = 'line-end';
240 } else {
241 # All done
242 break;
243 }
244 } else {
245 $curChar = $curTwoChar = $text[$i];
246 if ( ( $i + 1 ) < $lengthText ) {
247 $curTwoChar .= $text[$i + 1];
248 }
249 if ( $curChar == '|' ) {
250 $found = 'pipe';
251 } elseif ( $curChar == '=' ) {
252 $found = 'equals';
253 } elseif ( $curChar == '<' ) {
254 $found = 'angle';
255 } elseif ( $curChar == "\n" ) {
256 if ( $inHeading ) {
257 $found = 'line-end';
258 } else {
259 $found = 'line-start';
260 }
261 } elseif ( $curTwoChar == $currentClosing ) {
262 $found = 'close';
263 $curChar = $curTwoChar;
264 } elseif ( $curChar == $currentClosing ) {
265 $found = 'close';
266 } elseif ( isset( $this->rules[$curTwoChar] ) ) {
267 $curChar = $curTwoChar;
268 $found = 'open';
269 $rule = $this->rules[$curChar];
270 } elseif ( isset( $this->rules[$curChar] ) ) {
271 $found = 'open';
272 $rule = $this->rules[$curChar];
273 } else {
274 # Some versions of PHP have a strcspn which stops on
275 # null characters; ignore these and continue.
276 # We also may get '-' and '}' characters here which
277 # don't match -{ or $currentClosing. Add these to
278 # output and continue.
279 if ( $curChar == '-' || $curChar == '}' ) {
280 self::addLiteral( $accum, $curChar );
281 }
282 ++$i;
283 continue;
284 }
285 }
286 }
287
288 if ( $found == 'angle' ) {
289 $matches = false;
290 // Handle </onlyinclude>
291 if ( $enableOnlyinclude
292 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
293 ) {
294 $findOnlyinclude = true;
295 continue;
296 }
297
298 // Determine element name
299 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
300 // Element name missing or not listed
301 self::addLiteral( $accum, '<' );
302 ++$i;
303 continue;
304 }
305 // Handle comments
306 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
307
308 // To avoid leaving blank lines, when a sequence of
309 // space-separated comments is both preceded and followed by
310 // a newline (ignoring spaces), then
311 // trim leading and trailing spaces and the trailing newline.
312
313 // Find the end
314 $endPos = strpos( $text, '-->', $i + 4 );
315 if ( $endPos === false ) {
316 // Unclosed comment in input, runs to end
317 $inner = substr( $text, $i );
318 $accum[] = [ 'comment', [ $inner ] ];
319 $i = $lengthText;
320 } else {
321 // Search backwards for leading whitespace
322 $wsStart = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
323
324 // Search forwards for trailing whitespace
325 // $wsEnd will be the position of the last space (or the '>' if there's none)
326 $wsEnd = $endPos + 2 + strspn( $text, " \t", $endPos + 3 );
327
328 // Keep looking forward as long as we're finding more
329 // comments.
330 $comments = [ [ $wsStart, $wsEnd ] ];
331 while ( substr( $text, $wsEnd + 1, 4 ) == '<!--' ) {
332 $c = strpos( $text, '-->', $wsEnd + 4 );
333 if ( $c === false ) {
334 break;
335 }
336 $c = $c + 2 + strspn( $text, " \t", $c + 3 );
337 $comments[] = [ $wsEnd + 1, $c ];
338 $wsEnd = $c;
339 }
340
341 // Eat the line if possible
342 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
343 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
344 // it's a possible beneficial b/c break.
345 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
346 && substr( $text, $wsEnd + 1, 1 ) == "\n"
347 ) {
348 // Remove leading whitespace from the end of the accumulator
349 $wsLength = $i - $wsStart;
350 $endIndex = count( $accum ) - 1;
351
352 // Sanity check
353 if ( $wsLength > 0
354 && $endIndex >= 0
355 && is_string( $accum[$endIndex] )
356 && strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength
357 ) {
358 $accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength );
359 }
360
361 // Dump all but the last comment to the accumulator
362 foreach ( $comments as $j => $com ) {
363 $startPos = $com[0];
364 $endPos = $com[1] + 1;
365 if ( $j == ( count( $comments ) - 1 ) ) {
366 break;
367 }
368 $inner = substr( $text, $startPos, $endPos - $startPos );
369 $accum[] = [ 'comment', [ $inner ] ];
370 }
371
372 // Do a line-start run next time to look for headings after the comment
373 $fakeLineStart = true;
374 } else {
375 // No line to eat, just take the comment itself
376 $startPos = $i;
377 $endPos += 2;
378 }
379
380 if ( $stack->top ) {
381 $part = $stack->top->getCurrentPart();
382 if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
383 $part->visualEnd = $wsStart;
384 }
385 // Else comments abutting, no change in visual end
386 $part->commentEnd = $endPos;
387 }
388 $i = $endPos + 1;
389 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
390 $accum[] = [ 'comment', [ $inner ] ];
391 }
392 continue;
393 }
394 $name = $matches[1];
395 $lowerName = strtolower( $name );
396 $attrStart = $i + strlen( $name ) + 1;
397
398 // Find end of tag
399 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
400 if ( $tagEndPos === false ) {
401 // Infinite backtrack
402 // Disable tag search to prevent worst-case O(N^2) performance
403 $noMoreGT = true;
404 self::addLiteral( $accum, '<' );
405 ++$i;
406 continue;
407 }
408
409 // Handle ignored tags
410 if ( in_array( $lowerName, $ignoredTags ) ) {
411 $accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ];
412 $i = $tagEndPos + 1;
413 continue;
414 }
415
416 $tagStartPos = $i;
417 if ( $text[$tagEndPos - 1] == '/' ) {
418 // Short end tag
419 $attrEnd = $tagEndPos - 1;
420 $inner = null;
421 $i = $tagEndPos + 1;
422 $close = null;
423 } else {
424 $attrEnd = $tagEndPos;
425 // Find closing tag
426 if (
427 !isset( $noMoreClosingTag[$name] ) &&
428 preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
429 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
430 ) {
431 $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
432 $i = $matches[0][1] + strlen( $matches[0][0] );
433 $close = $matches[0][0];
434 } else {
435 // No end tag
436 if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
437 // Let it run out to the end of the text.
438 $inner = substr( $text, $tagEndPos + 1 );
439 $i = $lengthText;
440 $close = null;
441 } else {
442 // Don't match the tag, treat opening tag as literal and resume parsing.
443 $i = $tagEndPos + 1;
444 self::addLiteral( $accum,
445 substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
446 // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
447 $noMoreClosingTag[$name] = true;
448 continue;
449 }
450 }
451 }
452 // <includeonly> and <noinclude> just become <ignore> tags
453 if ( in_array( $lowerName, $ignoredElements ) ) {
454 $accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ];
455 continue;
456 }
457
458 if ( $attrEnd <= $attrStart ) {
459 $attr = '';
460 } else {
461 // Note that the attr element contains the whitespace between name and attribute,
462 // this is necessary for precise reconstruction during pre-save transform.
463 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
464 }
465
466 $children = [
467 [ 'name', [ $name ] ],
468 [ 'attr', [ $attr ] ] ];
469 if ( $inner !== null ) {
470 $children[] = [ 'inner', [ $inner ] ];
471 }
472 if ( $close !== null ) {
473 $children[] = [ 'close', [ $close ] ];
474 }
475 $accum[] = [ 'ext', $children ];
476 } elseif ( $found == 'line-start' ) {
477 // Is this the start of a heading?
478 // Line break belongs before the heading element in any case
479 if ( $fakeLineStart ) {
480 $fakeLineStart = false;
481 } else {
482 self::addLiteral( $accum, $curChar );
483 $i++;
484 }
485
486 $count = strspn( $text, '=', $i, 6 );
487 if ( $count == 1 && $findEquals ) {
488 // DWIM: This looks kind of like a name/value separator.
489 // Let's let the equals handler have it and break the potential
490 // heading. This is heuristic, but AFAICT the methods for
491 // completely correct disambiguation are very complex.
492 } elseif ( $count > 0 ) {
493 $piece = [
494 'open' => "\n",
495 'close' => "\n",
496 'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ],
497 'startPos' => $i,
498 'count' => $count ];
499 $stack->push( $piece );
500 $accum =& $stack->getAccum();
501 extract( $stack->getFlags() );
502 $i += $count;
503 }
504 } elseif ( $found == 'line-end' ) {
505 $piece = $stack->top;
506 // A heading must be open, otherwise \n wouldn't have been in the search list
507 assert( $piece->open === "\n" );
508 $part = $piece->getCurrentPart();
509 // Search back through the input to see if it has a proper close.
510 // Do this using the reversed string since the other solutions
511 // (end anchor, etc.) are inefficient.
512 $wsLength = strspn( $revText, " \t", $lengthText - $i );
513 $searchStart = $i - $wsLength;
514 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
515 // Comment found at line end
516 // Search for equals signs before the comment
517 $searchStart = $part->visualEnd;
518 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
519 }
520 $count = $piece->count;
521 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
522 if ( $equalsLength > 0 ) {
523 if ( $searchStart - $equalsLength == $piece->startPos ) {
524 // This is just a single string of equals signs on its own line
525 // Replicate the doHeadings behavior /={count}(.+)={count}/
526 // First find out how many equals signs there really are (don't stop at 6)
527 $count = $equalsLength;
528 if ( $count < 3 ) {
529 $count = 0;
530 } else {
531 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
532 }
533 } else {
534 $count = min( $equalsLength, $count );
535 }
536 if ( $count > 0 ) {
537 // Normal match, output <h>
538 $element = [ [ 'possible-h',
539 array_merge(
540 [
541 [ '@level', [ $count ] ],
542 [ '@i', [ $headingIndex++ ] ]
543 ],
544 $accum
545 )
546 ] ];
547 } else {
548 // Single equals sign on its own line, count=0
549 $element = $accum;
550 }
551 } else {
552 // No match, no <h>, just pass down the inner text
553 $element = $accum;
554 }
555 // Unwind the stack
556 $stack->pop();
557 $accum =& $stack->getAccum();
558 extract( $stack->getFlags() );
559
560 // Append the result to the enclosing accumulator
561 array_splice( $accum, count( $accum ), 0, $element );
562
563 // Note that we do NOT increment the input pointer.
564 // This is because the closing linebreak could be the opening linebreak of
565 // another heading. Infinite loops are avoided because the next iteration MUST
566 // hit the heading open case above, which unconditionally increments the
567 // input pointer.
568 } elseif ( $found == 'open' ) {
569 # count opening brace characters
570 $curLen = strlen( $curChar );
571 $count = ( $curLen > 1 ) ?
572 # allow the final character to repeat
573 strspn( $text, $curChar[$curLen-1], $i+1 ) + 1 :
574 strspn( $text, $curChar, $i );
575
576 # we need to add to stack only if opening brace count is enough for one of the rules
577 if ( $count >= $rule['min'] ) {
578 # Add it to the stack
579 $piece = [
580 'open' => $curChar,
581 'close' => $rule['end'],
582 'count' => $count,
583 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
584 ];
585
586 $stack->push( $piece );
587 $accum =& $stack->getAccum();
588 extract( $stack->getFlags() );
589 } else {
590 # Add literal brace(s)
591 self::addLiteral( $accum, str_repeat( $curChar, $count ) );
592 }
593 $i += $count;
594 } elseif ( $found == 'close' ) {
595 $piece = $stack->top;
596 # lets check if there are enough characters for closing brace
597 $maxCount = $piece->count;
598 if ( $piece->close === '}-' && $curChar === '}' ) {
599 $maxCount--; # don't try to match closing '-' as a '}'
600 }
601 $curLen = strlen( $curChar );
602 $count = ( $curLen > 1 ) ? $curLen :
603 strspn( $text, $curChar, $i, $maxCount );
604
605 # check for maximum matching characters (if there are 5 closing
606 # characters, we will probably need only 3 - depending on the rules)
607 $rule = $this->rules[$piece->open];
608 if ( $piece->close === '}-' && $piece->count > 2 ) {
609 # tweak for -{..{{ }}..}-
610 $rule = $this->rules['{'];
611 }
612 if ( $count > $rule['max'] ) {
613 # The specified maximum exists in the callback array, unless the caller
614 # has made an error
615 $matchingCount = $rule['max'];
616 } else {
617 # Count is less than the maximum
618 # Skip any gaps in the callback array to find the true largest match
619 # Need to use array_key_exists not isset because the callback can be null
620 $matchingCount = $count;
621 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
622 --$matchingCount;
623 }
624 }
625
626 if ( $matchingCount <= 0 ) {
627 # No matching element found in callback array
628 # Output a literal closing brace and continue
629 $endText = substr( $text, $i, $count );
630 self::addLiteral( $accum, $endText );
631 $i += $count;
632 continue;
633 }
634 $name = $rule['names'][$matchingCount];
635 if ( $name === null ) {
636 // No element, just literal text
637 $endText = substr( $text, $i, $matchingCount );
638 $element = $piece->breakSyntax( $matchingCount );
639 self::addLiteral( $element, $endText );
640 } else {
641 # Create XML element
642 $parts = $piece->parts;
643 $titleAccum = $parts[0]->out;
644 unset( $parts[0] );
645
646 $children = [];
647
648 # The invocation is at the start of the line if lineStart is set in
649 # the stack, and all opening brackets are used up.
650 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
651 $children[] = [ '@lineStart', [ 1 ] ];
652 }
653 $titleNode = [ 'title', $titleAccum ];
654 $children[] = $titleNode;
655 $argIndex = 1;
656 foreach ( $parts as $part ) {
657 if ( isset( $part->eqpos ) ) {
658 $equalsNode = $part->out[$part->eqpos];
659 $nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ];
660 $valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ];
661 $partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ];
662 $children[] = $partNode;
663 } else {
664 $nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ];
665 $valueNode = [ 'value', $part->out ];
666 $partNode = [ 'part', [ $nameNode, $valueNode ] ];
667 $children[] = $partNode;
668 }
669 }
670 $element = [ [ $name, $children ] ];
671 }
672
673 # Advance input pointer
674 $i += $matchingCount;
675
676 # Unwind the stack
677 $stack->pop();
678 $accum =& $stack->getAccum();
679
680 # Re-add the old stack element if it still has unmatched opening characters remaining
681 if ( $matchingCount < $piece->count ) {
682 $piece->parts = [ new PPDPart_Hash ];
683 $piece->count -= $matchingCount;
684 # do we still qualify for any callback with remaining count?
685 $min = $this->rules[$piece->open]['min'];
686 if ( $piece->count >= $min ) {
687 $stack->push( $piece );
688 $accum =& $stack->getAccum();
689 } else {
690 $s = substr( $piece->open, 0, -1 );
691 $s .= str_repeat(
692 substr( $piece->open, -1 ),
693 $piece->count - strlen( $s )
694 );
695 self::addLiteral( $accum, $s );
696 }
697 }
698
699 extract( $stack->getFlags() );
700
701 # Add XML element to the enclosing accumulator
702 array_splice( $accum, count( $accum ), 0, $element );
703 } elseif ( $found == 'pipe' ) {
704 $findEquals = true; // shortcut for getFlags()
705 $stack->addPart();
706 $accum =& $stack->getAccum();
707 ++$i;
708 } elseif ( $found == 'equals' ) {
709 $findEquals = false; // shortcut for getFlags()
710 $accum[] = [ 'equals', [ '=' ] ];
711 $stack->getCurrentPart()->eqpos = count( $accum ) - 1;
712 ++$i;
713 } elseif ( $found == 'dash' ) {
714 self::addLiteral( $accum, '-' );
715 ++$i;
716 }
717 }
718
719 # Output any remaining unclosed brackets
720 foreach ( $stack->stack as $piece ) {
721 array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() );
722 }
723
724 # Enable top-level headings
725 foreach ( $stack->rootAccum as &$node ) {
726 if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) {
727 $node[PPNode_Hash_Tree::NAME] = 'h';
728 }
729 }
730
731 $rootStore = [ [ 'root', $stack->rootAccum ] ];
732 $rootNode = new PPNode_Hash_Tree( $rootStore, 0 );
733
734 // Cache
735 $tree = json_encode( $rootStore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
736 if ( $tree !== false ) {
737 $this->cacheSetTree( $text, $flags, $tree );
738 }
739
740 return $rootNode;
741 }
742
743 private static function addLiteral( array &$accum, $text ) {
744 $n = count( $accum );
745 if ( $n && is_string( $accum[$n - 1] ) ) {
746 $accum[$n - 1] .= $text;
747 } else {
748 $accum[] = $text;
749 }
750 }
751 }
752
753 /**
754 * Stack class to help Preprocessor::preprocessToObj()
755 * @ingroup Parser
756 */
757 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
758 class PPDStack_Hash extends PPDStack {
759 // @codingStandardsIgnoreEnd
760
761 public function __construct() {
762 $this->elementClass = 'PPDStackElement_Hash';
763 parent::__construct();
764 $this->rootAccum = [];
765 }
766 }
767
768 /**
769 * @ingroup Parser
770 */
771 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
772 class PPDStackElement_Hash extends PPDStackElement {
773 // @codingStandardsIgnoreEnd
774
775 public function __construct( $data = [] ) {
776 $this->partClass = 'PPDPart_Hash';
777 parent::__construct( $data );
778 }
779
780 /**
781 * Get the accumulator that would result if the close is not found.
782 *
783 * @param int|bool $openingCount
784 * @return array
785 */
786 public function breakSyntax( $openingCount = false ) {
787 if ( $this->open == "\n" ) {
788 $accum = $this->parts[0]->out;
789 } else {
790 if ( $openingCount === false ) {
791 $openingCount = $this->count;
792 }
793 $s = substr( $this->open, 0, -1 );
794 $s .= str_repeat(
795 substr( $this->open, -1 ),
796 $openingCount - strlen( $s )
797 );
798 $accum = [ $s ];
799 $lastIndex = 0;
800 $first = true;
801 foreach ( $this->parts as $part ) {
802 if ( $first ) {
803 $first = false;
804 } elseif ( is_string( $accum[$lastIndex] ) ) {
805 $accum[$lastIndex] .= '|';
806 } else {
807 $accum[++$lastIndex] = '|';
808 }
809 foreach ( $part->out as $node ) {
810 if ( is_string( $node ) && is_string( $accum[$lastIndex] ) ) {
811 $accum[$lastIndex] .= $node;
812 } else {
813 $accum[++$lastIndex] = $node;
814 }
815 }
816 }
817 }
818 return $accum;
819 }
820 }
821
822 /**
823 * @ingroup Parser
824 */
825 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
826 class PPDPart_Hash extends PPDPart {
827 // @codingStandardsIgnoreEnd
828
829 public function __construct( $out = '' ) {
830 if ( $out !== '' ) {
831 $accum = [ $out ];
832 } else {
833 $accum = [];
834 }
835 parent::__construct( $accum );
836 }
837 }
838
839 /**
840 * An expansion frame, used as a context to expand the result of preprocessToObj()
841 * @ingroup Parser
842 */
843 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
844 class PPFrame_Hash implements PPFrame {
845 // @codingStandardsIgnoreEnd
846
847 /**
848 * @var Parser
849 */
850 public $parser;
851
852 /**
853 * @var Preprocessor
854 */
855 public $preprocessor;
856
857 /**
858 * @var Title
859 */
860 public $title;
861 public $titleCache;
862
863 /**
864 * Hashtable listing templates which are disallowed for expansion in this frame,
865 * having been encountered previously in parent frames.
866 */
867 public $loopCheckHash;
868
869 /**
870 * Recursion depth of this frame, top = 0
871 * Note that this is NOT the same as expansion depth in expand()
872 */
873 public $depth;
874
875 private $volatile = false;
876 private $ttl = null;
877
878 /**
879 * @var array
880 */
881 protected $childExpansionCache;
882
883 /**
884 * Construct a new preprocessor frame.
885 * @param Preprocessor $preprocessor The parent preprocessor
886 */
887 public function __construct( $preprocessor ) {
888 $this->preprocessor = $preprocessor;
889 $this->parser = $preprocessor->parser;
890 $this->title = $this->parser->mTitle;
891 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
892 $this->loopCheckHash = [];
893 $this->depth = 0;
894 $this->childExpansionCache = [];
895 }
896
897 /**
898 * Create a new child frame
899 * $args is optionally a multi-root PPNode or array containing the template arguments
900 *
901 * @param array|bool|PPNode_Hash_Array $args
902 * @param Title|bool $title
903 * @param int $indexOffset
904 * @throws MWException
905 * @return PPTemplateFrame_Hash
906 */
907 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
908 $namedArgs = [];
909 $numberedArgs = [];
910 if ( $title === false ) {
911 $title = $this->title;
912 }
913 if ( $args !== false ) {
914 if ( $args instanceof PPNode_Hash_Array ) {
915 $args = $args->value;
916 } elseif ( !is_array( $args ) ) {
917 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
918 }
919 foreach ( $args as $arg ) {
920 $bits = $arg->splitArg();
921 if ( $bits['index'] !== '' ) {
922 // Numbered parameter
923 $index = $bits['index'] - $indexOffset;
924 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
925 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
926 wfEscapeWikiText( $this->title ),
927 wfEscapeWikiText( $title ),
928 wfEscapeWikiText( $index ) )->text() );
929 $this->parser->addTrackingCategory( 'duplicate-args-category' );
930 }
931 $numberedArgs[$index] = $bits['value'];
932 unset( $namedArgs[$index] );
933 } else {
934 // Named parameter
935 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
936 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
937 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
938 wfEscapeWikiText( $this->title ),
939 wfEscapeWikiText( $title ),
940 wfEscapeWikiText( $name ) )->text() );
941 $this->parser->addTrackingCategory( 'duplicate-args-category' );
942 }
943 $namedArgs[$name] = $bits['value'];
944 unset( $numberedArgs[$name] );
945 }
946 }
947 }
948 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
949 }
950
951 /**
952 * @throws MWException
953 * @param string|int $key
954 * @param string|PPNode $root
955 * @param int $flags
956 * @return string
957 */
958 public function cachedExpand( $key, $root, $flags = 0 ) {
959 // we don't have a parent, so we don't have a cache
960 return $this->expand( $root, $flags );
961 }
962
963 /**
964 * @throws MWException
965 * @param string|PPNode $root
966 * @param int $flags
967 * @return string
968 */
969 public function expand( $root, $flags = 0 ) {
970 static $expansionDepth = 0;
971 if ( is_string( $root ) ) {
972 return $root;
973 }
974
975 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
976 $this->parser->limitationWarn( 'node-count-exceeded',
977 $this->parser->mPPNodeCount,
978 $this->parser->mOptions->getMaxPPNodeCount()
979 );
980 return '<span class="error">Node-count limit exceeded</span>';
981 }
982 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
983 $this->parser->limitationWarn( 'expansion-depth-exceeded',
984 $expansionDepth,
985 $this->parser->mOptions->getMaxPPExpandDepth()
986 );
987 return '<span class="error">Expansion depth limit exceeded</span>';
988 }
989 ++$expansionDepth;
990 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
991 $this->parser->mHighestExpansionDepth = $expansionDepth;
992 }
993
994 $outStack = [ '', '' ];
995 $iteratorStack = [ false, $root ];
996 $indexStack = [ 0, 0 ];
997
998 while ( count( $iteratorStack ) > 1 ) {
999 $level = count( $outStack ) - 1;
1000 $iteratorNode =& $iteratorStack[$level];
1001 $out =& $outStack[$level];
1002 $index =& $indexStack[$level];
1003
1004 if ( is_array( $iteratorNode ) ) {
1005 if ( $index >= count( $iteratorNode ) ) {
1006 // All done with this iterator
1007 $iteratorStack[$level] = false;
1008 $contextNode = false;
1009 } else {
1010 $contextNode = $iteratorNode[$index];
1011 $index++;
1012 }
1013 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
1014 if ( $index >= $iteratorNode->getLength() ) {
1015 // All done with this iterator
1016 $iteratorStack[$level] = false;
1017 $contextNode = false;
1018 } else {
1019 $contextNode = $iteratorNode->item( $index );
1020 $index++;
1021 }
1022 } else {
1023 // Copy to $contextNode and then delete from iterator stack,
1024 // because this is not an iterator but we do have to execute it once
1025 $contextNode = $iteratorStack[$level];
1026 $iteratorStack[$level] = false;
1027 }
1028
1029 $newIterator = false;
1030 $contextName = false;
1031 $contextChildren = false;
1032
1033 if ( $contextNode === false ) {
1034 // nothing to do
1035 } elseif ( is_string( $contextNode ) ) {
1036 $out .= $contextNode;
1037 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
1038 $newIterator = $contextNode;
1039 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
1040 // No output
1041 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
1042 $out .= $contextNode->value;
1043 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
1044 $contextName = $contextNode->name;
1045 $contextChildren = $contextNode->getRawChildren();
1046 } elseif ( is_array( $contextNode ) ) {
1047 // Node descriptor array
1048 if ( count( $contextNode ) !== 2 ) {
1049 throw new MWException( __METHOD__.
1050 ': found an array where a node descriptor should be' );
1051 }
1052 list( $contextName, $contextChildren ) = $contextNode;
1053 } else {
1054 throw new MWException( __METHOD__ . ': Invalid parameter type' );
1055 }
1056
1057 // Handle node descriptor array or tree object
1058 if ( $contextName === false ) {
1059 // Not a node, already handled above
1060 } elseif ( $contextName[0] === '@' ) {
1061 // Attribute: no output
1062 } elseif ( $contextName === 'template' ) {
1063 # Double-brace expansion
1064 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1065 if ( $flags & PPFrame::NO_TEMPLATES ) {
1066 $newIterator = $this->virtualBracketedImplode(
1067 '{{', '|', '}}',
1068 $bits['title'],
1069 $bits['parts']
1070 );
1071 } else {
1072 $ret = $this->parser->braceSubstitution( $bits, $this );
1073 if ( isset( $ret['object'] ) ) {
1074 $newIterator = $ret['object'];
1075 } else {
1076 $out .= $ret['text'];
1077 }
1078 }
1079 } elseif ( $contextName === 'tplarg' ) {
1080 # Triple-brace expansion
1081 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
1082 if ( $flags & PPFrame::NO_ARGS ) {
1083 $newIterator = $this->virtualBracketedImplode(
1084 '{{{', '|', '}}}',
1085 $bits['title'],
1086 $bits['parts']
1087 );
1088 } else {
1089 $ret = $this->parser->argSubstitution( $bits, $this );
1090 if ( isset( $ret['object'] ) ) {
1091 $newIterator = $ret['object'];
1092 } else {
1093 $out .= $ret['text'];
1094 }
1095 }
1096 } elseif ( $contextName === 'comment' ) {
1097 # HTML-style comment
1098 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1099 # Not in RECOVER_COMMENTS mode (msgnw) though.
1100 if ( ( $this->parser->ot['html']
1101 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
1102 || ( $flags & PPFrame::STRIP_COMMENTS )
1103 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
1104 ) {
1105 $out .= '';
1106 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1107 # Add a strip marker in PST mode so that pstPass2() can
1108 # run some old-fashioned regexes on the result.
1109 # Not in RECOVER_COMMENTS mode (extractSections) though.
1110 $out .= $this->parser->insertStripItem( $contextChildren[0] );
1111 } else {
1112 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1113 $out .= $contextChildren[0];
1114 }
1115 } elseif ( $contextName === 'ignore' ) {
1116 # Output suppression used by <includeonly> etc.
1117 # OT_WIKI will only respect <ignore> in substed templates.
1118 # The other output types respect it unless NO_IGNORE is set.
1119 # extractSections() sets NO_IGNORE and so never respects it.
1120 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
1121 || ( $flags & PPFrame::NO_IGNORE )
1122 ) {
1123 $out .= $contextChildren[0];
1124 } else {
1125 // $out .= '';
1126 }
1127 } elseif ( $contextName === 'ext' ) {
1128 # Extension tag
1129 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
1130 [ 'attr' => null, 'inner' => null, 'close' => null ];
1131 if ( $flags & PPFrame::NO_TAGS ) {
1132 $s = '<' . $bits['name']->getFirstChild()->value;
1133 if ( $bits['attr'] ) {
1134 $s .= $bits['attr']->getFirstChild()->value;
1135 }
1136 if ( $bits['inner'] ) {
1137 $s .= '>' . $bits['inner']->getFirstChild()->value;
1138 if ( $bits['close'] ) {
1139 $s .= $bits['close']->getFirstChild()->value;
1140 }
1141 } else {
1142 $s .= '/>';
1143 }
1144 $out .= $s;
1145 } else {
1146 $out .= $this->parser->extensionSubstitution( $bits, $this );
1147 }
1148 } elseif ( $contextName === 'h' ) {
1149 # Heading
1150 if ( $this->parser->ot['html'] ) {
1151 # Expand immediately and insert heading index marker
1152 $s = $this->expand( $contextChildren, $flags );
1153 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
1154 $titleText = $this->title->getPrefixedDBkey();
1155 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
1156 $serial = count( $this->parser->mHeadings ) - 1;
1157 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
1158 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1159 $this->parser->mStripState->addGeneral( $marker, '' );
1160 $out .= $s;
1161 } else {
1162 # Expand in virtual stack
1163 $newIterator = $contextChildren;
1164 }
1165 } else {
1166 # Generic recursive expansion
1167 $newIterator = $contextChildren;
1168 }
1169
1170 if ( $newIterator !== false ) {
1171 $outStack[] = '';
1172 $iteratorStack[] = $newIterator;
1173 $indexStack[] = 0;
1174 } elseif ( $iteratorStack[$level] === false ) {
1175 // Return accumulated value to parent
1176 // With tail recursion
1177 while ( $iteratorStack[$level] === false && $level > 0 ) {
1178 $outStack[$level - 1] .= $out;
1179 array_pop( $outStack );
1180 array_pop( $iteratorStack );
1181 array_pop( $indexStack );
1182 $level--;
1183 }
1184 }
1185 }
1186 --$expansionDepth;
1187 return $outStack[0];
1188 }
1189
1190 /**
1191 * @param string $sep
1192 * @param int $flags
1193 * @param string|PPNode $args,...
1194 * @return string
1195 */
1196 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1197 $args = array_slice( func_get_args(), 2 );
1198
1199 $first = true;
1200 $s = '';
1201 foreach ( $args as $root ) {
1202 if ( $root instanceof PPNode_Hash_Array ) {
1203 $root = $root->value;
1204 }
1205 if ( !is_array( $root ) ) {
1206 $root = [ $root ];
1207 }
1208 foreach ( $root as $node ) {
1209 if ( $first ) {
1210 $first = false;
1211 } else {
1212 $s .= $sep;
1213 }
1214 $s .= $this->expand( $node, $flags );
1215 }
1216 }
1217 return $s;
1218 }
1219
1220 /**
1221 * Implode with no flags specified
1222 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1223 * @param string $sep
1224 * @param string|PPNode $args,...
1225 * @return string
1226 */
1227 public function implode( $sep /*, ... */ ) {
1228 $args = array_slice( func_get_args(), 1 );
1229
1230 $first = true;
1231 $s = '';
1232 foreach ( $args as $root ) {
1233 if ( $root instanceof PPNode_Hash_Array ) {
1234 $root = $root->value;
1235 }
1236 if ( !is_array( $root ) ) {
1237 $root = [ $root ];
1238 }
1239 foreach ( $root as $node ) {
1240 if ( $first ) {
1241 $first = false;
1242 } else {
1243 $s .= $sep;
1244 }
1245 $s .= $this->expand( $node );
1246 }
1247 }
1248 return $s;
1249 }
1250
1251 /**
1252 * Makes an object that, when expand()ed, will be the same as one obtained
1253 * with implode()
1254 *
1255 * @param string $sep
1256 * @param string|PPNode $args,...
1257 * @return PPNode_Hash_Array
1258 */
1259 public function virtualImplode( $sep /*, ... */ ) {
1260 $args = array_slice( func_get_args(), 1 );
1261 $out = [];
1262 $first = true;
1263
1264 foreach ( $args as $root ) {
1265 if ( $root instanceof PPNode_Hash_Array ) {
1266 $root = $root->value;
1267 }
1268 if ( !is_array( $root ) ) {
1269 $root = [ $root ];
1270 }
1271 foreach ( $root as $node ) {
1272 if ( $first ) {
1273 $first = false;
1274 } else {
1275 $out[] = $sep;
1276 }
1277 $out[] = $node;
1278 }
1279 }
1280 return new PPNode_Hash_Array( $out );
1281 }
1282
1283 /**
1284 * Virtual implode with brackets
1285 *
1286 * @param string $start
1287 * @param string $sep
1288 * @param string $end
1289 * @param string|PPNode $args,...
1290 * @return PPNode_Hash_Array
1291 */
1292 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1293 $args = array_slice( func_get_args(), 3 );
1294 $out = [ $start ];
1295 $first = true;
1296
1297 foreach ( $args as $root ) {
1298 if ( $root instanceof PPNode_Hash_Array ) {
1299 $root = $root->value;
1300 }
1301 if ( !is_array( $root ) ) {
1302 $root = [ $root ];
1303 }
1304 foreach ( $root as $node ) {
1305 if ( $first ) {
1306 $first = false;
1307 } else {
1308 $out[] = $sep;
1309 }
1310 $out[] = $node;
1311 }
1312 }
1313 $out[] = $end;
1314 return new PPNode_Hash_Array( $out );
1315 }
1316
1317 public function __toString() {
1318 return 'frame{}';
1319 }
1320
1321 /**
1322 * @param bool $level
1323 * @return array|bool|string
1324 */
1325 public function getPDBK( $level = false ) {
1326 if ( $level === false ) {
1327 return $this->title->getPrefixedDBkey();
1328 } else {
1329 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1330 }
1331 }
1332
1333 /**
1334 * @return array
1335 */
1336 public function getArguments() {
1337 return [];
1338 }
1339
1340 /**
1341 * @return array
1342 */
1343 public function getNumberedArguments() {
1344 return [];
1345 }
1346
1347 /**
1348 * @return array
1349 */
1350 public function getNamedArguments() {
1351 return [];
1352 }
1353
1354 /**
1355 * Returns true if there are no arguments in this frame
1356 *
1357 * @return bool
1358 */
1359 public function isEmpty() {
1360 return true;
1361 }
1362
1363 /**
1364 * @param int|string $name
1365 * @return bool Always false in this implementation.
1366 */
1367 public function getArgument( $name ) {
1368 return false;
1369 }
1370
1371 /**
1372 * Returns true if the infinite loop check is OK, false if a loop is detected
1373 *
1374 * @param Title $title
1375 *
1376 * @return bool
1377 */
1378 public function loopCheck( $title ) {
1379 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1380 }
1381
1382 /**
1383 * Return true if the frame is a template frame
1384 *
1385 * @return bool
1386 */
1387 public function isTemplate() {
1388 return false;
1389 }
1390
1391 /**
1392 * Get a title of frame
1393 *
1394 * @return Title
1395 */
1396 public function getTitle() {
1397 return $this->title;
1398 }
1399
1400 /**
1401 * Set the volatile flag
1402 *
1403 * @param bool $flag
1404 */
1405 public function setVolatile( $flag = true ) {
1406 $this->volatile = $flag;
1407 }
1408
1409 /**
1410 * Get the volatile flag
1411 *
1412 * @return bool
1413 */
1414 public function isVolatile() {
1415 return $this->volatile;
1416 }
1417
1418 /**
1419 * Set the TTL
1420 *
1421 * @param int $ttl
1422 */
1423 public function setTTL( $ttl ) {
1424 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
1425 $this->ttl = $ttl;
1426 }
1427 }
1428
1429 /**
1430 * Get the TTL
1431 *
1432 * @return int|null
1433 */
1434 public function getTTL() {
1435 return $this->ttl;
1436 }
1437 }
1438
1439 /**
1440 * Expansion frame with template arguments
1441 * @ingroup Parser
1442 */
1443 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1444 class PPTemplateFrame_Hash extends PPFrame_Hash {
1445 // @codingStandardsIgnoreEnd
1446
1447 public $numberedArgs, $namedArgs, $parent;
1448 public $numberedExpansionCache, $namedExpansionCache;
1449
1450 /**
1451 * @param Preprocessor $preprocessor
1452 * @param bool|PPFrame $parent
1453 * @param array $numberedArgs
1454 * @param array $namedArgs
1455 * @param bool|Title $title
1456 */
1457 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
1458 $namedArgs = [], $title = false
1459 ) {
1460 parent::__construct( $preprocessor );
1461
1462 $this->parent = $parent;
1463 $this->numberedArgs = $numberedArgs;
1464 $this->namedArgs = $namedArgs;
1465 $this->title = $title;
1466 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1467 $this->titleCache = $parent->titleCache;
1468 $this->titleCache[] = $pdbk;
1469 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1470 if ( $pdbk !== false ) {
1471 $this->loopCheckHash[$pdbk] = true;
1472 }
1473 $this->depth = $parent->depth + 1;
1474 $this->numberedExpansionCache = $this->namedExpansionCache = [];
1475 }
1476
1477 public function __toString() {
1478 $s = 'tplframe{';
1479 $first = true;
1480 $args = $this->numberedArgs + $this->namedArgs;
1481 foreach ( $args as $name => $value ) {
1482 if ( $first ) {
1483 $first = false;
1484 } else {
1485 $s .= ', ';
1486 }
1487 $s .= "\"$name\":\"" .
1488 str_replace( '"', '\\"', $value->__toString() ) . '"';
1489 }
1490 $s .= '}';
1491 return $s;
1492 }
1493
1494 /**
1495 * @throws MWException
1496 * @param string|int $key
1497 * @param string|PPNode $root
1498 * @param int $flags
1499 * @return string
1500 */
1501 public function cachedExpand( $key, $root, $flags = 0 ) {
1502 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
1503 return $this->parent->childExpansionCache[$key];
1504 }
1505 $retval = $this->expand( $root, $flags );
1506 if ( !$this->isVolatile() ) {
1507 $this->parent->childExpansionCache[$key] = $retval;
1508 }
1509 return $retval;
1510 }
1511
1512 /**
1513 * Returns true if there are no arguments in this frame
1514 *
1515 * @return bool
1516 */
1517 public function isEmpty() {
1518 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1519 }
1520
1521 /**
1522 * @return array
1523 */
1524 public function getArguments() {
1525 $arguments = [];
1526 foreach ( array_merge(
1527 array_keys( $this->numberedArgs ),
1528 array_keys( $this->namedArgs ) ) as $key ) {
1529 $arguments[$key] = $this->getArgument( $key );
1530 }
1531 return $arguments;
1532 }
1533
1534 /**
1535 * @return array
1536 */
1537 public function getNumberedArguments() {
1538 $arguments = [];
1539 foreach ( array_keys( $this->numberedArgs ) as $key ) {
1540 $arguments[$key] = $this->getArgument( $key );
1541 }
1542 return $arguments;
1543 }
1544
1545 /**
1546 * @return array
1547 */
1548 public function getNamedArguments() {
1549 $arguments = [];
1550 foreach ( array_keys( $this->namedArgs ) as $key ) {
1551 $arguments[$key] = $this->getArgument( $key );
1552 }
1553 return $arguments;
1554 }
1555
1556 /**
1557 * @param int $index
1558 * @return string|bool
1559 */
1560 public function getNumberedArgument( $index ) {
1561 if ( !isset( $this->numberedArgs[$index] ) ) {
1562 return false;
1563 }
1564 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1565 # No trimming for unnamed arguments
1566 $this->numberedExpansionCache[$index] = $this->parent->expand(
1567 $this->numberedArgs[$index],
1568 PPFrame::STRIP_COMMENTS
1569 );
1570 }
1571 return $this->numberedExpansionCache[$index];
1572 }
1573
1574 /**
1575 * @param string $name
1576 * @return string|bool
1577 */
1578 public function getNamedArgument( $name ) {
1579 if ( !isset( $this->namedArgs[$name] ) ) {
1580 return false;
1581 }
1582 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1583 # Trim named arguments post-expand, for backwards compatibility
1584 $this->namedExpansionCache[$name] = trim(
1585 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1586 }
1587 return $this->namedExpansionCache[$name];
1588 }
1589
1590 /**
1591 * @param int|string $name
1592 * @return string|bool
1593 */
1594 public function getArgument( $name ) {
1595 $text = $this->getNumberedArgument( $name );
1596 if ( $text === false ) {
1597 $text = $this->getNamedArgument( $name );
1598 }
1599 return $text;
1600 }
1601
1602 /**
1603 * Return true if the frame is a template frame
1604 *
1605 * @return bool
1606 */
1607 public function isTemplate() {
1608 return true;
1609 }
1610
1611 public function setVolatile( $flag = true ) {
1612 parent::setVolatile( $flag );
1613 $this->parent->setVolatile( $flag );
1614 }
1615
1616 public function setTTL( $ttl ) {
1617 parent::setTTL( $ttl );
1618 $this->parent->setTTL( $ttl );
1619 }
1620 }
1621
1622 /**
1623 * Expansion frame with custom arguments
1624 * @ingroup Parser
1625 */
1626 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1627 class PPCustomFrame_Hash extends PPFrame_Hash {
1628 // @codingStandardsIgnoreEnd
1629
1630 public $args;
1631
1632 public function __construct( $preprocessor, $args ) {
1633 parent::__construct( $preprocessor );
1634 $this->args = $args;
1635 }
1636
1637 public function __toString() {
1638 $s = 'cstmframe{';
1639 $first = true;
1640 foreach ( $this->args as $name => $value ) {
1641 if ( $first ) {
1642 $first = false;
1643 } else {
1644 $s .= ', ';
1645 }
1646 $s .= "\"$name\":\"" .
1647 str_replace( '"', '\\"', $value->__toString() ) . '"';
1648 }
1649 $s .= '}';
1650 return $s;
1651 }
1652
1653 /**
1654 * @return bool
1655 */
1656 public function isEmpty() {
1657 return !count( $this->args );
1658 }
1659
1660 /**
1661 * @param int|string $index
1662 * @return string|bool
1663 */
1664 public function getArgument( $index ) {
1665 if ( !isset( $this->args[$index] ) ) {
1666 return false;
1667 }
1668 return $this->args[$index];
1669 }
1670
1671 public function getArguments() {
1672 return $this->args;
1673 }
1674 }
1675
1676 /**
1677 * @ingroup Parser
1678 */
1679 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1680 class PPNode_Hash_Tree implements PPNode {
1681 // @codingStandardsIgnoreEnd
1682
1683 public $name;
1684
1685 /**
1686 * The store array for children of this node. It is "raw" in the sense that
1687 * nodes are two-element arrays ("descriptors") rather than PPNode_Hash_*
1688 * objects.
1689 */
1690 private $rawChildren;
1691
1692 /**
1693 * The store array for the siblings of this node, including this node itself.
1694 */
1695 private $store;
1696
1697 /**
1698 * The index into $this->store which contains the descriptor of this node.
1699 */
1700 private $index;
1701
1702 /**
1703 * The offset of the name within descriptors, used in some places for
1704 * readability.
1705 */
1706 const NAME = 0;
1707
1708 /**
1709 * The offset of the child list within descriptors, used in some places for
1710 * readability.
1711 */
1712 const CHILDREN = 1;
1713
1714 /**
1715 * Construct an object using the data from $store[$index]. The rest of the
1716 * store array can be accessed via getNextSibling().
1717 *
1718 * @param array $store
1719 * @param integer $index
1720 */
1721 public function __construct( array $store, $index ) {
1722 $this->store = $store;
1723 $this->index = $index;
1724 list( $this->name, $this->rawChildren ) = $this->store[$index];
1725 }
1726
1727 /**
1728 * Construct an appropriate PPNode_Hash_* object with a class that depends
1729 * on what is at the relevant store index.
1730 *
1731 * @param array $store
1732 * @param integer $index
1733 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text
1734 */
1735 public static function factory( array $store, $index ) {
1736 if ( !isset( $store[$index] ) ) {
1737 return false;
1738 }
1739
1740 $descriptor = $store[$index];
1741 if ( is_string( $descriptor ) ) {
1742 $class = 'PPNode_Hash_Text';
1743 } elseif ( is_array( $descriptor ) ) {
1744 if ( $descriptor[self::NAME][0] === '@' ) {
1745 $class = 'PPNode_Hash_Attr';
1746 } else {
1747 $class = 'PPNode_Hash_Tree';
1748 }
1749 } else {
1750 throw new MWException( __METHOD__.': invalid node descriptor' );
1751 }
1752 return new $class( $store, $index );
1753 }
1754
1755 /**
1756 * Convert a node to XML, for debugging
1757 */
1758 public function __toString() {
1759 $inner = '';
1760 $attribs = '';
1761 for ( $node = $this->getFirstChild(); $node; $node = $node->getNextSibling() ) {
1762 if ( $node instanceof PPNode_Hash_Attr ) {
1763 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1764 } else {
1765 $inner .= $node->__toString();
1766 }
1767 }
1768 if ( $inner === '' ) {
1769 return "<{$this->name}$attribs/>";
1770 } else {
1771 return "<{$this->name}$attribs>$inner</{$this->name}>";
1772 }
1773 }
1774
1775 /**
1776 * @return PPNode_Hash_Array
1777 */
1778 public function getChildren() {
1779 $children = [];
1780 foreach ( $this->rawChildren as $i => $child ) {
1781 $children[] = self::factory( $this->rawChildren, $i );
1782 }
1783 return new PPNode_Hash_Array( $children );
1784 }
1785
1786 /**
1787 * Get the first child, or false if there is none. Note that this will
1788 * return a temporary proxy object: different instances will be returned
1789 * if this is called more than once on the same node.
1790 *
1791 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|boolean
1792 */
1793 public function getFirstChild() {
1794 if ( !isset( $this->rawChildren[0] ) ) {
1795 return false;
1796 } else {
1797 return self::factory( $this->rawChildren, 0 );
1798 }
1799 }
1800
1801 /**
1802 * Get the next sibling, or false if there is none. Note that this will
1803 * return a temporary proxy object: different instances will be returned
1804 * if this is called more than once on the same node.
1805 *
1806 * @return PPNode_Hash_Tree|PPNode_Hash_Attr|PPNode_Hash_Text|boolean
1807 */
1808 public function getNextSibling() {
1809 return self::factory( $this->store, $this->index + 1 );
1810 }
1811
1812 /**
1813 * Get an array of the children with a given node name
1814 *
1815 * @param string $name
1816 * @return PPNode_Hash_Array
1817 */
1818 public function getChildrenOfType( $name ) {
1819 $children = [];
1820 foreach ( $this->rawChildren as $i => $child ) {
1821 if ( is_array( $child ) && $child[self::NAME] === $name ) {
1822 $children[] = self::factory( $this->rawChildren, $i );
1823 }
1824 }
1825 return new PPNode_Hash_Array( $children );
1826 }
1827
1828 /**
1829 * Get the raw child array. For internal use.
1830 * @return array
1831 */
1832 public function getRawChildren() {
1833 return $this->rawChildren;
1834 }
1835
1836 /**
1837 * @return bool
1838 */
1839 public function getLength() {
1840 return false;
1841 }
1842
1843 /**
1844 * @param int $i
1845 * @return bool
1846 */
1847 public function item( $i ) {
1848 return false;
1849 }
1850
1851 /**
1852 * @return string
1853 */
1854 public function getName() {
1855 return $this->name;
1856 }
1857
1858 /**
1859 * Split a "<part>" node into an associative array containing:
1860 * - name PPNode name
1861 * - index String index
1862 * - value PPNode value
1863 *
1864 * @throws MWException
1865 * @return array
1866 */
1867 public function splitArg() {
1868 return self::splitRawArg( $this->rawChildren );
1869 }
1870
1871 /**
1872 * Like splitArg() but for a raw child array. For internal use only.
1873 */
1874 public static function splitRawArg( array $children ) {
1875 $bits = [];
1876 foreach ( $children as $i => $child ) {
1877 if ( !is_array( $child ) ) {
1878 continue;
1879 }
1880 if ( $child[self::NAME] === 'name' ) {
1881 $bits['name'] = new self( $children, $i );
1882 if ( isset( $child[self::CHILDREN][0][self::NAME] )
1883 && $child[self::CHILDREN][0][self::NAME] === '@index'
1884 ) {
1885 $bits['index'] = $child[self::CHILDREN][0][self::CHILDREN][0];
1886 }
1887 } elseif ( $child[self::NAME] === 'value' ) {
1888 $bits['value'] = new self( $children, $i );
1889 }
1890 }
1891
1892 if ( !isset( $bits['name'] ) ) {
1893 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1894 }
1895 if ( !isset( $bits['index'] ) ) {
1896 $bits['index'] = '';
1897 }
1898 return $bits;
1899 }
1900
1901 /**
1902 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1903 * All values in the resulting array are PPNodes. Inner and close are optional.
1904 *
1905 * @throws MWException
1906 * @return array
1907 */
1908 public function splitExt() {
1909 return self::splitRawExt( $this->rawChildren );
1910 }
1911
1912 /**
1913 * Like splitExt() but for a raw child array. For internal use only.
1914 */
1915 public static function splitRawExt( array $children ) {
1916 $bits = [];
1917 foreach ( $children as $i => $child ) {
1918 if ( !is_array( $child ) ) {
1919 continue;
1920 }
1921 switch ( $child[self::NAME] ) {
1922 case 'name':
1923 $bits['name'] = new self( $children, $i );
1924 break;
1925 case 'attr':
1926 $bits['attr'] = new self( $children, $i );
1927 break;
1928 case 'inner':
1929 $bits['inner'] = new self( $children, $i );
1930 break;
1931 case 'close':
1932 $bits['close'] = new self( $children, $i );
1933 break;
1934 }
1935 }
1936 if ( !isset( $bits['name'] ) ) {
1937 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1938 }
1939 return $bits;
1940 }
1941
1942 /**
1943 * Split an "<h>" node
1944 *
1945 * @throws MWException
1946 * @return array
1947 */
1948 public function splitHeading() {
1949 if ( $this->name !== 'h' ) {
1950 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1951 }
1952 return self::splitRawHeading( $this->rawChildren );
1953 }
1954
1955 /**
1956 * Like splitHeading() but for a raw child array. For internal use only.
1957 */
1958 public static function splitRawHeading( array $children ) {
1959 $bits = [];
1960 foreach ( $children as $i => $child ) {
1961 if ( !is_array( $child ) ) {
1962 continue;
1963 }
1964 if ( $child[self::NAME] === '@i' ) {
1965 $bits['i'] = $child[self::CHILDREN][0];
1966 } elseif ( $child[self::NAME] === '@level' ) {
1967 $bits['level'] = $child[self::CHILDREN][0];
1968 }
1969 }
1970 if ( !isset( $bits['i'] ) ) {
1971 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1972 }
1973 return $bits;
1974 }
1975
1976 /**
1977 * Split a "<template>" or "<tplarg>" node
1978 *
1979 * @throws MWException
1980 * @return array
1981 */
1982 public function splitTemplate() {
1983 return self::splitRawTemplate( $this->rawChildren );
1984 }
1985
1986 /**
1987 * Like splitTemplate() but for a raw child array. For internal use only.
1988 */
1989 public static function splitRawTemplate( array $children ) {
1990 $parts = [];
1991 $bits = [ 'lineStart' => '' ];
1992 foreach ( $children as $i => $child ) {
1993 if ( !is_array( $child ) ) {
1994 continue;
1995 }
1996 switch ( $child[self::NAME] ) {
1997 case 'title':
1998 $bits['title'] = new self( $children, $i );
1999 break;
2000 case 'part':
2001 $parts[] = new self( $children, $i );
2002 break;
2003 case '@lineStart':
2004 $bits['lineStart'] = '1';
2005 break;
2006 }
2007 }
2008 if ( !isset( $bits['title'] ) ) {
2009 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
2010 }
2011 $bits['parts'] = new PPNode_Hash_Array( $parts );
2012 return $bits;
2013 }
2014 }
2015
2016 /**
2017 * @ingroup Parser
2018 */
2019 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
2020 class PPNode_Hash_Text implements PPNode {
2021 // @codingStandardsIgnoreEnd
2022
2023 public $value;
2024 private $store, $index;
2025
2026 /**
2027 * Construct an object using the data from $store[$index]. The rest of the
2028 * store array can be accessed via getNextSibling().
2029 *
2030 * @param array $store
2031 * @param integer $index
2032 */
2033 public function __construct( array $store, $index ) {
2034 $this->value = $store[$index];
2035 if ( !is_scalar( $this->value ) ) {
2036 throw new MWException( __CLASS__ . ' given object instead of string' );
2037 }
2038 $this->store = $store;
2039 $this->index = $index;
2040 }
2041
2042 public function __toString() {
2043 return htmlspecialchars( $this->value );
2044 }
2045
2046 public function getNextSibling() {
2047 return PPNode_Hash_Tree::factory( $this->store, $this->index + 1 );
2048 }
2049
2050 public function getChildren() {
2051 return false;
2052 }
2053
2054 public function getFirstChild() {
2055 return false;
2056 }
2057
2058 public function getChildrenOfType( $name ) {
2059 return false;
2060 }
2061
2062 public function getLength() {
2063 return false;
2064 }
2065
2066 public function item( $i ) {
2067 return false;
2068 }
2069
2070 public function getName() {
2071 return '#text';
2072 }
2073
2074 public function splitArg() {
2075 throw new MWException( __METHOD__ . ': not supported' );
2076 }
2077
2078 public function splitExt() {
2079 throw new MWException( __METHOD__ . ': not supported' );
2080 }
2081
2082 public function splitHeading() {
2083 throw new MWException( __METHOD__ . ': not supported' );
2084 }
2085 }
2086
2087 /**
2088 * @ingroup Parser
2089 */
2090 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
2091 class PPNode_Hash_Array implements PPNode {
2092 // @codingStandardsIgnoreEnd
2093
2094 public $value;
2095
2096 public function __construct( $value ) {
2097 $this->value = $value;
2098 }
2099
2100 public function __toString() {
2101 return var_export( $this, true );
2102 }
2103
2104 public function getLength() {
2105 return count( $this->value );
2106 }
2107
2108 public function item( $i ) {
2109 return $this->value[$i];
2110 }
2111
2112 public function getName() {
2113 return '#nodelist';
2114 }
2115
2116 public function getNextSibling() {
2117 return false;
2118 }
2119
2120 public function getChildren() {
2121 return false;
2122 }
2123
2124 public function getFirstChild() {
2125 return false;
2126 }
2127
2128 public function getChildrenOfType( $name ) {
2129 return false;
2130 }
2131
2132 public function splitArg() {
2133 throw new MWException( __METHOD__ . ': not supported' );
2134 }
2135
2136 public function splitExt() {
2137 throw new MWException( __METHOD__ . ': not supported' );
2138 }
2139
2140 public function splitHeading() {
2141 throw new MWException( __METHOD__ . ': not supported' );
2142 }
2143 }
2144
2145 /**
2146 * @ingroup Parser
2147 */
2148 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
2149 class PPNode_Hash_Attr implements PPNode {
2150 // @codingStandardsIgnoreEnd
2151
2152 public $name, $value;
2153 private $store, $index;
2154
2155 /**
2156 * Construct an object using the data from $store[$index]. The rest of the
2157 * store array can be accessed via getNextSibling().
2158 *
2159 * @param array $store
2160 * @param integer $index
2161 */
2162 public function __construct( array $store, $index ) {
2163 $descriptor = $store[$index];
2164 if ( $descriptor[PPNode_Hash_Tree::NAME][0] !== '@' ) {
2165 throw new MWException( __METHOD__.': invalid name in attribute descriptor' );
2166 }
2167 $this->name = substr( $descriptor[PPNode_Hash_Tree::NAME], 1 );
2168 $this->value = $descriptor[PPNode_Hash_Tree::CHILDREN][0];
2169 $this->store = $store;
2170 $this->index = $index;
2171 }
2172
2173 public function __toString() {
2174 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
2175 }
2176
2177 public function getName() {
2178 return $this->name;
2179 }
2180
2181 public function getNextSibling() {
2182 return PPNode_Hash_Tree::factory( $this->store, $this->index + 1 );
2183 }
2184
2185 public function getChildren() {
2186 return false;
2187 }
2188
2189 public function getFirstChild() {
2190 return false;
2191 }
2192
2193 public function getChildrenOfType( $name ) {
2194 return false;
2195 }
2196
2197 public function getLength() {
2198 return false;
2199 }
2200
2201 public function item( $i ) {
2202 return false;
2203 }
2204
2205 public function splitArg() {
2206 throw new MWException( __METHOD__ . ': not supported' );
2207 }
2208
2209 public function splitExt() {
2210 throw new MWException( __METHOD__ . ': not supported' );
2211 }
2212
2213 public function splitHeading() {
2214 throw new MWException( __METHOD__ . ': not supported' );
2215 }
2216 }