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
{
36 const CACHE_VERSION
= 1;
38 function __construct( $parser ) {
39 $this->parser
= $parser;
43 * @return PPFrame_Hash
46 return new PPFrame_Hash( $this );
51 * @return PPCustomFrame_Hash
53 function newCustomFrame( $args ) {
54 return new PPCustomFrame_Hash( $this, $args );
58 * @param array $values
59 * @return PPNode_Hash_Array
61 function newPartNodeArray( $values ) {
64 foreach ( $values as $k => $val ) {
65 $partNode = new PPNode_Hash_Tree( 'part' );
66 $nameNode = new PPNode_Hash_Tree( 'name' );
69 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $k ) );
70 $partNode->addChild( $nameNode );
72 $nameNode->addChild( new PPNode_Hash_Text( $k ) );
73 $partNode->addChild( $nameNode );
74 $partNode->addChild( new PPNode_Hash_Text( '=' ) );
77 $valueNode = new PPNode_Hash_Tree( 'value' );
78 $valueNode->addChild( new PPNode_Hash_Text( $val ) );
79 $partNode->addChild( $valueNode );
84 $node = new PPNode_Hash_Array( $list );
89 * Preprocess some wikitext and return the document tree.
90 * This is the ghost of Parser::replace_variables().
92 * @param string $text The text to parse
93 * @param int $flags Bitwise combination of:
94 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<includeonly>" as if the text is being
95 * included. Default is to assume a direct page view.
97 * The generated DOM tree must depend only on the input text and the flags.
98 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
100 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
101 * change in the DOM tree for a given text, must be passed through the section identifier
102 * in the section edit link and thus back to extractSections().
104 * The output of this function is currently only cached in process memory, but a persistent
105 * cache may be implemented at a later date which takes further advantage of these strict
106 * dependency requirements.
108 * @throws MWException
109 * @return PPNode_Hash_Tree
111 function preprocessToObj( $text, $flags = 0 ) {
112 wfProfileIn( __METHOD__
);
115 global $wgMemc, $wgPreprocessorCacheThreshold;
117 $cacheable = $wgPreprocessorCacheThreshold !== false
118 && strlen( $text ) > $wgPreprocessorCacheThreshold;
121 wfProfileIn( __METHOD__
. '-cacheable' );
123 $cacheKey = wfMemcKey( 'preprocess-hash', md5( $text ), $flags );
124 $cacheValue = $wgMemc->get( $cacheKey );
126 $version = substr( $cacheValue, 0, 8 );
127 if ( intval( $version ) == self
::CACHE_VERSION
) {
128 $hash = unserialize( substr( $cacheValue, 8 ) );
130 wfDebugLog( "Preprocessor",
131 "Loaded preprocessor hash from memcached (key $cacheKey)" );
132 wfProfileOut( __METHOD__
. '-cacheable' );
133 wfProfileOut( __METHOD__
);
137 wfProfileIn( __METHOD__
. '-cache-miss' );
152 'names' => array( 2 => null ),
158 $forInclusion = $flags & Parser
::PTD_FOR_INCLUSION
;
160 $xmlishElements = $this->parser
->getStripList();
161 $enableOnlyinclude = false;
162 if ( $forInclusion ) {
163 $ignoredTags = array( 'includeonly', '/includeonly' );
164 $ignoredElements = array( 'noinclude' );
165 $xmlishElements[] = 'noinclude';
166 if ( strpos( $text, '<onlyinclude>' ) !== false
167 && strpos( $text, '</onlyinclude>' ) !== false
169 $enableOnlyinclude = true;
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_Hash
;
183 $searchBase = "[{<\n";
184 // For fast reverse searches
185 $revText = strrev( $text );
186 $lengthText = strlen( $text );
188 // Input pointer, starts out pointing to a pseudo-newline before the start
190 // Current accumulator
191 $accum =& $stack->getAccum();
192 // True to find equals signs in arguments
194 // True to take notice of pipe characters
197 // True if $i is inside a possible heading
199 // True if there are no more greater-than (>) signs right of $i
201 // True to ignore all input up to the next <onlyinclude>
202 $findOnlyinclude = $enableOnlyinclude;
203 // Do a line-start run without outputting an LF character
204 $fakeLineStart = true;
209 if ( $findOnlyinclude ) {
210 // Ignore all input up to the next <onlyinclude>
211 $startPos = strpos( $text, '<onlyinclude>', $i );
212 if ( $startPos === false ) {
213 // Ignored section runs to the end
214 $accum->addNodeWithText( 'ignore', substr( $text, $i ) );
217 $tagEndPos = $startPos +
strlen( '<onlyinclude>' ); // past-the-end
218 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i ) );
220 $findOnlyinclude = false;
223 if ( $fakeLineStart ) {
224 $found = 'line-start';
227 # Find next opening brace, closing brace or pipe
228 $search = $searchBase;
229 if ( $stack->top
=== false ) {
230 $currentClosing = '';
232 $currentClosing = $stack->top
->close
;
233 $search .= $currentClosing;
239 // First equals will be for the template
243 # Output literal section, advance input counter
244 $literalLength = strcspn( $text, $search, $i );
245 if ( $literalLength > 0 ) {
246 $accum->addLiteral( substr( $text, $i, $literalLength ) );
247 $i +
= $literalLength;
249 if ( $i >= $lengthText ) {
250 if ( $currentClosing == "\n" ) {
251 // Do a past-the-end run to finish off the heading
259 $curChar = $text[$i];
260 if ( $curChar == '|' ) {
262 } elseif ( $curChar == '=' ) {
264 } elseif ( $curChar == '<' ) {
266 } elseif ( $curChar == "\n" ) {
270 $found = 'line-start';
272 } elseif ( $curChar == $currentClosing ) {
274 } elseif ( isset( $rules[$curChar] ) ) {
276 $rule = $rules[$curChar];
278 # Some versions of PHP have a strcspn which stops on null characters
279 # Ignore and continue
286 if ( $found == 'angle' ) {
288 // Handle </onlyinclude>
289 if ( $enableOnlyinclude
290 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
292 $findOnlyinclude = true;
296 // Determine element name
297 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i +
1 ) ) {
298 // Element name missing or not listed
299 $accum->addLiteral( '<' );
304 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
306 // To avoid leaving blank lines, when a sequence of
307 // space-separated comments is both preceded and followed by
308 // a newline (ignoring spaces), then
309 // trim leading and trailing spaces and the trailing newline.
312 $endPos = strpos( $text, '-->', $i +
4 );
313 if ( $endPos === false ) {
314 // Unclosed comment in input, runs to end
315 $inner = substr( $text, $i );
316 $accum->addNodeWithText( 'comment', $inner );
319 // Search backwards for leading whitespace
320 $wsStart = $i ?
( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
322 // Search forwards for trailing whitespace
323 // $wsEnd will be the position of the last space (or the '>' if there's none)
324 $wsEnd = $endPos +
2 +
strspn( $text, " \t", $endPos +
3 );
326 // Keep looking forward as long as we're finding more
328 $comments = array( array( $wsStart, $wsEnd ) );
329 while ( substr( $text, $wsEnd +
1, 4 ) == '<!--' ) {
330 $c = strpos( $text, '-->', $wsEnd +
4 );
331 if ( $c === false ) {
334 $c = $c +
2 +
strspn( $text, " \t", $c +
3 );
335 $comments[] = array( $wsEnd +
1, $c );
339 // Eat the line if possible
340 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
341 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
342 // it's a possible beneficial b/c break.
343 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
344 && substr( $text, $wsEnd +
1, 1 ) == "\n"
346 // Remove leading whitespace from the end of the accumulator
347 // Sanity check first though
348 $wsLength = $i - $wsStart;
350 && $accum->lastNode
instanceof PPNode_Hash_Text
351 && strspn( $accum->lastNode
->value
, " \t", -$wsLength ) === $wsLength
353 $accum->lastNode
->value
= substr( $accum->lastNode
->value
, 0, -$wsLength );
356 // Dump all but the last comment to the accumulator
357 foreach ( $comments as $j => $com ) {
359 $endPos = $com[1] +
1;
360 if ( $j == ( count( $comments ) - 1 ) ) {
363 $inner = substr( $text, $startPos, $endPos - $startPos );
364 $accum->addNodeWithText( 'comment', $inner );
367 // Do a line-start run next time to look for headings after the comment
368 $fakeLineStart = true;
370 // No line to eat, just take the comment itself
376 $part = $stack->top
->getCurrentPart();
377 if ( !( isset( $part->commentEnd
) && $part->commentEnd
== $wsStart - 1 ) ) {
378 $part->visualEnd
= $wsStart;
380 // Else comments abutting, no change in visual end
381 $part->commentEnd
= $endPos;
384 $inner = substr( $text, $startPos, $endPos - $startPos +
1 );
385 $accum->addNodeWithText( 'comment', $inner );
390 $lowerName = strtolower( $name );
391 $attrStart = $i +
strlen( $name ) +
1;
394 $tagEndPos = $noMoreGT ?
false : strpos( $text, '>', $attrStart );
395 if ( $tagEndPos === false ) {
396 // Infinite backtrack
397 // Disable tag search to prevent worst-case O(N^2) performance
399 $accum->addLiteral( '<' );
404 // Handle ignored tags
405 if ( in_array( $lowerName, $ignoredTags ) ) {
406 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i +
1 ) );
412 if ( $text[$tagEndPos - 1] == '/' ) {
414 $attrEnd = $tagEndPos - 1;
419 $attrEnd = $tagEndPos;
421 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
422 $text, $matches, PREG_OFFSET_CAPTURE
, $tagEndPos +
1 )
424 $inner = substr( $text, $tagEndPos +
1, $matches[0][1] - $tagEndPos - 1 );
425 $i = $matches[0][1] +
strlen( $matches[0][0] );
426 $close = $matches[0][0];
428 // No end tag -- let it run out to the end of the text.
429 $inner = substr( $text, $tagEndPos +
1 );
434 // <includeonly> and <noinclude> just become <ignore> tags
435 if ( in_array( $lowerName, $ignoredElements ) ) {
436 $accum->addNodeWithText( 'ignore', substr( $text, $tagStartPos, $i - $tagStartPos ) );
440 if ( $attrEnd <= $attrStart ) {
443 // Note that the attr element contains the whitespace between name and attribute,
444 // this is necessary for precise reconstruction during pre-save transform.
445 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
448 $extNode = new PPNode_Hash_Tree( 'ext' );
449 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'name', $name ) );
450 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'attr', $attr ) );
451 if ( $inner !== null ) {
452 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'inner', $inner ) );
454 if ( $close !== null ) {
455 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'close', $close ) );
457 $accum->addNode( $extNode );
458 } elseif ( $found == 'line-start' ) {
459 // Is this the start of a heading?
460 // Line break belongs before the heading element in any case
461 if ( $fakeLineStart ) {
462 $fakeLineStart = false;
464 $accum->addLiteral( $curChar );
468 $count = strspn( $text, '=', $i, 6 );
469 if ( $count == 1 && $findEquals ) {
470 // DWIM: This looks kind of like a name/value separator.
471 // Let's let the equals handler have it and break the potential
472 // heading. This is heuristic, but AFAICT the methods for
473 // completely correct disambiguation are very complex.
474 } elseif ( $count > 0 ) {
478 'parts' => array( new PPDPart_Hash( str_repeat( '=', $count ) ) ),
481 $stack->push( $piece );
482 $accum =& $stack->getAccum();
483 extract( $stack->getFlags() );
486 } elseif ( $found == 'line-end' ) {
487 $piece = $stack->top
;
488 // A heading must be open, otherwise \n wouldn't have been in the search list
489 assert( '$piece->open == "\n"' );
490 $part = $piece->getCurrentPart();
491 // Search back through the input to see if it has a proper close.
492 // Do this using the reversed string since the other solutions
493 // (end anchor, etc.) are inefficient.
494 $wsLength = strspn( $revText, " \t", $lengthText - $i );
495 $searchStart = $i - $wsLength;
496 if ( isset( $part->commentEnd
) && $searchStart - 1 == $part->commentEnd
) {
497 // Comment found at line end
498 // Search for equals signs before the comment
499 $searchStart = $part->visualEnd
;
500 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
502 $count = $piece->count
;
503 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
504 if ( $equalsLength > 0 ) {
505 if ( $searchStart - $equalsLength == $piece->startPos
) {
506 // This is just a single string of equals signs on its own line
507 // Replicate the doHeadings behavior /={count}(.+)={count}/
508 // First find out how many equals signs there really are (don't stop at 6)
509 $count = $equalsLength;
513 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
516 $count = min( $equalsLength, $count );
519 // Normal match, output <h>
520 $element = new PPNode_Hash_Tree( 'possible-h' );
521 $element->addChild( new PPNode_Hash_Attr( 'level', $count ) );
522 $element->addChild( new PPNode_Hash_Attr( 'i', $headingIndex++
) );
523 $element->lastChild
->nextSibling
= $accum->firstNode
;
524 $element->lastChild
= $accum->lastNode
;
526 // Single equals sign on its own line, count=0
530 // No match, no <h>, just pass down the inner text
535 $accum =& $stack->getAccum();
536 extract( $stack->getFlags() );
538 // Append the result to the enclosing accumulator
539 if ( $element instanceof PPNode
) {
540 $accum->addNode( $element );
542 $accum->addAccum( $element );
544 // Note that we do NOT increment the input pointer.
545 // This is because the closing linebreak could be the opening linebreak of
546 // another heading. Infinite loops are avoided because the next iteration MUST
547 // hit the heading open case above, which unconditionally increments the
549 } elseif ( $found == 'open' ) {
550 # count opening brace characters
551 $count = strspn( $text, $curChar, $i );
553 # we need to add to stack only if opening brace count is enough for one of the rules
554 if ( $count >= $rule['min'] ) {
555 # Add it to the stack
558 'close' => $rule['end'],
560 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
563 $stack->push( $piece );
564 $accum =& $stack->getAccum();
565 extract( $stack->getFlags() );
567 # Add literal brace(s)
568 $accum->addLiteral( str_repeat( $curChar, $count ) );
571 } elseif ( $found == 'close' ) {
572 $piece = $stack->top
;
573 # lets check if there are enough characters for closing brace
574 $maxCount = $piece->count
;
575 $count = strspn( $text, $curChar, $i, $maxCount );
577 # check for maximum matching characters (if there are 5 closing
578 # characters, we will probably need only 3 - depending on the rules)
579 $rule = $rules[$piece->open
];
580 if ( $count > $rule['max'] ) {
581 # The specified maximum exists in the callback array, unless the caller
583 $matchingCount = $rule['max'];
585 # Count is less than the maximum
586 # Skip any gaps in the callback array to find the true largest match
587 # Need to use array_key_exists not isset because the callback can be null
588 $matchingCount = $count;
589 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
594 if ( $matchingCount <= 0 ) {
595 # No matching element found in callback array
596 # Output a literal closing brace and continue
597 $accum->addLiteral( str_repeat( $curChar, $count ) );
601 $name = $rule['names'][$matchingCount];
602 if ( $name === null ) {
603 // No element, just literal text
604 $element = $piece->breakSyntax( $matchingCount );
605 $element->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
608 # Note: $parts is already XML, does not need to be encoded further
609 $parts = $piece->parts
;
610 $titleAccum = $parts[0]->out
;
613 $element = new PPNode_Hash_Tree( $name );
615 # The invocation is at the start of the line if lineStart is set in
616 # the stack, and all opening brackets are used up.
617 if ( $maxCount == $matchingCount && !empty( $piece->lineStart
) ) {
618 $element->addChild( new PPNode_Hash_Attr( 'lineStart', 1 ) );
620 $titleNode = new PPNode_Hash_Tree( 'title' );
621 $titleNode->firstChild
= $titleAccum->firstNode
;
622 $titleNode->lastChild
= $titleAccum->lastNode
;
623 $element->addChild( $titleNode );
625 foreach ( $parts as $part ) {
626 if ( isset( $part->eqpos
) ) {
629 for ( $node = $part->out
->firstNode
; $node; $node = $node->nextSibling
) {
630 if ( $node === $part->eqpos
) {
637 wfProfileOut( __METHOD__
. '-cache-miss' );
638 wfProfileOut( __METHOD__
. '-cacheable' );
640 wfProfileOut( __METHOD__
);
641 throw new MWException( __METHOD__
. ': eqpos not found' );
643 if ( $node->name
!== 'equals' ) {
645 wfProfileOut( __METHOD__
. '-cache-miss' );
646 wfProfileOut( __METHOD__
. '-cacheable' );
648 wfProfileOut( __METHOD__
);
649 throw new MWException( __METHOD__
. ': eqpos is not equals' );
653 // Construct name node
654 $nameNode = new PPNode_Hash_Tree( 'name' );
655 if ( $lastNode !== false ) {
656 $lastNode->nextSibling
= false;
657 $nameNode->firstChild
= $part->out
->firstNode
;
658 $nameNode->lastChild
= $lastNode;
661 // Construct value node
662 $valueNode = new PPNode_Hash_Tree( 'value' );
663 if ( $equalsNode->nextSibling
!== false ) {
664 $valueNode->firstChild
= $equalsNode->nextSibling
;
665 $valueNode->lastChild
= $part->out
->lastNode
;
667 $partNode = new PPNode_Hash_Tree( 'part' );
668 $partNode->addChild( $nameNode );
669 $partNode->addChild( $equalsNode->firstChild
);
670 $partNode->addChild( $valueNode );
671 $element->addChild( $partNode );
673 $partNode = new PPNode_Hash_Tree( 'part' );
674 $nameNode = new PPNode_Hash_Tree( 'name' );
675 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $argIndex++
) );
676 $valueNode = new PPNode_Hash_Tree( 'value' );
677 $valueNode->firstChild
= $part->out
->firstNode
;
678 $valueNode->lastChild
= $part->out
->lastNode
;
679 $partNode->addChild( $nameNode );
680 $partNode->addChild( $valueNode );
681 $element->addChild( $partNode );
686 # Advance input pointer
687 $i +
= $matchingCount;
691 $accum =& $stack->getAccum();
693 # Re-add the old stack element if it still has unmatched opening characters remaining
694 if ( $matchingCount < $piece->count
) {
695 $piece->parts
= array( new PPDPart_Hash
);
696 $piece->count
-= $matchingCount;
697 # do we still qualify for any callback with remaining count?
698 $min = $rules[$piece->open
]['min'];
699 if ( $piece->count
>= $min ) {
700 $stack->push( $piece );
701 $accum =& $stack->getAccum();
703 $accum->addLiteral( str_repeat( $piece->open
, $piece->count
) );
707 extract( $stack->getFlags() );
709 # Add XML element to the enclosing accumulator
710 if ( $element instanceof PPNode
) {
711 $accum->addNode( $element );
713 $accum->addAccum( $element );
715 } elseif ( $found == 'pipe' ) {
716 $findEquals = true; // shortcut for getFlags()
718 $accum =& $stack->getAccum();
720 } elseif ( $found == 'equals' ) {
721 $findEquals = false; // shortcut for getFlags()
722 $accum->addNodeWithText( 'equals', '=' );
723 $stack->getCurrentPart()->eqpos
= $accum->lastNode
;
728 # Output any remaining unclosed brackets
729 foreach ( $stack->stack
as $piece ) {
730 $stack->rootAccum
->addAccum( $piece->breakSyntax() );
733 # Enable top-level headings
734 for ( $node = $stack->rootAccum
->firstNode
; $node; $node = $node->nextSibling
) {
735 if ( isset( $node->name
) && $node->name
=== 'possible-h' ) {
740 $rootNode = new PPNode_Hash_Tree( 'root' );
741 $rootNode->firstChild
= $stack->rootAccum
->firstNode
;
742 $rootNode->lastChild
= $stack->rootAccum
->lastNode
;
746 $cacheValue = sprintf( "%08d", self
::CACHE_VERSION
) . serialize( $rootNode );
747 $wgMemc->set( $cacheKey, $cacheValue, 86400 );
748 wfProfileOut( __METHOD__
. '-cache-miss' );
749 wfProfileOut( __METHOD__
. '-cacheable' );
750 wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
753 wfProfileOut( __METHOD__
);
759 * Stack class to help Preprocessor::preprocessToObj()
762 class PPDStack_Hash
extends PPDStack
{
763 function __construct() {
764 $this->elementClass
= 'PPDStackElement_Hash';
765 parent
::__construct();
766 $this->rootAccum
= new PPDAccum_Hash
;
773 class PPDStackElement_Hash
extends PPDStackElement
{
774 function __construct( $data = array() ) {
775 $this->partClass
= 'PPDPart_Hash';
776 parent
::__construct( $data );
780 * Get the accumulator that would result if the close is not found.
782 * @param int|bool $openingCount
783 * @return PPDAccum_Hash
785 function breakSyntax( $openingCount = false ) {
786 if ( $this->open
== "\n" ) {
787 $accum = $this->parts
[0]->out
;
789 if ( $openingCount === false ) {
790 $openingCount = $this->count
;
792 $accum = new PPDAccum_Hash
;
793 $accum->addLiteral( str_repeat( $this->open
, $openingCount ) );
795 foreach ( $this->parts
as $part ) {
799 $accum->addLiteral( '|' );
801 $accum->addAccum( $part->out
);
811 class PPDPart_Hash
extends PPDPart
{
812 function __construct( $out = '' ) {
813 $accum = new PPDAccum_Hash
;
815 $accum->addLiteral( $out );
817 parent
::__construct( $accum );
824 class PPDAccum_Hash
{
825 var $firstNode, $lastNode;
827 function __construct() {
828 $this->firstNode
= $this->lastNode
= false;
832 * Append a string literal
835 function addLiteral( $s ) {
836 if ( $this->lastNode
=== false ) {
837 $this->firstNode
= $this->lastNode
= new PPNode_Hash_Text( $s );
838 } elseif ( $this->lastNode
instanceof PPNode_Hash_Text
) {
839 $this->lastNode
->value
.= $s;
841 $this->lastNode
->nextSibling
= new PPNode_Hash_Text( $s );
842 $this->lastNode
= $this->lastNode
->nextSibling
;
848 * @param PPNode $node
850 function addNode( PPNode
$node ) {
851 if ( $this->lastNode
=== false ) {
852 $this->firstNode
= $this->lastNode
= $node;
854 $this->lastNode
->nextSibling
= $node;
855 $this->lastNode
= $node;
860 * Append a tree node with text contents
861 * @param string $name
862 * @param string $value
864 function addNodeWithText( $name, $value ) {
865 $node = PPNode_Hash_Tree
::newWithText( $name, $value );
866 $this->addNode( $node );
870 * Append a PPDAccum_Hash
871 * Takes over ownership of the nodes in the source argument. These nodes may
872 * subsequently be modified, especially nextSibling.
873 * @param PPDAccum_Hash $accum
875 function addAccum( $accum ) {
876 if ( $accum->lastNode
=== false ) {
878 } elseif ( $this->lastNode
=== false ) {
879 $this->firstNode
= $accum->firstNode
;
880 $this->lastNode
= $accum->lastNode
;
882 $this->lastNode
->nextSibling
= $accum->firstNode
;
883 $this->lastNode
= $accum->lastNode
;
889 * An expansion frame, used as a context to expand the result of preprocessToObj()
892 class PPFrame_Hash
implements PPFrame
{
911 * Hashtable listing templates which are disallowed for expansion in this frame,
912 * having been encountered previously in parent frames.
917 * Recursion depth of this frame, top = 0
918 * Note that this is NOT the same as expansion depth in expand()
922 private $volatile = false;
928 protected $childExpansionCache;
931 * Construct a new preprocessor frame.
932 * @param Preprocessor $preprocessor The parent preprocessor
934 function __construct( $preprocessor ) {
935 $this->preprocessor
= $preprocessor;
936 $this->parser
= $preprocessor->parser
;
937 $this->title
= $this->parser
->mTitle
;
938 $this->titleCache
= array( $this->title ?
$this->title
->getPrefixedDBkey() : false );
939 $this->loopCheckHash
= array();
941 $this->childExpansionCache
= array();
945 * Create a new child frame
946 * $args is optionally a multi-root PPNode or array containing the template arguments
948 * @param array|bool|PPNode_Hash_Array $args
949 * @param Title|bool $title
950 * @param int $indexOffset
951 * @throws MWException
952 * @return PPTemplateFrame_Hash
954 function newChild( $args = false, $title = false, $indexOffset = 0 ) {
955 $namedArgs = array();
956 $numberedArgs = array();
957 if ( $title === false ) {
958 $title = $this->title
;
960 if ( $args !== false ) {
961 if ( $args instanceof PPNode_Hash_Array
) {
962 $args = $args->value
;
963 } elseif ( !is_array( $args ) ) {
964 throw new MWException( __METHOD__
. ': $args must be array or PPNode_Hash_Array' );
966 foreach ( $args as $arg ) {
967 $bits = $arg->splitArg();
968 if ( $bits['index'] !== '' ) {
969 // Numbered parameter
970 $index = $bits['index'] - $indexOffset;
971 $numberedArgs[$index] = $bits['value'];
972 unset( $namedArgs[$index] );
975 $name = trim( $this->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
976 $namedArgs[$name] = $bits['value'];
977 unset( $numberedArgs[$name] );
981 return new PPTemplateFrame_Hash( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
985 * @throws MWException
986 * @param string|int $key
987 * @param string|PPNode_Hash|DOMDocument $root
991 function cachedExpand( $key, $root, $flags = 0 ) {
992 // we don't have a parent, so we don't have a cache
993 return $this->expand( $root, $flags );
997 * @throws MWException
998 * @param string|PPNode$root
1002 function expand( $root, $flags = 0 ) {
1003 static $expansionDepth = 0;
1004 if ( is_string( $root ) ) {
1008 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
1009 $this->parser
->limitationWarn( 'node-count-exceeded',
1010 $this->parser
->mPPNodeCount
,
1011 $this->parser
->mOptions
->getMaxPPNodeCount()
1013 return '<span class="error">Node-count limit exceeded</span>';
1015 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
1016 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
1018 $this->parser
->mOptions
->getMaxPPExpandDepth()
1020 return '<span class="error">Expansion depth limit exceeded</span>';
1023 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
1024 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
1027 $outStack = array( '', '' );
1028 $iteratorStack = array( false, $root );
1029 $indexStack = array( 0, 0 );
1031 while ( count( $iteratorStack ) > 1 ) {
1032 $level = count( $outStack ) - 1;
1033 $iteratorNode =& $iteratorStack[$level];
1034 $out =& $outStack[$level];
1035 $index =& $indexStack[$level];
1037 if ( is_array( $iteratorNode ) ) {
1038 if ( $index >= count( $iteratorNode ) ) {
1039 // All done with this iterator
1040 $iteratorStack[$level] = false;
1041 $contextNode = false;
1043 $contextNode = $iteratorNode[$index];
1046 } elseif ( $iteratorNode instanceof PPNode_Hash_Array
) {
1047 if ( $index >= $iteratorNode->getLength() ) {
1048 // All done with this iterator
1049 $iteratorStack[$level] = false;
1050 $contextNode = false;
1052 $contextNode = $iteratorNode->item( $index );
1056 // Copy to $contextNode and then delete from iterator stack,
1057 // because this is not an iterator but we do have to execute it once
1058 $contextNode = $iteratorStack[$level];
1059 $iteratorStack[$level] = false;
1062 $newIterator = false;
1064 if ( $contextNode === false ) {
1066 } elseif ( is_string( $contextNode ) ) {
1067 $out .= $contextNode;
1068 } elseif ( is_array( $contextNode ) ||
$contextNode instanceof PPNode_Hash_Array
) {
1069 $newIterator = $contextNode;
1070 } elseif ( $contextNode instanceof PPNode_Hash_Attr
) {
1072 } elseif ( $contextNode instanceof PPNode_Hash_Text
) {
1073 $out .= $contextNode->value
;
1074 } elseif ( $contextNode instanceof PPNode_Hash_Tree
) {
1075 if ( $contextNode->name
== 'template' ) {
1076 # Double-brace expansion
1077 $bits = $contextNode->splitTemplate();
1078 if ( $flags & PPFrame
::NO_TEMPLATES
) {
1079 $newIterator = $this->virtualBracketedImplode(
1085 $ret = $this->parser
->braceSubstitution( $bits, $this );
1086 if ( isset( $ret['object'] ) ) {
1087 $newIterator = $ret['object'];
1089 $out .= $ret['text'];
1092 } elseif ( $contextNode->name
== 'tplarg' ) {
1093 # Triple-brace expansion
1094 $bits = $contextNode->splitTemplate();
1095 if ( $flags & PPFrame
::NO_ARGS
) {
1096 $newIterator = $this->virtualBracketedImplode(
1102 $ret = $this->parser
->argSubstitution( $bits, $this );
1103 if ( isset( $ret['object'] ) ) {
1104 $newIterator = $ret['object'];
1106 $out .= $ret['text'];
1109 } elseif ( $contextNode->name
== 'comment' ) {
1110 # HTML-style comment
1111 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1112 if ( $this->parser
->ot
['html']
1113 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
1114 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
1117 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
1118 # Add a strip marker in PST mode so that pstPass2() can
1119 # run some old-fashioned regexes on the result.
1120 # Not in RECOVER_COMMENTS mode (extractSections) though.
1121 $out .= $this->parser
->insertStripItem( $contextNode->firstChild
->value
);
1123 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1124 $out .= $contextNode->firstChild
->value
;
1126 } elseif ( $contextNode->name
== 'ignore' ) {
1127 # Output suppression used by <includeonly> etc.
1128 # OT_WIKI will only respect <ignore> in substed templates.
1129 # The other output types respect it unless NO_IGNORE is set.
1130 # extractSections() sets NO_IGNORE and so never respects it.
1131 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] )
1132 ||
( $flags & PPFrame
::NO_IGNORE
)
1134 $out .= $contextNode->firstChild
->value
;
1138 } elseif ( $contextNode->name
== 'ext' ) {
1140 $bits = $contextNode->splitExt() +
array( 'attr' => null, 'inner' => null, 'close' => null );
1141 $out .= $this->parser
->extensionSubstitution( $bits, $this );
1142 } elseif ( $contextNode->name
== 'h' ) {
1144 if ( $this->parser
->ot
['html'] ) {
1145 # Expand immediately and insert heading index marker
1147 for ( $node = $contextNode->firstChild
; $node; $node = $node->nextSibling
) {
1148 $s .= $this->expand( $node, $flags );
1151 $bits = $contextNode->splitHeading();
1152 $titleText = $this->title
->getPrefixedDBkey();
1153 $this->parser
->mHeadings
[] = array( $titleText, $bits['i'] );
1154 $serial = count( $this->parser
->mHeadings
) - 1;
1155 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser
::MARKER_SUFFIX
;
1156 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1157 $this->parser
->mStripState
->addGeneral( $marker, '' );
1160 # Expand in virtual stack
1161 $newIterator = $contextNode->getChildren();
1164 # Generic recursive expansion
1165 $newIterator = $contextNode->getChildren();
1168 throw new MWException( __METHOD__
. ': Invalid parameter type' );
1171 if ( $newIterator !== false ) {
1173 $iteratorStack[] = $newIterator;
1175 } elseif ( $iteratorStack[$level] === false ) {
1176 // Return accumulated value to parent
1177 // With tail recursion
1178 while ( $iteratorStack[$level] === false && $level > 0 ) {
1179 $outStack[$level - 1] .= $out;
1180 array_pop( $outStack );
1181 array_pop( $iteratorStack );
1182 array_pop( $indexStack );
1188 return $outStack[0];
1192 * @param string $sep
1196 function implodeWithFlags( $sep, $flags /*, ... */ ) {
1197 $args = array_slice( func_get_args(), 2 );
1201 foreach ( $args as $root ) {
1202 if ( $root instanceof PPNode_Hash_Array
) {
1203 $root = $root->value
;
1205 if ( !is_array( $root ) ) {
1206 $root = array( $root );
1208 foreach ( $root as $node ) {
1214 $s .= $this->expand( $node, $flags );
1221 * Implode with no flags specified
1222 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1223 * @param string $sep
1226 function implode( $sep /*, ... */ ) {
1227 $args = array_slice( func_get_args(), 1 );
1231 foreach ( $args as $root ) {
1232 if ( $root instanceof PPNode_Hash_Array
) {
1233 $root = $root->value
;
1235 if ( !is_array( $root ) ) {
1236 $root = array( $root );
1238 foreach ( $root as $node ) {
1244 $s .= $this->expand( $node );
1251 * Makes an object that, when expand()ed, will be the same as one obtained
1254 * @param string $sep
1255 * @return PPNode_Hash_Array
1257 function virtualImplode( $sep /*, ... */ ) {
1258 $args = array_slice( func_get_args(), 1 );
1262 foreach ( $args as $root ) {
1263 if ( $root instanceof PPNode_Hash_Array
) {
1264 $root = $root->value
;
1266 if ( !is_array( $root ) ) {
1267 $root = array( $root );
1269 foreach ( $root as $node ) {
1278 return new PPNode_Hash_Array( $out );
1282 * Virtual implode with brackets
1284 * @param string $start
1285 * @param string $sep
1286 * @param string $end
1287 * @return PPNode_Hash_Array
1289 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1290 $args = array_slice( func_get_args(), 3 );
1291 $out = array( $start );
1294 foreach ( $args as $root ) {
1295 if ( $root instanceof PPNode_Hash_Array
) {
1296 $root = $root->value
;
1298 if ( !is_array( $root ) ) {
1299 $root = array( $root );
1301 foreach ( $root as $node ) {
1311 return new PPNode_Hash_Array( $out );
1314 function __toString() {
1319 * @param bool $level
1320 * @return array|bool|string
1322 function getPDBK( $level = false ) {
1323 if ( $level === false ) {
1324 return $this->title
->getPrefixedDBkey();
1326 return isset( $this->titleCache
[$level] ) ?
$this->titleCache
[$level] : false;
1333 function getArguments() {
1340 function getNumberedArguments() {
1347 function getNamedArguments() {
1352 * Returns true if there are no arguments in this frame
1356 function isEmpty() {
1361 * @param string $name
1364 function getArgument( $name ) {
1369 * Returns true if the infinite loop check is OK, false if a loop is detected
1371 * @param Title $title
1375 function loopCheck( $title ) {
1376 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
1380 * Return true if the frame is a template frame
1384 function isTemplate() {
1389 * Get a title of frame
1393 function getTitle() {
1394 return $this->title
;
1398 * Set the volatile flag
1402 function setVolatile( $flag = true ) {
1403 $this->volatile
= $flag;
1407 * Get the volatile flag
1411 function isVolatile() {
1412 return $this->volatile
;
1420 function setTTL( $ttl ) {
1421 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
1437 * Expansion frame with template arguments
1440 class PPTemplateFrame_Hash
extends PPFrame_Hash
{
1441 var $numberedArgs, $namedArgs, $parent;
1442 var $numberedExpansionCache, $namedExpansionCache;
1445 * @param Preprocessor $preprocessor
1446 * @param bool|PPFrame $parent
1447 * @param array $numberedArgs
1448 * @param array $namedArgs
1449 * @param bool|Title $title
1451 function __construct( $preprocessor, $parent = false, $numberedArgs = array(),
1452 $namedArgs = array(), $title = false
1454 parent
::__construct( $preprocessor );
1456 $this->parent
= $parent;
1457 $this->numberedArgs
= $numberedArgs;
1458 $this->namedArgs
= $namedArgs;
1459 $this->title
= $title;
1460 $pdbk = $title ?
$title->getPrefixedDBkey() : false;
1461 $this->titleCache
= $parent->titleCache
;
1462 $this->titleCache
[] = $pdbk;
1463 $this->loopCheckHash
= /*clone*/ $parent->loopCheckHash
;
1464 if ( $pdbk !== false ) {
1465 $this->loopCheckHash
[$pdbk] = true;
1467 $this->depth
= $parent->depth +
1;
1468 $this->numberedExpansionCache
= $this->namedExpansionCache
= array();
1471 function __toString() {
1474 $args = $this->numberedArgs +
$this->namedArgs
;
1475 foreach ( $args as $name => $value ) {
1481 $s .= "\"$name\":\"" .
1482 str_replace( '"', '\\"', $value->__toString() ) . '"';
1489 * @throws MWException
1490 * @param string|int $key
1491 * @param string|PPNode_Hash|DOMDocument $root
1495 function cachedExpand( $key, $root, $flags = 0 ) {
1496 if ( isset( $this->parent
->childExpansionCache
[$key] ) ) {
1497 return $this->parent
->childExpansionCache
[$key];
1499 $retval = $this->expand( $root, $flags );
1500 if ( !$this->isVolatile() ) {
1501 $this->parent
->childExpansionCache
[$key] = $retval;
1507 * Returns true if there are no arguments in this frame
1511 function isEmpty() {
1512 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
1518 function getArguments() {
1519 $arguments = array();
1520 foreach ( array_merge(
1521 array_keys( $this->numberedArgs
),
1522 array_keys( $this->namedArgs
) ) as $key ) {
1523 $arguments[$key] = $this->getArgument( $key );
1531 function getNumberedArguments() {
1532 $arguments = array();
1533 foreach ( array_keys( $this->numberedArgs
) as $key ) {
1534 $arguments[$key] = $this->getArgument( $key );
1542 function getNamedArguments() {
1543 $arguments = array();
1544 foreach ( array_keys( $this->namedArgs
) as $key ) {
1545 $arguments[$key] = $this->getArgument( $key );
1552 * @return array|bool
1554 function getNumberedArgument( $index ) {
1555 if ( !isset( $this->numberedArgs
[$index] ) ) {
1558 if ( !isset( $this->numberedExpansionCache
[$index] ) ) {
1559 # No trimming for unnamed arguments
1560 $this->numberedExpansionCache
[$index] = $this->parent
->expand(
1561 $this->numberedArgs
[$index],
1562 PPFrame
::STRIP_COMMENTS
1565 return $this->numberedExpansionCache
[$index];
1569 * @param string $name
1572 function getNamedArgument( $name ) {
1573 if ( !isset( $this->namedArgs
[$name] ) ) {
1576 if ( !isset( $this->namedExpansionCache
[$name] ) ) {
1577 # Trim named arguments post-expand, for backwards compatibility
1578 $this->namedExpansionCache
[$name] = trim(
1579 $this->parent
->expand( $this->namedArgs
[$name], PPFrame
::STRIP_COMMENTS
) );
1581 return $this->namedExpansionCache
[$name];
1585 * @param string $name
1586 * @return array|bool
1588 function getArgument( $name ) {
1589 $text = $this->getNumberedArgument( $name );
1590 if ( $text === false ) {
1591 $text = $this->getNamedArgument( $name );
1597 * Return true if the frame is a template frame
1601 function isTemplate() {
1605 function setVolatile( $flag = true ) {
1606 parent
::setVolatile( $flag );
1607 $this->parent
->setVolatile( $flag );
1610 function setTTL( $ttl ) {
1611 parent
::setTTL( $ttl );
1612 $this->parent
->setTTL( $ttl );
1617 * Expansion frame with custom arguments
1620 class PPCustomFrame_Hash
extends PPFrame_Hash
{
1623 function __construct( $preprocessor, $args ) {
1624 parent
::__construct( $preprocessor );
1625 $this->args
= $args;
1628 function __toString() {
1631 foreach ( $this->args
as $name => $value ) {
1637 $s .= "\"$name\":\"" .
1638 str_replace( '"', '\\"', $value->__toString() ) . '"';
1647 function isEmpty() {
1648 return !count( $this->args
);
1655 function getArgument( $index ) {
1656 if ( !isset( $this->args
[$index] ) ) {
1659 return $this->args
[$index];
1662 function getArguments() {
1670 class PPNode_Hash_Tree
implements PPNode
{
1671 var $name, $firstChild, $lastChild, $nextSibling;
1673 function __construct( $name ) {
1674 $this->name
= $name;
1675 $this->firstChild
= $this->lastChild
= $this->nextSibling
= false;
1678 function __toString() {
1681 for ( $node = $this->firstChild
; $node; $node = $node->nextSibling
) {
1682 if ( $node instanceof PPNode_Hash_Attr
) {
1683 $attribs .= ' ' . $node->name
. '="' . htmlspecialchars( $node->value
) . '"';
1685 $inner .= $node->__toString();
1688 if ( $inner === '' ) {
1689 return "<{$this->name}$attribs/>";
1691 return "<{$this->name}$attribs>$inner</{$this->name}>";
1696 * @param string $name
1697 * @param string $text
1698 * @return PPNode_Hash_Tree
1700 static function newWithText( $name, $text ) {
1701 $obj = new self( $name );
1702 $obj->addChild( new PPNode_Hash_Text( $text ) );
1706 function addChild( $node ) {
1707 if ( $this->lastChild
=== false ) {
1708 $this->firstChild
= $this->lastChild
= $node;
1710 $this->lastChild
->nextSibling
= $node;
1711 $this->lastChild
= $node;
1716 * @return PPNode_Hash_Array
1718 function getChildren() {
1719 $children = array();
1720 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1721 $children[] = $child;
1723 return new PPNode_Hash_Array( $children );
1726 function getFirstChild() {
1727 return $this->firstChild
;
1730 function getNextSibling() {
1731 return $this->nextSibling
;
1734 function getChildrenOfType( $name ) {
1735 $children = array();
1736 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1737 if ( isset( $child->name
) && $child->name
=== $name ) {
1738 $children[] = $child;
1747 function getLength() {
1755 function item( $i ) {
1762 function getName() {
1767 * Split a "<part>" node into an associative array containing:
1768 * - name PPNode name
1769 * - index String index
1770 * - value PPNode value
1772 * @throws MWException
1775 function splitArg() {
1777 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1778 if ( !isset( $child->name
) ) {
1781 if ( $child->name
=== 'name' ) {
1782 $bits['name'] = $child;
1783 if ( $child->firstChild
instanceof PPNode_Hash_Attr
1784 && $child->firstChild
->name
=== 'index'
1786 $bits['index'] = $child->firstChild
->value
;
1788 } elseif ( $child->name
=== 'value' ) {
1789 $bits['value'] = $child;
1793 if ( !isset( $bits['name'] ) ) {
1794 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
1796 if ( !isset( $bits['index'] ) ) {
1797 $bits['index'] = '';
1803 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1804 * All values in the resulting array are PPNodes. Inner and close are optional.
1806 * @throws MWException
1809 function splitExt() {
1811 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1812 if ( !isset( $child->name
) ) {
1815 if ( $child->name
== 'name' ) {
1816 $bits['name'] = $child;
1817 } elseif ( $child->name
== 'attr' ) {
1818 $bits['attr'] = $child;
1819 } elseif ( $child->name
== 'inner' ) {
1820 $bits['inner'] = $child;
1821 } elseif ( $child->name
== 'close' ) {
1822 $bits['close'] = $child;
1825 if ( !isset( $bits['name'] ) ) {
1826 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
1832 * Split an "<h>" node
1834 * @throws MWException
1837 function splitHeading() {
1838 if ( $this->name
!== 'h' ) {
1839 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1842 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1843 if ( !isset( $child->name
) ) {
1846 if ( $child->name
== 'i' ) {
1847 $bits['i'] = $child->value
;
1848 } elseif ( $child->name
== 'level' ) {
1849 $bits['level'] = $child->value
;
1852 if ( !isset( $bits['i'] ) ) {
1853 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1859 * Split a "<template>" or "<tplarg>" node
1861 * @throws MWException
1864 function splitTemplate() {
1866 $bits = array( 'lineStart' => '' );
1867 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1868 if ( !isset( $child->name
) ) {
1871 if ( $child->name
== 'title' ) {
1872 $bits['title'] = $child;
1874 if ( $child->name
== 'part' ) {
1877 if ( $child->name
== 'lineStart' ) {
1878 $bits['lineStart'] = '1';
1881 if ( !isset( $bits['title'] ) ) {
1882 throw new MWException( 'Invalid node passed to ' . __METHOD__
);
1884 $bits['parts'] = new PPNode_Hash_Array( $parts );
1892 class PPNode_Hash_Text
implements PPNode
{
1893 var $value, $nextSibling;
1895 function __construct( $value ) {
1896 if ( is_object( $value ) ) {
1897 throw new MWException( __CLASS__
. ' given object instead of string' );
1899 $this->value
= $value;
1902 function __toString() {
1903 return htmlspecialchars( $this->value
);
1906 function getNextSibling() {
1907 return $this->nextSibling
;
1910 function getChildren() {
1914 function getFirstChild() {
1918 function getChildrenOfType( $name ) {
1922 function getLength() {
1926 function item( $i ) {
1930 function getName() {
1934 function splitArg() {
1935 throw new MWException( __METHOD__
. ': not supported' );
1938 function splitExt() {
1939 throw new MWException( __METHOD__
. ': not supported' );
1942 function splitHeading() {
1943 throw new MWException( __METHOD__
. ': not supported' );
1950 class PPNode_Hash_Array
implements PPNode
{
1951 var $value, $nextSibling;
1953 function __construct( $value ) {
1954 $this->value
= $value;
1957 function __toString() {
1958 return var_export( $this, true );
1961 function getLength() {
1962 return count( $this->value
);
1965 function item( $i ) {
1966 return $this->value
[$i];
1969 function getName() {
1973 function getNextSibling() {
1974 return $this->nextSibling
;
1977 function getChildren() {
1981 function getFirstChild() {
1985 function getChildrenOfType( $name ) {
1989 function splitArg() {
1990 throw new MWException( __METHOD__
. ': not supported' );
1993 function splitExt() {
1994 throw new MWException( __METHOD__
. ': not supported' );
1997 function splitHeading() {
1998 throw new MWException( __METHOD__
. ': not supported' );
2005 class PPNode_Hash_Attr
implements PPNode
{
2006 var $name, $value, $nextSibling;
2008 function __construct( $name, $value ) {
2009 $this->name
= $name;
2010 $this->value
= $value;
2013 function __toString() {
2014 return "<@{$this->name}>" . htmlspecialchars( $this->value
) . "</@{$this->name}>";
2017 function getName() {
2021 function getNextSibling() {
2022 return $this->nextSibling
;
2025 function getChildren() {
2029 function getFirstChild() {
2033 function getChildrenOfType( $name ) {
2037 function getLength() {
2041 function item( $i ) {
2045 function splitArg() {
2046 throw new MWException( __METHOD__
. ': not supported' );
2049 function splitExt() {
2050 throw new MWException( __METHOD__
. ': not supported' );
2053 function splitHeading() {
2054 throw new MWException( __METHOD__
. ': not supported' );