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;
37 * @param $parser Parser
39 function __construct( $parser ) {
40 $this->parser = $parser;
44 * @return PPFrame_HipHop
47 return new PPFrame_HipHop( $this );
52 * @return PPCustomFrame_HipHop
54 function newCustomFrame( array $args ) {
55 return new PPCustomFrame_HipHop( $this, $args );
59 * @param $values array
60 * @return PPNode_HipHop_Array
62 function newPartNodeArray( $values ) {
65 foreach ( $values as $k => $val ) {
66 $partNode = new PPNode_HipHop_Tree( 'part' );
67 $nameNode = new PPNode_HipHop_Tree( 'name' );
70 $nameNode->addChild( new PPNode_HipHop_Attr( 'index', $k ) );
71 $partNode->addChild( $nameNode );
73 $nameNode->addChild( new PPNode_HipHop_Text( $k ) );
74 $partNode->addChild( $nameNode );
75 $partNode->addChild( new PPNode_HipHop_Text( '=' ) );
78 $valueNode = new PPNode_HipHop_Tree( 'value' );
79 $valueNode->addChild( new PPNode_HipHop_Text( $val ) );
80 $partNode->addChild( $valueNode );
85 $node = new PPNode_HipHop_Array( $list );
90 * Preprocess some wikitext and return the document tree.
91 * This is the ghost of Parser::replace_variables().
93 * @param $text String: the text to parse
94 * @param $flags Integer: bitwise combination of:
95 * Parser::PTD_FOR_INCLUSION Handle <noinclude>/<includeonly> as if the text is being
96 * included. Default is to assume a direct page view.
98 * The generated DOM tree must depend only on the input text and the flags.
99 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
101 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
102 * change in the DOM tree for a given text, must be passed through the section identifier
103 * in the section edit link and thus back to extractSections().
105 * The output of this function is currently only cached in process memory, but a persistent
106 * cache may be implemented at a later date which takes further advantage of these strict
107 * dependency requirements.
109 * @throws MWException
110 * @return PPNode_HipHop_Tree
112 function preprocessToObj( string $text, int $flags = 0 ) {
113 wfProfileIn( __METHOD__ );
116 global $wgMemc, $wgPreprocessorCacheThreshold;
118 $cacheable = ($wgPreprocessorCacheThreshold !== false && strlen( $text ) > $wgPreprocessorCacheThreshold);
120 wfProfileIn( __METHOD__.'-cacheable' );
122 $cacheKey = strval( wfMemcKey( 'preprocess-hash', md5($text), $flags ) );
123 $cacheValue = strval( $wgMemc->get( $cacheKey ) );
124 if ( $cacheValue !== '' ) {
125 $version = substr( $cacheValue, 0, 8 );
126 if ( intval( $version ) == self::CACHE_VERSION ) {
127 $hash = unserialize( substr( $cacheValue, 8 ) );
129 wfDebugLog( "Preprocessor",
130 "Loaded preprocessor hash from memcached (key $cacheKey)" );
131 wfProfileOut( __METHOD__.'-cacheable' );
132 wfProfileOut( __METHOD__ );
136 wfProfileIn( __METHOD__.'-cache-miss' );
151 'names' => array( 2 => 'LITERAL' ),
157 $forInclusion = (bool)( $flags & Parser::PTD_FOR_INCLUSION );
159 $xmlishElements = (array)$this->parser->getStripList();
160 $enableOnlyinclude = false;
161 if ( $forInclusion ) {
162 $ignoredTags = array( 'includeonly', '/includeonly' );
163 $ignoredElements = array( 'noinclude' );
164 $xmlishElements[] = 'noinclude';
165 if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) {
166 $enableOnlyinclude = true;
168 } else if ( $this->parser->ot['wiki'] ) {
169 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude', 'includeonly', '/includeonly' );
170 $ignoredElements = array();
172 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
173 $ignoredElements = array( 'includeonly' );
174 $xmlishElements[] = 'includeonly';
176 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
178 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
179 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
181 $stack = new PPDStack_HipHop;
183 $searchBase = "[{<\n";
184 $revText = strrev( $text ); // For fast reverse searches
186 $i = 0; # Input pointer, starts out pointing to a pseudo-newline before the start
187 $accum = $stack->getAccum(); # Current accumulator
190 'findPipe' => false, # True to take notice of pipe characters
191 'findEquals' => false, # True to find equals signs in arguments
192 'inHeading' => false, # True if $i is inside a possible heading
194 $noMoreGT = false; # True if there are no more greater-than (>) signs right of $i
195 $findOnlyinclude = $enableOnlyinclude; # True to ignore all input up to the next <onlyinclude>
196 $fakeLineStart = true; # Do a line-start run without outputting an LF character
201 if ( $findOnlyinclude ) {
202 // Ignore all input up to the next <onlyinclude>
203 $variantStartPos = strpos( $text, '<onlyinclude>', $i );
204 if ( $variantStartPos === false ) {
205 // Ignored section runs to the end
206 $accum->addNodeWithText( 'ignore', strval( substr( $text, $i ) ) );
209 $startPos1 = intval( $variantStartPos );
210 $tagEndPos = $startPos1 + strlen( '<onlyinclude>' ); // past-the-end
211 $accum->addNodeWithText( 'ignore', strval( substr( $text, $i, $tagEndPos - $i ) ) );
213 $findOnlyinclude = false;
216 if ( $fakeLineStart ) {
217 $found = 'line-start';
220 # Find next opening brace, closing brace or pipe
221 $search = $searchBase;
222 if ( $stack->top === false ) {
223 $currentClosing = '';
225 $currentClosing = strval( $stack->getTop()->close );
226 $search .= $currentClosing;
228 if ( $stackFlags['findPipe'] ) {
231 if ( $stackFlags['findEquals'] ) {
232 // First equals will be for the template
236 # Output literal section, advance input counter
237 $literalLength = intval( strcspn( $text, $search, $i ) );
238 if ( $literalLength > 0 ) {
239 $accum->addLiteral( strval( substr( $text, $i, $literalLength ) ) );
240 $i += $literalLength;
242 if ( $i >= strlen( $text ) ) {
243 if ( $currentClosing === "\n" ) {
244 // Do a past-the-end run to finish off the heading
252 $curChar = $text[$i];
253 if ( $curChar === '|' ) {
255 } elseif ( $curChar === '=' ) {
257 } elseif ( $curChar === '<' ) {
259 } elseif ( $curChar === "\n" ) {
260 if ( $stackFlags['inHeading'] ) {
263 $found = 'line-start';
265 } elseif ( $curChar === $currentClosing ) {
267 } elseif ( isset( $rules[$curChar] ) ) {
269 $rule = $rules[$curChar];
271 # Some versions of PHP have a strcspn which stops on null characters
272 # Ignore and continue
279 if ( $found === 'angle' ) {
281 // Handle </onlyinclude>
282 if ( $enableOnlyinclude
283 && substr( $text, $i, strlen( '</onlyinclude>' ) ) === '</onlyinclude>' )
285 $findOnlyinclude = true;
289 // Determine element name
290 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
291 // Element name missing or not listed
292 $accum->addLiteral( '<' );
297 if ( isset( $matches[2] ) && $matches[2] === '!--' ) {
298 // To avoid leaving blank lines, when a comment is both preceded
299 // and followed by a newline (ignoring spaces), trim leading and
300 // trailing spaces and one of the newlines.
303 $variantEndPos = strpos( $text, '-->', $i + 4 );
304 if ( $variantEndPos === false ) {
305 // Unclosed comment in input, runs to end
306 $inner = strval( substr( $text, $i ) );
307 $accum->addNodeWithText( 'comment', $inner );
308 $i = strlen( $text );
310 $endPos = intval( $variantEndPos );
311 // Search backwards for leading whitespace
313 $wsStart = $i - intval( strspn( $revText, ' ', strlen( $text ) - $i ) );
317 // Search forwards for trailing whitespace
318 // $wsEnd will be the position of the last space (or the '>' if there's none)
319 $wsEnd = $endPos + 2 + intval( strspn( $text, ' ', $endPos + 3 ) );
320 // Eat the line if possible
321 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
322 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
323 // it's a possible beneficial b/c break.
324 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) === "\n"
325 && substr( $text, $wsEnd + 1, 1 ) === "\n" )
327 $startPos2 = $wsStart;
328 $endPos = $wsEnd + 1;
329 // Remove leading whitespace from the end of the accumulator
330 // Sanity check first though
331 $wsLength = $i - $wsStart;
333 && $accum->lastNode instanceof PPNode_HipHop_Text
334 && substr( $accum->lastNode->value, -$wsLength ) === str_repeat( ' ', $wsLength ) )
336 $accum->lastNode->value = strval( substr( $accum->lastNode->value, 0, -$wsLength ) );
338 // Do a line-start run next time to look for headings after the comment
339 $fakeLineStart = true;
341 // No line to eat, just take the comment itself
347 $part = $stack->getTop()->getCurrentPart();
348 if ( ! (isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 )) {
349 $part->visualEnd = $wsStart;
351 // Else comments abutting, no change in visual end
352 $part->commentEnd = $endPos;
355 $inner = strval( substr( $text, $startPos2, $endPos - $startPos2 + 1 ) );
356 $accum->addNodeWithText( 'comment', $inner );
360 $name = strval( $matches[1] );
361 $lowerName = strtolower( $name );
362 $attrStart = $i + strlen( $name ) + 1;
365 $variantTagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
366 if ( $variantTagEndPos === false ) {
367 // Infinite backtrack
368 // Disable tag search to prevent worst-case O(N^2) performance
370 $accum->addLiteral( '<' );
374 $tagEndPos = intval( $variantTagEndPos );
376 // Handle ignored tags
377 if ( in_array( $lowerName, $ignoredTags ) ) {
378 $accum->addNodeWithText( 'ignore', strval( substr( $text, $i, $tagEndPos - $i + 1 ) ) );
385 if ( $text[$tagEndPos-1] === '/' ) {
387 $attrEnd = $tagEndPos - 1;
393 $attrEnd = $tagEndPos;
396 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
397 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) )
399 $inner = strval( substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 ) );
400 $i = intval( $matches[0][1] ) + strlen( $matches[0][0] );
401 $close = strval( $matches[0][0] );
404 // No end tag -- let it run out to the end of the text.
405 $inner = strval( substr( $text, $tagEndPos + 1 ) );
406 $i = strlen( $text );
410 // <includeonly> and <noinclude> just become <ignore> tags
411 if ( in_array( $lowerName, $ignoredElements ) ) {
412 $accum->addNodeWithText( 'ignore', strval( substr( $text, $tagStartPos, $i - $tagStartPos ) ) );
416 if ( $attrEnd <= $attrStart ) {
419 // Note that the attr element contains the whitespace between name and attribute,
420 // this is necessary for precise reconstruction during pre-save transform.
421 $attr = strval( substr( $text, $attrStart, $attrEnd - $attrStart ) );
424 $extNode = new PPNode_HipHop_Tree( 'ext' );
425 $extNode->addChild( PPNode_HipHop_Tree::newWithText( 'name', $name ) );
426 $extNode->addChild( PPNode_HipHop_Tree::newWithText( 'attr', $attr ) );
428 $extNode->addChild( PPNode_HipHop_Tree::newWithText( 'inner', $inner ) );
431 $extNode->addChild( PPNode_HipHop_Tree::newWithText( 'close', $close ) );
433 $accum->addNode( $extNode );
436 elseif ( $found === 'line-start' ) {
437 // Is this the start of a heading?
438 // Line break belongs before the heading element in any case
439 if ( $fakeLineStart ) {
440 $fakeLineStart = false;
442 $accum->addLiteral( $curChar );
446 $count = intval( strspn( $text, '=', $i, 6 ) );
447 if ( $count == 1 && $stackFlags['findEquals'] ) {
448 // DWIM: This looks kind of like a name/value separator
449 // Let's let the equals handler have it and break the potential heading
450 // This is heuristic, but AFAICT the methods for completely correct disambiguation are very complex.
451 } elseif ( $count > 0 ) {
455 'parts' => array( new PPDPart_HipHop( str_repeat( '=', $count ) ) ),
458 $stack->push( $partData );
459 $accum = $stack->getAccum();
460 $stackFlags = $stack->getFlags();
463 } elseif ( $found === 'line-end' ) {
464 $piece = $stack->getTop();
465 // A heading must be open, otherwise \n wouldn't have been in the search list
466 assert( $piece->open === "\n" ); // Passing the assert condition directly instead of string, as
467 // HPHP /compiler/ chokes on strings when ASSERT_ACTIVE != 0.
468 $part = $piece->getCurrentPart();
469 // Search back through the input to see if it has a proper close
470 // Do this using the reversed string since the other solutions (end anchor, etc.) are inefficient
471 $wsLength = intval( strspn( $revText, " \t", strlen( $text ) - $i ) );
472 $searchStart = $i - $wsLength;
473 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
474 // Comment found at line end
475 // Search for equals signs before the comment
476 $searchStart = intval( $part->visualEnd );
477 $searchStart -= intval( strspn( $revText, " \t", strlen( $text ) - $searchStart ) );
479 $count = intval( $piece->count );
480 $equalsLength = intval( strspn( $revText, '=', strlen( $text ) - $searchStart ) );
482 $resultAccum = $accum;
483 if ( $equalsLength > 0 ) {
484 if ( $searchStart - $equalsLength == $piece->startPos ) {
485 // This is just a single string of equals signs on its own line
486 // Replicate the doHeadings behaviour /={count}(.+)={count}/
487 // First find out how many equals signs there really are (don't stop at 6)
488 $count = $equalsLength;
492 $count = intval( ( $count - 1 ) / 2 );
498 if ( $count > $equalsLength ) {
499 $count = $equalsLength;
503 // Normal match, output <h>
504 $tree = new PPNode_HipHop_Tree( 'possible-h' );
505 $tree->addChild( new PPNode_HipHop_Attr( 'level', $count ) );
506 $tree->addChild( new PPNode_HipHop_Attr( 'i', $headingIndex++ ) );
507 $tree->lastChild->nextSibling = $accum->firstNode;
508 $tree->lastChild = $accum->lastNode;
511 // Single equals sign on its own line, count=0
512 // Output $resultAccum
515 // No match, no <h>, just pass down the inner text
516 // Output $resultAccum
520 $accum = $stack->getAccum();
521 $stackFlags = $stack->getFlags();
523 // Append the result to the enclosing accumulator
525 $accum->addNode( $tree );
527 $accum->addAccum( $resultAccum );
529 // Note that we do NOT increment the input pointer.
530 // This is because the closing linebreak could be the opening linebreak of
531 // another heading. Infinite loops are avoided because the next iteration MUST
532 // hit the heading open case above, which unconditionally increments the
534 } elseif ( $found === 'open' ) {
535 # count opening brace characters
536 $count = intval( strspn( $text, $curChar, $i ) );
538 # we need to add to stack only if opening brace count is enough for one of the rules
539 if ( $count >= $rule['min'] ) {
540 # Add it to the stack
543 'close' => $rule['end'],
545 'lineStart' => ($i == 0 || $text[$i-1] === "\n"),
548 $stack->push( $partData );
549 $accum = $stack->getAccum();
550 $stackFlags = $stack->getFlags();
552 # Add literal brace(s)
553 $accum->addLiteral( str_repeat( $curChar, $count ) );
556 } elseif ( $found === 'close' ) {
557 $piece = $stack->getTop();
558 # lets check if there are enough characters for closing brace
559 $maxCount = intval( $piece->count );
560 $count = intval( strspn( $text, $curChar, $i, $maxCount ) );
562 # check for maximum matching characters (if there are 5 closing
563 # characters, we will probably need only 3 - depending on the rules)
564 $rule = $rules[$piece->open];
565 if ( $count > $rule['max'] ) {
566 # The specified maximum exists in the callback array, unless the caller
568 $matchingCount = intval( $rule['max'] );
570 # Count is less than the maximum
571 # Skip any gaps in the callback array to find the true largest match
572 # Need to use array_key_exists not isset because the callback can be null
573 $matchingCount = $count;
574 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
579 if ($matchingCount <= 0) {
580 # No matching element found in callback array
581 # Output a literal closing brace and continue
582 $accum->addLiteral( str_repeat( $curChar, $count ) );
586 $name = strval( $rule['names'][$matchingCount] );
588 if ( $name === 'LITERAL' ) {
589 // No element, just literal text
590 $resultAccum = $piece->breakSyntax( $matchingCount );
591 $resultAccum->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
594 # Note: $parts is already XML, does not need to be encoded further
596 $parts = $piece->parts;
597 $titleAccum = PPDAccum_HipHop::cast( $parts[0]->out );
600 $tree = new PPNode_HipHop_Tree( $name );
602 # The invocation is at the start of the line if lineStart is set in
603 # the stack, and all opening brackets are used up.
604 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
605 $tree->addChild( new PPNode_HipHop_Attr( 'lineStart', 1 ) );
607 $titleNode = new PPNode_HipHop_Tree( 'title' );
608 $titleNode->firstChild = $titleAccum->firstNode;
609 $titleNode->lastChild = $titleAccum->lastNode;
610 $tree->addChild( $titleNode );
612 foreach ( $parts as $variantPart ) {
613 $part = PPDPart_HipHop::cast( $variantPart );
614 if ( isset( $part->eqpos ) ) {
617 for ( $node = $part->out->firstNode; $node; $node = $node->nextSibling ) {
618 if ( $node === $part->eqpos ) {
624 throw new MWException( __METHOD__. ': eqpos not found' );
626 if ( $node->name !== 'equals' ) {
627 throw new MWException( __METHOD__ .': eqpos is not equals' );
631 // Construct name node
632 $nameNode = new PPNode_HipHop_Tree( 'name' );
633 if ( $lastNode !== false ) {
634 $lastNode->nextSibling = false;
635 $nameNode->firstChild = $part->out->firstNode;
636 $nameNode->lastChild = $lastNode;
639 // Construct value node
640 $valueNode = new PPNode_HipHop_Tree( 'value' );
641 if ( $equalsNode->nextSibling !== false ) {
642 $valueNode->firstChild = $equalsNode->nextSibling;
643 $valueNode->lastChild = $part->out->lastNode;
645 $partNode = new PPNode_HipHop_Tree( 'part' );
646 $partNode->addChild( $nameNode );
647 $partNode->addChild( $equalsNode->firstChild );
648 $partNode->addChild( $valueNode );
649 $tree->addChild( $partNode );
651 $partNode = new PPNode_HipHop_Tree( 'part' );
652 $nameNode = new PPNode_HipHop_Tree( 'name' );
653 $nameNode->addChild( new PPNode_HipHop_Attr( 'index', $argIndex++ ) );
654 $valueNode = new PPNode_HipHop_Tree( 'value' );
655 $valueNode->firstChild = $part->out->firstNode;
656 $valueNode->lastChild = $part->out->lastNode;
657 $partNode->addChild( $nameNode );
658 $partNode->addChild( $valueNode );
659 $tree->addChild( $partNode );
664 # Advance input pointer
665 $i += $matchingCount;
669 $accum = $stack->getAccum();
671 # Re-add the old stack element if it still has unmatched opening characters remaining
672 if ($matchingCount < $piece->count) {
673 $piece->parts = array( new PPDPart_HipHop );
674 $piece->count -= $matchingCount;
675 # do we still qualify for any callback with remaining count?
676 $names = $rules[$piece->open]['names'];
678 $enclosingAccum = $accum;
679 while ( $piece->count ) {
680 if ( array_key_exists( $piece->count, $names ) ) {
681 $stack->push( $piece );
682 $accum = $stack->getAccum();
688 $enclosingAccum->addLiteral( str_repeat( $piece->open, $skippedBraces ) );
691 $stackFlags = $stack->getFlags();
693 # Add XML element to the enclosing accumulator
695 $accum->addNode( $tree );
697 $accum->addAccum( $resultAccum );
699 } elseif ( $found === 'pipe' ) {
700 $stackFlags['findEquals'] = true; // shortcut for getFlags()
702 $accum = $stack->getAccum();
704 } elseif ( $found === 'equals' ) {
705 $stackFlags['findEquals'] = false; // shortcut for getFlags()
706 $accum->addNodeWithText( 'equals', '=' );
707 $stack->getCurrentPart()->eqpos = $accum->lastNode;
712 # Output any remaining unclosed brackets
713 foreach ( $stack->stack as $variantPiece ) {
714 $piece = PPDStackElement_HipHop::cast( $variantPiece );
715 $stack->rootAccum->addAccum( $piece->breakSyntax() );
718 # Enable top-level headings
719 for ( $node = $stack->rootAccum->firstNode; $node; $node = $node->nextSibling ) {
720 if ( isset( $node->name ) && $node->name === 'possible-h' ) {
725 $rootNode = new PPNode_HipHop_Tree( 'root' );
726 $rootNode->firstChild = $stack->rootAccum->firstNode;
727 $rootNode->lastChild = $stack->rootAccum->lastNode;
731 $cacheValue = sprintf( "%08d", self::CACHE_VERSION ) . serialize( $rootNode );
732 $wgMemc->set( $cacheKey, $cacheValue, 86400 );
733 wfProfileOut( __METHOD__.'-cache-miss' );
734 wfProfileOut( __METHOD__.'-cacheable' );
735 wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
738 wfProfileOut( __METHOD__ );
746 * Stack class to help Preprocessor::preprocessToObj()
749 class PPDStack_HipHop {
750 var $stack, $rootAccum;
758 static $false = false;
760 function __construct() {
761 $this->stack = array();
763 $this->rootAccum = new PPDAccum_HipHop;
764 $this->accum = $this->rootAccum;
771 return count( $this->stack );
774 function getAccum() {
775 return PPDAccum_HipHop::cast( $this->accum );
778 function getCurrentPart() {
779 return $this->getTop()->getCurrentPart();
783 return PPDStackElement_HipHop::cast( $this->top );
786 function push( $data ) {
787 if ( $data instanceof PPDStackElement_HipHop ) {
788 $this->stack[] = $data;
790 $this->stack[] = new PPDStackElement_HipHop( $data );
792 $this->top = $this->stack[ count( $this->stack ) - 1 ];
793 $this->accum = $this->top->getAccum();
797 if ( !count( $this->stack ) ) {
798 throw new MWException( __METHOD__.': no elements remaining' );
800 $temp = array_pop( $this->stack );
802 if ( count( $this->stack ) ) {
803 $this->top = $this->stack[ count( $this->stack ) - 1 ];
804 $this->accum = $this->top->getAccum();
806 $this->top = self::$false;
807 $this->accum = $this->rootAccum;
812 function addPart( $s = '' ) {
813 $this->top->addPart( $s );
814 $this->accum = $this->top->getAccum();
820 function getFlags() {
821 if ( !count( $this->stack ) ) {
823 'findEquals' => false,
825 'inHeading' => false,
828 return $this->top->getFlags();
836 class PPDStackElement_HipHop {
837 var $open, // Opening character (\n for heading)
838 $close, // Matching closing character
839 $count, // Number of opening characters found (number of "=" for heading)
840 $parts, // Array of PPDPart objects describing pipe-separated parts.
841 $lineStart; // True if the open char appeared at the start of the input line. Not set for headings.
844 * @param $obj PPDStackElement_HipHop
845 * @return PPDStackElement_HipHop
847 static function cast( PPDStackElement_HipHop $obj ) {
854 function __construct( $data = array() ) {
855 $this->parts = array( new PPDPart_HipHop );
857 foreach ( $data as $name => $value ) {
858 $this->$name = $value;
863 * @return PPDAccum_HipHop
865 function getAccum() {
866 return PPDAccum_HipHop::cast( $this->parts[count($this->parts) - 1]->out );
872 function addPart( $s = '' ) {
873 $this->parts[] = new PPDPart_HipHop( $s );
877 * @return PPDPart_HipHop
879 function getCurrentPart() {
880 return PPDPart_HipHop::cast( $this->parts[count($this->parts) - 1] );
886 function getFlags() {
887 $partCount = count( $this->parts );
888 $findPipe = $this->open !== "\n" && $this->open !== '[';
890 'findPipe' => $findPipe,
891 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts[$partCount - 1]->eqpos ),
892 'inHeading' => $this->open === "\n",
897 * Get the accumulator that would result if the close is not found.
899 * @param $openingCount bool
900 * @return PPDAccum_HipHop
902 function breakSyntax( $openingCount = false ) {
903 if ( $this->open === "\n" ) {
904 $accum = PPDAccum_HipHop::cast( $this->parts[0]->out );
906 if ( $openingCount === false ) {
907 $openingCount = $this->count;
909 $accum = new PPDAccum_HipHop;
910 $accum->addLiteral( str_repeat( $this->open, $openingCount ) );
912 foreach ( $this->parts as $part ) {
916 $accum->addLiteral( '|' );
918 $accum->addAccum( $part->out );
928 class PPDPart_HipHop {
929 var $out; // Output accumulator object
931 // Optional member variables:
932 // eqpos Position of equals sign in output accumulator
933 // commentEnd Past-the-end input pointer for the last comment encountered
934 // visualEnd Past-the-end input pointer for the end of the accumulator minus comments
936 function __construct( $out = '' ) {
937 $this->out = new PPDAccum_HipHop;
939 $this->out->addLiteral( $out );
943 static function cast( PPDPart_HipHop $obj ) {
951 class PPDAccum_HipHop {
952 var $firstNode, $lastNode;
954 function __construct() {
955 $this->firstNode = $this->lastNode = false;
958 static function cast( PPDAccum_HipHop $obj ) {
963 * Append a string literal
965 function addLiteral( string $s ) {
966 if ( $this->lastNode === false ) {
967 $this->firstNode = $this->lastNode = new PPNode_HipHop_Text( $s );
968 } elseif ( $this->lastNode instanceof PPNode_HipHop_Text ) {
969 $this->lastNode->value .= $s;
971 $this->lastNode->nextSibling = new PPNode_HipHop_Text( $s );
972 $this->lastNode = $this->lastNode->nextSibling;
979 function addNode( PPNode $node ) {
980 if ( $this->lastNode === false ) {
981 $this->firstNode = $this->lastNode = $node;
983 $this->lastNode->nextSibling = $node;
984 $this->lastNode = $node;
989 * Append a tree node with text contents
991 function addNodeWithText( string $name, string $value ) {
992 $node = PPNode_HipHop_Tree::newWithText( $name, $value );
993 $this->addNode( $node );
997 * Append a PPDAccum_HipHop
998 * Takes over ownership of the nodes in the source argument. These nodes may
999 * subsequently be modified, especially nextSibling.
1001 function addAccum( PPDAccum_HipHop $accum ) {
1002 if ( $accum->lastNode === false ) {
1004 } elseif ( $this->lastNode === false ) {
1005 $this->firstNode = $accum->firstNode;
1006 $this->lastNode = $accum->lastNode;
1008 $this->lastNode->nextSibling = $accum->firstNode;
1009 $this->lastNode = $accum->lastNode;
1015 * An expansion frame, used as a context to expand the result of preprocessToObj()
1018 class PPFrame_HipHop implements PPFrame {
1037 * Hashtable listing templates which are disallowed for expansion in this frame,
1038 * having been encountered previously in parent frames.
1043 * Recursion depth of this frame, top = 0
1044 * Note that this is NOT the same as expansion depth in expand()
1049 * Construct a new preprocessor frame.
1050 * @param $preprocessor Preprocessor: the parent preprocessor
1052 function __construct( $preprocessor ) {
1053 $this->preprocessor = $preprocessor;
1054 $this->parser = $preprocessor->parser;
1055 $this->title = $this->parser->mTitle;
1056 $this->titleCache = array( $this->title ? $this->title->getPrefixedDBkey() : false );
1057 $this->loopCheckHash = array();
1062 * Create a new child frame
1063 * $args is optionally a multi-root PPNode or array containing the template arguments
1065 * @param $args PPNode_HipHop_Array|array|bool
1066 * @param $title Title|bool
1068 * @throws MWException
1069 * @return PPTemplateFrame_HipHop
1071 function newChild( $args = false, $title = false ) {
1072 $namedArgs = array();
1073 $numberedArgs = array();
1074 if ( $title === false ) {
1075 $title = $this->title;
1077 if ( $args !== false ) {
1078 if ( $args instanceof PPNode_HipHop_Array ) {
1079 $args = $args->value;
1080 } elseif ( !is_array( $args ) ) {
1081 throw new MWException( __METHOD__ . ': $args must be array or PPNode_HipHop_Array' );
1083 foreach ( $args as $arg ) {
1084 $bits = $arg->splitArg();
1085 if ( $bits['index'] !== '' ) {
1086 // Numbered parameter
1087 $numberedArgs[$bits['index']] = $bits['value'];
1088 unset( $namedArgs[$bits['index']] );
1091 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
1092 $namedArgs[$name] = $bits['value'];
1093 unset( $numberedArgs[$name] );
1097 return new PPTemplateFrame_HipHop( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
1101 * @throws MWException
1106 function expand( $root, $flags = 0 ) {
1107 static $expansionDepth = 0;
1108 if ( is_string( $root ) ) {
1112 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
1113 $this->parser->limitationWarn( 'node-count-exceeded',
1114 $this->parser->mPPNodeCount,
1115 $this->parser->mOptions->getMaxPPNodeCount()
1117 return '<span class="error">Node-count limit exceeded</span>';
1119 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
1120 $this->parser->limitationWarn( 'expansion-depth-exceeded',
1122 $this->parser->mOptions->getMaxPPExpandDepth()
1124 return '<span class="error">Expansion depth limit exceeded</span>';
1127 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
1128 $this->parser->mHighestExpansionDepth = $expansionDepth;
1131 $outStack = array( '', '' );
1132 $iteratorStack = array( false, $root );
1133 $indexStack = array( 0, 0 );
1135 while ( count( $iteratorStack ) > 1 ) {
1136 $level = count( $outStack ) - 1;
1137 $iteratorNode =& $iteratorStack[ $level ];
1138 $out =& $outStack[$level];
1139 $index =& $indexStack[$level];
1141 if ( is_array( $iteratorNode ) ) {
1142 if ( $index >= count( $iteratorNode ) ) {
1143 // All done with this iterator
1144 $iteratorStack[$level] = false;
1145 $contextNode = false;
1147 $contextNode = $iteratorNode[$index];
1150 } elseif ( $iteratorNode instanceof PPNode_HipHop_Array ) {
1151 if ( $index >= $iteratorNode->getLength() ) {
1152 // All done with this iterator
1153 $iteratorStack[$level] = false;
1154 $contextNode = false;
1156 $contextNode = $iteratorNode->item( $index );
1160 // Copy to $contextNode and then delete from iterator stack,
1161 // because this is not an iterator but we do have to execute it once
1162 $contextNode = $iteratorStack[$level];
1163 $iteratorStack[$level] = false;
1166 $newIterator = false;
1168 if ( $contextNode === false ) {
1170 } elseif ( is_string( $contextNode ) ) {
1171 $out .= $contextNode;
1172 } elseif ( is_array( $contextNode ) || $contextNode instanceof PPNode_HipHop_Array ) {
1173 $newIterator = $contextNode;
1174 } elseif ( $contextNode instanceof PPNode_HipHop_Attr ) {
1176 } elseif ( $contextNode instanceof PPNode_HipHop_Text ) {
1177 $out .= $contextNode->value;
1178 } elseif ( $contextNode instanceof PPNode_HipHop_Tree ) {
1179 if ( $contextNode->name === 'template' ) {
1180 # Double-brace expansion
1181 $bits = $contextNode->splitTemplate();
1182 if ( $flags & PPFrame::NO_TEMPLATES ) {
1183 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $bits['title'], $bits['parts'] );
1185 $ret = $this->parser->braceSubstitution( $bits, $this );
1186 if ( isset( $ret['object'] ) ) {
1187 $newIterator = $ret['object'];
1189 $out .= $ret['text'];
1192 } elseif ( $contextNode->name === 'tplarg' ) {
1193 # Triple-brace expansion
1194 $bits = $contextNode->splitTemplate();
1195 if ( $flags & PPFrame::NO_ARGS ) {
1196 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $bits['title'], $bits['parts'] );
1198 $ret = $this->parser->argSubstitution( $bits, $this );
1199 if ( isset( $ret['object'] ) ) {
1200 $newIterator = $ret['object'];
1202 $out .= $ret['text'];
1205 } elseif ( $contextNode->name === 'comment' ) {
1206 # HTML-style comment
1207 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1208 if ( $this->parser->ot['html']
1209 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
1210 || ( $flags & PPFrame::STRIP_COMMENTS ) )
1214 # Add a strip marker in PST mode so that pstPass2() can run some old-fashioned regexes on the result
1215 # Not in RECOVER_COMMENTS mode (extractSections) though
1216 elseif ( $this->parser->ot['wiki'] && ! ( $flags & PPFrame::RECOVER_COMMENTS ) ) {
1217 $out .= $this->parser->insertStripItem( $contextNode->firstChild->value );
1219 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1221 $out .= $contextNode->firstChild->value;
1223 } elseif ( $contextNode->name === 'ignore' ) {
1224 # Output suppression used by <includeonly> etc.
1225 # OT_WIKI will only respect <ignore> in substed templates.
1226 # The other output types respect it unless NO_IGNORE is set.
1227 # extractSections() sets NO_IGNORE and so never respects it.
1228 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] ) || ( $flags & PPFrame::NO_IGNORE ) ) {
1229 $out .= $contextNode->firstChild->value;
1233 } elseif ( $contextNode->name === 'ext' ) {
1235 $bits = $contextNode->splitExt() + array( 'attr' => null, 'inner' => null, 'close' => null );
1236 $out .= $this->parser->extensionSubstitution( $bits, $this );
1237 } elseif ( $contextNode->name === 'h' ) {
1239 if ( $this->parser->ot['html'] ) {
1240 # Expand immediately and insert heading index marker
1242 for ( $node = $contextNode->firstChild; $node; $node = $node->nextSibling ) {
1243 $s .= $this->expand( $node, $flags );
1246 $bits = $contextNode->splitHeading();
1247 $titleText = $this->title->getPrefixedDBkey();
1248 $this->parser->mHeadings[] = array( $titleText, $bits['i'] );
1249 $serial = count( $this->parser->mHeadings ) - 1;
1250 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser::MARKER_SUFFIX;
1251 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1252 $this->parser->mStripState->addGeneral( $marker, '' );
1255 # Expand in virtual stack
1256 $newIterator = $contextNode->getChildren();
1259 # Generic recursive expansion
1260 $newIterator = $contextNode->getChildren();
1263 throw new MWException( __METHOD__.': Invalid parameter type' );
1266 if ( $newIterator !== false ) {
1268 $iteratorStack[] = $newIterator;
1270 } elseif ( $iteratorStack[$level] === false ) {
1271 // Return accumulated value to parent
1272 // With tail recursion
1273 while ( $iteratorStack[$level] === false && $level > 0 ) {
1274 $outStack[$level - 1] .= $out;
1275 array_pop( $outStack );
1276 array_pop( $iteratorStack );
1277 array_pop( $indexStack );
1283 return $outStack[0];
1291 function implodeWithFlags( $sep, $flags /*, ... */ ) {
1292 $args = array_slice( func_get_args(), 2 );
1296 foreach ( $args as $root ) {
1297 if ( $root instanceof PPNode_HipHop_Array ) {
1298 $root = $root->value;
1300 if ( !is_array( $root ) ) {
1301 $root = array( $root );
1303 foreach ( $root as $node ) {
1309 $s .= $this->expand( $node, $flags );
1316 * Implode with no flags specified
1317 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1321 function implode( $sep /*, ... */ ) {
1322 $args = array_slice( func_get_args(), 1 );
1326 foreach ( $args as $root ) {
1327 if ( $root instanceof PPNode_HipHop_Array ) {
1328 $root = $root->value;
1330 if ( !is_array( $root ) ) {
1331 $root = array( $root );
1333 foreach ( $root as $node ) {
1339 $s .= $this->expand( $node );
1346 * Makes an object that, when expand()ed, will be the same as one obtained
1350 * @return PPNode_HipHop_Array
1352 function virtualImplode( $sep /*, ... */ ) {
1353 $args = array_slice( func_get_args(), 1 );
1357 foreach ( $args as $root ) {
1358 if ( $root instanceof PPNode_HipHop_Array ) {
1359 $root = $root->value;
1361 if ( !is_array( $root ) ) {
1362 $root = array( $root );
1364 foreach ( $root as $node ) {
1373 return new PPNode_HipHop_Array( $out );
1377 * Virtual implode with brackets
1382 * @return PPNode_HipHop_Array
1384 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1385 $args = array_slice( func_get_args(), 3 );
1386 $out = array( $start );
1389 foreach ( $args as $root ) {
1390 if ( $root instanceof PPNode_HipHop_Array ) {
1391 $root = $root->value;
1393 if ( !is_array( $root ) ) {
1394 $root = array( $root );
1396 foreach ( $root as $node ) {
1406 return new PPNode_HipHop_Array( $out );
1409 function __toString() {
1414 * @param $level bool
1415 * @return array|bool|String
1417 function getPDBK( $level = false ) {
1418 if ( $level === false ) {
1419 return $this->title->getPrefixedDBkey();
1421 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1428 function getArguments() {
1435 function getNumberedArguments() {
1442 function getNamedArguments() {
1447 * Returns true if there are no arguments in this frame
1451 function isEmpty() {
1459 function getArgument( $name ) {
1464 * Returns true if the infinite loop check is OK, false if a loop is detected
1466 * @param $title Title
1470 function loopCheck( $title ) {
1471 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1475 * Return true if the frame is a template frame
1479 function isTemplate() {
1484 * Get a title of frame
1488 function getTitle() {
1489 return $this->title;
1494 * Expansion frame with template arguments
1497 class PPTemplateFrame_HipHop extends PPFrame_HipHop {
1498 var $numberedArgs, $namedArgs, $parent;
1499 var $numberedExpansionCache, $namedExpansionCache;
1502 * @param $preprocessor Preprocessor_HipHop
1503 * @param $parent bool
1504 * @param $numberedArgs array
1505 * @param $namedArgs array
1506 * @param $title Title|bool
1508 function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
1509 parent::__construct( $preprocessor );
1511 $this->parent = $parent;
1512 $this->numberedArgs = $numberedArgs;
1513 $this->namedArgs = $namedArgs;
1514 $this->title = $title;
1515 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1516 $this->titleCache = $parent->titleCache;
1517 $this->titleCache[] = $pdbk;
1518 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1519 if ( $pdbk !== false ) {
1520 $this->loopCheckHash[$pdbk] = true;
1522 $this->depth = $parent->depth + 1;
1523 $this->numberedExpansionCache = $this->namedExpansionCache = array();
1526 function __toString() {
1529 $args = $this->numberedArgs + $this->namedArgs;
1530 foreach ( $args as $name => $value ) {
1536 $s .= "\"$name\":\"" .
1537 str_replace( '"', '\\"', $value->__toString() ) . '"';
1543 * Returns true if there are no arguments in this frame
1547 function isEmpty() {
1548 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1554 function getArguments() {
1555 $arguments = array();
1556 foreach ( array_merge(
1557 array_keys($this->numberedArgs),
1558 array_keys($this->namedArgs)) as $key ) {
1559 $arguments[$key] = $this->getArgument($key);
1567 function getNumberedArguments() {
1568 $arguments = array();
1569 foreach ( array_keys($this->numberedArgs) as $key ) {
1570 $arguments[$key] = $this->getArgument($key);
1578 function getNamedArguments() {
1579 $arguments = array();
1580 foreach ( array_keys($this->namedArgs) as $key ) {
1581 $arguments[$key] = $this->getArgument($key);
1588 * @return array|bool
1590 function getNumberedArgument( $index ) {
1591 if ( !isset( $this->numberedArgs[$index] ) ) {
1594 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1595 # No trimming for unnamed arguments
1596 $this->numberedExpansionCache[$index] = $this->parent->expand( $this->numberedArgs[$index], PPFrame::STRIP_COMMENTS );
1598 return $this->numberedExpansionCache[$index];
1605 function getNamedArgument( $name ) {
1606 if ( !isset( $this->namedArgs[$name] ) ) {
1609 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1610 # Trim named arguments post-expand, for backwards compatibility
1611 $this->namedExpansionCache[$name] = trim(
1612 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
1614 return $this->namedExpansionCache[$name];
1619 * @return array|bool
1621 function getArgument( $name ) {
1622 $text = $this->getNumberedArgument( $name );
1623 if ( $text === false ) {
1624 $text = $this->getNamedArgument( $name );
1630 * Return true if the frame is a template frame
1634 function isTemplate() {
1640 * Expansion frame with custom arguments
1643 class PPCustomFrame_HipHop extends PPFrame_HipHop {
1646 function __construct( $preprocessor, $args ) {
1647 parent::__construct( $preprocessor );
1648 $this->args = $args;
1651 function __toString() {
1654 foreach ( $this->args as $name => $value ) {
1660 $s .= "\"$name\":\"" .
1661 str_replace( '"', '\\"', $value->__toString() ) . '"';
1670 function isEmpty() {
1671 return !count( $this->args );
1678 function getArgument( $index ) {
1679 if ( !isset( $this->args[$index] ) ) {
1682 return $this->args[$index];
1689 class PPNode_HipHop_Tree implements PPNode {
1690 var $name, $firstChild, $lastChild, $nextSibling;
1692 function __construct( $name ) {
1693 $this->name = $name;
1694 $this->firstChild = $this->lastChild = $this->nextSibling = false;
1697 function __toString() {
1700 for ( $node = $this->firstChild; $node; $node = $node->nextSibling ) {
1701 if ( $node instanceof PPNode_HipHop_Attr ) {
1702 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1704 $inner .= $node->__toString();
1707 if ( $inner === '' ) {
1708 return "<{$this->name}$attribs/>";
1710 return "<{$this->name}$attribs>$inner</{$this->name}>";
1717 * @return PPNode_HipHop_Tree
1719 static function newWithText( $name, $text ) {
1720 $obj = new self( $name );
1721 $obj->addChild( new PPNode_HipHop_Text( $text ) );
1725 function addChild( $node ) {
1726 if ( $this->lastChild === false ) {
1727 $this->firstChild = $this->lastChild = $node;
1729 $this->lastChild->nextSibling = $node;
1730 $this->lastChild = $node;
1735 * @return PPNode_HipHop_Array
1737 function getChildren() {
1738 $children = array();
1739 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1740 $children[] = $child;
1742 return new PPNode_HipHop_Array( $children );
1745 function getFirstChild() {
1746 return $this->firstChild;
1749 function getNextSibling() {
1750 return $this->nextSibling;
1754 * @param $name string
1757 function getChildrenOfType( $name ) {
1758 $children = array();
1759 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1760 if ( isset( $child->name ) && $child->name === $name ) {
1761 $children[] = $name;
1770 function getLength() {
1778 function item( $i ) {
1785 function getName() {
1790 * Split a <part> node into an associative array containing:
1792 * index String index
1793 * value PPNode value
1795 * @throws MWException
1798 function splitArg() {
1800 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1801 if ( !isset( $child->name ) ) {
1804 if ( $child->name === 'name' ) {
1805 $bits['name'] = $child;
1806 if ( $child->firstChild instanceof PPNode_HipHop_Attr
1807 && $child->firstChild->name === 'index' )
1809 $bits['index'] = $child->firstChild->value;
1811 } elseif ( $child->name === 'value' ) {
1812 $bits['value'] = $child;
1816 if ( !isset( $bits['name'] ) ) {
1817 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1819 if ( !isset( $bits['index'] ) ) {
1820 $bits['index'] = '';
1826 * Split an <ext> node into an associative array containing name, attr, inner and close
1827 * All values in the resulting array are PPNodes. Inner and close are optional.
1829 * @throws MWException
1832 function splitExt() {
1834 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1835 if ( !isset( $child->name ) ) {
1838 if ( $child->name === 'name' ) {
1839 $bits['name'] = $child;
1840 } elseif ( $child->name === 'attr' ) {
1841 $bits['attr'] = $child;
1842 } elseif ( $child->name === 'inner' ) {
1843 $bits['inner'] = $child;
1844 } elseif ( $child->name === 'close' ) {
1845 $bits['close'] = $child;
1848 if ( !isset( $bits['name'] ) ) {
1849 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1857 * @throws MWException
1860 function splitHeading() {
1861 if ( $this->name !== 'h' ) {
1862 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1865 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1866 if ( !isset( $child->name ) ) {
1869 if ( $child->name === 'i' ) {
1870 $bits['i'] = $child->value;
1871 } elseif ( $child->name === 'level' ) {
1872 $bits['level'] = $child->value;
1875 if ( !isset( $bits['i'] ) ) {
1876 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1882 * Split a <template> or <tplarg> node
1886 function splitTemplate() {
1888 $bits = array( 'lineStart' => '' );
1889 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1890 if ( !isset( $child->name ) ) {
1893 if ( $child->name === 'title' ) {
1894 $bits['title'] = $child;
1896 if ( $child->name === 'part' ) {
1899 if ( $child->name === 'lineStart' ) {
1900 $bits['lineStart'] = '1';
1903 if ( !isset( $bits['title'] ) ) {
1904 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
1906 $bits['parts'] = new PPNode_HipHop_Array( $parts );
1914 class PPNode_HipHop_Text implements PPNode {
1915 var $value, $nextSibling;
1917 function __construct( $value ) {
1918 if ( is_object( $value ) ) {
1919 throw new MWException( __CLASS__ . ' given object instead of string' );
1921 $this->value = $value;
1924 function __toString() {
1925 return htmlspecialchars( $this->value );
1928 function getNextSibling() {
1929 return $this->nextSibling;
1932 function getChildren() { return false; }
1933 function getFirstChild() { return false; }
1934 function getChildrenOfType( $name ) { return false; }
1935 function getLength() { return false; }
1936 function item( $i ) { return false; }
1937 function getName() { return '#text'; }
1938 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1939 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1940 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1946 class PPNode_HipHop_Array implements PPNode {
1947 var $value, $nextSibling;
1949 function __construct( $value ) {
1950 $this->value = $value;
1953 function __toString() {
1954 return var_export( $this, true );
1957 function getLength() {
1958 return count( $this->value );
1961 function item( $i ) {
1962 return $this->value[$i];
1965 function getName() { return '#nodelist'; }
1967 function getNextSibling() {
1968 return $this->nextSibling;
1971 function getChildren() { return false; }
1972 function getFirstChild() { return false; }
1973 function getChildrenOfType( $name ) { return false; }
1974 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1975 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1976 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1982 class PPNode_HipHop_Attr implements PPNode {
1983 var $name, $value, $nextSibling;
1985 function __construct( $name, $value ) {
1986 $this->name = $name;
1987 $this->value = $value;
1990 function __toString() {
1991 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
1994 function getName() {
1998 function getNextSibling() {
1999 return $this->nextSibling;
2002 function getChildren() { return false; }
2003 function getFirstChild() { return false; }
2004 function getChildrenOfType( $name ) { return false; }
2005 function getLength() { return false; }
2006 function item( $i ) { return false; }
2007 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
2008 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
2009 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }