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>';
1105 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
1106 $this->parser->mHighestExpansionDepth = $expansionDepth;
1109 $outStack = array( '', '' );
1110 $iteratorStack = array( false, $root );
1111 $indexStack = array( 0, 0 );
1113 while ( count( $iteratorStack ) > 1 ) {
1114 $level = count( $outStack ) - 1;
1115 $iteratorNode =& $iteratorStack[ $level ];
1116 $out =& $outStack[$level];
1117 $index =& $indexStack[$level];
1119 if ( is_array( $iteratorNode ) ) {
1120 if ( $index >= count( $iteratorNode ) ) {
1121 // All done with this iterator
1122 $iteratorStack[$level] = false;
1123 $contextNode = false;
1125 $contextNode = $iteratorNode[$index];
1128 } elseif ( $iteratorNode instanceof PPNode_HipHop_Array ) {
1129 if ( $index >= $iteratorNode->getLength() ) {
1130 // All done with this iterator
1131 $iteratorStack[$level] = false;
1132 $contextNode = false;
1134 $contextNode = $iteratorNode->item( $index );
1138 // Copy to $contextNode and then delete from iterator stack,
1139 // because this is not an iterator but we do have to execute it once
1140 $contextNode = $iteratorStack[$level];
1141 $iteratorStack[$level] = false;
1144 $newIterator = false;
1146 if ( $contextNode === false ) {
1148 } elseif ( is_string( $contextNode ) ) {
1149 $out .= $contextNode;
1150 } elseif ( is_array( $contextNode ) || $contextNode instanceof PPNode_HipHop_Array ) {
1151 $newIterator = $contextNode;
1152 } elseif ( $contextNode instanceof PPNode_HipHop_Attr ) {
1154 } elseif ( $contextNode instanceof PPNode_HipHop_Text ) {
1155 $out .= $contextNode->value;
1156 } elseif ( $contextNode instanceof PPNode_HipHop_Tree ) {
1157 if ( $contextNode->name === 'template' ) {
1158 # Double-brace expansion
1159 $bits = $contextNode->splitTemplate();
1160 if ( $flags & PPFrame::NO_TEMPLATES ) {
1161 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $bits['title'], $bits['parts'] );
1163 $ret = $this->parser->braceSubstitution( $bits, $this );
1164 if ( isset( $ret['object'] ) ) {
1165 $newIterator = $ret['object'];
1167 $out .= $ret['text'];
1170 } elseif ( $contextNode->name === 'tplarg' ) {
1171 # Triple-brace expansion
1172 $bits = $contextNode->splitTemplate();
1173 if ( $flags & PPFrame::NO_ARGS ) {
1174 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $bits['title'], $bits['parts'] );
1176 $ret = $this->parser->argSubstitution( $bits, $this );
1177 if ( isset( $ret['object'] ) ) {
1178 $newIterator = $ret['object'];
1180 $out .= $ret['text'];
1183 } elseif ( $contextNode->name === 'comment' ) {
1184 # HTML-style comment
1185 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1186 if ( $this->parser->ot['html']
1187 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
1188 || ( $flags & PPFrame::STRIP_COMMENTS ) )
1192 # Add a strip marker in PST mode so that pstPass2() can run some old-fashioned regexes on the result
1193 # Not in RECOVER_COMMENTS mode (extractSections) though
1194 elseif ( $this->parser->ot['wiki'] && ! ( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1195 $out .= $this->parser->insertStripItem( $contextNode->firstChild->value );
1197 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1199 $out .= $contextNode->firstChild->value;
1201 } elseif ( $contextNode->name === 'ignore' ) {
1202 # Output suppression used by <includeonly> etc.
1203 # OT_WIKI will only respect <ignore> in substed templates.
1204 # The other output types respect it unless NO_IGNORE is set.
1205 # extractSections() sets NO_IGNORE and so never respects it.
1206 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] ) || ( $flags & PPFrame::NO_IGNORE ) ) {
1207 $out .= $contextNode->firstChild->value;
1211 } elseif ( $contextNode->name === 'ext' ) {
1213 $bits = $contextNode->splitExt() + array( 'attr' => null, 'inner' => null, 'close' => null );
1214 $out .= $this->parser->extensionSubstitution( $bits, $this );
1215 } elseif ( $contextNode->name === 'h' ) {
1217 if ( $this->parser->ot['html'] ) {
1218 # Expand immediately and insert heading index marker
1220 for ( $node = $contextNode->firstChild; $node; $node = $node->nextSibling ) {
1221 $s .= $this->expand( $node, $flags );
1224 $bits = $contextNode->splitHeading();
1225 $titleText = $this->title->getPrefixedDBkey();
1226 $this->parser->mHeadings[] = array( $titleText, $bits['i'] );
1227 $serial = count( $this->parser->mHeadings ) - 1;
1228 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser::MARKER_SUFFIX;
1229 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1230 $this->parser->mStripState->addGeneral( $marker, '' );
1233 # Expand in virtual stack
1234 $newIterator = $contextNode->getChildren();
1237 # Generic recursive expansion
1238 $newIterator = $contextNode->getChildren();
1241 throw new MWException( __METHOD__.': Invalid parameter type' );
1244 if ( $newIterator !== false ) {
1246 $iteratorStack[] = $newIterator;
1248 } elseif ( $iteratorStack[$level] === false ) {
1249 // Return accumulated value to parent
1250 // With tail recursion
1251 while ( $iteratorStack[$level] === false && $level > 0 ) {
1252 $outStack[$level - 1] .= $out;
1253 array_pop( $outStack );
1254 array_pop( $iteratorStack );
1255 array_pop( $indexStack );
1261 return $outStack[0];
1269 function implodeWithFlags( $sep, $flags /*, ... */ ) {
1270 $args = array_slice( func_get_args(), 2 );
1274 foreach ( $args as $root ) {
1275 if ( $root instanceof PPNode_HipHop_Array ) {
1276 $root = $root->value;
1278 if ( !is_array( $root ) ) {
1279 $root = array( $root );
1281 foreach ( $root as $node ) {
1287 $s .= $this->expand( $node, $flags );
1294 * Implode with no flags specified
1295 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1298 function implode( $sep /*, ... */ ) {
1299 $args = array_slice( func_get_args(), 1 );
1303 foreach ( $args as $root ) {
1304 if ( $root instanceof PPNode_HipHop_Array ) {
1305 $root = $root->value;
1307 if ( !is_array( $root ) ) {
1308 $root = array( $root );
1310 foreach ( $root as $node ) {
1316 $s .= $this->expand( $node );
1323 * Makes an object that, when expand()ed, will be the same as one obtained
1326 * @return PPNode_HipHop_Array
1328 function virtualImplode( $sep /*, ... */ ) {
1329 $args = array_slice( func_get_args(), 1 );
1333 foreach ( $args as $root ) {
1334 if ( $root instanceof PPNode_HipHop_Array ) {
1335 $root = $root->value;
1337 if ( !is_array( $root ) ) {
1338 $root = array( $root );
1340 foreach ( $root as $node ) {
1349 return new PPNode_HipHop_Array( $out );
1353 * Virtual implode with brackets
1355 * @return PPNode_HipHop_Array
1357 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1358 $args = array_slice( func_get_args(), 3 );
1359 $out = array( $start );
1362 foreach ( $args as $root ) {
1363 if ( $root instanceof PPNode_HipHop_Array ) {
1364 $root = $root->value;
1366 if ( !is_array( $root ) ) {
1367 $root = array( $root );
1369 foreach ( $root as $node ) {
1379 return new PPNode_HipHop_Array( $out );
1382 function __toString() {
1387 * @param $level bool
1388 * @return array|bool|String
1390 function getPDBK( $level = false ) {
1391 if ( $level === false ) {
1392 return $this->title->getPrefixedDBkey();
1394 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1401 function getArguments() {
1408 function getNumberedArguments() {
1415 function getNamedArguments() {
1420 * Returns true if there are no arguments in this frame
1424 function isEmpty() {
1432 function getArgument( $name ) {
1437 * Returns true if the infinite loop check is OK, false if a loop is detected
1439 * @param $title Title
1443 function loopCheck( $title ) {
1444 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1448 * Return true if the frame is a template frame
1452 function isTemplate() {
1457 * Get a title of frame
1461 function getTitle() {
1462 return $this->title;
1467 * Expansion frame with template arguments
1470 class PPTemplateFrame_HipHop extends PPFrame_HipHop {
1471 var $numberedArgs, $namedArgs, $parent;
1472 var $numberedExpansionCache, $namedExpansionCache;
1475 * @param $preprocessor
1477 * @param $numberedArgs array
1478 * @param $namedArgs array
1479 * @param $title Title
1481 function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
1482 parent::__construct( $preprocessor );
1484 $this->parent = $parent;
1485 $this->numberedArgs = $numberedArgs;
1486 $this->namedArgs = $namedArgs;
1487 $this->title = $title;
1488 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1489 $this->titleCache = $parent->titleCache;
1490 $this->titleCache[] = $pdbk;
1491 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1492 if ( $pdbk !== false ) {
1493 $this->loopCheckHash[$pdbk] = true;
1495 $this->depth = $parent->depth + 1;
1496 $this->numberedExpansionCache = $this->namedExpansionCache = array();
1499 function __toString() {
1502 $args = $this->numberedArgs + $this->namedArgs;
1503 foreach ( $args as $name => $value ) {
1509 $s .= "\"$name\":\"" .
1510 str_replace( '"', '\\"', $value->__toString() ) . '"';
1516 * Returns true if there are no arguments in this frame
1520 function isEmpty() {
1521 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1527 function getArguments() {
1528 $arguments = array();
1529 foreach ( array_merge(
1530 array_keys($this->numberedArgs),
1531 array_keys($this->namedArgs)) as $key ) {
1532 $arguments[$key] = $this->getArgument($key);
1540 function getNumberedArguments() {
1541 $arguments = array();
1542 foreach ( array_keys($this->numberedArgs) as $key ) {
1543 $arguments[$key] = $this->getArgument($key);
1551 function getNamedArguments() {
1552 $arguments = array();
1553 foreach ( array_keys($this->namedArgs) as $key ) {
1554 $arguments[$key] = $this->getArgument($key);
1561 * @return array|bool
1563 function getNumberedArgument( $index ) {
1564 if ( !isset( $this->numberedArgs[$index] ) ) {
1567 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1568 # No trimming for unnamed arguments
1569 $this->numberedExpansionCache[$index] = $this->parent->expand( $this->numberedArgs[$index], PPFrame::STRIP_COMMENTS );
1571 return $this->numberedExpansionCache[$index];
1578 function getNamedArgument( $name ) {
1579 if ( !isset( $this->namedArgs[$name] ) ) {
1582 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1583 # Trim named arguments post-expand, for backwards compatibility
1584 $this->namedExpansionCache[$name] = trim(
1585 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1587 return $this->namedExpansionCache[$name];
1592 * @return array|bool
1594 function getArgument( $name ) {
1595 $text = $this->getNumberedArgument( $name );
1596 if ( $text === false ) {
1597 $text = $this->getNamedArgument( $name );
1603 * Return true if the frame is a template frame
1607 function isTemplate() {
1613 * Expansion frame with custom arguments
1616 class PPCustomFrame_HipHop extends PPFrame_HipHop {
1619 function __construct( $preprocessor, $args ) {
1620 parent::__construct( $preprocessor );
1621 $this->args = $args;
1624 function __toString() {
1627 foreach ( $this->args as $name => $value ) {
1633 $s .= "\"$name\":\"" .
1634 str_replace( '"', '\\"', $value->__toString() ) . '"';
1643 function isEmpty() {
1644 return !count( $this->args );
1651 function getArgument( $index ) {
1652 if ( !isset( $this->args[$index] ) ) {
1655 return $this->args[$index];
1662 class PPNode_HipHop_Tree implements PPNode {
1663 var $name, $firstChild, $lastChild, $nextSibling;
1665 function __construct( $name ) {
1666 $this->name = $name;
1667 $this->firstChild = $this->lastChild = $this->nextSibling = false;
1670 function __toString() {
1673 for ( $node = $this->firstChild; $node; $node = $node->nextSibling ) {
1674 if ( $node instanceof PPNode_HipHop_Attr ) {
1675 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1677 $inner .= $node->__toString();
1680 if ( $inner === '' ) {
1681 return "<{$this->name}$attribs/>";
1683 return "<{$this->name}$attribs>$inner</{$this->name}>";
1690 * @return PPNode_HipHop_Tree
1692 static function newWithText( $name, $text ) {
1693 $obj = new self( $name );
1694 $obj->addChild( new PPNode_HipHop_Text( $text ) );
1698 function addChild( $node ) {
1699 if ( $this->lastChild === false ) {
1700 $this->firstChild = $this->lastChild = $node;
1702 $this->lastChild->nextSibling = $node;
1703 $this->lastChild = $node;
1708 * @return PPNode_HipHop_Array
1710 function getChildren() {
1711 $children = array();
1712 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1713 $children[] = $child;
1715 return new PPNode_HipHop_Array( $children );
1718 function getFirstChild() {
1719 return $this->firstChild;
1722 function getNextSibling() {
1723 return $this->nextSibling;
1726 function getChildrenOfType( $name ) {
1727 $children = array();
1728 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1729 if ( isset( $child->name ) && $child->name === $name ) {
1730 $children[] = $name;
1739 function getLength() {
1747 function item( $i ) {
1754 function getName() {
1759 * Split a <part> node into an associative array containing:
1761 * index String index
1762 * value PPNode value
1766 function splitArg() {
1768 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1769 if ( !isset( $child->name ) ) {
1772 if ( $child->name === 'name' ) {
1773 $bits['name'] = $child;
1774 if ( $child->firstChild instanceof PPNode_HipHop_Attr
1775 && $child->firstChild->name === 'index' )
1777 $bits['index'] = $child->firstChild->value;
1779 } elseif ( $child->name === 'value' ) {
1780 $bits['value'] = $child;
1784 if ( !isset( $bits['name'] ) ) {
1785 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1787 if ( !isset( $bits['index'] ) ) {
1788 $bits['index'] = '';
1794 * Split an <ext> node into an associative array containing name, attr, inner and close
1795 * All values in the resulting array are PPNodes. Inner and close are optional.
1799 function splitExt() {
1801 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1802 if ( !isset( $child->name ) ) {
1805 if ( $child->name === 'name' ) {
1806 $bits['name'] = $child;
1807 } elseif ( $child->name === 'attr' ) {
1808 $bits['attr'] = $child;
1809 } elseif ( $child->name === 'inner' ) {
1810 $bits['inner'] = $child;
1811 } elseif ( $child->name === 'close' ) {
1812 $bits['close'] = $child;
1815 if ( !isset( $bits['name'] ) ) {
1816 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1826 function splitHeading() {
1827 if ( $this->name !== 'h' ) {
1828 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1831 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1832 if ( !isset( $child->name ) ) {
1835 if ( $child->name === 'i' ) {
1836 $bits['i'] = $child->value;
1837 } elseif ( $child->name === 'level' ) {
1838 $bits['level'] = $child->value;
1841 if ( !isset( $bits['i'] ) ) {
1842 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1848 * Split a <template> or <tplarg> node
1852 function splitTemplate() {
1854 $bits = array( 'lineStart' => '' );
1855 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1856 if ( !isset( $child->name ) ) {
1859 if ( $child->name === 'title' ) {
1860 $bits['title'] = $child;
1862 if ( $child->name === 'part' ) {
1865 if ( $child->name === 'lineStart' ) {
1866 $bits['lineStart'] = '1';
1869 if ( !isset( $bits['title'] ) ) {
1870 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
1872 $bits['parts'] = new PPNode_HipHop_Array( $parts );
1880 class PPNode_HipHop_Text implements PPNode {
1881 var $value, $nextSibling;
1883 function __construct( $value ) {
1884 if ( is_object( $value ) ) {
1885 throw new MWException( __CLASS__ . ' given object instead of string' );
1887 $this->value = $value;
1890 function __toString() {
1891 return htmlspecialchars( $this->value );
1894 function getNextSibling() {
1895 return $this->nextSibling;
1898 function getChildren() { return false; }
1899 function getFirstChild() { return false; }
1900 function getChildrenOfType( $name ) { return false; }
1901 function getLength() { return false; }
1902 function item( $i ) { return false; }
1903 function getName() { return '#text'; }
1904 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1905 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1906 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1912 class PPNode_HipHop_Array implements PPNode {
1913 var $value, $nextSibling;
1915 function __construct( $value ) {
1916 $this->value = $value;
1919 function __toString() {
1920 return var_export( $this, true );
1923 function getLength() {
1924 return count( $this->value );
1927 function item( $i ) {
1928 return $this->value[$i];
1931 function getName() { return '#nodelist'; }
1933 function getNextSibling() {
1934 return $this->nextSibling;
1937 function getChildren() { return false; }
1938 function getFirstChild() { return false; }
1939 function getChildrenOfType( $name ) { return false; }
1940 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1941 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1942 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1948 class PPNode_HipHop_Attr implements PPNode {
1949 var $name, $value, $nextSibling;
1951 function __construct( $name, $value ) {
1952 $this->name = $name;
1953 $this->value = $value;
1956 function __toString() {
1957 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
1960 function getName() {
1964 function getNextSibling() {
1965 return $this->nextSibling;
1968 function getChildren() { return false; }
1969 function getFirstChild() { return false; }
1970 function getChildrenOfType( $name ) { return false; }
1971 function getLength() { return false; }
1972 function item( $i ) { return false; }
1973 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1974 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1975 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }