3 * Preprocessor using PHP's dom extension
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
27 class Preprocessor_DOM
implements Preprocessor
{
36 const CACHE_VERSION
= 1;
38 function __construct( $parser ) {
39 $this->parser
= $parser;
40 $mem = ini_get( 'memory_limit' );
41 $this->memoryLimit
= false;
42 if ( strval( $mem ) !== '' && $mem != -1 ) {
43 if ( preg_match( '/^\d+$/', $mem ) ) {
44 $this->memoryLimit
= $mem;
45 } elseif ( preg_match( '/^(\d+)M$/i', $mem, $m ) ) {
46 $this->memoryLimit
= $m[1] * 1048576;
55 return new PPFrame_DOM( $this );
60 * @return PPCustomFrame_DOM
62 function newCustomFrame( $args ) {
63 return new PPCustomFrame_DOM( $this, $args );
67 * @param array $values
70 function newPartNodeArray( $values ) {
71 //NOTE: DOM manipulation is slower than building & parsing XML! (or so Tim sais)
74 foreach ( $values as $k => $val ) {
76 $xml .= "<part><name index=\"$k\"/><value>"
77 . htmlspecialchars( $val ) . "</value></part>";
79 $xml .= "<part><name>" . htmlspecialchars( $k )
80 . "</name>=<value>" . htmlspecialchars( $val ) . "</value></part>";
86 wfProfileIn( __METHOD__
. '-loadXML' );
87 $dom = new DOMDocument();
89 $result = $dom->loadXML( $xml );
92 // Try running the XML through UtfNormal to get rid of invalid characters
93 $xml = UtfNormal
::cleanUp( $xml );
94 // 1 << 19 == XML_PARSE_HUGE, needed so newer versions of libxml2
95 // don't barf when the XML is >256 levels deep
96 $result = $dom->loadXML( $xml, 1 << 19 );
98 wfProfileOut( __METHOD__
. '-loadXML' );
101 throw new MWException( 'Parameters passed to ' . __METHOD__
. ' result in invalid XML' );
104 $root = $dom->documentElement
;
105 $node = new PPNode_DOM( $root->childNodes
);
110 * @throws MWException
113 function memCheck() {
114 if ( $this->memoryLimit
=== false ) {
117 $usage = memory_get_usage();
118 if ( $usage > $this->memoryLimit
* 0.9 ) {
119 $limit = intval( $this->memoryLimit
* 0.9 / 1048576 +
0.5 );
120 throw new MWException( "Preprocessor hit 90% memory limit ($limit MB)" );
122 return $usage <= $this->memoryLimit
* 0.8;
126 * Preprocess some wikitext and return the document tree.
127 * This is the ghost of Parser::replace_variables().
129 * @param string $text The text to parse
130 * @param int $flags Bitwise combination of:
131 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<includeonly>"
132 * as if the text is being included. Default
133 * is to assume a direct page view.
135 * The generated DOM tree must depend only on the input text and the flags.
136 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
138 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
139 * change in the DOM tree for a given text, must be passed through the section identifier
140 * in the section edit link and thus back to extractSections().
142 * The output of this function is currently only cached in process memory, but a persistent
143 * cache may be implemented at a later date which takes further advantage of these strict
144 * dependency requirements.
146 * @throws MWException
149 function preprocessToObj( $text, $flags = 0 ) {
150 wfProfileIn( __METHOD__
);
151 global $wgMemc, $wgPreprocessorCacheThreshold;
154 $cacheable = ( $wgPreprocessorCacheThreshold !== false
155 && strlen( $text ) > $wgPreprocessorCacheThreshold );
157 wfProfileIn( __METHOD__
. '-cacheable' );
159 $cacheKey = wfMemcKey( 'preprocess-xml', md5( $text ), $flags );
160 $cacheValue = $wgMemc->get( $cacheKey );
162 $version = substr( $cacheValue, 0, 8 );
163 if ( intval( $version ) == self
::CACHE_VERSION
) {
164 $xml = substr( $cacheValue, 8 );
166 wfDebugLog( "Preprocessor", "Loaded preprocessor XML from memcached (key $cacheKey)" );
169 if ( $xml === false ) {
170 wfProfileIn( __METHOD__
. '-cache-miss' );
171 $xml = $this->preprocessToXml( $text, $flags );
172 $cacheValue = sprintf( "%08d", self
::CACHE_VERSION
) . $xml;
173 $wgMemc->set( $cacheKey, $cacheValue, 86400 );
174 wfProfileOut( __METHOD__
. '-cache-miss' );
175 wfDebugLog( "Preprocessor", "Saved preprocessor XML to memcached (key $cacheKey)" );
178 $xml = $this->preprocessToXml( $text, $flags );
181 // Fail if the number of elements exceeds acceptable limits
182 // Do not attempt to generate the DOM
183 $this->parser
->mGeneratedPPNodeCount +
= substr_count( $xml, '<' );
184 $max = $this->parser
->mOptions
->getMaxGeneratedPPNodeCount();
185 if ( $this->parser
->mGeneratedPPNodeCount
> $max ) {
187 wfProfileOut( __METHOD__
. '-cacheable' );
189 wfProfileOut( __METHOD__
);
190 throw new MWException( __METHOD__
. ': generated node count limit exceeded' );
193 wfProfileIn( __METHOD__
. '-loadXML' );
194 $dom = new DOMDocument
;
195 wfSuppressWarnings();
196 $result = $dom->loadXML( $xml );
199 // Try running the XML through UtfNormal to get rid of invalid characters
200 $xml = UtfNormal
::cleanUp( $xml );
201 // 1 << 19 == XML_PARSE_HUGE, needed so newer versions of libxml2
202 // don't barf when the XML is >256 levels deep.
203 $result = $dom->loadXML( $xml, 1 << 19 );
206 $obj = new PPNode_DOM( $dom->documentElement
);
208 wfProfileOut( __METHOD__
. '-loadXML' );
211 wfProfileOut( __METHOD__
. '-cacheable' );
214 wfProfileOut( __METHOD__
);
217 throw new MWException( __METHOD__
. ' generated invalid XML' );
223 * @param string $text
227 function preprocessToXml( $text, $flags = 0 ) {
228 wfProfileIn( __METHOD__
);
241 'names' => array( 2 => null ),
247 $forInclusion = $flags & Parser
::PTD_FOR_INCLUSION
;
249 $xmlishElements = $this->parser
->getStripList();
250 $enableOnlyinclude = false;
251 if ( $forInclusion ) {
252 $ignoredTags = array( 'includeonly', '/includeonly' );
253 $ignoredElements = array( 'noinclude' );
254 $xmlishElements[] = 'noinclude';
255 if ( strpos( $text, '<onlyinclude>' ) !== false
256 && strpos( $text, '</onlyinclude>' ) !== false
258 $enableOnlyinclude = true;
261 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
262 $ignoredElements = array( 'includeonly' );
263 $xmlishElements[] = 'includeonly';
265 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
267 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
268 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
270 $stack = new PPDStack
;
272 $searchBase = "[{<\n"; #}
273 // For fast reverse searches
274 $revText = strrev( $text );
275 $lengthText = strlen( $text );
277 // Input pointer, starts out pointing to a pseudo-newline before the start
279 // Current accumulator
280 $accum =& $stack->getAccum();
282 // True to find equals signs in arguments
284 // True to take notice of pipe characters
287 // True if $i is inside a possible heading
289 // True if there are no more greater-than (>) signs right of $i
291 // True to ignore all input up to the next <onlyinclude>
292 $findOnlyinclude = $enableOnlyinclude;
293 // Do a line-start run without outputting an LF character
294 $fakeLineStart = true;
299 if ( $findOnlyinclude ) {
300 // Ignore all input up to the next <onlyinclude>
301 $startPos = strpos( $text, '<onlyinclude>', $i );
302 if ( $startPos === false ) {
303 // Ignored section runs to the end
304 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i ) ) . '</ignore>';
307 $tagEndPos = $startPos +
strlen( '<onlyinclude>' ); // past-the-end
308 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i ) ) . '</ignore>';
310 $findOnlyinclude = false;
313 if ( $fakeLineStart ) {
314 $found = 'line-start';
317 # Find next opening brace, closing brace or pipe
318 $search = $searchBase;
319 if ( $stack->top
=== false ) {
320 $currentClosing = '';
322 $currentClosing = $stack->top
->close
;
323 $search .= $currentClosing;
329 // First equals will be for the template
333 # Output literal section, advance input counter
334 $literalLength = strcspn( $text, $search, $i );
335 if ( $literalLength > 0 ) {
336 $accum .= htmlspecialchars( substr( $text, $i, $literalLength ) );
337 $i +
= $literalLength;
339 if ( $i >= $lengthText ) {
340 if ( $currentClosing == "\n" ) {
341 // Do a past-the-end run to finish off the heading
349 $curChar = $text[$i];
350 if ( $curChar == '|' ) {
352 } elseif ( $curChar == '=' ) {
354 } elseif ( $curChar == '<' ) {
356 } elseif ( $curChar == "\n" ) {
360 $found = 'line-start';
362 } elseif ( $curChar == $currentClosing ) {
364 } elseif ( isset( $rules[$curChar] ) ) {
366 $rule = $rules[$curChar];
368 # Some versions of PHP have a strcspn which stops on null characters
369 # Ignore and continue
376 if ( $found == 'angle' ) {
378 // Handle </onlyinclude>
379 if ( $enableOnlyinclude
380 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
382 $findOnlyinclude = true;
386 // Determine element name
387 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i +
1 ) ) {
388 // Element name missing or not listed
394 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
396 // To avoid leaving blank lines, when a sequence of
397 // space-separated comments is both preceded and followed by
398 // a newline (ignoring spaces), then
399 // trim leading and trailing spaces and the trailing newline.
402 $endPos = strpos( $text, '-->', $i +
4 );
403 if ( $endPos === false ) {
404 // Unclosed comment in input, runs to end
405 $inner = substr( $text, $i );
406 $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>';
409 // Search backwards for leading whitespace
410 $wsStart = $i ?
( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
412 // Search forwards for trailing whitespace
413 // $wsEnd will be the position of the last space (or the '>' if there's none)
414 $wsEnd = $endPos +
2 +
strspn( $text, " \t", $endPos +
3 );
416 // Keep looking forward as long as we're finding more
418 $comments = array( array( $wsStart, $wsEnd ) );
419 while ( substr( $text, $wsEnd +
1, 4 ) == '<!--' ) {
420 $c = strpos( $text, '-->', $wsEnd +
4 );
421 if ( $c === false ) {
424 $c = $c +
2 +
strspn( $text, " \t", $c +
3 );
425 $comments[] = array( $wsEnd +
1, $c );
429 // Eat the line if possible
430 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
431 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
432 // it's a possible beneficial b/c break.
433 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
434 && substr( $text, $wsEnd +
1, 1 ) == "\n"
436 // Remove leading whitespace from the end of the accumulator
437 // Sanity check first though
438 $wsLength = $i - $wsStart;
440 && strspn( $accum, " \t", -$wsLength ) === $wsLength
442 $accum = substr( $accum, 0, -$wsLength );
445 // Dump all but the last comment to the accumulator
446 foreach ( $comments as $j => $com ) {
448 $endPos = $com[1] +
1;
449 if ( $j == ( count( $comments ) - 1 ) ) {
452 $inner = substr( $text, $startPos, $endPos - $startPos );
453 $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>';
456 // Do a line-start run next time to look for headings after the comment
457 $fakeLineStart = true;
459 // No line to eat, just take the comment itself
465 $part = $stack->top
->getCurrentPart();
466 if ( !( isset( $part->commentEnd
) && $part->commentEnd
== $wsStart - 1 ) ) {
467 $part->visualEnd
= $wsStart;
469 // Else comments abutting, no change in visual end
470 $part->commentEnd
= $endPos;
473 $inner = substr( $text, $startPos, $endPos - $startPos +
1 );
474 $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>';
479 $lowerName = strtolower( $name );
480 $attrStart = $i +
strlen( $name ) +
1;
483 $tagEndPos = $noMoreGT ?
false : strpos( $text, '>', $attrStart );
484 if ( $tagEndPos === false ) {
485 // Infinite backtrack
486 // Disable tag search to prevent worst-case O(N^2) performance
493 // Handle ignored tags
494 if ( in_array( $lowerName, $ignoredTags ) ) {
496 . htmlspecialchars( substr( $text, $i, $tagEndPos - $i +
1 ) )
503 if ( $text[$tagEndPos - 1] == '/' ) {
504 $attrEnd = $tagEndPos - 1;
509 $attrEnd = $tagEndPos;
511 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
512 $text, $matches, PREG_OFFSET_CAPTURE
, $tagEndPos +
1 )
514 $inner = substr( $text, $tagEndPos +
1, $matches[0][1] - $tagEndPos - 1 );
515 $i = $matches[0][1] +
strlen( $matches[0][0] );
516 $close = '<close>' . htmlspecialchars( $matches[0][0] ) . '</close>';
518 // No end tag -- let it run out to the end of the text.
519 $inner = substr( $text, $tagEndPos +
1 );
524 // <includeonly> and <noinclude> just become <ignore> tags
525 if ( in_array( $lowerName, $ignoredElements ) ) {
526 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $tagStartPos, $i - $tagStartPos ) )
532 if ( $attrEnd <= $attrStart ) {
535 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
537 $accum .= '<name>' . htmlspecialchars( $name ) . '</name>' .
538 // Note that the attr element contains the whitespace between name and attribute,
539 // this is necessary for precise reconstruction during pre-save transform.
540 '<attr>' . htmlspecialchars( $attr ) . '</attr>';
541 if ( $inner !== null ) {
542 $accum .= '<inner>' . htmlspecialchars( $inner ) . '</inner>';
544 $accum .= $close . '</ext>';
545 } elseif ( $found == 'line-start' ) {
546 // Is this the start of a heading?
547 // Line break belongs before the heading element in any case
548 if ( $fakeLineStart ) {
549 $fakeLineStart = false;
555 $count = strspn( $text, '=', $i, 6 );
556 if ( $count == 1 && $findEquals ) {
557 // DWIM: This looks kind of like a name/value separator.
558 // Let's let the equals handler have it and break the
559 // potential heading. This is heuristic, but AFAICT the
560 // methods for completely correct disambiguation are very
562 } elseif ( $count > 0 ) {
566 'parts' => array( new PPDPart( str_repeat( '=', $count ) ) ),
569 $stack->push( $piece );
570 $accum =& $stack->getAccum();
571 $flags = $stack->getFlags();
575 } elseif ( $found == 'line-end' ) {
576 $piece = $stack->top
;
577 // A heading must be open, otherwise \n wouldn't have been in the search list
578 assert( '$piece->open == "\n"' );
579 $part = $piece->getCurrentPart();
580 // Search back through the input to see if it has a proper close.
581 // Do this using the reversed string since the other solutions
582 // (end anchor, etc.) are inefficient.
583 $wsLength = strspn( $revText, " \t", $lengthText - $i );
584 $searchStart = $i - $wsLength;
585 if ( isset( $part->commentEnd
) && $searchStart - 1 == $part->commentEnd
) {
586 // Comment found at line end
587 // Search for equals signs before the comment
588 $searchStart = $part->visualEnd
;
589 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
591 $count = $piece->count
;
592 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
593 if ( $equalsLength > 0 ) {
594 if ( $searchStart - $equalsLength == $piece->startPos
) {
595 // This is just a single string of equals signs on its own line
596 // Replicate the doHeadings behavior /={count}(.+)={count}/
597 // First find out how many equals signs there really are (don't stop at 6)
598 $count = $equalsLength;
602 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
605 $count = min( $equalsLength, $count );
608 // Normal match, output <h>
609 $element = "<h level=\"$count\" i=\"$headingIndex\">$accum</h>";
612 // Single equals sign on its own line, count=0
616 // No match, no <h>, just pass down the inner text
621 $accum =& $stack->getAccum();
622 $flags = $stack->getFlags();
625 // Append the result to the enclosing accumulator
627 // Note that we do NOT increment the input pointer.
628 // This is because the closing linebreak could be the opening linebreak of
629 // another heading. Infinite loops are avoided because the next iteration MUST
630 // hit the heading open case above, which unconditionally increments the
632 } elseif ( $found == 'open' ) {
633 # count opening brace characters
634 $count = strspn( $text, $curChar, $i );
636 # we need to add to stack only if opening brace count is enough for one of the rules
637 if ( $count >= $rule['min'] ) {
638 # Add it to the stack
641 'close' => $rule['end'],
643 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
646 $stack->push( $piece );
647 $accum =& $stack->getAccum();
648 $flags = $stack->getFlags();
651 # Add literal brace(s)
652 $accum .= htmlspecialchars( str_repeat( $curChar, $count ) );
655 } elseif ( $found == 'close' ) {
656 $piece = $stack->top
;
657 # lets check if there are enough characters for closing brace
658 $maxCount = $piece->count
;
659 $count = strspn( $text, $curChar, $i, $maxCount );
661 # check for maximum matching characters (if there are 5 closing
662 # characters, we will probably need only 3 - depending on the rules)
663 $rule = $rules[$piece->open
];
664 if ( $count > $rule['max'] ) {
665 # The specified maximum exists in the callback array, unless the caller
667 $matchingCount = $rule['max'];
669 # Count is less than the maximum
670 # Skip any gaps in the callback array to find the true largest match
671 # Need to use array_key_exists not isset because the callback can be null
672 $matchingCount = $count;
673 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
678 if ( $matchingCount <= 0 ) {
679 # No matching element found in callback array
680 # Output a literal closing brace and continue
681 $accum .= htmlspecialchars( str_repeat( $curChar, $count ) );
685 $name = $rule['names'][$matchingCount];
686 if ( $name === null ) {
687 // No element, just literal text
688 $element = $piece->breakSyntax( $matchingCount ) . str_repeat( $rule['end'], $matchingCount );
691 # Note: $parts is already XML, does not need to be encoded further
692 $parts = $piece->parts
;
693 $title = $parts[0]->out
;
696 # The invocation is at the start of the line if lineStart is set in
697 # the stack, and all opening brackets are used up.
698 if ( $maxCount == $matchingCount && !empty( $piece->lineStart
) ) {
699 $attr = ' lineStart="1"';
704 $element = "<$name$attr>";
705 $element .= "<title>$title</title>";
707 foreach ( $parts as $part ) {
708 if ( isset( $part->eqpos
) ) {
709 $argName = substr( $part->out
, 0, $part->eqpos
);
710 $argValue = substr( $part->out
, $part->eqpos +
1 );
711 $element .= "<part><name>$argName</name>=<value>$argValue</value></part>";
713 $element .= "<part><name index=\"$argIndex\" /><value>{$part->out}</value></part>";
717 $element .= "</$name>";
720 # Advance input pointer
721 $i +
= $matchingCount;
725 $accum =& $stack->getAccum();
727 # Re-add the old stack element if it still has unmatched opening characters remaining
728 if ( $matchingCount < $piece->count
) {
729 $piece->parts
= array( new PPDPart
);
730 $piece->count
-= $matchingCount;
731 # do we still qualify for any callback with remaining count?
732 $min = $rules[$piece->open
]['min'];
733 if ( $piece->count
>= $min ) {
734 $stack->push( $piece );
735 $accum =& $stack->getAccum();
737 $accum .= str_repeat( $piece->open
, $piece->count
);
740 $flags = $stack->getFlags();
743 # Add XML element to the enclosing accumulator
745 } elseif ( $found == 'pipe' ) {
746 $findEquals = true; // shortcut for getFlags()
748 $accum =& $stack->getAccum();
750 } elseif ( $found == 'equals' ) {
751 $findEquals = false; // shortcut for getFlags()
752 $stack->getCurrentPart()->eqpos
= strlen( $accum );
758 # Output any remaining unclosed brackets
759 foreach ( $stack->stack
as $piece ) {
760 $stack->rootAccum
.= $piece->breakSyntax();
762 $stack->rootAccum
.= '</root>';
763 $xml = $stack->rootAccum
;
765 wfProfileOut( __METHOD__
);
772 * Stack class to help Preprocessor::preprocessToObj()
776 var $stack, $rootAccum;
783 var $elementClass = 'PPDStackElement';
785 static $false = false;
787 function __construct() {
788 $this->stack
= array();
790 $this->rootAccum
= '';
791 $this->accum
=& $this->rootAccum
;
798 return count( $this->stack
);
801 function &getAccum() {
805 function getCurrentPart() {
806 if ( $this->top
=== false ) {
809 return $this->top
->getCurrentPart();
813 function push( $data ) {
814 if ( $data instanceof $this->elementClass
) {
815 $this->stack
[] = $data;
817 $class = $this->elementClass
;
818 $this->stack
[] = new $class( $data );
820 $this->top
= $this->stack
[count( $this->stack
) - 1];
821 $this->accum
=& $this->top
->getAccum();
825 if ( !count( $this->stack
) ) {
826 throw new MWException( __METHOD__
. ': no elements remaining' );
828 $temp = array_pop( $this->stack
);
830 if ( count( $this->stack
) ) {
831 $this->top
= $this->stack
[count( $this->stack
) - 1];
832 $this->accum
=& $this->top
->getAccum();
834 $this->top
= self
::$false;
835 $this->accum
=& $this->rootAccum
;
840 function addPart( $s = '' ) {
841 $this->top
->addPart( $s );
842 $this->accum
=& $this->top
->getAccum();
848 function getFlags() {
849 if ( !count( $this->stack
) ) {
851 'findEquals' => false,
853 'inHeading' => false,
856 return $this->top
->getFlags();
864 class PPDStackElement
{
865 var $open, // Opening character (\n for heading)
866 $close, // Matching closing character
867 $count, // Number of opening characters found (number of "=" for heading)
868 $parts, // Array of PPDPart objects describing pipe-separated parts.
869 $lineStart; // True if the open char appeared at the start of the input line. Not set for headings.
871 var $partClass = 'PPDPart';
873 function __construct( $data = array() ) {
874 $class = $this->partClass
;
875 $this->parts
= array( new $class );
877 foreach ( $data as $name => $value ) {
878 $this->$name = $value;
882 function &getAccum() {
883 return $this->parts
[count( $this->parts
) - 1]->out
;
886 function addPart( $s = '' ) {
887 $class = $this->partClass
;
888 $this->parts
[] = new $class( $s );
891 function getCurrentPart() {
892 return $this->parts
[count( $this->parts
) - 1];
898 function getFlags() {
899 $partCount = count( $this->parts
);
900 $findPipe = $this->open
!= "\n" && $this->open
!= '[';
902 'findPipe' => $findPipe,
903 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts
[$partCount - 1]->eqpos
),
904 'inHeading' => $this->open
== "\n",
909 * Get the output string that would result if the close is not found.
911 * @param bool|int $openingCount
914 function breakSyntax( $openingCount = false ) {
915 if ( $this->open
== "\n" ) {
916 $s = $this->parts
[0]->out
;
918 if ( $openingCount === false ) {
919 $openingCount = $this->count
;
921 $s = str_repeat( $this->open
, $openingCount );
923 foreach ( $this->parts
as $part ) {
940 var $out; // Output accumulator string
942 // Optional member variables:
943 // eqpos Position of equals sign in output accumulator
944 // commentEnd Past-the-end input pointer for the last comment encountered
945 // visualEnd Past-the-end input pointer for the end of the accumulator minus comments
947 function __construct( $out = '' ) {
953 * An expansion frame, used as a context to expand the result of preprocessToObj()
956 class PPFrame_DOM
implements PPFrame
{
975 * Hashtable listing templates which are disallowed for expansion in this frame,
976 * having been encountered previously in parent frames.
981 * Recursion depth of this frame, top = 0
982 * Note that this is NOT the same as expansion depth in expand()
986 private $volatile = false;
991 protected $childExpansionCache;
994 * Construct a new preprocessor frame.
995 * @param Preprocessor $preprocessor The parent preprocessor
997 function __construct( $preprocessor ) {
998 $this->preprocessor
= $preprocessor;
999 $this->parser
= $preprocessor->parser
;
1000 $this->title
= $this->parser
->mTitle
;
1001 $this->titleCache
= array( $this->title ?
$this->title
->getPrefixedDBkey() : false );
1002 $this->loopCheckHash
= array();
1004 $this->childExpansionCache
= array();
1008 * Create a new child frame
1009 * $args is optionally a multi-root PPNode or array containing the template arguments
1011 * @param bool|array $args
1012 * @param Title|bool $title
1013 * @param int $indexOffset
1014 * @return PPTemplateFrame_DOM
1016 function newChild( $args = false, $title = false, $indexOffset = 0 ) {
1017 $namedArgs = array();
1018 $numberedArgs = array();
1019 if ( $title === false ) {
1020 $title = $this->title
;
1022 if ( $args !== false ) {
1024 if ( $args instanceof PPNode
) {
1025 $args = $args->node
;
1027 foreach ( $args as $arg ) {
1028 if ( $arg instanceof PPNode
) {
1032 $xpath = new DOMXPath( $arg->ownerDocument
);
1035 $nameNodes = $xpath->query( 'name', $arg );
1036 $value = $xpath->query( 'value', $arg );
1037 if ( $nameNodes->item( 0 )->hasAttributes() ) {
1038 // Numbered parameter
1039 $index = $nameNodes->item( 0 )->attributes
->getNamedItem( 'index' )->textContent
;
1040 $index = $index - $indexOffset;
1041 $numberedArgs[$index] = $value->item( 0 );
1042 unset( $namedArgs[$index] );
1045 $name = trim( $this->expand( $nameNodes->item( 0 ), PPFrame
::STRIP_COMMENTS
) );
1046 $namedArgs[$name] = $value->item( 0 );
1047 unset( $numberedArgs[$name] );
1051 return new PPTemplateFrame_DOM( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
1055 * @throws MWException
1056 * @param string|int $key
1057 * @param string|PPNode_DOM|DOMDocument $root
1061 function cachedExpand( $key, $root, $flags = 0 ) {
1062 // we don't have a parent, so we don't have a cache
1063 return $this->expand( $root, $flags );
1067 * @throws MWException
1068 * @param string|PPNode_DOM|DOMDocument $root
1072 function expand( $root, $flags = 0 ) {
1073 static $expansionDepth = 0;
1074 if ( is_string( $root ) ) {
1078 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
1079 $this->parser
->limitationWarn( 'node-count-exceeded',
1080 $this->parser
->mPPNodeCount
,
1081 $this->parser
->mOptions
->getMaxPPNodeCount()
1083 return '<span class="error">Node-count limit exceeded</span>';
1086 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
1087 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
1089 $this->parser
->mOptions
->getMaxPPExpandDepth()
1091 return '<span class="error">Expansion depth limit exceeded</span>';
1093 wfProfileIn( __METHOD__
);
1095 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
1096 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
1099 if ( $root instanceof PPNode_DOM
) {
1100 $root = $root->node
;
1102 if ( $root instanceof DOMDocument
) {
1103 $root = $root->documentElement
;
1106 $outStack = array( '', '' );
1107 $iteratorStack = array( false, $root );
1108 $indexStack = array( 0, 0 );
1110 while ( count( $iteratorStack ) > 1 ) {
1111 $level = count( $outStack ) - 1;
1112 $iteratorNode =& $iteratorStack[$level];
1113 $out =& $outStack[$level];
1114 $index =& $indexStack[$level];
1116 if ( $iteratorNode instanceof PPNode_DOM
) {
1117 $iteratorNode = $iteratorNode->node
;
1120 if ( is_array( $iteratorNode ) ) {
1121 if ( $index >= count( $iteratorNode ) ) {
1122 // All done with this iterator
1123 $iteratorStack[$level] = false;
1124 $contextNode = false;
1126 $contextNode = $iteratorNode[$index];
1129 } elseif ( $iteratorNode instanceof DOMNodeList
) {
1130 if ( $index >= $iteratorNode->length
) {
1131 // All done with this iterator
1132 $iteratorStack[$level] = false;
1133 $contextNode = false;
1135 $contextNode = $iteratorNode->item( $index );
1139 // Copy to $contextNode and then delete from iterator stack,
1140 // because this is not an iterator but we do have to execute it once
1141 $contextNode = $iteratorStack[$level];
1142 $iteratorStack[$level] = false;
1145 if ( $contextNode instanceof PPNode_DOM
) {
1146 $contextNode = $contextNode->node
;
1149 $newIterator = false;
1151 if ( $contextNode === false ) {
1153 } elseif ( is_string( $contextNode ) ) {
1154 $out .= $contextNode;
1155 } elseif ( is_array( $contextNode ) ||
$contextNode instanceof DOMNodeList
) {
1156 $newIterator = $contextNode;
1157 } elseif ( $contextNode instanceof DOMNode
) {
1158 if ( $contextNode->nodeType
== XML_TEXT_NODE
) {
1159 $out .= $contextNode->nodeValue
;
1160 } elseif ( $contextNode->nodeName
== 'template' ) {
1161 # Double-brace expansion
1162 $xpath = new DOMXPath( $contextNode->ownerDocument
);
1163 $titles = $xpath->query( 'title', $contextNode );
1164 $title = $titles->item( 0 );
1165 $parts = $xpath->query( 'part', $contextNode );
1166 if ( $flags & PPFrame
::NO_TEMPLATES
) {
1167 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $title, $parts );
1169 $lineStart = $contextNode->getAttribute( 'lineStart' );
1171 'title' => new PPNode_DOM( $title ),
1172 'parts' => new PPNode_DOM( $parts ),
1173 'lineStart' => $lineStart );
1174 $ret = $this->parser
->braceSubstitution( $params, $this );
1175 if ( isset( $ret['object'] ) ) {
1176 $newIterator = $ret['object'];
1178 $out .= $ret['text'];
1181 } elseif ( $contextNode->nodeName
== 'tplarg' ) {
1182 # Triple-brace expansion
1183 $xpath = new DOMXPath( $contextNode->ownerDocument
);
1184 $titles = $xpath->query( 'title', $contextNode );
1185 $title = $titles->item( 0 );
1186 $parts = $xpath->query( 'part', $contextNode );
1187 if ( $flags & PPFrame
::NO_ARGS
) {
1188 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $title, $parts );
1191 'title' => new PPNode_DOM( $title ),
1192 'parts' => new PPNode_DOM( $parts ) );
1193 $ret = $this->parser
->argSubstitution( $params, $this );
1194 if ( isset( $ret['object'] ) ) {
1195 $newIterator = $ret['object'];
1197 $out .= $ret['text'];
1200 } elseif ( $contextNode->nodeName
== 'comment' ) {
1201 # HTML-style comment
1202 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1203 if ( $this->parser
->ot
['html']
1204 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
1205 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
1208 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
1209 # Add a strip marker in PST mode so that pstPass2() can
1210 # run some old-fashioned regexes on the result.
1211 # Not in RECOVER_COMMENTS mode (extractSections) though.
1212 $out .= $this->parser
->insertStripItem( $contextNode->textContent
);
1214 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1215 $out .= $contextNode->textContent
;
1217 } elseif ( $contextNode->nodeName
== 'ignore' ) {
1218 # Output suppression used by <includeonly> etc.
1219 # OT_WIKI will only respect <ignore> in substed templates.
1220 # The other output types respect it unless NO_IGNORE is set.
1221 # extractSections() sets NO_IGNORE and so never respects it.
1222 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] )
1223 ||
( $flags & PPFrame
::NO_IGNORE
)
1225 $out .= $contextNode->textContent
;
1229 } elseif ( $contextNode->nodeName
== 'ext' ) {
1231 $xpath = new DOMXPath( $contextNode->ownerDocument
);
1232 $names = $xpath->query( 'name', $contextNode );
1233 $attrs = $xpath->query( 'attr', $contextNode );
1234 $inners = $xpath->query( 'inner', $contextNode );
1235 $closes = $xpath->query( 'close', $contextNode );
1237 'name' => new PPNode_DOM( $names->item( 0 ) ),
1238 'attr' => $attrs->length
> 0 ?
new PPNode_DOM( $attrs->item( 0 ) ) : null,
1239 'inner' => $inners->length
> 0 ?
new PPNode_DOM( $inners->item( 0 ) ) : null,
1240 'close' => $closes->length
> 0 ?
new PPNode_DOM( $closes->item( 0 ) ) : null,
1242 $out .= $this->parser
->extensionSubstitution( $params, $this );
1243 } elseif ( $contextNode->nodeName
== 'h' ) {
1245 $s = $this->expand( $contextNode->childNodes
, $flags );
1247 # Insert a heading marker only for <h> children of <root>
1248 # This is to stop extractSections from going over multiple tree levels
1249 if ( $contextNode->parentNode
->nodeName
== 'root' && $this->parser
->ot
['html'] ) {
1250 # Insert heading index marker
1251 $headingIndex = $contextNode->getAttribute( 'i' );
1252 $titleText = $this->title
->getPrefixedDBkey();
1253 $this->parser
->mHeadings
[] = array( $titleText, $headingIndex );
1254 $serial = count( $this->parser
->mHeadings
) - 1;
1255 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser
::MARKER_SUFFIX
;
1256 $count = $contextNode->getAttribute( 'level' );
1257 $s = substr( $s, 0, $count ) . $marker . substr( $s, $count );
1258 $this->parser
->mStripState
->addGeneral( $marker, '' );
1262 # Generic recursive expansion
1263 $newIterator = $contextNode->childNodes
;
1266 wfProfileOut( __METHOD__
);
1267 throw new MWException( __METHOD__
. ': Invalid parameter type' );
1270 if ( $newIterator !== false ) {
1271 if ( $newIterator instanceof PPNode_DOM
) {
1272 $newIterator = $newIterator->node
;
1275 $iteratorStack[] = $newIterator;
1277 } elseif ( $iteratorStack[$level] === false ) {
1278 // Return accumulated value to parent
1279 // With tail recursion
1280 while ( $iteratorStack[$level] === false && $level > 0 ) {
1281 $outStack[$level - 1] .= $out;
1282 array_pop( $outStack );
1283 array_pop( $iteratorStack );
1284 array_pop( $indexStack );
1290 wfProfileOut( __METHOD__
);
1291 return $outStack[0];
1295 * @param string $sep
1299 function implodeWithFlags( $sep, $flags /*, ... */ ) {
1300 $args = array_slice( func_get_args(), 2 );
1304 foreach ( $args as $root ) {
1305 if ( $root instanceof PPNode_DOM
) {
1306 $root = $root->node
;
1308 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
1309 $root = array( $root );
1311 foreach ( $root as $node ) {
1317 $s .= $this->expand( $node, $flags );
1324 * Implode with no flags specified
1325 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1327 * @param string $sep
1330 function implode( $sep /*, ... */ ) {
1331 $args = array_slice( func_get_args(), 1 );
1335 foreach ( $args as $root ) {
1336 if ( $root instanceof PPNode_DOM
) {
1337 $root = $root->node
;
1339 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
1340 $root = array( $root );
1342 foreach ( $root as $node ) {
1348 $s .= $this->expand( $node );
1355 * Makes an object that, when expand()ed, will be the same as one obtained
1358 * @param string $sep
1361 function virtualImplode( $sep /*, ... */ ) {
1362 $args = array_slice( func_get_args(), 1 );
1366 foreach ( $args as $root ) {
1367 if ( $root instanceof PPNode_DOM
) {
1368 $root = $root->node
;
1370 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
1371 $root = array( $root );
1373 foreach ( $root as $node ) {
1386 * Virtual implode with brackets
1387 * @param string $start
1388 * @param string $sep
1389 * @param string $end
1392 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1393 $args = array_slice( func_get_args(), 3 );
1394 $out = array( $start );
1397 foreach ( $args as $root ) {
1398 if ( $root instanceof PPNode_DOM
) {
1399 $root = $root->node
;
1401 if ( !is_array( $root ) && !( $root instanceof DOMNodeList
) ) {
1402 $root = array( $root );
1404 foreach ( $root as $node ) {
1417 function __toString() {
1421 function getPDBK( $level = false ) {
1422 if ( $level === false ) {
1423 return $this->title
->getPrefixedDBkey();
1425 return isset( $this->titleCache
[$level] ) ?
$this->titleCache
[$level] : false;
1432 function getArguments() {
1439 function getNumberedArguments() {
1446 function getNamedArguments() {
1451 * Returns true if there are no arguments in this frame
1455 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
1469 function loopCheck( $title ) {
1470 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
1474 * Return true if the frame is a template frame
1478 function isTemplate() {
1483 * Get a title of frame
1487 function getTitle() {
1488 return $this->title
;
1492 * Set the volatile flag
1496 function setVolatile( $flag = true ) {
1497 $this->volatile
= $flag;
1501 * Get the volatile flag
1505 function isVolatile() {
1506 return $this->volatile
;
1511 * Expansion frame with template arguments
1514 class PPTemplateFrame_DOM
extends PPFrame_DOM
{
1515 var $numberedArgs, $namedArgs;
1521 var $numberedExpansionCache, $namedExpansionCache;
1524 * @param Preprocessor $preprocessor
1525 * @param bool|PPFrame_DOM $parent
1526 * @param array $numberedArgs
1527 * @param array $namedArgs
1528 * @param bool|Title $title
1530 function __construct( $preprocessor, $parent = false, $numberedArgs = array(),
1531 $namedArgs = array(), $title = false
1533 parent
::__construct( $preprocessor );
1535 $this->parent
= $parent;
1536 $this->numberedArgs
= $numberedArgs;
1537 $this->namedArgs
= $namedArgs;
1538 $this->title
= $title;
1539 $pdbk = $title ?
$title->getPrefixedDBkey() : false;
1540 $this->titleCache
= $parent->titleCache
;
1541 $this->titleCache
[] = $pdbk;
1542 $this->loopCheckHash
= /*clone*/ $parent->loopCheckHash
;
1543 if ( $pdbk !== false ) {
1544 $this->loopCheckHash
[$pdbk] = true;
1546 $this->depth
= $parent->depth +
1;
1547 $this->numberedExpansionCache
= $this->namedExpansionCache
= array();
1550 function __toString() {
1553 $args = $this->numberedArgs +
$this->namedArgs
;
1554 foreach ( $args as $name => $value ) {
1560 $s .= "\"$name\":\"" .
1561 str_replace( '"', '\\"', $value->ownerDocument
->saveXML( $value ) ) . '"';
1568 * @throws MWException
1569 * @param string|int $key
1570 * @param string|PPNode_DOM|DOMDocument $root
1574 function cachedExpand( $key, $root, $flags = 0 ) {
1575 if ( isset( $this->parent
->childExpansionCache
[$key] ) ) {
1576 return $this->parent
->childExpansionCache
[$key];
1578 $retval = $this->expand( $root, $flags );
1579 if ( !$this->isVolatile() ) {
1580 $this->parent
->childExpansionCache
[$key] = $retval;
1586 * Returns true if there are no arguments in this frame
1590 function isEmpty() {
1591 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
1594 function getArguments() {
1595 $arguments = array();
1596 foreach ( array_merge(
1597 array_keys( $this->numberedArgs
),
1598 array_keys( $this->namedArgs
) ) as $key ) {
1599 $arguments[$key] = $this->getArgument( $key );
1604 function getNumberedArguments() {
1605 $arguments = array();
1606 foreach ( array_keys( $this->numberedArgs
) as $key ) {
1607 $arguments[$key] = $this->getArgument( $key );
1612 function getNamedArguments() {
1613 $arguments = array();
1614 foreach ( array_keys( $this->namedArgs
) as $key ) {
1615 $arguments[$key] = $this->getArgument( $key );
1620 function getNumberedArgument( $index ) {
1621 if ( !isset( $this->numberedArgs
[$index] ) ) {
1624 if ( !isset( $this->numberedExpansionCache
[$index] ) ) {
1625 # No trimming for unnamed arguments
1626 $this->numberedExpansionCache
[$index] = $this->parent
->expand(
1627 $this->numberedArgs
[$index],
1628 PPFrame
::STRIP_COMMENTS
1631 return $this->numberedExpansionCache
[$index];
1634 function getNamedArgument( $name ) {
1635 if ( !isset( $this->namedArgs
[$name] ) ) {
1638 if ( !isset( $this->namedExpansionCache
[$name] ) ) {
1639 # Trim named arguments post-expand, for backwards compatibility
1640 $this->namedExpansionCache
[$name] = trim(
1641 $this->parent
->expand( $this->namedArgs
[$name], PPFrame
::STRIP_COMMENTS
) );
1643 return $this->namedExpansionCache
[$name];
1646 function getArgument( $name ) {
1647 $text = $this->getNumberedArgument( $name );
1648 if ( $text === false ) {
1649 $text = $this->getNamedArgument( $name );
1655 * Return true if the frame is a template frame
1659 function isTemplate() {
1663 function setVolatile( $flag = true ) {
1664 parent
::setVolatile( $flag );
1665 $this->parent
->setVolatile( $flag );
1670 * Expansion frame with custom arguments
1673 class PPCustomFrame_DOM
extends PPFrame_DOM
{
1676 function __construct( $preprocessor, $args ) {
1677 parent
::__construct( $preprocessor );
1678 $this->args
= $args;
1681 function __toString() {
1684 foreach ( $this->args
as $name => $value ) {
1690 $s .= "\"$name\":\"" .
1691 str_replace( '"', '\\"', $value->__toString() ) . '"';
1700 function isEmpty() {
1701 return !count( $this->args
);
1704 function getArgument( $index ) {
1705 if ( !isset( $this->args
[$index] ) ) {
1708 return $this->args
[$index];
1711 function getArguments() {
1719 class PPNode_DOM
implements PPNode
{
1727 function __construct( $node, $xpath = false ) {
1728 $this->node
= $node;
1734 function getXPath() {
1735 if ( $this->xpath
=== null ) {
1736 $this->xpath
= new DOMXPath( $this->node
->ownerDocument
);
1738 return $this->xpath
;
1741 function __toString() {
1742 if ( $this->node
instanceof DOMNodeList
) {
1744 foreach ( $this->node
as $node ) {
1745 $s .= $node->ownerDocument
->saveXML( $node );
1748 $s = $this->node
->ownerDocument
->saveXML( $this->node
);
1754 * @return bool|PPNode_DOM
1756 function getChildren() {
1757 return $this->node
->childNodes ?
new self( $this->node
->childNodes
) : false;
1761 * @return bool|PPNode_DOM
1763 function getFirstChild() {
1764 return $this->node
->firstChild ?
new self( $this->node
->firstChild
) : false;
1768 * @return bool|PPNode_DOM
1770 function getNextSibling() {
1771 return $this->node
->nextSibling ?
new self( $this->node
->nextSibling
) : false;
1775 * @param string $type
1777 * @return bool|PPNode_DOM
1779 function getChildrenOfType( $type ) {
1780 return new self( $this->getXPath()->query( $type, $this->node
) );
1786 function getLength() {
1787 if ( $this->node
instanceof DOMNodeList
) {
1788 return $this->node
->length
;
1796 * @return bool|PPNode_DOM
1798 function item( $i ) {
1799 $item = $this->node
->item( $i );
1800 return $item ?
new self( $item ) : false;
1806 function getName() {
1807 if ( $this->node
instanceof DOMNodeList
) {
1810 return $this->node
->nodeName
;
1815 * Split a "<part>" node into an associative array containing:
1816 * - name PPNode name
1817 * - index String index
1818 * - value PPNode value
1820 * @throws MWException
1823 function splitArg() {
1824 $xpath = $this->getXPath();
1825 $names = $xpath->query( 'name', $this->node
);
1826 $values = $xpath->query( 'value', $this->node
);
1827 if ( !$names->length ||
!$values->length
) {
1828 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
1830 $name = $names->item( 0 );
1831 $index = $name->getAttribute( 'index' );
1833 'name' => new self( $name ),
1835 'value' => new self( $values->item( 0 ) ) );
1839 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1840 * All values in the resulting array are PPNodes. Inner and close are optional.
1842 * @throws MWException
1845 function splitExt() {
1846 $xpath = $this->getXPath();
1847 $names = $xpath->query( 'name', $this->node
);
1848 $attrs = $xpath->query( 'attr', $this->node
);
1849 $inners = $xpath->query( 'inner', $this->node
);
1850 $closes = $xpath->query( 'close', $this->node
);
1851 if ( !$names->length ||
!$attrs->length
) {
1852 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
1855 'name' => new self( $names->item( 0 ) ),
1856 'attr' => new self( $attrs->item( 0 ) ) );
1857 if ( $inners->length
) {
1858 $parts['inner'] = new self( $inners->item( 0 ) );
1860 if ( $closes->length
) {
1861 $parts['close'] = new self( $closes->item( 0 ) );
1867 * Split a "<h>" node
1868 * @throws MWException
1871 function splitHeading() {
1872 if ( $this->getName() !== 'h' ) {
1873 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1876 'i' => $this->node
->getAttribute( 'i' ),
1877 'level' => $this->node
->getAttribute( 'level' ),
1878 'contents' => $this->getChildren()