Merge "(bug 18195) Allow changing preferences via API"
[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
1106 $outStack = array( '', '' );
1107 $iteratorStack = array( false, $root );
1108 $indexStack = array( 0, 0 );
1109
1110 while ( count( $iteratorStack ) > 1 ) {
1111 $level = count( $outStack ) - 1;
1112 $iteratorNode =& $iteratorStack[ $level ];
1113 $out =& $outStack[$level];
1114 $index =& $indexStack[$level];
1115
1116 if ( is_array( $iteratorNode ) ) {
1117 if ( $index >= count( $iteratorNode ) ) {
1118 // All done with this iterator
1119 $iteratorStack[$level] = false;
1120 $contextNode = false;
1121 } else {
1122 $contextNode = $iteratorNode[$index];
1123 $index++;
1124 }
1125 } elseif ( $iteratorNode instanceof PPNode_HipHop_Array ) {
1126 if ( $index >= $iteratorNode->getLength() ) {
1127 // All done with this iterator
1128 $iteratorStack[$level] = false;
1129 $contextNode = false;
1130 } else {
1131 $contextNode = $iteratorNode->item( $index );
1132 $index++;
1133 }
1134 } else {
1135 // Copy to $contextNode and then delete from iterator stack,
1136 // because this is not an iterator but we do have to execute it once
1137 $contextNode = $iteratorStack[$level];
1138 $iteratorStack[$level] = false;
1139 }
1140
1141 $newIterator = false;
1142
1143 if ( $contextNode === false ) {
1144 // nothing to do
1145 } elseif ( is_string( $contextNode ) ) {
1146 $out .= $contextNode;
1147 } elseif ( is_array( $contextNode ) || $contextNode instanceof PPNode_HipHop_Array ) {
1148 $newIterator = $contextNode;
1149 } elseif ( $contextNode instanceof PPNode_HipHop_Attr ) {
1150 // No output
1151 } elseif ( $contextNode instanceof PPNode_HipHop_Text ) {
1152 $out .= $contextNode->value;
1153 } elseif ( $contextNode instanceof PPNode_HipHop_Tree ) {
1154 if ( $contextNode->name === 'template' ) {
1155 # Double-brace expansion
1156 $bits = $contextNode->splitTemplate();
1157 if ( $flags & PPFrame::NO_TEMPLATES ) {
1158 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $bits['title'], $bits['parts'] );
1159 } else {
1160 $ret = $this->parser->braceSubstitution( $bits, $this );
1161 if ( isset( $ret['object'] ) ) {
1162 $newIterator = $ret['object'];
1163 } else {
1164 $out .= $ret['text'];
1165 }
1166 }
1167 } elseif ( $contextNode->name === 'tplarg' ) {
1168 # Triple-brace expansion
1169 $bits = $contextNode->splitTemplate();
1170 if ( $flags & PPFrame::NO_ARGS ) {
1171 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $bits['title'], $bits['parts'] );
1172 } else {
1173 $ret = $this->parser->argSubstitution( $bits, $this );
1174 if ( isset( $ret['object'] ) ) {
1175 $newIterator = $ret['object'];
1176 } else {
1177 $out .= $ret['text'];
1178 }
1179 }
1180 } elseif ( $contextNode->name === 'comment' ) {
1181 # HTML-style comment
1182 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1183 if ( $this->parser->ot['html']
1184 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
1185 || ( $flags & PPFrame::STRIP_COMMENTS ) )
1186 {
1187 $out .= '';
1188 }
1189 # Add a strip marker in PST mode so that pstPass2() can run some old-fashioned regexes on the result
1190 # Not in RECOVER_COMMENTS mode (extractSections) though
1191 elseif ( $this->parser->ot['wiki'] && ! ( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1192 $out .= $this->parser->insertStripItem( $contextNode->firstChild->value );
1193 }
1194 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1195 else {
1196 $out .= $contextNode->firstChild->value;
1197 }
1198 } elseif ( $contextNode->name === 'ignore' ) {
1199 # Output suppression used by <includeonly> etc.
1200 # OT_WIKI will only respect <ignore> in substed templates.
1201 # The other output types respect it unless NO_IGNORE is set.
1202 # extractSections() sets NO_IGNORE and so never respects it.
1203 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] ) || ( $flags & PPFrame::NO_IGNORE ) ) {
1204 $out .= $contextNode->firstChild->value;
1205 } else {
1206 //$out .= '';
1207 }
1208 } elseif ( $contextNode->name === 'ext' ) {
1209 # Extension tag
1210 $bits = $contextNode->splitExt() + array( 'attr' => null, 'inner' => null, 'close' => null );
1211 $out .= $this->parser->extensionSubstitution( $bits, $this );
1212 } elseif ( $contextNode->name === 'h' ) {
1213 # Heading
1214 if ( $this->parser->ot['html'] ) {
1215 # Expand immediately and insert heading index marker
1216 $s = '';
1217 for ( $node = $contextNode->firstChild; $node; $node = $node->nextSibling ) {
1218 $s .= $this->expand( $node, $flags );
1219 }
1220
1221 $bits = $contextNode->splitHeading();
1222 $titleText = $this->title->getPrefixedDBkey();
1223 $this->parser->mHeadings[] = array( $titleText, $bits['i'] );
1224 $serial = count( $this->parser->mHeadings ) - 1;
1225 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser::MARKER_SUFFIX;
1226 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1227 $this->parser->mStripState->addGeneral( $marker, '' );
1228 $out .= $s;
1229 } else {
1230 # Expand in virtual stack
1231 $newIterator = $contextNode->getChildren();
1232 }
1233 } else {
1234 # Generic recursive expansion
1235 $newIterator = $contextNode->getChildren();
1236 }
1237 } else {
1238 throw new MWException( __METHOD__.': Invalid parameter type' );
1239 }
1240
1241 if ( $newIterator !== false ) {
1242 $outStack[] = '';
1243 $iteratorStack[] = $newIterator;
1244 $indexStack[] = 0;
1245 } elseif ( $iteratorStack[$level] === false ) {
1246 // Return accumulated value to parent
1247 // With tail recursion
1248 while ( $iteratorStack[$level] === false && $level > 0 ) {
1249 $outStack[$level - 1] .= $out;
1250 array_pop( $outStack );
1251 array_pop( $iteratorStack );
1252 array_pop( $indexStack );
1253 $level--;
1254 }
1255 }
1256 }
1257 --$expansionDepth;
1258 return $outStack[0];
1259 }
1260
1261 /**
1262 * @param $sep
1263 * @param $flags
1264 * @return string
1265 */
1266 function implodeWithFlags( $sep, $flags /*, ... */ ) {
1267 $args = array_slice( func_get_args(), 2 );
1268
1269 $first = true;
1270 $s = '';
1271 foreach ( $args as $root ) {
1272 if ( $root instanceof PPNode_HipHop_Array ) {
1273 $root = $root->value;
1274 }
1275 if ( !is_array( $root ) ) {
1276 $root = array( $root );
1277 }
1278 foreach ( $root as $node ) {
1279 if ( $first ) {
1280 $first = false;
1281 } else {
1282 $s .= $sep;
1283 }
1284 $s .= $this->expand( $node, $flags );
1285 }
1286 }
1287 return $s;
1288 }
1289
1290 /**
1291 * Implode with no flags specified
1292 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1293 * @return string
1294 */
1295 function implode( $sep /*, ... */ ) {
1296 $args = array_slice( func_get_args(), 1 );
1297
1298 $first = true;
1299 $s = '';
1300 foreach ( $args as $root ) {
1301 if ( $root instanceof PPNode_HipHop_Array ) {
1302 $root = $root->value;
1303 }
1304 if ( !is_array( $root ) ) {
1305 $root = array( $root );
1306 }
1307 foreach ( $root as $node ) {
1308 if ( $first ) {
1309 $first = false;
1310 } else {
1311 $s .= $sep;
1312 }
1313 $s .= $this->expand( $node );
1314 }
1315 }
1316 return $s;
1317 }
1318
1319 /**
1320 * Makes an object that, when expand()ed, will be the same as one obtained
1321 * with implode()
1322 *
1323 * @return PPNode_HipHop_Array
1324 */
1325 function virtualImplode( $sep /*, ... */ ) {
1326 $args = array_slice( func_get_args(), 1 );
1327 $out = array();
1328 $first = true;
1329
1330 foreach ( $args as $root ) {
1331 if ( $root instanceof PPNode_HipHop_Array ) {
1332 $root = $root->value;
1333 }
1334 if ( !is_array( $root ) ) {
1335 $root = array( $root );
1336 }
1337 foreach ( $root as $node ) {
1338 if ( $first ) {
1339 $first = false;
1340 } else {
1341 $out[] = $sep;
1342 }
1343 $out[] = $node;
1344 }
1345 }
1346 return new PPNode_HipHop_Array( $out );
1347 }
1348
1349 /**
1350 * Virtual implode with brackets
1351 *
1352 * @return PPNode_HipHop_Array
1353 */
1354 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1355 $args = array_slice( func_get_args(), 3 );
1356 $out = array( $start );
1357 $first = true;
1358
1359 foreach ( $args as $root ) {
1360 if ( $root instanceof PPNode_HipHop_Array ) {
1361 $root = $root->value;
1362 }
1363 if ( !is_array( $root ) ) {
1364 $root = array( $root );
1365 }
1366 foreach ( $root as $node ) {
1367 if ( $first ) {
1368 $first = false;
1369 } else {
1370 $out[] = $sep;
1371 }
1372 $out[] = $node;
1373 }
1374 }
1375 $out[] = $end;
1376 return new PPNode_HipHop_Array( $out );
1377 }
1378
1379 function __toString() {
1380 return 'frame{}';
1381 }
1382
1383 /**
1384 * @param $level bool
1385 * @return array|bool|String
1386 */
1387 function getPDBK( $level = false ) {
1388 if ( $level === false ) {
1389 return $this->title->getPrefixedDBkey();
1390 } else {
1391 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1392 }
1393 }
1394
1395 /**
1396 * @return array
1397 */
1398 function getArguments() {
1399 return array();
1400 }
1401
1402 /**
1403 * @return array
1404 */
1405 function getNumberedArguments() {
1406 return array();
1407 }
1408
1409 /**
1410 * @return array
1411 */
1412 function getNamedArguments() {
1413 return array();
1414 }
1415
1416 /**
1417 * Returns true if there are no arguments in this frame
1418 *
1419 * @return bool
1420 */
1421 function isEmpty() {
1422 return true;
1423 }
1424
1425 /**
1426 * @param $name
1427 * @return bool
1428 */
1429 function getArgument( $name ) {
1430 return false;
1431 }
1432
1433 /**
1434 * Returns true if the infinite loop check is OK, false if a loop is detected
1435 *
1436 * @param $title Title
1437 *
1438 * @return bool
1439 */
1440 function loopCheck( $title ) {
1441 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1442 }
1443
1444 /**
1445 * Return true if the frame is a template frame
1446 *
1447 * @return bool
1448 */
1449 function isTemplate() {
1450 return false;
1451 }
1452
1453 /**
1454 * Get a title of frame
1455 *
1456 * @return Title
1457 */
1458 function getTitle() {
1459 return $this->title;
1460 }
1461 }
1462
1463 /**
1464 * Expansion frame with template arguments
1465 * @ingroup Parser
1466 */
1467 class PPTemplateFrame_HipHop extends PPFrame_HipHop {
1468 var $numberedArgs, $namedArgs, $parent;
1469 var $numberedExpansionCache, $namedExpansionCache;
1470
1471 /**
1472 * @param $preprocessor
1473 * @param $parent
1474 * @param $numberedArgs array
1475 * @param $namedArgs array
1476 * @param $title Title
1477 */
1478 function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
1479 parent::__construct( $preprocessor );
1480
1481 $this->parent = $parent;
1482 $this->numberedArgs = $numberedArgs;
1483 $this->namedArgs = $namedArgs;
1484 $this->title = $title;
1485 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1486 $this->titleCache = $parent->titleCache;
1487 $this->titleCache[] = $pdbk;
1488 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1489 if ( $pdbk !== false ) {
1490 $this->loopCheckHash[$pdbk] = true;
1491 }
1492 $this->depth = $parent->depth + 1;
1493 $this->numberedExpansionCache = $this->namedExpansionCache = array();
1494 }
1495
1496 function __toString() {
1497 $s = 'tplframe{';
1498 $first = true;
1499 $args = $this->numberedArgs + $this->namedArgs;
1500 foreach ( $args as $name => $value ) {
1501 if ( $first ) {
1502 $first = false;
1503 } else {
1504 $s .= ', ';
1505 }
1506 $s .= "\"$name\":\"" .
1507 str_replace( '"', '\\"', $value->__toString() ) . '"';
1508 }
1509 $s .= '}';
1510 return $s;
1511 }
1512 /**
1513 * Returns true if there are no arguments in this frame
1514 *
1515 * @return bool
1516 */
1517 function isEmpty() {
1518 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1519 }
1520
1521 /**
1522 * @return array
1523 */
1524 function getArguments() {
1525 $arguments = array();
1526 foreach ( array_merge(
1527 array_keys($this->numberedArgs),
1528 array_keys($this->namedArgs)) as $key ) {
1529 $arguments[$key] = $this->getArgument($key);
1530 }
1531 return $arguments;
1532 }
1533
1534 /**
1535 * @return array
1536 */
1537 function getNumberedArguments() {
1538 $arguments = array();
1539 foreach ( array_keys($this->numberedArgs) as $key ) {
1540 $arguments[$key] = $this->getArgument($key);
1541 }
1542 return $arguments;
1543 }
1544
1545 /**
1546 * @return array
1547 */
1548 function getNamedArguments() {
1549 $arguments = array();
1550 foreach ( array_keys($this->namedArgs) as $key ) {
1551 $arguments[$key] = $this->getArgument($key);
1552 }
1553 return $arguments;
1554 }
1555
1556 /**
1557 * @param $index
1558 * @return array|bool
1559 */
1560 function getNumberedArgument( $index ) {
1561 if ( !isset( $this->numberedArgs[$index] ) ) {
1562 return false;
1563 }
1564 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1565 # No trimming for unnamed arguments
1566 $this->numberedExpansionCache[$index] = $this->parent->expand( $this->numberedArgs[$index], PPFrame::STRIP_COMMENTS );
1567 }
1568 return $this->numberedExpansionCache[$index];
1569 }
1570
1571 /**
1572 * @param $name
1573 * @return bool
1574 */
1575 function getNamedArgument( $name ) {
1576 if ( !isset( $this->namedArgs[$name] ) ) {
1577 return false;
1578 }
1579 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1580 # Trim named arguments post-expand, for backwards compatibility
1581 $this->namedExpansionCache[$name] = trim(
1582 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1583 }
1584 return $this->namedExpansionCache[$name];
1585 }
1586
1587 /**
1588 * @param $name
1589 * @return array|bool
1590 */
1591 function getArgument( $name ) {
1592 $text = $this->getNumberedArgument( $name );
1593 if ( $text === false ) {
1594 $text = $this->getNamedArgument( $name );
1595 }
1596 return $text;
1597 }
1598
1599 /**
1600 * Return true if the frame is a template frame
1601 *
1602 * @return bool
1603 */
1604 function isTemplate() {
1605 return true;
1606 }
1607 }
1608
1609 /**
1610 * Expansion frame with custom arguments
1611 * @ingroup Parser
1612 */
1613 class PPCustomFrame_HipHop extends PPFrame_HipHop {
1614 var $args;
1615
1616 function __construct( $preprocessor, $args ) {
1617 parent::__construct( $preprocessor );
1618 $this->args = $args;
1619 }
1620
1621 function __toString() {
1622 $s = 'cstmframe{';
1623 $first = true;
1624 foreach ( $this->args as $name => $value ) {
1625 if ( $first ) {
1626 $first = false;
1627 } else {
1628 $s .= ', ';
1629 }
1630 $s .= "\"$name\":\"" .
1631 str_replace( '"', '\\"', $value->__toString() ) . '"';
1632 }
1633 $s .= '}';
1634 return $s;
1635 }
1636
1637 /**
1638 * @return bool
1639 */
1640 function isEmpty() {
1641 return !count( $this->args );
1642 }
1643
1644 /**
1645 * @param $index
1646 * @return bool
1647 */
1648 function getArgument( $index ) {
1649 if ( !isset( $this->args[$index] ) ) {
1650 return false;
1651 }
1652 return $this->args[$index];
1653 }
1654 }
1655
1656 /**
1657 * @ingroup Parser
1658 */
1659 class PPNode_HipHop_Tree implements PPNode {
1660 var $name, $firstChild, $lastChild, $nextSibling;
1661
1662 function __construct( $name ) {
1663 $this->name = $name;
1664 $this->firstChild = $this->lastChild = $this->nextSibling = false;
1665 }
1666
1667 function __toString() {
1668 $inner = '';
1669 $attribs = '';
1670 for ( $node = $this->firstChild; $node; $node = $node->nextSibling ) {
1671 if ( $node instanceof PPNode_HipHop_Attr ) {
1672 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1673 } else {
1674 $inner .= $node->__toString();
1675 }
1676 }
1677 if ( $inner === '' ) {
1678 return "<{$this->name}$attribs/>";
1679 } else {
1680 return "<{$this->name}$attribs>$inner</{$this->name}>";
1681 }
1682 }
1683
1684 /**
1685 * @param $name
1686 * @param $text
1687 * @return PPNode_HipHop_Tree
1688 */
1689 static function newWithText( $name, $text ) {
1690 $obj = new self( $name );
1691 $obj->addChild( new PPNode_HipHop_Text( $text ) );
1692 return $obj;
1693 }
1694
1695 function addChild( $node ) {
1696 if ( $this->lastChild === false ) {
1697 $this->firstChild = $this->lastChild = $node;
1698 } else {
1699 $this->lastChild->nextSibling = $node;
1700 $this->lastChild = $node;
1701 }
1702 }
1703
1704 /**
1705 * @return PPNode_HipHop_Array
1706 */
1707 function getChildren() {
1708 $children = array();
1709 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1710 $children[] = $child;
1711 }
1712 return new PPNode_HipHop_Array( $children );
1713 }
1714
1715 function getFirstChild() {
1716 return $this->firstChild;
1717 }
1718
1719 function getNextSibling() {
1720 return $this->nextSibling;
1721 }
1722
1723 function getChildrenOfType( $name ) {
1724 $children = array();
1725 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1726 if ( isset( $child->name ) && $child->name === $name ) {
1727 $children[] = $name;
1728 }
1729 }
1730 return $children;
1731 }
1732
1733 /**
1734 * @return bool
1735 */
1736 function getLength() {
1737 return false;
1738 }
1739
1740 /**
1741 * @param $i
1742 * @return bool
1743 */
1744 function item( $i ) {
1745 return false;
1746 }
1747
1748 /**
1749 * @return string
1750 */
1751 function getName() {
1752 return $this->name;
1753 }
1754
1755 /**
1756 * Split a <part> node into an associative array containing:
1757 * name PPNode name
1758 * index String index
1759 * value PPNode value
1760 *
1761 * @return array
1762 */
1763 function splitArg() {
1764 $bits = array();
1765 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1766 if ( !isset( $child->name ) ) {
1767 continue;
1768 }
1769 if ( $child->name === 'name' ) {
1770 $bits['name'] = $child;
1771 if ( $child->firstChild instanceof PPNode_HipHop_Attr
1772 && $child->firstChild->name === 'index' )
1773 {
1774 $bits['index'] = $child->firstChild->value;
1775 }
1776 } elseif ( $child->name === 'value' ) {
1777 $bits['value'] = $child;
1778 }
1779 }
1780
1781 if ( !isset( $bits['name'] ) ) {
1782 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1783 }
1784 if ( !isset( $bits['index'] ) ) {
1785 $bits['index'] = '';
1786 }
1787 return $bits;
1788 }
1789
1790 /**
1791 * Split an <ext> node into an associative array containing name, attr, inner and close
1792 * All values in the resulting array are PPNodes. Inner and close are optional.
1793 *
1794 * @return array
1795 */
1796 function splitExt() {
1797 $bits = array();
1798 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1799 if ( !isset( $child->name ) ) {
1800 continue;
1801 }
1802 if ( $child->name === 'name' ) {
1803 $bits['name'] = $child;
1804 } elseif ( $child->name === 'attr' ) {
1805 $bits['attr'] = $child;
1806 } elseif ( $child->name === 'inner' ) {
1807 $bits['inner'] = $child;
1808 } elseif ( $child->name === 'close' ) {
1809 $bits['close'] = $child;
1810 }
1811 }
1812 if ( !isset( $bits['name'] ) ) {
1813 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1814 }
1815 return $bits;
1816 }
1817
1818 /**
1819 * Split an <h> node
1820 *
1821 * @return array
1822 */
1823 function splitHeading() {
1824 if ( $this->name !== 'h' ) {
1825 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1826 }
1827 $bits = array();
1828 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1829 if ( !isset( $child->name ) ) {
1830 continue;
1831 }
1832 if ( $child->name === 'i' ) {
1833 $bits['i'] = $child->value;
1834 } elseif ( $child->name === 'level' ) {
1835 $bits['level'] = $child->value;
1836 }
1837 }
1838 if ( !isset( $bits['i'] ) ) {
1839 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1840 }
1841 return $bits;
1842 }
1843
1844 /**
1845 * Split a <template> or <tplarg> node
1846 *
1847 * @return array
1848 */
1849 function splitTemplate() {
1850 $parts = array();
1851 $bits = array( 'lineStart' => '' );
1852 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1853 if ( !isset( $child->name ) ) {
1854 continue;
1855 }
1856 if ( $child->name === 'title' ) {
1857 $bits['title'] = $child;
1858 }
1859 if ( $child->name === 'part' ) {
1860 $parts[] = $child;
1861 }
1862 if ( $child->name === 'lineStart' ) {
1863 $bits['lineStart'] = '1';
1864 }
1865 }
1866 if ( !isset( $bits['title'] ) ) {
1867 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
1868 }
1869 $bits['parts'] = new PPNode_HipHop_Array( $parts );
1870 return $bits;
1871 }
1872 }
1873
1874 /**
1875 * @ingroup Parser
1876 */
1877 class PPNode_HipHop_Text implements PPNode {
1878 var $value, $nextSibling;
1879
1880 function __construct( $value ) {
1881 if ( is_object( $value ) ) {
1882 throw new MWException( __CLASS__ . ' given object instead of string' );
1883 }
1884 $this->value = $value;
1885 }
1886
1887 function __toString() {
1888 return htmlspecialchars( $this->value );
1889 }
1890
1891 function getNextSibling() {
1892 return $this->nextSibling;
1893 }
1894
1895 function getChildren() { return false; }
1896 function getFirstChild() { return false; }
1897 function getChildrenOfType( $name ) { return false; }
1898 function getLength() { return false; }
1899 function item( $i ) { return false; }
1900 function getName() { return '#text'; }
1901 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1902 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1903 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1904 }
1905
1906 /**
1907 * @ingroup Parser
1908 */
1909 class PPNode_HipHop_Array implements PPNode {
1910 var $value, $nextSibling;
1911
1912 function __construct( $value ) {
1913 $this->value = $value;
1914 }
1915
1916 function __toString() {
1917 return var_export( $this, true );
1918 }
1919
1920 function getLength() {
1921 return count( $this->value );
1922 }
1923
1924 function item( $i ) {
1925 return $this->value[$i];
1926 }
1927
1928 function getName() { return '#nodelist'; }
1929
1930 function getNextSibling() {
1931 return $this->nextSibling;
1932 }
1933
1934 function getChildren() { return false; }
1935 function getFirstChild() { return false; }
1936 function getChildrenOfType( $name ) { return false; }
1937 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1938 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1939 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1940 }
1941
1942 /**
1943 * @ingroup Parser
1944 */
1945 class PPNode_HipHop_Attr implements PPNode {
1946 var $name, $value, $nextSibling;
1947
1948 function __construct( $name, $value ) {
1949 $this->name = $name;
1950 $this->value = $value;
1951 }
1952
1953 function __toString() {
1954 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
1955 }
1956
1957 function getName() {
1958 return $this->name;
1959 }
1960
1961 function getNextSibling() {
1962 return $this->nextSibling;
1963 }
1964
1965 function getChildren() { return false; }
1966 function getFirstChild() { return false; }
1967 function getChildrenOfType( $name ) { return false; }
1968 function getLength() { return false; }
1969 function item( $i ) { return false; }
1970 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1971 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1972 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1973 }