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