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