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>
29 * @codingStandardsIgnoreStart
31 class Preprocessor_Hash
implements Preprocessor
{
32 // @codingStandardsIgnoreEnd
39 const CACHE_VERSION
= 1;
41 public function __construct( $parser ) {
42 $this->parser
= $parser;
46 * @return PPFrame_Hash
48 public function newFrame() {
49 return new PPFrame_Hash( $this );
54 * @return PPCustomFrame_Hash
56 public function newCustomFrame( $args ) {
57 return new PPCustomFrame_Hash( $this, $args );
61 * @param array $values
62 * @return PPNode_Hash_Array
64 public function newPartNodeArray( $values ) {
67 foreach ( $values as $k => $val ) {
68 $partNode = new PPNode_Hash_Tree( 'part' );
69 $nameNode = new PPNode_Hash_Tree( 'name' );
72 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $k ) );
73 $partNode->addChild( $nameNode );
75 $nameNode->addChild( new PPNode_Hash_Text( $k ) );
76 $partNode->addChild( $nameNode );
77 $partNode->addChild( new PPNode_Hash_Text( '=' ) );
80 $valueNode = new PPNode_Hash_Tree( 'value' );
81 $valueNode->addChild( new PPNode_Hash_Text( $val ) );
82 $partNode->addChild( $valueNode );
87 $node = new PPNode_Hash_Array( $list );
92 * Preprocess some wikitext and return the document tree.
93 * This is the ghost of Parser::replace_variables().
95 * @param string $text The text to parse
96 * @param int $flags Bitwise combination of:
97 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<includeonly>" as if the text is being
98 * included. Default is to assume a direct page view.
100 * The generated DOM tree must depend only on the input text and the flags.
101 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
103 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
104 * change in the DOM tree for a given text, must be passed through the section identifier
105 * in the section edit link and thus back to extractSections().
107 * The output of this function is currently only cached in process memory, but a persistent
108 * cache may be implemented at a later date which takes further advantage of these strict
109 * dependency requirements.
111 * @throws MWException
112 * @return PPNode_Hash_Tree
114 public function preprocessToObj( $text, $flags = 0 ) {
117 global $wgMemc, $wgPreprocessorCacheThreshold;
119 $cacheable = $wgPreprocessorCacheThreshold !== false
120 && strlen( $text ) > $wgPreprocessorCacheThreshold;
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)" );
150 'names' => array( 2 => null ),
156 $forInclusion = $flags & Parser
::PTD_FOR_INCLUSION
;
158 $xmlishElements = $this->parser
->getStripList();
159 $enableOnlyinclude = false;
160 if ( $forInclusion ) {
161 $ignoredTags = array( 'includeonly', '/includeonly' );
162 $ignoredElements = array( 'noinclude' );
163 $xmlishElements[] = 'noinclude';
164 if ( strpos( $text, '<onlyinclude>' ) !== false
165 && strpos( $text, '</onlyinclude>' ) !== false
167 $enableOnlyinclude = true;
170 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
171 $ignoredElements = array( 'includeonly' );
172 $xmlishElements[] = 'includeonly';
174 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
176 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
177 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
179 $stack = new PPDStack_Hash
;
181 $searchBase = "[{<\n";
182 // For fast reverse searches
183 $revText = strrev( $text );
184 $lengthText = strlen( $text );
186 // Input pointer, starts out pointing to a pseudo-newline before the start
188 // Current accumulator
189 $accum =& $stack->getAccum();
190 // True to find equals signs in arguments
192 // True to take notice of pipe characters
195 // True if $i is inside a possible heading
197 // True if there are no more greater-than (>) signs right of $i
199 // True to ignore all input up to the next <onlyinclude>
200 $findOnlyinclude = $enableOnlyinclude;
201 // Do a line-start run without outputting an LF character
202 $fakeLineStart = true;
207 if ( $findOnlyinclude ) {
208 // Ignore all input up to the next <onlyinclude>
209 $startPos = strpos( $text, '<onlyinclude>', $i );
210 if ( $startPos === false ) {
211 // Ignored section runs to the end
212 $accum->addNodeWithText( 'ignore', substr( $text, $i ) );
215 $tagEndPos = $startPos +
strlen( '<onlyinclude>' ); // past-the-end
216 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i ) );
218 $findOnlyinclude = false;
221 if ( $fakeLineStart ) {
222 $found = 'line-start';
225 # Find next opening brace, closing brace or pipe
226 $search = $searchBase;
227 if ( $stack->top
=== false ) {
228 $currentClosing = '';
230 $currentClosing = $stack->top
->close
;
231 $search .= $currentClosing;
237 // First equals will be for the template
241 # Output literal section, advance input counter
242 $literalLength = strcspn( $text, $search, $i );
243 if ( $literalLength > 0 ) {
244 $accum->addLiteral( substr( $text, $i, $literalLength ) );
245 $i +
= $literalLength;
247 if ( $i >= $lengthText ) {
248 if ( $currentClosing == "\n" ) {
249 // Do a past-the-end run to finish off the heading
257 $curChar = $text[$i];
258 if ( $curChar == '|' ) {
260 } elseif ( $curChar == '=' ) {
262 } elseif ( $curChar == '<' ) {
264 } elseif ( $curChar == "\n" ) {
268 $found = 'line-start';
270 } elseif ( $curChar == $currentClosing ) {
272 } elseif ( isset( $rules[$curChar] ) ) {
274 $rule = $rules[$curChar];
276 # Some versions of PHP have a strcspn which stops on null characters
277 # Ignore and continue
284 if ( $found == 'angle' ) {
286 // Handle </onlyinclude>
287 if ( $enableOnlyinclude
288 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
290 $findOnlyinclude = true;
294 // Determine element name
295 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i +
1 ) ) {
296 // Element name missing or not listed
297 $accum->addLiteral( '<' );
302 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
304 // To avoid leaving blank lines, when a sequence of
305 // space-separated comments is both preceded and followed by
306 // a newline (ignoring spaces), then
307 // trim leading and trailing spaces and the trailing newline.
310 $endPos = strpos( $text, '-->', $i +
4 );
311 if ( $endPos === false ) {
312 // Unclosed comment in input, runs to end
313 $inner = substr( $text, $i );
314 $accum->addNodeWithText( 'comment', $inner );
317 // Search backwards for leading whitespace
318 $wsStart = $i ?
( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
320 // Search forwards for trailing whitespace
321 // $wsEnd will be the position of the last space (or the '>' if there's none)
322 $wsEnd = $endPos +
2 +
strspn( $text, " \t", $endPos +
3 );
324 // Keep looking forward as long as we're finding more
326 $comments = array( array( $wsStart, $wsEnd ) );
327 while ( substr( $text, $wsEnd +
1, 4 ) == '<!--' ) {
328 $c = strpos( $text, '-->', $wsEnd +
4 );
329 if ( $c === false ) {
332 $c = $c +
2 +
strspn( $text, " \t", $c +
3 );
333 $comments[] = array( $wsEnd +
1, $c );
337 // Eat the line if possible
338 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
339 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
340 // it's a possible beneficial b/c break.
341 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
342 && substr( $text, $wsEnd +
1, 1 ) == "\n"
344 // Remove leading whitespace from the end of the accumulator
345 // Sanity check first though
346 $wsLength = $i - $wsStart;
348 && $accum->lastNode
instanceof PPNode_Hash_Text
349 && strspn( $accum->lastNode
->value
, " \t", -$wsLength ) === $wsLength
351 $accum->lastNode
->value
= substr( $accum->lastNode
->value
, 0, -$wsLength );
354 // Dump all but the last comment to the accumulator
355 foreach ( $comments as $j => $com ) {
357 $endPos = $com[1] +
1;
358 if ( $j == ( count( $comments ) - 1 ) ) {
361 $inner = substr( $text, $startPos, $endPos - $startPos );
362 $accum->addNodeWithText( 'comment', $inner );
365 // Do a line-start run next time to look for headings after the comment
366 $fakeLineStart = true;
368 // No line to eat, just take the comment itself
374 $part = $stack->top
->getCurrentPart();
375 if ( !( isset( $part->commentEnd
) && $part->commentEnd
== $wsStart - 1 ) ) {
376 $part->visualEnd
= $wsStart;
378 // Else comments abutting, no change in visual end
379 $part->commentEnd
= $endPos;
382 $inner = substr( $text, $startPos, $endPos - $startPos +
1 );
383 $accum->addNodeWithText( 'comment', $inner );
388 $lowerName = strtolower( $name );
389 $attrStart = $i +
strlen( $name ) +
1;
392 $tagEndPos = $noMoreGT ?
false : strpos( $text, '>', $attrStart );
393 if ( $tagEndPos === false ) {
394 // Infinite backtrack
395 // Disable tag search to prevent worst-case O(N^2) performance
397 $accum->addLiteral( '<' );
402 // Handle ignored tags
403 if ( in_array( $lowerName, $ignoredTags ) ) {
404 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i +
1 ) );
410 if ( $text[$tagEndPos - 1] == '/' ) {
412 $attrEnd = $tagEndPos - 1;
417 $attrEnd = $tagEndPos;
419 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
420 $text, $matches, PREG_OFFSET_CAPTURE
, $tagEndPos +
1 )
422 $inner = substr( $text, $tagEndPos +
1, $matches[0][1] - $tagEndPos - 1 );
423 $i = $matches[0][1] +
strlen( $matches[0][0] );
424 $close = $matches[0][0];
426 // No end tag -- let it run out to the end of the text.
427 $inner = substr( $text, $tagEndPos +
1 );
432 // <includeonly> and <noinclude> just become <ignore> tags
433 if ( in_array( $lowerName, $ignoredElements ) ) {
434 $accum->addNodeWithText( 'ignore', substr( $text, $tagStartPos, $i - $tagStartPos ) );
438 if ( $attrEnd <= $attrStart ) {
441 // Note that the attr element contains the whitespace between name and attribute,
442 // this is necessary for precise reconstruction during pre-save transform.
443 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
446 $extNode = new PPNode_Hash_Tree( 'ext' );
447 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'name', $name ) );
448 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'attr', $attr ) );
449 if ( $inner !== null ) {
450 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'inner', $inner ) );
452 if ( $close !== null ) {
453 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'close', $close ) );
455 $accum->addNode( $extNode );
456 } elseif ( $found == 'line-start' ) {
457 // Is this the start of a heading?
458 // Line break belongs before the heading element in any case
459 if ( $fakeLineStart ) {
460 $fakeLineStart = false;
462 $accum->addLiteral( $curChar );
466 $count = strspn( $text, '=', $i, 6 );
467 if ( $count == 1 && $findEquals ) {
468 // DWIM: This looks kind of like a name/value separator.
469 // Let's let the equals handler have it and break the potential
470 // heading. This is heuristic, but AFAICT the methods for
471 // completely correct disambiguation are very complex.
472 } elseif ( $count > 0 ) {
476 'parts' => array( new PPDPart_Hash( str_repeat( '=', $count ) ) ),
479 $stack->push( $piece );
480 $accum =& $stack->getAccum();
481 extract( $stack->getFlags() );
484 } elseif ( $found == 'line-end' ) {
485 $piece = $stack->top
;
486 // A heading must be open, otherwise \n wouldn't have been in the search list
487 assert( '$piece->open == "\n"' );
488 $part = $piece->getCurrentPart();
489 // Search back through the input to see if it has a proper close.
490 // Do this using the reversed string since the other solutions
491 // (end anchor, etc.) are inefficient.
492 $wsLength = strspn( $revText, " \t", $lengthText - $i );
493 $searchStart = $i - $wsLength;
494 if ( isset( $part->commentEnd
) && $searchStart - 1 == $part->commentEnd
) {
495 // Comment found at line end
496 // Search for equals signs before the comment
497 $searchStart = $part->visualEnd
;
498 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
500 $count = $piece->count
;
501 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
502 if ( $equalsLength > 0 ) {
503 if ( $searchStart - $equalsLength == $piece->startPos
) {
504 // This is just a single string of equals signs on its own line
505 // Replicate the doHeadings behavior /={count}(.+)={count}/
506 // First find out how many equals signs there really are (don't stop at 6)
507 $count = $equalsLength;
511 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
514 $count = min( $equalsLength, $count );
517 // Normal match, output <h>
518 $element = new PPNode_Hash_Tree( 'possible-h' );
519 $element->addChild( new PPNode_Hash_Attr( 'level', $count ) );
520 $element->addChild( new PPNode_Hash_Attr( 'i', $headingIndex++
) );
521 $element->lastChild
->nextSibling
= $accum->firstNode
;
522 $element->lastChild
= $accum->lastNode
;
524 // Single equals sign on its own line, count=0
528 // No match, no <h>, just pass down the inner text
533 $accum =& $stack->getAccum();
534 extract( $stack->getFlags() );
536 // Append the result to the enclosing accumulator
537 if ( $element instanceof PPNode
) {
538 $accum->addNode( $element );
540 $accum->addAccum( $element );
542 // Note that we do NOT increment the input pointer.
543 // This is because the closing linebreak could be the opening linebreak of
544 // another heading. Infinite loops are avoided because the next iteration MUST
545 // hit the heading open case above, which unconditionally increments the
547 } elseif ( $found == 'open' ) {
548 # count opening brace characters
549 $count = strspn( $text, $curChar, $i );
551 # we need to add to stack only if opening brace count is enough for one of the rules
552 if ( $count >= $rule['min'] ) {
553 # Add it to the stack
556 'close' => $rule['end'],
558 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
561 $stack->push( $piece );
562 $accum =& $stack->getAccum();
563 extract( $stack->getFlags() );
565 # Add literal brace(s)
566 $accum->addLiteral( str_repeat( $curChar, $count ) );
569 } elseif ( $found == 'close' ) {
570 $piece = $stack->top
;
571 # lets check if there are enough characters for closing brace
572 $maxCount = $piece->count
;
573 $count = strspn( $text, $curChar, $i, $maxCount );
575 # check for maximum matching characters (if there are 5 closing
576 # characters, we will probably need only 3 - depending on the rules)
577 $rule = $rules[$piece->open
];
578 if ( $count > $rule['max'] ) {
579 # The specified maximum exists in the callback array, unless the caller
581 $matchingCount = $rule['max'];
583 # Count is less than the maximum
584 # Skip any gaps in the callback array to find the true largest match
585 # Need to use array_key_exists not isset because the callback can be null
586 $matchingCount = $count;
587 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
592 if ( $matchingCount <= 0 ) {
593 # No matching element found in callback array
594 # Output a literal closing brace and continue
595 $accum->addLiteral( str_repeat( $curChar, $count ) );
599 $name = $rule['names'][$matchingCount];
600 if ( $name === null ) {
601 // No element, just literal text
602 $element = $piece->breakSyntax( $matchingCount );
603 $element->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
606 # Note: $parts is already XML, does not need to be encoded further
607 $parts = $piece->parts
;
608 $titleAccum = $parts[0]->out
;
611 $element = new PPNode_Hash_Tree( $name );
613 # The invocation is at the start of the line if lineStart is set in
614 # the stack, and all opening brackets are used up.
615 if ( $maxCount == $matchingCount && !empty( $piece->lineStart
) ) {
616 $element->addChild( new PPNode_Hash_Attr( 'lineStart', 1 ) );
618 $titleNode = new PPNode_Hash_Tree( 'title' );
619 $titleNode->firstChild
= $titleAccum->firstNode
;
620 $titleNode->lastChild
= $titleAccum->lastNode
;
621 $element->addChild( $titleNode );
623 foreach ( $parts as $part ) {
624 if ( isset( $part->eqpos
) ) {
627 for ( $node = $part->out
->firstNode
; $node; $node = $node->nextSibling
) {
628 if ( $node === $part->eqpos
) {
636 throw new MWException( __METHOD__
. ': eqpos not found' );
638 if ( $node->name
!== 'equals' ) {
641 throw new MWException( __METHOD__
. ': eqpos is not equals' );
645 // Construct name node
646 $nameNode = new PPNode_Hash_Tree( 'name' );
647 if ( $lastNode !== false ) {
648 $lastNode->nextSibling
= false;
649 $nameNode->firstChild
= $part->out
->firstNode
;
650 $nameNode->lastChild
= $lastNode;
653 // Construct value node
654 $valueNode = new PPNode_Hash_Tree( 'value' );
655 if ( $equalsNode->nextSibling
!== false ) {
656 $valueNode->firstChild
= $equalsNode->nextSibling
;
657 $valueNode->lastChild
= $part->out
->lastNode
;
659 $partNode = new PPNode_Hash_Tree( 'part' );
660 $partNode->addChild( $nameNode );
661 $partNode->addChild( $equalsNode->firstChild
);
662 $partNode->addChild( $valueNode );
663 $element->addChild( $partNode );
665 $partNode = new PPNode_Hash_Tree( 'part' );
666 $nameNode = new PPNode_Hash_Tree( 'name' );
667 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $argIndex++
) );
668 $valueNode = new PPNode_Hash_Tree( 'value' );
669 $valueNode->firstChild
= $part->out
->firstNode
;
670 $valueNode->lastChild
= $part->out
->lastNode
;
671 $partNode->addChild( $nameNode );
672 $partNode->addChild( $valueNode );
673 $element->addChild( $partNode );
678 # Advance input pointer
679 $i +
= $matchingCount;
683 $accum =& $stack->getAccum();
685 # Re-add the old stack element if it still has unmatched opening characters remaining
686 if ( $matchingCount < $piece->count
) {
687 $piece->parts
= array( new PPDPart_Hash
);
688 $piece->count
-= $matchingCount;
689 # do we still qualify for any callback with remaining count?
690 $min = $rules[$piece->open
]['min'];
691 if ( $piece->count
>= $min ) {
692 $stack->push( $piece );
693 $accum =& $stack->getAccum();
695 $accum->addLiteral( str_repeat( $piece->open
, $piece->count
) );
699 extract( $stack->getFlags() );
701 # Add XML element to the enclosing accumulator
702 if ( $element instanceof PPNode
) {
703 $accum->addNode( $element );
705 $accum->addAccum( $element );
707 } elseif ( $found == 'pipe' ) {
708 $findEquals = true; // shortcut for getFlags()
710 $accum =& $stack->getAccum();
712 } elseif ( $found == 'equals' ) {
713 $findEquals = false; // shortcut for getFlags()
714 $accum->addNodeWithText( 'equals', '=' );
715 $stack->getCurrentPart()->eqpos
= $accum->lastNode
;
720 # Output any remaining unclosed brackets
721 foreach ( $stack->stack
as $piece ) {
722 $stack->rootAccum
->addAccum( $piece->breakSyntax() );
725 # Enable top-level headings
726 for ( $node = $stack->rootAccum
->firstNode
; $node; $node = $node->nextSibling
) {
727 if ( isset( $node->name
) && $node->name
=== 'possible-h' ) {
732 $rootNode = new PPNode_Hash_Tree( 'root' );
733 $rootNode->firstChild
= $stack->rootAccum
->firstNode
;
734 $rootNode->lastChild
= $stack->rootAccum
->lastNode
;
738 $cacheValue = sprintf( "%08d", self
::CACHE_VERSION
) . serialize( $rootNode );
739 $wgMemc->set( $cacheKey, $cacheValue, 86400 );
740 wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
748 * Stack class to help Preprocessor::preprocessToObj()
750 * @codingStandardsIgnoreStart
752 class PPDStack_Hash
extends PPDStack
{
753 // @codingStandardsIgnoreEnd
755 public function __construct() {
756 $this->elementClass
= 'PPDStackElement_Hash';
757 parent
::__construct();
758 $this->rootAccum
= new PPDAccum_Hash
;
764 * @codingStandardsIgnoreStart
766 class PPDStackElement_Hash
extends PPDStackElement
{
767 // @codingStandardsIgnoreENd
769 public function __construct( $data = array() ) {
770 $this->partClass
= 'PPDPart_Hash';
771 parent
::__construct( $data );
775 * Get the accumulator that would result if the close is not found.
777 * @param int|bool $openingCount
778 * @return PPDAccum_Hash
780 public function breakSyntax( $openingCount = false ) {
781 if ( $this->open
== "\n" ) {
782 $accum = $this->parts
[0]->out
;
784 if ( $openingCount === false ) {
785 $openingCount = $this->count
;
787 $accum = new PPDAccum_Hash
;
788 $accum->addLiteral( str_repeat( $this->open
, $openingCount ) );
790 foreach ( $this->parts
as $part ) {
794 $accum->addLiteral( '|' );
796 $accum->addAccum( $part->out
);
805 * @codingStandardsIgnoreStart
807 class PPDPart_Hash
extends PPDPart
{
808 // @codingStandardsIgnoreEnd
810 public function __construct( $out = '' ) {
811 $accum = new PPDAccum_Hash
;
813 $accum->addLiteral( $out );
815 parent
::__construct( $accum );
821 * @codingStandardsIgnoreStart
823 class PPDAccum_Hash
{
824 // @codingStandardsIgnoreEnd
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()
892 * @codingStandardsIgnoreStart
894 class PPFrame_Hash
implements PPFrame
{
895 // @codingStandardsIgnoreEnd
905 public $preprocessor;
914 * Hashtable listing templates which are disallowed for expansion in this frame,
915 * having been encountered previously in parent frames.
917 public $loopCheckHash;
920 * Recursion depth of this frame, top = 0
921 * Note that this is NOT the same as expansion depth in expand()
925 private $volatile = false;
931 protected $childExpansionCache;
934 * Construct a new preprocessor frame.
935 * @param Preprocessor $preprocessor The parent preprocessor
937 public function __construct( $preprocessor ) {
938 $this->preprocessor
= $preprocessor;
939 $this->parser
= $preprocessor->parser
;
940 $this->title
= $this->parser
->mTitle
;
941 $this->titleCache
= array( $this->title ?
$this->title
->getPrefixedDBkey() : false );
942 $this->loopCheckHash
= array();
944 $this->childExpansionCache
= array();
948 * Create a new child frame
949 * $args is optionally a multi-root PPNode or array containing the template arguments
951 * @param array|bool|PPNode_Hash_Array $args
952 * @param Title|bool $title
953 * @param int $indexOffset
954 * @throws MWException
955 * @return PPTemplateFrame_Hash
957 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
958 $namedArgs = array();
959 $numberedArgs = array();
960 if ( $title === false ) {
961 $title = $this->title
;
963 if ( $args !== false ) {
964 if ( $args instanceof PPNode_Hash_Array
) {
965 $args = $args->value
;
966 } elseif ( !is_array( $args ) ) {
967 throw new MWException( __METHOD__
. ': $args must be array or PPNode_Hash_Array' );
969 foreach ( $args as $arg ) {
970 $bits = $arg->splitArg();
971 if ( $bits['index'] !== '' ) {
972 // Numbered parameter
973 $index = $bits['index'] - $indexOffset;
974 if ( isset( $namedArgs[$index] ) ||
isset( $numberedArgs[$index] ) ) {
975 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
976 wfEscapeWikiText( $this->title
),
977 wfEscapeWikiText( $title ),
978 wfEscapeWikiText( $index ) )->text() );
979 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
981 $numberedArgs[$index] = $bits['value'];
982 unset( $namedArgs[$index] );
985 $name = trim( $this->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
986 if ( isset( $namedArgs[$name] ) ||
isset( $numberedArgs[$name] ) ) {
987 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
988 wfEscapeWikiText( $this->title
),
989 wfEscapeWikiText( $title ),
990 wfEscapeWikiText( $name ) )->text() );
991 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
993 $namedArgs[$name] = $bits['value'];
994 unset( $numberedArgs[$name] );
998 return new PPTemplateFrame_Hash( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
1002 * @throws MWException
1003 * @param string|int $key
1004 * @param string|PPNode $root
1008 public function cachedExpand( $key, $root, $flags = 0 ) {
1009 // we don't have a parent, so we don't have a cache
1010 return $this->expand( $root, $flags );
1014 * @throws MWException
1015 * @param string|PPNode $root
1019 public function expand( $root, $flags = 0 ) {
1020 static $expansionDepth = 0;
1021 if ( is_string( $root ) ) {
1025 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
1026 $this->parser
->limitationWarn( 'node-count-exceeded',
1027 $this->parser
->mPPNodeCount
,
1028 $this->parser
->mOptions
->getMaxPPNodeCount()
1030 return '<span class="error">Node-count limit exceeded</span>';
1032 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
1033 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
1035 $this->parser
->mOptions
->getMaxPPExpandDepth()
1037 return '<span class="error">Expansion depth limit exceeded</span>';
1040 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
1041 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
1044 $outStack = array( '', '' );
1045 $iteratorStack = array( false, $root );
1046 $indexStack = array( 0, 0 );
1048 while ( count( $iteratorStack ) > 1 ) {
1049 $level = count( $outStack ) - 1;
1050 $iteratorNode =& $iteratorStack[$level];
1051 $out =& $outStack[$level];
1052 $index =& $indexStack[$level];
1054 if ( is_array( $iteratorNode ) ) {
1055 if ( $index >= count( $iteratorNode ) ) {
1056 // All done with this iterator
1057 $iteratorStack[$level] = false;
1058 $contextNode = false;
1060 $contextNode = $iteratorNode[$index];
1063 } elseif ( $iteratorNode instanceof PPNode_Hash_Array
) {
1064 if ( $index >= $iteratorNode->getLength() ) {
1065 // All done with this iterator
1066 $iteratorStack[$level] = false;
1067 $contextNode = false;
1069 $contextNode = $iteratorNode->item( $index );
1073 // Copy to $contextNode and then delete from iterator stack,
1074 // because this is not an iterator but we do have to execute it once
1075 $contextNode = $iteratorStack[$level];
1076 $iteratorStack[$level] = false;
1079 $newIterator = false;
1081 if ( $contextNode === false ) {
1083 } elseif ( is_string( $contextNode ) ) {
1084 $out .= $contextNode;
1085 } elseif ( is_array( $contextNode ) ||
$contextNode instanceof PPNode_Hash_Array
) {
1086 $newIterator = $contextNode;
1087 } elseif ( $contextNode instanceof PPNode_Hash_Attr
) {
1089 } elseif ( $contextNode instanceof PPNode_Hash_Text
) {
1090 $out .= $contextNode->value
;
1091 } elseif ( $contextNode instanceof PPNode_Hash_Tree
) {
1092 if ( $contextNode->name
== 'template' ) {
1093 # Double-brace expansion
1094 $bits = $contextNode->splitTemplate();
1095 if ( $flags & PPFrame
::NO_TEMPLATES
) {
1096 $newIterator = $this->virtualBracketedImplode(
1102 $ret = $this->parser
->braceSubstitution( $bits, $this );
1103 if ( isset( $ret['object'] ) ) {
1104 $newIterator = $ret['object'];
1106 $out .= $ret['text'];
1109 } elseif ( $contextNode->name
== 'tplarg' ) {
1110 # Triple-brace expansion
1111 $bits = $contextNode->splitTemplate();
1112 if ( $flags & PPFrame
::NO_ARGS
) {
1113 $newIterator = $this->virtualBracketedImplode(
1119 $ret = $this->parser
->argSubstitution( $bits, $this );
1120 if ( isset( $ret['object'] ) ) {
1121 $newIterator = $ret['object'];
1123 $out .= $ret['text'];
1126 } elseif ( $contextNode->name
== 'comment' ) {
1127 # HTML-style comment
1128 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1129 if ( $this->parser
->ot
['html']
1130 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
1131 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
1134 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
1135 # Add a strip marker in PST mode so that pstPass2() can
1136 # run some old-fashioned regexes on the result.
1137 # Not in RECOVER_COMMENTS mode (extractSections) though.
1138 $out .= $this->parser
->insertStripItem( $contextNode->firstChild
->value
);
1140 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1141 $out .= $contextNode->firstChild
->value
;
1143 } elseif ( $contextNode->name
== 'ignore' ) {
1144 # Output suppression used by <includeonly> etc.
1145 # OT_WIKI will only respect <ignore> in substed templates.
1146 # The other output types respect it unless NO_IGNORE is set.
1147 # extractSections() sets NO_IGNORE and so never respects it.
1148 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] )
1149 ||
( $flags & PPFrame
::NO_IGNORE
)
1151 $out .= $contextNode->firstChild
->value
;
1155 } elseif ( $contextNode->name
== 'ext' ) {
1157 $bits = $contextNode->splitExt() +
array( 'attr' => null, 'inner' => null, 'close' => null );
1158 if ( $flags & PPFrame
::NO_TAGS
) {
1159 $s = '<' . $bits['name']->firstChild
->value
;
1160 if ( $bits['attr'] ) {
1161 $s .= $bits['attr']->firstChild
->value
;
1163 if ( $bits['inner'] ) {
1164 $s .= '>' . $bits['inner']->firstChild
->value
;
1165 if ( $bits['close'] ) {
1166 $s .= $bits['close']->firstChild
->value
;
1173 $out .= $this->parser
->extensionSubstitution( $bits, $this );
1175 } elseif ( $contextNode->name
== 'h' ) {
1177 if ( $this->parser
->ot
['html'] ) {
1178 # Expand immediately and insert heading index marker
1180 for ( $node = $contextNode->firstChild
; $node; $node = $node->nextSibling
) {
1181 $s .= $this->expand( $node, $flags );
1184 $bits = $contextNode->splitHeading();
1185 $titleText = $this->title
->getPrefixedDBkey();
1186 $this->parser
->mHeadings
[] = array( $titleText, $bits['i'] );
1187 $serial = count( $this->parser
->mHeadings
) - 1;
1188 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser
::MARKER_SUFFIX
;
1189 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1190 $this->parser
->mStripState
->addGeneral( $marker, '' );
1193 # Expand in virtual stack
1194 $newIterator = $contextNode->getChildren();
1197 # Generic recursive expansion
1198 $newIterator = $contextNode->getChildren();
1201 throw new MWException( __METHOD__
. ': Invalid parameter type' );
1204 if ( $newIterator !== false ) {
1206 $iteratorStack[] = $newIterator;
1208 } elseif ( $iteratorStack[$level] === false ) {
1209 // Return accumulated value to parent
1210 // With tail recursion
1211 while ( $iteratorStack[$level] === false && $level > 0 ) {
1212 $outStack[$level - 1] .= $out;
1213 array_pop( $outStack );
1214 array_pop( $iteratorStack );
1215 array_pop( $indexStack );
1221 return $outStack[0];
1225 * @param string $sep
1227 * @param string|PPNode $args,...
1230 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1231 $args = array_slice( func_get_args(), 2 );
1235 foreach ( $args as $root ) {
1236 if ( $root instanceof PPNode_Hash_Array
) {
1237 $root = $root->value
;
1239 if ( !is_array( $root ) ) {
1240 $root = array( $root );
1242 foreach ( $root as $node ) {
1248 $s .= $this->expand( $node, $flags );
1255 * Implode with no flags specified
1256 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1257 * @param string $sep
1258 * @param string|PPNode $args,...
1261 public function implode( $sep /*, ... */ ) {
1262 $args = array_slice( func_get_args(), 1 );
1266 foreach ( $args as $root ) {
1267 if ( $root instanceof PPNode_Hash_Array
) {
1268 $root = $root->value
;
1270 if ( !is_array( $root ) ) {
1271 $root = array( $root );
1273 foreach ( $root as $node ) {
1279 $s .= $this->expand( $node );
1286 * Makes an object that, when expand()ed, will be the same as one obtained
1289 * @param string $sep
1290 * @param string|PPNode $args,...
1291 * @return PPNode_Hash_Array
1293 public function virtualImplode( $sep /*, ... */ ) {
1294 $args = array_slice( func_get_args(), 1 );
1298 foreach ( $args as $root ) {
1299 if ( $root instanceof PPNode_Hash_Array
) {
1300 $root = $root->value
;
1302 if ( !is_array( $root ) ) {
1303 $root = array( $root );
1305 foreach ( $root as $node ) {
1314 return new PPNode_Hash_Array( $out );
1318 * Virtual implode with brackets
1320 * @param string $start
1321 * @param string $sep
1322 * @param string $end
1323 * @param string|PPNode $args,...
1324 * @return PPNode_Hash_Array
1326 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1327 $args = array_slice( func_get_args(), 3 );
1328 $out = array( $start );
1331 foreach ( $args as $root ) {
1332 if ( $root instanceof PPNode_Hash_Array
) {
1333 $root = $root->value
;
1335 if ( !is_array( $root ) ) {
1336 $root = array( $root );
1338 foreach ( $root as $node ) {
1348 return new PPNode_Hash_Array( $out );
1351 public function __toString() {
1356 * @param bool $level
1357 * @return array|bool|string
1359 public function getPDBK( $level = false ) {
1360 if ( $level === false ) {
1361 return $this->title
->getPrefixedDBkey();
1363 return isset( $this->titleCache
[$level] ) ?
$this->titleCache
[$level] : false;
1370 public function getArguments() {
1377 public function getNumberedArguments() {
1384 public function getNamedArguments() {
1389 * Returns true if there are no arguments in this frame
1393 public function isEmpty() {
1398 * @param string $name
1401 public function getArgument( $name ) {
1406 * Returns true if the infinite loop check is OK, false if a loop is detected
1408 * @param Title $title
1412 public function loopCheck( $title ) {
1413 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
1417 * Return true if the frame is a template frame
1421 public function isTemplate() {
1426 * Get a title of frame
1430 public function getTitle() {
1431 return $this->title
;
1435 * Set the volatile flag
1439 public function setVolatile( $flag = true ) {
1440 $this->volatile
= $flag;
1444 * Get the volatile flag
1448 public function isVolatile() {
1449 return $this->volatile
;
1457 public function setTTL( $ttl ) {
1458 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
1468 public function getTTL() {
1474 * Expansion frame with template arguments
1476 * @codingStandardsIgnoreStart
1478 class PPTemplateFrame_Hash
extends PPFrame_Hash
{
1479 // @codingStandardsIgnoreEnd
1481 public $numberedArgs, $namedArgs, $parent;
1482 public $numberedExpansionCache, $namedExpansionCache;
1485 * @param Preprocessor $preprocessor
1486 * @param bool|PPFrame $parent
1487 * @param array $numberedArgs
1488 * @param array $namedArgs
1489 * @param bool|Title $title
1491 public function __construct( $preprocessor, $parent = false, $numberedArgs = array(),
1492 $namedArgs = array(), $title = false
1494 parent
::__construct( $preprocessor );
1496 $this->parent
= $parent;
1497 $this->numberedArgs
= $numberedArgs;
1498 $this->namedArgs
= $namedArgs;
1499 $this->title
= $title;
1500 $pdbk = $title ?
$title->getPrefixedDBkey() : false;
1501 $this->titleCache
= $parent->titleCache
;
1502 $this->titleCache
[] = $pdbk;
1503 $this->loopCheckHash
= /*clone*/ $parent->loopCheckHash
;
1504 if ( $pdbk !== false ) {
1505 $this->loopCheckHash
[$pdbk] = true;
1507 $this->depth
= $parent->depth +
1;
1508 $this->numberedExpansionCache
= $this->namedExpansionCache
= array();
1511 public function __toString() {
1514 $args = $this->numberedArgs +
$this->namedArgs
;
1515 foreach ( $args as $name => $value ) {
1521 $s .= "\"$name\":\"" .
1522 str_replace( '"', '\\"', $value->__toString() ) . '"';
1529 * @throws MWException
1530 * @param string|int $key
1531 * @param string|PPNode $root
1535 public function cachedExpand( $key, $root, $flags = 0 ) {
1536 if ( isset( $this->parent
->childExpansionCache
[$key] ) ) {
1537 return $this->parent
->childExpansionCache
[$key];
1539 $retval = $this->expand( $root, $flags );
1540 if ( !$this->isVolatile() ) {
1541 $this->parent
->childExpansionCache
[$key] = $retval;
1547 * Returns true if there are no arguments in this frame
1551 public function isEmpty() {
1552 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
1558 public function getArguments() {
1559 $arguments = array();
1560 foreach ( array_merge(
1561 array_keys( $this->numberedArgs
),
1562 array_keys( $this->namedArgs
) ) as $key ) {
1563 $arguments[$key] = $this->getArgument( $key );
1571 public function getNumberedArguments() {
1572 $arguments = array();
1573 foreach ( array_keys( $this->numberedArgs
) as $key ) {
1574 $arguments[$key] = $this->getArgument( $key );
1582 public function getNamedArguments() {
1583 $arguments = array();
1584 foreach ( array_keys( $this->namedArgs
) as $key ) {
1585 $arguments[$key] = $this->getArgument( $key );
1592 * @return array|bool
1594 public function getNumberedArgument( $index ) {
1595 if ( !isset( $this->numberedArgs
[$index] ) ) {
1598 if ( !isset( $this->numberedExpansionCache
[$index] ) ) {
1599 # No trimming for unnamed arguments
1600 $this->numberedExpansionCache
[$index] = $this->parent
->expand(
1601 $this->numberedArgs
[$index],
1602 PPFrame
::STRIP_COMMENTS
1605 return $this->numberedExpansionCache
[$index];
1609 * @param string $name
1612 public function getNamedArgument( $name ) {
1613 if ( !isset( $this->namedArgs
[$name] ) ) {
1616 if ( !isset( $this->namedExpansionCache
[$name] ) ) {
1617 # Trim named arguments post-expand, for backwards compatibility
1618 $this->namedExpansionCache
[$name] = trim(
1619 $this->parent
->expand( $this->namedArgs
[$name], PPFrame
::STRIP_COMMENTS
) );
1621 return $this->namedExpansionCache
[$name];
1625 * @param string $name
1626 * @return array|bool
1628 public function getArgument( $name ) {
1629 $text = $this->getNumberedArgument( $name );
1630 if ( $text === false ) {
1631 $text = $this->getNamedArgument( $name );
1637 * Return true if the frame is a template frame
1641 public function isTemplate() {
1645 public function setVolatile( $flag = true ) {
1646 parent
::setVolatile( $flag );
1647 $this->parent
->setVolatile( $flag );
1650 public function setTTL( $ttl ) {
1651 parent
::setTTL( $ttl );
1652 $this->parent
->setTTL( $ttl );
1657 * Expansion frame with custom arguments
1659 * @codingStandardsIgnoreStart
1661 class PPCustomFrame_Hash
extends PPFrame_Hash
{
1662 // @codingStandardsIgnoreEnd
1666 public function __construct( $preprocessor, $args ) {
1667 parent
::__construct( $preprocessor );
1668 $this->args
= $args;
1671 public function __toString() {
1674 foreach ( $this->args
as $name => $value ) {
1680 $s .= "\"$name\":\"" .
1681 str_replace( '"', '\\"', $value->__toString() ) . '"';
1690 public function isEmpty() {
1691 return !count( $this->args
);
1698 public function getArgument( $index ) {
1699 if ( !isset( $this->args
[$index] ) ) {
1702 return $this->args
[$index];
1705 public function getArguments() {
1712 * @codingStandardsIgnoreStart
1714 class PPNode_Hash_Tree
implements PPNode
{
1715 // @codingStandardsIgnoreEnd
1717 public $name, $firstChild, $lastChild, $nextSibling;
1719 public function __construct( $name ) {
1720 $this->name
= $name;
1721 $this->firstChild
= $this->lastChild
= $this->nextSibling
= false;
1724 public function __toString() {
1727 for ( $node = $this->firstChild
; $node; $node = $node->nextSibling
) {
1728 if ( $node instanceof PPNode_Hash_Attr
) {
1729 $attribs .= ' ' . $node->name
. '="' . htmlspecialchars( $node->value
) . '"';
1731 $inner .= $node->__toString();
1734 if ( $inner === '' ) {
1735 return "<{$this->name}$attribs/>";
1737 return "<{$this->name}$attribs>$inner</{$this->name}>";
1742 * @param string $name
1743 * @param string $text
1744 * @return PPNode_Hash_Tree
1746 public static function newWithText( $name, $text ) {
1747 $obj = new self( $name );
1748 $obj->addChild( new PPNode_Hash_Text( $text ) );
1752 public function addChild( $node ) {
1753 if ( $this->lastChild
=== false ) {
1754 $this->firstChild
= $this->lastChild
= $node;
1756 $this->lastChild
->nextSibling
= $node;
1757 $this->lastChild
= $node;
1762 * @return PPNode_Hash_Array
1764 public function getChildren() {
1765 $children = array();
1766 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1767 $children[] = $child;
1769 return new PPNode_Hash_Array( $children );
1772 public function getFirstChild() {
1773 return $this->firstChild
;
1776 public function getNextSibling() {
1777 return $this->nextSibling
;
1780 public function getChildrenOfType( $name ) {
1781 $children = array();
1782 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1783 if ( isset( $child->name
) && $child->name
=== $name ) {
1784 $children[] = $child;
1793 public function getLength() {
1801 public function item( $i ) {
1808 public function getName() {
1813 * Split a "<part>" node into an associative array containing:
1814 * - name PPNode name
1815 * - index String index
1816 * - value PPNode value
1818 * @throws MWException
1821 public function splitArg() {
1823 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1824 if ( !isset( $child->name
) ) {
1827 if ( $child->name
=== 'name' ) {
1828 $bits['name'] = $child;
1829 if ( $child->firstChild
instanceof PPNode_Hash_Attr
1830 && $child->firstChild
->name
=== 'index'
1832 $bits['index'] = $child->firstChild
->value
;
1834 } elseif ( $child->name
=== 'value' ) {
1835 $bits['value'] = $child;
1839 if ( !isset( $bits['name'] ) ) {
1840 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
1842 if ( !isset( $bits['index'] ) ) {
1843 $bits['index'] = '';
1849 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1850 * All values in the resulting array are PPNodes. Inner and close are optional.
1852 * @throws MWException
1855 public function splitExt() {
1857 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1858 if ( !isset( $child->name
) ) {
1861 if ( $child->name
== 'name' ) {
1862 $bits['name'] = $child;
1863 } elseif ( $child->name
== 'attr' ) {
1864 $bits['attr'] = $child;
1865 } elseif ( $child->name
== 'inner' ) {
1866 $bits['inner'] = $child;
1867 } elseif ( $child->name
== 'close' ) {
1868 $bits['close'] = $child;
1871 if ( !isset( $bits['name'] ) ) {
1872 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
1878 * Split an "<h>" node
1880 * @throws MWException
1883 public function splitHeading() {
1884 if ( $this->name
!== 'h' ) {
1885 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1888 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1889 if ( !isset( $child->name
) ) {
1892 if ( $child->name
== 'i' ) {
1893 $bits['i'] = $child->value
;
1894 } elseif ( $child->name
== 'level' ) {
1895 $bits['level'] = $child->value
;
1898 if ( !isset( $bits['i'] ) ) {
1899 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1905 * Split a "<template>" or "<tplarg>" node
1907 * @throws MWException
1910 public function splitTemplate() {
1912 $bits = array( 'lineStart' => '' );
1913 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1914 if ( !isset( $child->name
) ) {
1917 if ( $child->name
== 'title' ) {
1918 $bits['title'] = $child;
1920 if ( $child->name
== 'part' ) {
1923 if ( $child->name
== 'lineStart' ) {
1924 $bits['lineStart'] = '1';
1927 if ( !isset( $bits['title'] ) ) {
1928 throw new MWException( 'Invalid node passed to ' . __METHOD__
);
1930 $bits['parts'] = new PPNode_Hash_Array( $parts );
1937 * @codingStandardsIgnoreStart
1939 class PPNode_Hash_Text
implements PPNode
{
1940 // @codingStandardsIgnoreEnd
1942 public $value, $nextSibling;
1944 public function __construct( $value ) {
1945 if ( is_object( $value ) ) {
1946 throw new MWException( __CLASS__
. ' given object instead of string' );
1948 $this->value
= $value;
1951 public function __toString() {
1952 return htmlspecialchars( $this->value
);
1955 public function getNextSibling() {
1956 return $this->nextSibling
;
1959 public function getChildren() {
1963 public function getFirstChild() {
1967 public function getChildrenOfType( $name ) {
1971 public function getLength() {
1975 public function item( $i ) {
1979 public function getName() {
1983 public function splitArg() {
1984 throw new MWException( __METHOD__
. ': not supported' );
1987 public function splitExt() {
1988 throw new MWException( __METHOD__
. ': not supported' );
1991 public function splitHeading() {
1992 throw new MWException( __METHOD__
. ': not supported' );
1998 * @codingStandardsIgnoreStart
2000 class PPNode_Hash_Array
implements PPNode
{
2001 // @codingStandardsIgnoreEnd
2003 public $value, $nextSibling;
2005 public function __construct( $value ) {
2006 $this->value
= $value;
2009 public function __toString() {
2010 return var_export( $this, true );
2013 public function getLength() {
2014 return count( $this->value
);
2017 public function item( $i ) {
2018 return $this->value
[$i];
2021 public function getName() {
2025 public function getNextSibling() {
2026 return $this->nextSibling
;
2029 public function getChildren() {
2033 public function getFirstChild() {
2037 public function getChildrenOfType( $name ) {
2041 public function splitArg() {
2042 throw new MWException( __METHOD__
. ': not supported' );
2045 public function splitExt() {
2046 throw new MWException( __METHOD__
. ': not supported' );
2049 public function splitHeading() {
2050 throw new MWException( __METHOD__
. ': not supported' );
2056 * @codingStandardsIgnoreStart
2058 class PPNode_Hash_Attr
implements PPNode
{
2059 // @codingStandardsIgnoreEnd
2061 public $name, $value, $nextSibling;
2063 public function __construct( $name, $value ) {
2064 $this->name
= $name;
2065 $this->value
= $value;
2068 public function __toString() {
2069 return "<@{$this->name}>" . htmlspecialchars( $this->value
) . "</@{$this->name}>";
2072 public function getName() {
2076 public function getNextSibling() {
2077 return $this->nextSibling
;
2080 public function getChildren() {
2084 public function getFirstChild() {
2088 public function getChildrenOfType( $name ) {
2092 public function getLength() {
2096 public function item( $i ) {
2100 public function splitArg() {
2101 throw new MWException( __METHOD__
. ': not supported' );
2104 public function splitExt() {
2105 throw new MWException( __METHOD__
. ': not supported' );
2108 public function splitHeading() {
2109 throw new MWException( __METHOD__
. ': not supported' );