3 * Preprocessor using PHP arrays
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Differences from DOM schema:
26 * * attribute nodes are children
27 * * "<h>" nodes that aren't at the top are replaced with <possible-h>
30 class Preprocessor_Hash
implements Preprocessor
{
37 const CACHE_VERSION
= 1;
39 public function __construct( $parser ) {
40 $this->parser
= $parser;
44 * @return PPFrame_Hash
46 public function newFrame() {
47 return new PPFrame_Hash( $this );
52 * @return PPCustomFrame_Hash
54 public function newCustomFrame( $args ) {
55 return new PPCustomFrame_Hash( $this, $args );
59 * @param array $values
60 * @return PPNode_Hash_Array
62 public function newPartNodeArray( $values ) {
65 foreach ( $values as $k => $val ) {
66 $partNode = new PPNode_Hash_Tree( 'part' );
67 $nameNode = new PPNode_Hash_Tree( 'name' );
70 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $k ) );
71 $partNode->addChild( $nameNode );
73 $nameNode->addChild( new PPNode_Hash_Text( $k ) );
74 $partNode->addChild( $nameNode );
75 $partNode->addChild( new PPNode_Hash_Text( '=' ) );
78 $valueNode = new PPNode_Hash_Tree( 'value' );
79 $valueNode->addChild( new PPNode_Hash_Text( $val ) );
80 $partNode->addChild( $valueNode );
85 $node = new PPNode_Hash_Array( $list );
90 * Preprocess some wikitext and return the document tree.
91 * This is the ghost of Parser::replace_variables().
93 * @param string $text The text to parse
94 * @param int $flags Bitwise combination of:
95 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<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_Hash_Tree
112 public function preprocessToObj( $text, $flags = 0 ) {
113 wfProfileIn( __METHOD__
);
116 global $wgMemc, $wgPreprocessorCacheThreshold;
118 $cacheable = $wgPreprocessorCacheThreshold !== false
119 && strlen( $text ) > $wgPreprocessorCacheThreshold;
122 wfProfileIn( __METHOD__
. '-cacheable' );
124 $cacheKey = wfMemcKey( 'preprocess-hash', md5( $text ), $flags );
125 $cacheValue = $wgMemc->get( $cacheKey );
127 $version = substr( $cacheValue, 0, 8 );
128 if ( intval( $version ) == self
::CACHE_VERSION
) {
129 $hash = unserialize( substr( $cacheValue, 8 ) );
131 wfDebugLog( "Preprocessor",
132 "Loaded preprocessor hash from memcached (key $cacheKey)" );
133 wfProfileOut( __METHOD__
. '-cacheable' );
134 wfProfileOut( __METHOD__
);
138 wfProfileIn( __METHOD__
. '-cache-miss' );
153 'names' => array( 2 => null ),
159 $forInclusion = $flags & Parser
::PTD_FOR_INCLUSION
;
161 $xmlishElements = $this->parser
->getStripList();
162 $enableOnlyinclude = false;
163 if ( $forInclusion ) {
164 $ignoredTags = array( 'includeonly', '/includeonly' );
165 $ignoredElements = array( 'noinclude' );
166 $xmlishElements[] = 'noinclude';
167 if ( strpos( $text, '<onlyinclude>' ) !== false
168 && strpos( $text, '</onlyinclude>' ) !== false
170 $enableOnlyinclude = true;
173 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
174 $ignoredElements = array( 'includeonly' );
175 $xmlishElements[] = 'includeonly';
177 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
179 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
180 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
182 $stack = new PPDStack_Hash
;
184 $searchBase = "[{<\n";
185 // For fast reverse searches
186 $revText = strrev( $text );
187 $lengthText = strlen( $text );
189 // Input pointer, starts out pointing to a pseudo-newline before the start
191 // Current accumulator
192 $accum =& $stack->getAccum();
193 // True to find equals signs in arguments
195 // True to take notice of pipe characters
198 // True if $i is inside a possible heading
200 // True if there are no more greater-than (>) signs right of $i
202 // True to ignore all input up to the next <onlyinclude>
203 $findOnlyinclude = $enableOnlyinclude;
204 // Do a line-start run without outputting an LF character
205 $fakeLineStart = true;
210 if ( $findOnlyinclude ) {
211 // Ignore all input up to the next <onlyinclude>
212 $startPos = strpos( $text, '<onlyinclude>', $i );
213 if ( $startPos === false ) {
214 // Ignored section runs to the end
215 $accum->addNodeWithText( 'ignore', substr( $text, $i ) );
218 $tagEndPos = $startPos +
strlen( '<onlyinclude>' ); // past-the-end
219 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i ) );
221 $findOnlyinclude = false;
224 if ( $fakeLineStart ) {
225 $found = 'line-start';
228 # Find next opening brace, closing brace or pipe
229 $search = $searchBase;
230 if ( $stack->top
=== false ) {
231 $currentClosing = '';
233 $currentClosing = $stack->top
->close
;
234 $search .= $currentClosing;
240 // First equals will be for the template
244 # Output literal section, advance input counter
245 $literalLength = strcspn( $text, $search, $i );
246 if ( $literalLength > 0 ) {
247 $accum->addLiteral( substr( $text, $i, $literalLength ) );
248 $i +
= $literalLength;
250 if ( $i >= $lengthText ) {
251 if ( $currentClosing == "\n" ) {
252 // Do a past-the-end run to finish off the heading
260 $curChar = $text[$i];
261 if ( $curChar == '|' ) {
263 } elseif ( $curChar == '=' ) {
265 } elseif ( $curChar == '<' ) {
267 } elseif ( $curChar == "\n" ) {
271 $found = 'line-start';
273 } elseif ( $curChar == $currentClosing ) {
275 } elseif ( isset( $rules[$curChar] ) ) {
277 $rule = $rules[$curChar];
279 # Some versions of PHP have a strcspn which stops on null characters
280 # Ignore and continue
287 if ( $found == 'angle' ) {
289 // Handle </onlyinclude>
290 if ( $enableOnlyinclude
291 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
293 $findOnlyinclude = true;
297 // Determine element name
298 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i +
1 ) ) {
299 // Element name missing or not listed
300 $accum->addLiteral( '<' );
305 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
307 // To avoid leaving blank lines, when a sequence of
308 // space-separated comments is both preceded and followed by
309 // a newline (ignoring spaces), then
310 // trim leading and trailing spaces and the trailing newline.
313 $endPos = strpos( $text, '-->', $i +
4 );
314 if ( $endPos === false ) {
315 // Unclosed comment in input, runs to end
316 $inner = substr( $text, $i );
317 $accum->addNodeWithText( 'comment', $inner );
320 // Search backwards for leading whitespace
321 $wsStart = $i ?
( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
323 // Search forwards for trailing whitespace
324 // $wsEnd will be the position of the last space (or the '>' if there's none)
325 $wsEnd = $endPos +
2 +
strspn( $text, " \t", $endPos +
3 );
327 // Keep looking forward as long as we're finding more
329 $comments = array( array( $wsStart, $wsEnd ) );
330 while ( substr( $text, $wsEnd +
1, 4 ) == '<!--' ) {
331 $c = strpos( $text, '-->', $wsEnd +
4 );
332 if ( $c === false ) {
335 $c = $c +
2 +
strspn( $text, " \t", $c +
3 );
336 $comments[] = array( $wsEnd +
1, $c );
340 // Eat the line if possible
341 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
342 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
343 // it's a possible beneficial b/c break.
344 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
345 && substr( $text, $wsEnd +
1, 1 ) == "\n"
347 // Remove leading whitespace from the end of the accumulator
348 // Sanity check first though
349 $wsLength = $i - $wsStart;
351 && $accum->lastNode
instanceof PPNode_Hash_Text
352 && strspn( $accum->lastNode
->value
, " \t", -$wsLength ) === $wsLength
354 $accum->lastNode
->value
= substr( $accum->lastNode
->value
, 0, -$wsLength );
357 // Dump all but the last comment to the accumulator
358 foreach ( $comments as $j => $com ) {
360 $endPos = $com[1] +
1;
361 if ( $j == ( count( $comments ) - 1 ) ) {
364 $inner = substr( $text, $startPos, $endPos - $startPos );
365 $accum->addNodeWithText( 'comment', $inner );
368 // Do a line-start run next time to look for headings after the comment
369 $fakeLineStart = true;
371 // No line to eat, just take the comment itself
377 $part = $stack->top
->getCurrentPart();
378 if ( !( isset( $part->commentEnd
) && $part->commentEnd
== $wsStart - 1 ) ) {
379 $part->visualEnd
= $wsStart;
381 // Else comments abutting, no change in visual end
382 $part->commentEnd
= $endPos;
385 $inner = substr( $text, $startPos, $endPos - $startPos +
1 );
386 $accum->addNodeWithText( 'comment', $inner );
391 $lowerName = strtolower( $name );
392 $attrStart = $i +
strlen( $name ) +
1;
395 $tagEndPos = $noMoreGT ?
false : strpos( $text, '>', $attrStart );
396 if ( $tagEndPos === false ) {
397 // Infinite backtrack
398 // Disable tag search to prevent worst-case O(N^2) performance
400 $accum->addLiteral( '<' );
405 // Handle ignored tags
406 if ( in_array( $lowerName, $ignoredTags ) ) {
407 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i +
1 ) );
413 if ( $text[$tagEndPos - 1] == '/' ) {
415 $attrEnd = $tagEndPos - 1;
420 $attrEnd = $tagEndPos;
422 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
423 $text, $matches, PREG_OFFSET_CAPTURE
, $tagEndPos +
1 )
425 $inner = substr( $text, $tagEndPos +
1, $matches[0][1] - $tagEndPos - 1 );
426 $i = $matches[0][1] +
strlen( $matches[0][0] );
427 $close = $matches[0][0];
429 // No end tag -- let it run out to the end of the text.
430 $inner = substr( $text, $tagEndPos +
1 );
435 // <includeonly> and <noinclude> just become <ignore> tags
436 if ( in_array( $lowerName, $ignoredElements ) ) {
437 $accum->addNodeWithText( 'ignore', substr( $text, $tagStartPos, $i - $tagStartPos ) );
441 if ( $attrEnd <= $attrStart ) {
444 // Note that the attr element contains the whitespace between name and attribute,
445 // this is necessary for precise reconstruction during pre-save transform.
446 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
449 $extNode = new PPNode_Hash_Tree( 'ext' );
450 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'name', $name ) );
451 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'attr', $attr ) );
452 if ( $inner !== null ) {
453 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'inner', $inner ) );
455 if ( $close !== null ) {
456 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'close', $close ) );
458 $accum->addNode( $extNode );
459 } elseif ( $found == 'line-start' ) {
460 // Is this the start of a heading?
461 // Line break belongs before the heading element in any case
462 if ( $fakeLineStart ) {
463 $fakeLineStart = false;
465 $accum->addLiteral( $curChar );
469 $count = strspn( $text, '=', $i, 6 );
470 if ( $count == 1 && $findEquals ) {
471 // DWIM: This looks kind of like a name/value separator.
472 // Let's let the equals handler have it and break the potential
473 // heading. This is heuristic, but AFAICT the methods for
474 // completely correct disambiguation are very complex.
475 } elseif ( $count > 0 ) {
479 'parts' => array( new PPDPart_Hash( str_repeat( '=', $count ) ) ),
482 $stack->push( $piece );
483 $accum =& $stack->getAccum();
484 extract( $stack->getFlags() );
487 } elseif ( $found == 'line-end' ) {
488 $piece = $stack->top
;
489 // A heading must be open, otherwise \n wouldn't have been in the search list
490 assert( '$piece->open == "\n"' );
491 $part = $piece->getCurrentPart();
492 // Search back through the input to see if it has a proper close.
493 // Do this using the reversed string since the other solutions
494 // (end anchor, etc.) are inefficient.
495 $wsLength = strspn( $revText, " \t", $lengthText - $i );
496 $searchStart = $i - $wsLength;
497 if ( isset( $part->commentEnd
) && $searchStart - 1 == $part->commentEnd
) {
498 // Comment found at line end
499 // Search for equals signs before the comment
500 $searchStart = $part->visualEnd
;
501 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
503 $count = $piece->count
;
504 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
505 if ( $equalsLength > 0 ) {
506 if ( $searchStart - $equalsLength == $piece->startPos
) {
507 // This is just a single string of equals signs on its own line
508 // Replicate the doHeadings behavior /={count}(.+)={count}/
509 // First find out how many equals signs there really are (don't stop at 6)
510 $count = $equalsLength;
514 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
517 $count = min( $equalsLength, $count );
520 // Normal match, output <h>
521 $element = new PPNode_Hash_Tree( 'possible-h' );
522 $element->addChild( new PPNode_Hash_Attr( 'level', $count ) );
523 $element->addChild( new PPNode_Hash_Attr( 'i', $headingIndex++
) );
524 $element->lastChild
->nextSibling
= $accum->firstNode
;
525 $element->lastChild
= $accum->lastNode
;
527 // Single equals sign on its own line, count=0
531 // No match, no <h>, just pass down the inner text
536 $accum =& $stack->getAccum();
537 extract( $stack->getFlags() );
539 // Append the result to the enclosing accumulator
540 if ( $element instanceof PPNode
) {
541 $accum->addNode( $element );
543 $accum->addAccum( $element );
545 // Note that we do NOT increment the input pointer.
546 // This is because the closing linebreak could be the opening linebreak of
547 // another heading. Infinite loops are avoided because the next iteration MUST
548 // hit the heading open case above, which unconditionally increments the
550 } elseif ( $found == 'open' ) {
551 # count opening brace characters
552 $count = strspn( $text, $curChar, $i );
554 # we need to add to stack only if opening brace count is enough for one of the rules
555 if ( $count >= $rule['min'] ) {
556 # Add it to the stack
559 'close' => $rule['end'],
561 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
564 $stack->push( $piece );
565 $accum =& $stack->getAccum();
566 extract( $stack->getFlags() );
568 # Add literal brace(s)
569 $accum->addLiteral( str_repeat( $curChar, $count ) );
572 } elseif ( $found == 'close' ) {
573 $piece = $stack->top
;
574 # lets check if there are enough characters for closing brace
575 $maxCount = $piece->count
;
576 $count = strspn( $text, $curChar, $i, $maxCount );
578 # check for maximum matching characters (if there are 5 closing
579 # characters, we will probably need only 3 - depending on the rules)
580 $rule = $rules[$piece->open
];
581 if ( $count > $rule['max'] ) {
582 # The specified maximum exists in the callback array, unless the caller
584 $matchingCount = $rule['max'];
586 # Count is less than the maximum
587 # Skip any gaps in the callback array to find the true largest match
588 # Need to use array_key_exists not isset because the callback can be null
589 $matchingCount = $count;
590 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
595 if ( $matchingCount <= 0 ) {
596 # No matching element found in callback array
597 # Output a literal closing brace and continue
598 $accum->addLiteral( str_repeat( $curChar, $count ) );
602 $name = $rule['names'][$matchingCount];
603 if ( $name === null ) {
604 // No element, just literal text
605 $element = $piece->breakSyntax( $matchingCount );
606 $element->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
609 # Note: $parts is already XML, does not need to be encoded further
610 $parts = $piece->parts
;
611 $titleAccum = $parts[0]->out
;
614 $element = new PPNode_Hash_Tree( $name );
616 # The invocation is at the start of the line if lineStart is set in
617 # the stack, and all opening brackets are used up.
618 if ( $maxCount == $matchingCount && !empty( $piece->lineStart
) ) {
619 $element->addChild( new PPNode_Hash_Attr( 'lineStart', 1 ) );
621 $titleNode = new PPNode_Hash_Tree( 'title' );
622 $titleNode->firstChild
= $titleAccum->firstNode
;
623 $titleNode->lastChild
= $titleAccum->lastNode
;
624 $element->addChild( $titleNode );
626 foreach ( $parts as $part ) {
627 if ( isset( $part->eqpos
) ) {
630 for ( $node = $part->out
->firstNode
; $node; $node = $node->nextSibling
) {
631 if ( $node === $part->eqpos
) {
638 wfProfileOut( __METHOD__
. '-cache-miss' );
639 wfProfileOut( __METHOD__
. '-cacheable' );
641 wfProfileOut( __METHOD__
);
642 throw new MWException( __METHOD__
. ': eqpos not found' );
644 if ( $node->name
!== 'equals' ) {
646 wfProfileOut( __METHOD__
. '-cache-miss' );
647 wfProfileOut( __METHOD__
. '-cacheable' );
649 wfProfileOut( __METHOD__
);
650 throw new MWException( __METHOD__
. ': eqpos is not equals' );
654 // Construct name node
655 $nameNode = new PPNode_Hash_Tree( 'name' );
656 if ( $lastNode !== false ) {
657 $lastNode->nextSibling
= false;
658 $nameNode->firstChild
= $part->out
->firstNode
;
659 $nameNode->lastChild
= $lastNode;
662 // Construct value node
663 $valueNode = new PPNode_Hash_Tree( 'value' );
664 if ( $equalsNode->nextSibling
!== false ) {
665 $valueNode->firstChild
= $equalsNode->nextSibling
;
666 $valueNode->lastChild
= $part->out
->lastNode
;
668 $partNode = new PPNode_Hash_Tree( 'part' );
669 $partNode->addChild( $nameNode );
670 $partNode->addChild( $equalsNode->firstChild
);
671 $partNode->addChild( $valueNode );
672 $element->addChild( $partNode );
674 $partNode = new PPNode_Hash_Tree( 'part' );
675 $nameNode = new PPNode_Hash_Tree( 'name' );
676 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $argIndex++
) );
677 $valueNode = new PPNode_Hash_Tree( 'value' );
678 $valueNode->firstChild
= $part->out
->firstNode
;
679 $valueNode->lastChild
= $part->out
->lastNode
;
680 $partNode->addChild( $nameNode );
681 $partNode->addChild( $valueNode );
682 $element->addChild( $partNode );
687 # Advance input pointer
688 $i +
= $matchingCount;
692 $accum =& $stack->getAccum();
694 # Re-add the old stack element if it still has unmatched opening characters remaining
695 if ( $matchingCount < $piece->count
) {
696 $piece->parts
= array( new PPDPart_Hash
);
697 $piece->count
-= $matchingCount;
698 # do we still qualify for any callback with remaining count?
699 $min = $rules[$piece->open
]['min'];
700 if ( $piece->count
>= $min ) {
701 $stack->push( $piece );
702 $accum =& $stack->getAccum();
704 $accum->addLiteral( str_repeat( $piece->open
, $piece->count
) );
708 extract( $stack->getFlags() );
710 # Add XML element to the enclosing accumulator
711 if ( $element instanceof PPNode
) {
712 $accum->addNode( $element );
714 $accum->addAccum( $element );
716 } elseif ( $found == 'pipe' ) {
717 $findEquals = true; // shortcut for getFlags()
719 $accum =& $stack->getAccum();
721 } elseif ( $found == 'equals' ) {
722 $findEquals = false; // shortcut for getFlags()
723 $accum->addNodeWithText( 'equals', '=' );
724 $stack->getCurrentPart()->eqpos
= $accum->lastNode
;
729 # Output any remaining unclosed brackets
730 foreach ( $stack->stack
as $piece ) {
731 $stack->rootAccum
->addAccum( $piece->breakSyntax() );
734 # Enable top-level headings
735 for ( $node = $stack->rootAccum
->firstNode
; $node; $node = $node->nextSibling
) {
736 if ( isset( $node->name
) && $node->name
=== 'possible-h' ) {
741 $rootNode = new PPNode_Hash_Tree( 'root' );
742 $rootNode->firstChild
= $stack->rootAccum
->firstNode
;
743 $rootNode->lastChild
= $stack->rootAccum
->lastNode
;
747 $cacheValue = sprintf( "%08d", self
::CACHE_VERSION
) . serialize( $rootNode );
748 $wgMemc->set( $cacheKey, $cacheValue, 86400 );
749 wfProfileOut( __METHOD__
. '-cache-miss' );
750 wfProfileOut( __METHOD__
. '-cacheable' );
751 wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
754 wfProfileOut( __METHOD__
);
760 * Stack class to help Preprocessor::preprocessToObj()
763 class PPDStack_Hash
extends PPDStack
{
764 public function __construct() {
765 $this->elementClass
= 'PPDStackElement_Hash';
766 parent
::__construct();
767 $this->rootAccum
= new PPDAccum_Hash
;
774 class PPDStackElement_Hash
extends PPDStackElement
{
775 public function __construct( $data = array() ) {
776 $this->partClass
= 'PPDPart_Hash';
777 parent
::__construct( $data );
781 * Get the accumulator that would result if the close is not found.
783 * @param int|bool $openingCount
784 * @return PPDAccum_Hash
786 public function breakSyntax( $openingCount = false ) {
787 if ( $this->open
== "\n" ) {
788 $accum = $this->parts
[0]->out
;
790 if ( $openingCount === false ) {
791 $openingCount = $this->count
;
793 $accum = new PPDAccum_Hash
;
794 $accum->addLiteral( str_repeat( $this->open
, $openingCount ) );
796 foreach ( $this->parts
as $part ) {
800 $accum->addLiteral( '|' );
802 $accum->addAccum( $part->out
);
812 class PPDPart_Hash
extends PPDPart
{
813 public function __construct( $out = '' ) {
814 $accum = new PPDAccum_Hash
;
816 $accum->addLiteral( $out );
818 parent
::__construct( $accum );
825 class PPDAccum_Hash
{
826 public $firstNode, $lastNode;
828 public function __construct() {
829 $this->firstNode
= $this->lastNode
= false;
833 * Append a string literal
836 public function addLiteral( $s ) {
837 if ( $this->lastNode
=== false ) {
838 $this->firstNode
= $this->lastNode
= new PPNode_Hash_Text( $s );
839 } elseif ( $this->lastNode
instanceof PPNode_Hash_Text
) {
840 $this->lastNode
->value
.= $s;
842 $this->lastNode
->nextSibling
= new PPNode_Hash_Text( $s );
843 $this->lastNode
= $this->lastNode
->nextSibling
;
849 * @param PPNode $node
851 public function addNode( PPNode
$node ) {
852 if ( $this->lastNode
=== false ) {
853 $this->firstNode
= $this->lastNode
= $node;
855 $this->lastNode
->nextSibling
= $node;
856 $this->lastNode
= $node;
861 * Append a tree node with text contents
862 * @param string $name
863 * @param string $value
865 public function addNodeWithText( $name, $value ) {
866 $node = PPNode_Hash_Tree
::newWithText( $name, $value );
867 $this->addNode( $node );
871 * Append a PPDAccum_Hash
872 * Takes over ownership of the nodes in the source argument. These nodes may
873 * subsequently be modified, especially nextSibling.
874 * @param PPDAccum_Hash $accum
876 public function addAccum( $accum ) {
877 if ( $accum->lastNode
=== false ) {
879 } elseif ( $this->lastNode
=== false ) {
880 $this->firstNode
= $accum->firstNode
;
881 $this->lastNode
= $accum->lastNode
;
883 $this->lastNode
->nextSibling
= $accum->firstNode
;
884 $this->lastNode
= $accum->lastNode
;
890 * An expansion frame, used as a context to expand the result of preprocessToObj()
893 class PPFrame_Hash
implements PPFrame
{
903 public $preprocessor;
912 * Hashtable listing templates which are disallowed for expansion in this frame,
913 * having been encountered previously in parent frames.
915 public $loopCheckHash;
918 * Recursion depth of this frame, top = 0
919 * Note that this is NOT the same as expansion depth in expand()
923 private $volatile = false;
929 protected $childExpansionCache;
932 * Construct a new preprocessor frame.
933 * @param Preprocessor $preprocessor The parent preprocessor
935 public function __construct( $preprocessor ) {
936 $this->preprocessor
= $preprocessor;
937 $this->parser
= $preprocessor->parser
;
938 $this->title
= $this->parser
->mTitle
;
939 $this->titleCache
= array( $this->title ?
$this->title
->getPrefixedDBkey() : false );
940 $this->loopCheckHash
= array();
942 $this->childExpansionCache
= array();
946 * Create a new child frame
947 * $args is optionally a multi-root PPNode or array containing the template arguments
949 * @param array|bool|PPNode_Hash_Array $args
950 * @param Title|bool $title
951 * @param int $indexOffset
952 * @throws MWException
953 * @return PPTemplateFrame_Hash
955 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
956 $namedArgs = array();
957 $numberedArgs = array();
958 if ( $title === false ) {
959 $title = $this->title
;
961 if ( $args !== false ) {
962 if ( $args instanceof PPNode_Hash_Array
) {
963 $args = $args->value
;
964 } elseif ( !is_array( $args ) ) {
965 throw new MWException( __METHOD__
. ': $args must be array or PPNode_Hash_Array' );
967 foreach ( $args as $arg ) {
968 $bits = $arg->splitArg();
969 if ( $bits['index'] !== '' ) {
970 // Numbered parameter
971 $index = $bits['index'] - $indexOffset;
972 $numberedArgs[$index] = $bits['value'];
973 unset( $namedArgs[$index] );
976 $name = trim( $this->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
977 $namedArgs[$name] = $bits['value'];
978 unset( $numberedArgs[$name] );
982 return new PPTemplateFrame_Hash( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
986 * @throws MWException
987 * @param string|int $key
988 * @param string|PPNode_Hash|DOMDocument $root
992 public function cachedExpand( $key, $root, $flags = 0 ) {
993 // we don't have a parent, so we don't have a cache
994 return $this->expand( $root, $flags );
998 * @throws MWException
999 * @param string|PPNode $root
1003 public function expand( $root, $flags = 0 ) {
1004 static $expansionDepth = 0;
1005 if ( is_string( $root ) ) {
1009 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
1010 $this->parser
->limitationWarn( 'node-count-exceeded',
1011 $this->parser
->mPPNodeCount
,
1012 $this->parser
->mOptions
->getMaxPPNodeCount()
1014 return '<span class="error">Node-count limit exceeded</span>';
1016 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
1017 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
1019 $this->parser
->mOptions
->getMaxPPExpandDepth()
1021 return '<span class="error">Expansion depth limit exceeded</span>';
1024 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
1025 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
1028 $outStack = array( '', '' );
1029 $iteratorStack = array( false, $root );
1030 $indexStack = array( 0, 0 );
1032 while ( count( $iteratorStack ) > 1 ) {
1033 $level = count( $outStack ) - 1;
1034 $iteratorNode =& $iteratorStack[$level];
1035 $out =& $outStack[$level];
1036 $index =& $indexStack[$level];
1038 if ( is_array( $iteratorNode ) ) {
1039 if ( $index >= count( $iteratorNode ) ) {
1040 // All done with this iterator
1041 $iteratorStack[$level] = false;
1042 $contextNode = false;
1044 $contextNode = $iteratorNode[$index];
1047 } elseif ( $iteratorNode instanceof PPNode_Hash_Array
) {
1048 if ( $index >= $iteratorNode->getLength() ) {
1049 // All done with this iterator
1050 $iteratorStack[$level] = false;
1051 $contextNode = false;
1053 $contextNode = $iteratorNode->item( $index );
1057 // Copy to $contextNode and then delete from iterator stack,
1058 // because this is not an iterator but we do have to execute it once
1059 $contextNode = $iteratorStack[$level];
1060 $iteratorStack[$level] = false;
1063 $newIterator = false;
1065 if ( $contextNode === false ) {
1067 } elseif ( is_string( $contextNode ) ) {
1068 $out .= $contextNode;
1069 } elseif ( is_array( $contextNode ) ||
$contextNode instanceof PPNode_Hash_Array
) {
1070 $newIterator = $contextNode;
1071 } elseif ( $contextNode instanceof PPNode_Hash_Attr
) {
1073 } elseif ( $contextNode instanceof PPNode_Hash_Text
) {
1074 $out .= $contextNode->value
;
1075 } elseif ( $contextNode instanceof PPNode_Hash_Tree
) {
1076 if ( $contextNode->name
== 'template' ) {
1077 # Double-brace expansion
1078 $bits = $contextNode->splitTemplate();
1079 if ( $flags & PPFrame
::NO_TEMPLATES
) {
1080 $newIterator = $this->virtualBracketedImplode(
1086 $ret = $this->parser
->braceSubstitution( $bits, $this );
1087 if ( isset( $ret['object'] ) ) {
1088 $newIterator = $ret['object'];
1090 $out .= $ret['text'];
1093 } elseif ( $contextNode->name
== 'tplarg' ) {
1094 # Triple-brace expansion
1095 $bits = $contextNode->splitTemplate();
1096 if ( $flags & PPFrame
::NO_ARGS
) {
1097 $newIterator = $this->virtualBracketedImplode(
1103 $ret = $this->parser
->argSubstitution( $bits, $this );
1104 if ( isset( $ret['object'] ) ) {
1105 $newIterator = $ret['object'];
1107 $out .= $ret['text'];
1110 } elseif ( $contextNode->name
== 'comment' ) {
1111 # HTML-style comment
1112 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1113 if ( $this->parser
->ot
['html']
1114 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
1115 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
1118 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
1119 # Add a strip marker in PST mode so that pstPass2() can
1120 # run some old-fashioned regexes on the result.
1121 # Not in RECOVER_COMMENTS mode (extractSections) though.
1122 $out .= $this->parser
->insertStripItem( $contextNode->firstChild
->value
);
1124 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1125 $out .= $contextNode->firstChild
->value
;
1127 } elseif ( $contextNode->name
== 'ignore' ) {
1128 # Output suppression used by <includeonly> etc.
1129 # OT_WIKI will only respect <ignore> in substed templates.
1130 # The other output types respect it unless NO_IGNORE is set.
1131 # extractSections() sets NO_IGNORE and so never respects it.
1132 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] )
1133 ||
( $flags & PPFrame
::NO_IGNORE
)
1135 $out .= $contextNode->firstChild
->value
;
1139 } elseif ( $contextNode->name
== 'ext' ) {
1141 $bits = $contextNode->splitExt() +
array( 'attr' => null, 'inner' => null, 'close' => null );
1142 if ( $flags & PPFrame
::NO_TAGS
) {
1143 $s = '<' . $bits['name']->firstChild
->value
;
1144 if ( $bits['attr'] ) {
1145 $s .= $bits['attr']->firstChild
->value
;
1147 if ( $bits['inner'] ) {
1148 $s .= '>' . $bits['inner']->firstChild
->value
;
1149 if ( $bits['close'] ) {
1150 $s .= $bits['close']->firstChild
->value
;
1157 $out .= $this->parser
->extensionSubstitution( $bits, $this );
1159 } elseif ( $contextNode->name
== 'h' ) {
1161 if ( $this->parser
->ot
['html'] ) {
1162 # Expand immediately and insert heading index marker
1164 for ( $node = $contextNode->firstChild
; $node; $node = $node->nextSibling
) {
1165 $s .= $this->expand( $node, $flags );
1168 $bits = $contextNode->splitHeading();
1169 $titleText = $this->title
->getPrefixedDBkey();
1170 $this->parser
->mHeadings
[] = array( $titleText, $bits['i'] );
1171 $serial = count( $this->parser
->mHeadings
) - 1;
1172 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser
::MARKER_SUFFIX
;
1173 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1174 $this->parser
->mStripState
->addGeneral( $marker, '' );
1177 # Expand in virtual stack
1178 $newIterator = $contextNode->getChildren();
1181 # Generic recursive expansion
1182 $newIterator = $contextNode->getChildren();
1185 throw new MWException( __METHOD__
. ': Invalid parameter type' );
1188 if ( $newIterator !== false ) {
1190 $iteratorStack[] = $newIterator;
1192 } elseif ( $iteratorStack[$level] === false ) {
1193 // Return accumulated value to parent
1194 // With tail recursion
1195 while ( $iteratorStack[$level] === false && $level > 0 ) {
1196 $outStack[$level - 1] .= $out;
1197 array_pop( $outStack );
1198 array_pop( $iteratorStack );
1199 array_pop( $indexStack );
1205 return $outStack[0];
1209 * @param string $sep
1213 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1214 $args = array_slice( func_get_args(), 2 );
1218 foreach ( $args as $root ) {
1219 if ( $root instanceof PPNode_Hash_Array
) {
1220 $root = $root->value
;
1222 if ( !is_array( $root ) ) {
1223 $root = array( $root );
1225 foreach ( $root as $node ) {
1231 $s .= $this->expand( $node, $flags );
1238 * Implode with no flags specified
1239 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1240 * @param string $sep
1243 public function implode( $sep /*, ... */ ) {
1244 $args = array_slice( func_get_args(), 1 );
1248 foreach ( $args as $root ) {
1249 if ( $root instanceof PPNode_Hash_Array
) {
1250 $root = $root->value
;
1252 if ( !is_array( $root ) ) {
1253 $root = array( $root );
1255 foreach ( $root as $node ) {
1261 $s .= $this->expand( $node );
1268 * Makes an object that, when expand()ed, will be the same as one obtained
1271 * @param string $sep
1272 * @return PPNode_Hash_Array
1274 public function virtualImplode( $sep /*, ... */ ) {
1275 $args = array_slice( func_get_args(), 1 );
1279 foreach ( $args as $root ) {
1280 if ( $root instanceof PPNode_Hash_Array
) {
1281 $root = $root->value
;
1283 if ( !is_array( $root ) ) {
1284 $root = array( $root );
1286 foreach ( $root as $node ) {
1295 return new PPNode_Hash_Array( $out );
1299 * Virtual implode with brackets
1301 * @param string $start
1302 * @param string $sep
1303 * @param string $end
1304 * @return PPNode_Hash_Array
1306 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1307 $args = array_slice( func_get_args(), 3 );
1308 $out = array( $start );
1311 foreach ( $args as $root ) {
1312 if ( $root instanceof PPNode_Hash_Array
) {
1313 $root = $root->value
;
1315 if ( !is_array( $root ) ) {
1316 $root = array( $root );
1318 foreach ( $root as $node ) {
1328 return new PPNode_Hash_Array( $out );
1331 public function __toString() {
1336 * @param bool $level
1337 * @return array|bool|string
1339 public function getPDBK( $level = false ) {
1340 if ( $level === false ) {
1341 return $this->title
->getPrefixedDBkey();
1343 return isset( $this->titleCache
[$level] ) ?
$this->titleCache
[$level] : false;
1350 public function getArguments() {
1357 public function getNumberedArguments() {
1364 public function getNamedArguments() {
1369 * Returns true if there are no arguments in this frame
1373 public function isEmpty() {
1378 * @param string $name
1381 public function getArgument( $name ) {
1386 * Returns true if the infinite loop check is OK, false if a loop is detected
1388 * @param Title $title
1392 public function loopCheck( $title ) {
1393 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
1397 * Return true if the frame is a template frame
1401 public function isTemplate() {
1406 * Get a title of frame
1410 public function getTitle() {
1411 return $this->title
;
1415 * Set the volatile flag
1419 public function setVolatile( $flag = true ) {
1420 $this->volatile
= $flag;
1424 * Get the volatile flag
1428 public function isVolatile() {
1429 return $this->volatile
;
1437 public function setTTL( $ttl ) {
1438 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
1448 public function getTTL() {
1454 * Expansion frame with template arguments
1457 class PPTemplateFrame_Hash
extends PPFrame_Hash
{
1458 public $numberedArgs, $namedArgs, $parent;
1459 public $numberedExpansionCache, $namedExpansionCache;
1462 * @param Preprocessor $preprocessor
1463 * @param bool|PPFrame $parent
1464 * @param array $numberedArgs
1465 * @param array $namedArgs
1466 * @param bool|Title $title
1468 public function __construct( $preprocessor, $parent = false, $numberedArgs = array(),
1469 $namedArgs = array(), $title = false
1471 parent
::__construct( $preprocessor );
1473 $this->parent
= $parent;
1474 $this->numberedArgs
= $numberedArgs;
1475 $this->namedArgs
= $namedArgs;
1476 $this->title
= $title;
1477 $pdbk = $title ?
$title->getPrefixedDBkey() : false;
1478 $this->titleCache
= $parent->titleCache
;
1479 $this->titleCache
[] = $pdbk;
1480 $this->loopCheckHash
= /*clone*/ $parent->loopCheckHash
;
1481 if ( $pdbk !== false ) {
1482 $this->loopCheckHash
[$pdbk] = true;
1484 $this->depth
= $parent->depth +
1;
1485 $this->numberedExpansionCache
= $this->namedExpansionCache
= array();
1488 public function __toString() {
1491 $args = $this->numberedArgs +
$this->namedArgs
;
1492 foreach ( $args as $name => $value ) {
1498 $s .= "\"$name\":\"" .
1499 str_replace( '"', '\\"', $value->__toString() ) . '"';
1506 * @throws MWException
1507 * @param string|int $key
1508 * @param string|PPNode_Hash|DOMDocument $root
1512 public function cachedExpand( $key, $root, $flags = 0 ) {
1513 if ( isset( $this->parent
->childExpansionCache
[$key] ) ) {
1514 return $this->parent
->childExpansionCache
[$key];
1516 $retval = $this->expand( $root, $flags );
1517 if ( !$this->isVolatile() ) {
1518 $this->parent
->childExpansionCache
[$key] = $retval;
1524 * Returns true if there are no arguments in this frame
1528 public function isEmpty() {
1529 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
1535 public function getArguments() {
1536 $arguments = array();
1537 foreach ( array_merge(
1538 array_keys( $this->numberedArgs
),
1539 array_keys( $this->namedArgs
) ) as $key ) {
1540 $arguments[$key] = $this->getArgument( $key );
1548 public function getNumberedArguments() {
1549 $arguments = array();
1550 foreach ( array_keys( $this->numberedArgs
) as $key ) {
1551 $arguments[$key] = $this->getArgument( $key );
1559 public function getNamedArguments() {
1560 $arguments = array();
1561 foreach ( array_keys( $this->namedArgs
) as $key ) {
1562 $arguments[$key] = $this->getArgument( $key );
1569 * @return array|bool
1571 public function getNumberedArgument( $index ) {
1572 if ( !isset( $this->numberedArgs
[$index] ) ) {
1575 if ( !isset( $this->numberedExpansionCache
[$index] ) ) {
1576 # No trimming for unnamed arguments
1577 $this->numberedExpansionCache
[$index] = $this->parent
->expand(
1578 $this->numberedArgs
[$index],
1579 PPFrame
::STRIP_COMMENTS
1582 return $this->numberedExpansionCache
[$index];
1586 * @param string $name
1589 public function getNamedArgument( $name ) {
1590 if ( !isset( $this->namedArgs
[$name] ) ) {
1593 if ( !isset( $this->namedExpansionCache
[$name] ) ) {
1594 # Trim named arguments post-expand, for backwards compatibility
1595 $this->namedExpansionCache
[$name] = trim(
1596 $this->parent
->expand( $this->namedArgs
[$name], PPFrame
::STRIP_COMMENTS
) );
1598 return $this->namedExpansionCache
[$name];
1602 * @param string $name
1603 * @return array|bool
1605 public function getArgument( $name ) {
1606 $text = $this->getNumberedArgument( $name );
1607 if ( $text === false ) {
1608 $text = $this->getNamedArgument( $name );
1614 * Return true if the frame is a template frame
1618 public function isTemplate() {
1622 public function setVolatile( $flag = true ) {
1623 parent
::setVolatile( $flag );
1624 $this->parent
->setVolatile( $flag );
1627 public function setTTL( $ttl ) {
1628 parent
::setTTL( $ttl );
1629 $this->parent
->setTTL( $ttl );
1634 * Expansion frame with custom arguments
1637 class PPCustomFrame_Hash
extends PPFrame_Hash
{
1640 public function __construct( $preprocessor, $args ) {
1641 parent
::__construct( $preprocessor );
1642 $this->args
= $args;
1645 public function __toString() {
1648 foreach ( $this->args
as $name => $value ) {
1654 $s .= "\"$name\":\"" .
1655 str_replace( '"', '\\"', $value->__toString() ) . '"';
1664 public function isEmpty() {
1665 return !count( $this->args
);
1672 public function getArgument( $index ) {
1673 if ( !isset( $this->args
[$index] ) ) {
1676 return $this->args
[$index];
1679 public function getArguments() {
1687 class PPNode_Hash_Tree
implements PPNode
{
1688 public $name, $firstChild, $lastChild, $nextSibling;
1690 public function __construct( $name ) {
1691 $this->name
= $name;
1692 $this->firstChild
= $this->lastChild
= $this->nextSibling
= false;
1695 public function __toString() {
1698 for ( $node = $this->firstChild
; $node; $node = $node->nextSibling
) {
1699 if ( $node instanceof PPNode_Hash_Attr
) {
1700 $attribs .= ' ' . $node->name
. '="' . htmlspecialchars( $node->value
) . '"';
1702 $inner .= $node->__toString();
1705 if ( $inner === '' ) {
1706 return "<{$this->name}$attribs/>";
1708 return "<{$this->name}$attribs>$inner</{$this->name}>";
1713 * @param string $name
1714 * @param string $text
1715 * @return PPNode_Hash_Tree
1717 public static function newWithText( $name, $text ) {
1718 $obj = new self( $name );
1719 $obj->addChild( new PPNode_Hash_Text( $text ) );
1723 public function addChild( $node ) {
1724 if ( $this->lastChild
=== false ) {
1725 $this->firstChild
= $this->lastChild
= $node;
1727 $this->lastChild
->nextSibling
= $node;
1728 $this->lastChild
= $node;
1733 * @return PPNode_Hash_Array
1735 public function getChildren() {
1736 $children = array();
1737 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1738 $children[] = $child;
1740 return new PPNode_Hash_Array( $children );
1743 public function getFirstChild() {
1744 return $this->firstChild
;
1747 public function getNextSibling() {
1748 return $this->nextSibling
;
1751 public function getChildrenOfType( $name ) {
1752 $children = array();
1753 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1754 if ( isset( $child->name
) && $child->name
=== $name ) {
1755 $children[] = $child;
1764 public function getLength() {
1772 public function item( $i ) {
1779 public function getName() {
1784 * Split a "<part>" node into an associative array containing:
1785 * - name PPNode name
1786 * - index String index
1787 * - value PPNode value
1789 * @throws MWException
1792 public function splitArg() {
1794 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1795 if ( !isset( $child->name
) ) {
1798 if ( $child->name
=== 'name' ) {
1799 $bits['name'] = $child;
1800 if ( $child->firstChild
instanceof PPNode_Hash_Attr
1801 && $child->firstChild
->name
=== 'index'
1803 $bits['index'] = $child->firstChild
->value
;
1805 } elseif ( $child->name
=== 'value' ) {
1806 $bits['value'] = $child;
1810 if ( !isset( $bits['name'] ) ) {
1811 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
1813 if ( !isset( $bits['index'] ) ) {
1814 $bits['index'] = '';
1820 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1821 * All values in the resulting array are PPNodes. Inner and close are optional.
1823 * @throws MWException
1826 public function splitExt() {
1828 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1829 if ( !isset( $child->name
) ) {
1832 if ( $child->name
== 'name' ) {
1833 $bits['name'] = $child;
1834 } elseif ( $child->name
== 'attr' ) {
1835 $bits['attr'] = $child;
1836 } elseif ( $child->name
== 'inner' ) {
1837 $bits['inner'] = $child;
1838 } elseif ( $child->name
== 'close' ) {
1839 $bits['close'] = $child;
1842 if ( !isset( $bits['name'] ) ) {
1843 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
1849 * Split an "<h>" node
1851 * @throws MWException
1854 public function splitHeading() {
1855 if ( $this->name
!== 'h' ) {
1856 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1859 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1860 if ( !isset( $child->name
) ) {
1863 if ( $child->name
== 'i' ) {
1864 $bits['i'] = $child->value
;
1865 } elseif ( $child->name
== 'level' ) {
1866 $bits['level'] = $child->value
;
1869 if ( !isset( $bits['i'] ) ) {
1870 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1876 * Split a "<template>" or "<tplarg>" node
1878 * @throws MWException
1881 public function splitTemplate() {
1883 $bits = array( 'lineStart' => '' );
1884 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1885 if ( !isset( $child->name
) ) {
1888 if ( $child->name
== 'title' ) {
1889 $bits['title'] = $child;
1891 if ( $child->name
== 'part' ) {
1894 if ( $child->name
== 'lineStart' ) {
1895 $bits['lineStart'] = '1';
1898 if ( !isset( $bits['title'] ) ) {
1899 throw new MWException( 'Invalid node passed to ' . __METHOD__
);
1901 $bits['parts'] = new PPNode_Hash_Array( $parts );
1909 class PPNode_Hash_Text
implements PPNode
{
1910 public $value, $nextSibling;
1912 public function __construct( $value ) {
1913 if ( is_object( $value ) ) {
1914 throw new MWException( __CLASS__
. ' given object instead of string' );
1916 $this->value
= $value;
1919 public function __toString() {
1920 return htmlspecialchars( $this->value
);
1923 public function getNextSibling() {
1924 return $this->nextSibling
;
1927 public function getChildren() {
1931 public function getFirstChild() {
1935 public function getChildrenOfType( $name ) {
1939 public function getLength() {
1943 public function item( $i ) {
1947 public function getName() {
1951 public function splitArg() {
1952 throw new MWException( __METHOD__
. ': not supported' );
1955 public function splitExt() {
1956 throw new MWException( __METHOD__
. ': not supported' );
1959 public function splitHeading() {
1960 throw new MWException( __METHOD__
. ': not supported' );
1967 class PPNode_Hash_Array
implements PPNode
{
1968 public $value, $nextSibling;
1970 public function __construct( $value ) {
1971 $this->value
= $value;
1974 public function __toString() {
1975 return var_export( $this, true );
1978 public function getLength() {
1979 return count( $this->value
);
1982 public function item( $i ) {
1983 return $this->value
[$i];
1986 public function getName() {
1990 public function getNextSibling() {
1991 return $this->nextSibling
;
1994 public function getChildren() {
1998 public function getFirstChild() {
2002 public function getChildrenOfType( $name ) {
2006 public function splitArg() {
2007 throw new MWException( __METHOD__
. ': not supported' );
2010 public function splitExt() {
2011 throw new MWException( __METHOD__
. ': not supported' );
2014 public function splitHeading() {
2015 throw new MWException( __METHOD__
. ': not supported' );
2022 class PPNode_Hash_Attr
implements PPNode
{
2023 public $name, $value, $nextSibling;
2025 public function __construct( $name, $value ) {
2026 $this->name
= $name;
2027 $this->value
= $value;
2030 public function __toString() {
2031 return "<@{$this->name}>" . htmlspecialchars( $this->value
) . "</@{$this->name}>";
2034 public function getName() {
2038 public function getNextSibling() {
2039 return $this->nextSibling
;
2042 public function getChildren() {
2046 public function getFirstChild() {
2050 public function getChildrenOfType( $name ) {
2054 public function getLength() {
2058 public function item( $i ) {
2062 public function splitArg() {
2063 throw new MWException( __METHOD__
. ': not supported' );
2066 public function splitExt() {
2067 throw new MWException( __METHOD__
. ': not supported' );
2070 public function splitHeading() {
2071 throw new MWException( __METHOD__
. ': not supported' );