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