3 * A preprocessor optimised for HipHop, using HipHop-specific syntax.
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.
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.
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
28 class Preprocessor_HipHop implements Preprocessor {
34 const CACHE_VERSION = 1;
36 function __construct( $parser ) {
37 $this->parser = $parser;
41 * @return PPFrame_HipHop
44 return new PPFrame_HipHop( $this );
49 * @return PPCustomFrame_HipHop
51 function newCustomFrame( array $args ) {
52 return new PPCustomFrame_HipHop( $this, $args );
56 * @param $values array
57 * @return PPNode_HipHop_Array
59 function newPartNodeArray( $values ) {
62 foreach ( $values as $k => $val ) {
63 $partNode = new PPNode_HipHop_Tree( 'part' );
64 $nameNode = new PPNode_HipHop_Tree( 'name' );
67 $nameNode->addChild( new PPNode_HipHop_Attr( 'index', $k ) );
68 $partNode->addChild( $nameNode );
70 $nameNode->addChild( new PPNode_HipHop_Text( $k ) );
71 $partNode->addChild( $nameNode );
72 $partNode->addChild( new PPNode_HipHop_Text( '=' ) );
75 $valueNode = new PPNode_HipHop_Tree( 'value' );
76 $valueNode->addChild( new PPNode_HipHop_Text( $val ) );
77 $partNode->addChild( $valueNode );
82 $node = new PPNode_HipHop_Array( $list );
87 * Preprocess some wikitext and return the document tree.
88 * This is the ghost of Parser::replace_variables().
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.
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.
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().
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.
106 * @return PPNode_HipHop_Tree
108 function preprocessToObj( string $text, int $flags = 0 ) {
109 wfProfileIn( __METHOD__ );
112 global $wgMemc, $wgPreprocessorCacheThreshold;
114 $cacheable = ($wgPreprocessorCacheThreshold !== false && strlen( $text ) > $wgPreprocessorCacheThreshold);
116 wfProfileIn( __METHOD__.'-cacheable' );
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 ) );
125 wfDebugLog( "Preprocessor",
126 "Loaded preprocessor hash from memcached (key $cacheKey)" );
127 wfProfileOut( __METHOD__.'-cacheable' );
128 wfProfileOut( __METHOD__ );
132 wfProfileIn( __METHOD__.'-cache-miss' );
147 'names' => array( 2 => 'LITERAL' ),
153 $forInclusion = (bool)( $flags & Parser::PTD_FOR_INCLUSION );
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;
164 } else if ( $this->parser->ot['wiki'] ) {
165 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude', 'includeonly', '/includeonly' );
166 $ignoredElements = array();
168 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
169 $ignoredElements = array( 'includeonly' );
170 $xmlishElements[] = 'includeonly';
172 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
174 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
175 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
177 $stack = new PPDStack_HipHop;
179 $searchBase = "[{<\n";
180 $revText = strrev( $text ); // For fast reverse searches
182 $i = 0; # Input pointer, starts out pointing to a pseudo-newline before the start
183 $accum = $stack->getAccum(); # Current accumulator
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
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
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 ) ) );
205 $startPos1 = intval( $variantStartPos );
206 $tagEndPos = $startPos1 + strlen( '<onlyinclude>' ); // past-the-end
207 $accum->addNodeWithText( 'ignore', strval( substr( $text, $i, $tagEndPos - $i ) ) );
209 $findOnlyinclude = false;
212 if ( $fakeLineStart ) {
213 $found = 'line-start';
216 # Find next opening brace, closing brace or pipe
217 $search = $searchBase;
218 if ( $stack->top === false ) {
219 $currentClosing = '';
221 $currentClosing = strval( $stack->getTop()->close );
222 $search .= $currentClosing;
224 if ( $stackFlags['findPipe'] ) {
227 if ( $stackFlags['findEquals'] ) {
228 // First equals will be for the template
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;
238 if ( $i >= strlen( $text ) ) {
239 if ( $currentClosing === "\n" ) {
240 // Do a past-the-end run to finish off the heading
248 $curChar = $text[$i];
249 if ( $curChar === '|' ) {
251 } elseif ( $curChar === '=' ) {
253 } elseif ( $curChar === '<' ) {
255 } elseif ( $curChar === "\n" ) {
256 if ( $stackFlags['inHeading'] ) {
259 $found = 'line-start';
261 } elseif ( $curChar === $currentClosing ) {
263 } elseif ( isset( $rules[$curChar] ) ) {
265 $rule = $rules[$curChar];
267 # Some versions of PHP have a strcspn which stops on null characters
268 # Ignore and continue
275 if ( $found === 'angle' ) {
277 // Handle </onlyinclude>
278 if ( $enableOnlyinclude
279 && substr( $text, $i, strlen( '</onlyinclude>' ) ) === '</onlyinclude>' )
281 $findOnlyinclude = true;
285 // Determine element name
286 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
287 // Element name missing or not listed
288 $accum->addLiteral( '<' );
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.
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 );
306 $endPos = intval( $variantEndPos );
307 // Search backwards for leading whitespace
309 $wsStart = $i - intval( strspn( $revText, ' ', strlen( $text ) - $i ) );
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" )
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;
329 && $accum->lastNode instanceof PPNode_HipHop_Text
330 && substr( $accum->lastNode->value, -$wsLength ) === str_repeat( ' ', $wsLength ) )
332 $accum->lastNode->value = strval( substr( $accum->lastNode->value, 0, -$wsLength ) );
334 // Do a line-start run next time to look for headings after the comment
335 $fakeLineStart = true;
337 // No line to eat, just take the comment itself
343 $part = $stack->getTop()->getCurrentPart();
344 if ( ! (isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 )) {
345 $part->visualEnd = $wsStart;
347 // Else comments abutting, no change in visual end
348 $part->commentEnd = $endPos;
351 $inner = strval( substr( $text, $startPos2, $endPos - $startPos2 + 1 ) );
352 $accum->addNodeWithText( 'comment', $inner );
356 $name = strval( $matches[1] );
357 $lowerName = strtolower( $name );
358 $attrStart = $i + strlen( $name ) + 1;
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
366 $accum->addLiteral( '<' );
370 $tagEndPos = intval( $variantTagEndPos );
372 // Handle ignored tags
373 if ( in_array( $lowerName, $ignoredTags ) ) {
374 $accum->addNodeWithText( 'ignore', strval( substr( $text, $i, $tagEndPos - $i + 1 ) ) );
381 if ( $text[$tagEndPos-1] === '/' ) {
383 $attrEnd = $tagEndPos - 1;
389 $attrEnd = $tagEndPos;
392 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
393 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) )
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] );
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 );
406 // <includeonly> and <noinclude> just become <ignore> tags
407 if ( in_array( $lowerName, $ignoredElements ) ) {
408 $accum->addNodeWithText( 'ignore', strval( substr( $text, $tagStartPos, $i - $tagStartPos ) ) );
412 if ( $attrEnd <= $attrStart ) {
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 ) );
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 ) );
424 $extNode->addChild( PPNode_HipHop_Tree::newWithText( 'inner', $inner ) );
427 $extNode->addChild( PPNode_HipHop_Tree::newWithText( 'close', $close ) );
429 $accum->addNode( $extNode );
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;
438 $accum->addLiteral( $curChar );
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 ) {
451 'parts' => array( new PPDPart_HipHop( str_repeat( '=', $count ) ) ),
454 $stack->push( $partData );
455 $accum = $stack->getAccum();
456 $stackFlags = $stack->getFlags();
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 ) );
475 $count = intval( $piece->count );
476 $equalsLength = intval( strspn( $revText, '=', strlen( $text ) - $searchStart ) );
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;
488 $count = intval( ( $count - 1 ) / 2 );
494 if ( $count > $equalsLength ) {
495 $count = $equalsLength;
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;
507 // Single equals sign on its own line, count=0
508 // Output $resultAccum
511 // No match, no <h>, just pass down the inner text
512 // Output $resultAccum
516 $accum = $stack->getAccum();
517 $stackFlags = $stack->getFlags();
519 // Append the result to the enclosing accumulator
521 $accum->addNode( $tree );
523 $accum->addAccum( $resultAccum );
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
530 } elseif ( $found === 'open' ) {
531 # count opening brace characters
532 $count = intval( strspn( $text, $curChar, $i ) );
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
539 'close' => $rule['end'],
541 'lineStart' => ($i == 0 || $text[$i-1] === "\n"),
544 $stack->push( $partData );
545 $accum = $stack->getAccum();
546 $stackFlags = $stack->getFlags();
548 # Add literal brace(s)
549 $accum->addLiteral( str_repeat( $curChar, $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 ) );
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
564 $matchingCount = intval( $rule['max'] );
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'] ) ) {
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 ) );
582 $name = strval( $rule['names'][$matchingCount] );
584 if ( $name === 'LITERAL' ) {
585 // No element, just literal text
586 $resultAccum = $piece->breakSyntax( $matchingCount );
587 $resultAccum->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
590 # Note: $parts is already XML, does not need to be encoded further
592 $parts = $piece->parts;
593 $titleAccum = PPDAccum_HipHop::cast( $parts[0]->out );
596 $tree = new PPNode_HipHop_Tree( $name );
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 ) );
603 $titleNode = new PPNode_HipHop_Tree( 'title' );
604 $titleNode->firstChild = $titleAccum->firstNode;
605 $titleNode->lastChild = $titleAccum->lastNode;
606 $tree->addChild( $titleNode );
608 foreach ( $parts as $variantPart ) {
609 $part = PPDPart_HipHop::cast( $variantPart );
610 if ( isset( $part->eqpos ) ) {
613 for ( $node = $part->out->firstNode; $node; $node = $node->nextSibling ) {
614 if ( $node === $part->eqpos ) {
620 throw new MWException( __METHOD__. ': eqpos not found' );
622 if ( $node->name !== 'equals' ) {
623 throw new MWException( __METHOD__ .': eqpos is not equals' );
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;
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;
641 $partNode = new PPNode_HipHop_Tree( 'part' );
642 $partNode->addChild( $nameNode );
643 $partNode->addChild( $equalsNode->firstChild );
644 $partNode->addChild( $valueNode );
645 $tree->addChild( $partNode );
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 );
660 # Advance input pointer
661 $i += $matchingCount;
665 $accum = $stack->getAccum();
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'];
674 $enclosingAccum = $accum;
675 while ( $piece->count ) {
676 if ( array_key_exists( $piece->count, $names ) ) {
677 $stack->push( $piece );
678 $accum = $stack->getAccum();
684 $enclosingAccum->addLiteral( str_repeat( $piece->open, $skippedBraces ) );
687 $stackFlags = $stack->getFlags();
689 # Add XML element to the enclosing accumulator
691 $accum->addNode( $tree );
693 $accum->addAccum( $resultAccum );
695 } elseif ( $found === 'pipe' ) {
696 $stackFlags['findEquals'] = true; // shortcut for getFlags()
698 $accum = $stack->getAccum();
700 } elseif ( $found === 'equals' ) {
701 $stackFlags['findEquals'] = false; // shortcut for getFlags()
702 $accum->addNodeWithText( 'equals', '=' );
703 $stack->getCurrentPart()->eqpos = $accum->lastNode;
708 # Output any remaining unclosed brackets
709 foreach ( $stack->stack as $variantPiece ) {
710 $piece = PPDStackElement_HipHop::cast( $variantPiece );
711 $stack->rootAccum->addAccum( $piece->breakSyntax() );
714 # Enable top-level headings
715 for ( $node = $stack->rootAccum->firstNode; $node; $node = $node->nextSibling ) {
716 if ( isset( $node->name ) && $node->name === 'possible-h' ) {
721 $rootNode = new PPNode_HipHop_Tree( 'root' );
722 $rootNode->firstChild = $stack->rootAccum->firstNode;
723 $rootNode->lastChild = $stack->rootAccum->lastNode;
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)" );
734 wfProfileOut( __METHOD__ );
742 * Stack class to help Preprocessor::preprocessToObj()
745 class PPDStack_HipHop {
746 var $stack, $rootAccum;
754 static $false = false;
756 function __construct() {
757 $this->stack = array();
759 $this->rootAccum = new PPDAccum_HipHop;
760 $this->accum = $this->rootAccum;
767 return count( $this->stack );
770 function getAccum() {
771 return PPDAccum_HipHop::cast( $this->accum );
774 function getCurrentPart() {
775 return $this->getTop()->getCurrentPart();
779 return PPDStackElement_HipHop::cast( $this->top );
782 function push( $data ) {
783 if ( $data instanceof PPDStackElement_HipHop ) {
784 $this->stack[] = $data;
786 $this->stack[] = new PPDStackElement_HipHop( $data );
788 $this->top = $this->stack[ count( $this->stack ) - 1 ];
789 $this->accum = $this->top->getAccum();
793 if ( !count( $this->stack ) ) {
794 throw new MWException( __METHOD__.': no elements remaining' );
796 $temp = array_pop( $this->stack );
798 if ( count( $this->stack ) ) {
799 $this->top = $this->stack[ count( $this->stack ) - 1 ];
800 $this->accum = $this->top->getAccum();
802 $this->top = self::$false;
803 $this->accum = $this->rootAccum;
808 function addPart( $s = '' ) {
809 $this->top->addPart( $s );
810 $this->accum = $this->top->getAccum();
816 function getFlags() {
817 if ( !count( $this->stack ) ) {
819 'findEquals' => false,
821 'inHeading' => false,
824 return $this->top->getFlags();
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.
839 static function cast( PPDStackElement_HipHop $obj ) {
843 function __construct( $data = array() ) {
844 $this->parts = array( new PPDPart_HipHop );
846 foreach ( $data as $name => $value ) {
847 $this->$name = $value;
851 function getAccum() {
852 return PPDAccum_HipHop::cast( $this->parts[count($this->parts) - 1]->out );
855 function addPart( $s = '' ) {
856 $this->parts[] = new PPDPart_HipHop( $s );
859 function getCurrentPart() {
860 return PPDPart_HipHop::cast( $this->parts[count($this->parts) - 1] );
866 function getFlags() {
867 $partCount = count( $this->parts );
868 $findPipe = $this->open !== "\n" && $this->open !== '[';
870 'findPipe' => $findPipe,
871 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts[$partCount - 1]->eqpos ),
872 'inHeading' => $this->open === "\n",
877 * Get the accumulator that would result if the close is not found.
879 * @return PPDAccum_HipHop
881 function breakSyntax( $openingCount = false ) {
882 if ( $this->open === "\n" ) {
883 $accum = PPDAccum_HipHop::cast( $this->parts[0]->out );
885 if ( $openingCount === false ) {
886 $openingCount = $this->count;
888 $accum = new PPDAccum_HipHop;
889 $accum->addLiteral( str_repeat( $this->open, $openingCount ) );
891 foreach ( $this->parts as $part ) {
895 $accum->addLiteral( '|' );
897 $accum->addAccum( $part->out );
907 class PPDPart_HipHop {
908 var $out; // Output accumulator object
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
915 function __construct( $out = '' ) {
916 $this->out = new PPDAccum_HipHop;
918 $this->out->addLiteral( $out );
922 static function cast( PPDPart_HipHop $obj ) {
930 class PPDAccum_HipHop {
931 var $firstNode, $lastNode;
933 function __construct() {
934 $this->firstNode = $this->lastNode = false;
937 static function cast( PPDAccum_HipHop $obj ) {
942 * Append a string literal
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;
950 $this->lastNode->nextSibling = new PPNode_HipHop_Text( $s );
951 $this->lastNode = $this->lastNode->nextSibling;
958 function addNode( PPNode $node ) {
959 if ( $this->lastNode === false ) {
960 $this->firstNode = $this->lastNode = $node;
962 $this->lastNode->nextSibling = $node;
963 $this->lastNode = $node;
968 * Append a tree node with text contents
970 function addNodeWithText( string $name, string $value ) {
971 $node = PPNode_HipHop_Tree::newWithText( $name, $value );
972 $this->addNode( $node );
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.
980 function addAccum( PPDAccum_HipHop $accum ) {
981 if ( $accum->lastNode === false ) {
983 } elseif ( $this->lastNode === false ) {
984 $this->firstNode = $accum->firstNode;
985 $this->lastNode = $accum->lastNode;
987 $this->lastNode->nextSibling = $accum->firstNode;
988 $this->lastNode = $accum->lastNode;
994 * An expansion frame, used as a context to expand the result of preprocessToObj()
997 class PPFrame_HipHop implements PPFrame {
1016 * Hashtable listing templates which are disallowed for expansion in this frame,
1017 * having been encountered previously in parent frames.
1022 * Recursion depth of this frame, top = 0
1023 * Note that this is NOT the same as expansion depth in expand()
1028 * Construct a new preprocessor frame.
1029 * @param $preprocessor Preprocessor: the parent preprocessor
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();
1041 * Create a new child frame
1042 * $args is optionally a multi-root PPNode or array containing the template arguments
1044 * @param $args PPNode_HipHop_Array|array
1045 * @param $title Title|false
1047 * @return PPTemplateFrame_HipHop
1049 function newChild( $args = false, $title = false ) {
1050 $namedArgs = array();
1051 $numberedArgs = array();
1052 if ( $title === false ) {
1053 $title = $this->title;
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' );
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']] );
1069 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
1070 $namedArgs[$name] = $bits['value'];
1071 unset( $numberedArgs[$name] );
1075 return new PPTemplateFrame_HipHop( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
1079 * @throws MWException
1084 function expand( $root, $flags = 0 ) {
1085 static $expansionDepth = 0;
1086 if ( is_string( $root ) ) {
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()
1095 return '<span class="error">Node-count limit exceeded</span>';
1097 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
1098 $this->parser->limitationWarn( 'expansion-depth-exceeded',
1100 $this->parser->mOptions->getMaxPPExpandDepth()
1102 return '<span class="error">Expansion depth limit exceeded</span>';
1106 $outStack = array( '', '' );
1107 $iteratorStack = array( false, $root );
1108 $indexStack = array( 0, 0 );
1110 while ( count( $iteratorStack ) > 1 ) {
1111 $level = count( $outStack ) - 1;
1112 $iteratorNode =& $iteratorStack[ $level ];
1113 $out =& $outStack[$level];
1114 $index =& $indexStack[$level];
1116 if ( is_array( $iteratorNode ) ) {
1117 if ( $index >= count( $iteratorNode ) ) {
1118 // All done with this iterator
1119 $iteratorStack[$level] = false;
1120 $contextNode = false;
1122 $contextNode = $iteratorNode[$index];
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;
1131 $contextNode = $iteratorNode->item( $index );
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;
1141 $newIterator = false;
1143 if ( $contextNode === false ) {
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 ) {
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'] );
1160 $ret = $this->parser->braceSubstitution( $bits, $this );
1161 if ( isset( $ret['object'] ) ) {
1162 $newIterator = $ret['object'];
1164 $out .= $ret['text'];
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'] );
1173 $ret = $this->parser->argSubstitution( $bits, $this );
1174 if ( isset( $ret['object'] ) ) {
1175 $newIterator = $ret['object'];
1177 $out .= $ret['text'];
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 ) )
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 );
1194 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1196 $out .= $contextNode->firstChild->value;
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;
1208 } elseif ( $contextNode->name === 'ext' ) {
1210 $bits = $contextNode->splitExt() + array( 'attr' => null, 'inner' => null, 'close' => null );
1211 $out .= $this->parser->extensionSubstitution( $bits, $this );
1212 } elseif ( $contextNode->name === 'h' ) {
1214 if ( $this->parser->ot['html'] ) {
1215 # Expand immediately and insert heading index marker
1217 for ( $node = $contextNode->firstChild; $node; $node = $node->nextSibling ) {
1218 $s .= $this->expand( $node, $flags );
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, '' );
1230 # Expand in virtual stack
1231 $newIterator = $contextNode->getChildren();
1234 # Generic recursive expansion
1235 $newIterator = $contextNode->getChildren();
1238 throw new MWException( __METHOD__.': Invalid parameter type' );
1241 if ( $newIterator !== false ) {
1243 $iteratorStack[] = $newIterator;
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 );
1258 return $outStack[0];
1266 function implodeWithFlags( $sep, $flags /*, ... */ ) {
1267 $args = array_slice( func_get_args(), 2 );
1271 foreach ( $args as $root ) {
1272 if ( $root instanceof PPNode_HipHop_Array ) {
1273 $root = $root->value;
1275 if ( !is_array( $root ) ) {
1276 $root = array( $root );
1278 foreach ( $root as $node ) {
1284 $s .= $this->expand( $node, $flags );
1291 * Implode with no flags specified
1292 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1295 function implode( $sep /*, ... */ ) {
1296 $args = array_slice( func_get_args(), 1 );
1300 foreach ( $args as $root ) {
1301 if ( $root instanceof PPNode_HipHop_Array ) {
1302 $root = $root->value;
1304 if ( !is_array( $root ) ) {
1305 $root = array( $root );
1307 foreach ( $root as $node ) {
1313 $s .= $this->expand( $node );
1320 * Makes an object that, when expand()ed, will be the same as one obtained
1323 * @return PPNode_HipHop_Array
1325 function virtualImplode( $sep /*, ... */ ) {
1326 $args = array_slice( func_get_args(), 1 );
1330 foreach ( $args as $root ) {
1331 if ( $root instanceof PPNode_HipHop_Array ) {
1332 $root = $root->value;
1334 if ( !is_array( $root ) ) {
1335 $root = array( $root );
1337 foreach ( $root as $node ) {
1346 return new PPNode_HipHop_Array( $out );
1350 * Virtual implode with brackets
1352 * @return PPNode_HipHop_Array
1354 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1355 $args = array_slice( func_get_args(), 3 );
1356 $out = array( $start );
1359 foreach ( $args as $root ) {
1360 if ( $root instanceof PPNode_HipHop_Array ) {
1361 $root = $root->value;
1363 if ( !is_array( $root ) ) {
1364 $root = array( $root );
1366 foreach ( $root as $node ) {
1376 return new PPNode_HipHop_Array( $out );
1379 function __toString() {
1384 * @param $level bool
1385 * @return array|bool|String
1387 function getPDBK( $level = false ) {
1388 if ( $level === false ) {
1389 return $this->title->getPrefixedDBkey();
1391 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1398 function getArguments() {
1405 function getNumberedArguments() {
1412 function getNamedArguments() {
1417 * Returns true if there are no arguments in this frame
1421 function isEmpty() {
1429 function getArgument( $name ) {
1434 * Returns true if the infinite loop check is OK, false if a loop is detected
1436 * @param $title Title
1440 function loopCheck( $title ) {
1441 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1445 * Return true if the frame is a template frame
1449 function isTemplate() {
1454 * Get a title of frame
1458 function getTitle() {
1459 return $this->title;
1464 * Expansion frame with template arguments
1467 class PPTemplateFrame_HipHop extends PPFrame_HipHop {
1468 var $numberedArgs, $namedArgs, $parent;
1469 var $numberedExpansionCache, $namedExpansionCache;
1472 * @param $preprocessor
1474 * @param $numberedArgs array
1475 * @param $namedArgs array
1476 * @param $title Title
1478 function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
1479 parent::__construct( $preprocessor );
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;
1492 $this->depth = $parent->depth + 1;
1493 $this->numberedExpansionCache = $this->namedExpansionCache = array();
1496 function __toString() {
1499 $args = $this->numberedArgs + $this->namedArgs;
1500 foreach ( $args as $name => $value ) {
1506 $s .= "\"$name\":\"" .
1507 str_replace( '"', '\\"', $value->__toString() ) . '"';
1513 * Returns true if there are no arguments in this frame
1517 function isEmpty() {
1518 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
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);
1537 function getNumberedArguments() {
1538 $arguments = array();
1539 foreach ( array_keys($this->numberedArgs) as $key ) {
1540 $arguments[$key] = $this->getArgument($key);
1548 function getNamedArguments() {
1549 $arguments = array();
1550 foreach ( array_keys($this->namedArgs) as $key ) {
1551 $arguments[$key] = $this->getArgument($key);
1558 * @return array|bool
1560 function getNumberedArgument( $index ) {
1561 if ( !isset( $this->numberedArgs[$index] ) ) {
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 );
1568 return $this->numberedExpansionCache[$index];
1575 function getNamedArgument( $name ) {
1576 if ( !isset( $this->namedArgs[$name] ) ) {
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 ) );
1584 return $this->namedExpansionCache[$name];
1589 * @return array|bool
1591 function getArgument( $name ) {
1592 $text = $this->getNumberedArgument( $name );
1593 if ( $text === false ) {
1594 $text = $this->getNamedArgument( $name );
1600 * Return true if the frame is a template frame
1604 function isTemplate() {
1610 * Expansion frame with custom arguments
1613 class PPCustomFrame_HipHop extends PPFrame_HipHop {
1616 function __construct( $preprocessor, $args ) {
1617 parent::__construct( $preprocessor );
1618 $this->args = $args;
1621 function __toString() {
1624 foreach ( $this->args as $name => $value ) {
1630 $s .= "\"$name\":\"" .
1631 str_replace( '"', '\\"', $value->__toString() ) . '"';
1640 function isEmpty() {
1641 return !count( $this->args );
1648 function getArgument( $index ) {
1649 if ( !isset( $this->args[$index] ) ) {
1652 return $this->args[$index];
1659 class PPNode_HipHop_Tree implements PPNode {
1660 var $name, $firstChild, $lastChild, $nextSibling;
1662 function __construct( $name ) {
1663 $this->name = $name;
1664 $this->firstChild = $this->lastChild = $this->nextSibling = false;
1667 function __toString() {
1670 for ( $node = $this->firstChild; $node; $node = $node->nextSibling ) {
1671 if ( $node instanceof PPNode_HipHop_Attr ) {
1672 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1674 $inner .= $node->__toString();
1677 if ( $inner === '' ) {
1678 return "<{$this->name}$attribs/>";
1680 return "<{$this->name}$attribs>$inner</{$this->name}>";
1687 * @return PPNode_HipHop_Tree
1689 static function newWithText( $name, $text ) {
1690 $obj = new self( $name );
1691 $obj->addChild( new PPNode_HipHop_Text( $text ) );
1695 function addChild( $node ) {
1696 if ( $this->lastChild === false ) {
1697 $this->firstChild = $this->lastChild = $node;
1699 $this->lastChild->nextSibling = $node;
1700 $this->lastChild = $node;
1705 * @return PPNode_HipHop_Array
1707 function getChildren() {
1708 $children = array();
1709 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1710 $children[] = $child;
1712 return new PPNode_HipHop_Array( $children );
1715 function getFirstChild() {
1716 return $this->firstChild;
1719 function getNextSibling() {
1720 return $this->nextSibling;
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;
1736 function getLength() {
1744 function item( $i ) {
1751 function getName() {
1756 * Split a <part> node into an associative array containing:
1758 * index String index
1759 * value PPNode value
1763 function splitArg() {
1765 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1766 if ( !isset( $child->name ) ) {
1769 if ( $child->name === 'name' ) {
1770 $bits['name'] = $child;
1771 if ( $child->firstChild instanceof PPNode_HipHop_Attr
1772 && $child->firstChild->name === 'index' )
1774 $bits['index'] = $child->firstChild->value;
1776 } elseif ( $child->name === 'value' ) {
1777 $bits['value'] = $child;
1781 if ( !isset( $bits['name'] ) ) {
1782 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1784 if ( !isset( $bits['index'] ) ) {
1785 $bits['index'] = '';
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.
1796 function splitExt() {
1798 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1799 if ( !isset( $child->name ) ) {
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;
1812 if ( !isset( $bits['name'] ) ) {
1813 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1823 function splitHeading() {
1824 if ( $this->name !== 'h' ) {
1825 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1828 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1829 if ( !isset( $child->name ) ) {
1832 if ( $child->name === 'i' ) {
1833 $bits['i'] = $child->value;
1834 } elseif ( $child->name === 'level' ) {
1835 $bits['level'] = $child->value;
1838 if ( !isset( $bits['i'] ) ) {
1839 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1845 * Split a <template> or <tplarg> node
1849 function splitTemplate() {
1851 $bits = array( 'lineStart' => '' );
1852 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1853 if ( !isset( $child->name ) ) {
1856 if ( $child->name === 'title' ) {
1857 $bits['title'] = $child;
1859 if ( $child->name === 'part' ) {
1862 if ( $child->name === 'lineStart' ) {
1863 $bits['lineStart'] = '1';
1866 if ( !isset( $bits['title'] ) ) {
1867 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
1869 $bits['parts'] = new PPNode_HipHop_Array( $parts );
1877 class PPNode_HipHop_Text implements PPNode {
1878 var $value, $nextSibling;
1880 function __construct( $value ) {
1881 if ( is_object( $value ) ) {
1882 throw new MWException( __CLASS__ . ' given object instead of string' );
1884 $this->value = $value;
1887 function __toString() {
1888 return htmlspecialchars( $this->value );
1891 function getNextSibling() {
1892 return $this->nextSibling;
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' ); }
1909 class PPNode_HipHop_Array implements PPNode {
1910 var $value, $nextSibling;
1912 function __construct( $value ) {
1913 $this->value = $value;
1916 function __toString() {
1917 return var_export( $this, true );
1920 function getLength() {
1921 return count( $this->value );
1924 function item( $i ) {
1925 return $this->value[$i];
1928 function getName() { return '#nodelist'; }
1930 function getNextSibling() {
1931 return $this->nextSibling;
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' ); }
1945 class PPNode_HipHop_Attr implements PPNode {
1946 var $name, $value, $nextSibling;
1948 function __construct( $name, $value ) {
1949 $this->name = $name;
1950 $this->value = $value;
1953 function __toString() {
1954 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
1957 function getName() {
1961 function getNextSibling() {
1962 return $this->nextSibling;
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' ); }