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 ) {
116 global $wgMemc, $wgPreprocessorCacheThreshold;
118 $cacheable = $wgPreprocessorCacheThreshold !== false
119 && strlen( $text ) > $wgPreprocessorCacheThreshold;
122 $cacheKey = wfMemcKey( 'preprocess-hash', md5( $text ), $flags );
123 $cacheValue = $wgMemc->get( $cacheKey );
125 $version = substr( $cacheValue, 0, 8 );
126 if ( intval( $version ) == self
::CACHE_VERSION
) {
127 $hash = unserialize( substr( $cacheValue, 8 ) );
129 wfDebugLog( "Preprocessor",
130 "Loaded preprocessor hash from memcached (key $cacheKey)" );
148 'names' => array( 2 => null ),
154 $forInclusion = $flags & Parser
::PTD_FOR_INCLUSION
;
156 $xmlishElements = $this->parser
->getStripList();
157 $enableOnlyinclude = false;
158 if ( $forInclusion ) {
159 $ignoredTags = array( 'includeonly', '/includeonly' );
160 $ignoredElements = array( 'noinclude' );
161 $xmlishElements[] = 'noinclude';
162 if ( strpos( $text, '<onlyinclude>' ) !== false
163 && strpos( $text, '</onlyinclude>' ) !== false
165 $enableOnlyinclude = true;
168 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
169 $ignoredElements = array( 'includeonly' );
170 $xmlishElements[] = 'includeonly';
172 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
174 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
175 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
177 $stack = new PPDStack_Hash
;
179 $searchBase = "[{<\n";
180 // For fast reverse searches
181 $revText = strrev( $text );
182 $lengthText = strlen( $text );
184 // Input pointer, starts out pointing to a pseudo-newline before the start
186 // Current accumulator
187 $accum =& $stack->getAccum();
188 // True to find equals signs in arguments
190 // True to take notice of pipe characters
193 // True if $i is inside a possible heading
195 // True if there are no more greater-than (>) signs right of $i
197 // True to ignore all input up to the next <onlyinclude>
198 $findOnlyinclude = $enableOnlyinclude;
199 // Do a line-start run without outputting an LF character
200 $fakeLineStart = true;
205 if ( $findOnlyinclude ) {
206 // Ignore all input up to the next <onlyinclude>
207 $startPos = strpos( $text, '<onlyinclude>', $i );
208 if ( $startPos === false ) {
209 // Ignored section runs to the end
210 $accum->addNodeWithText( 'ignore', substr( $text, $i ) );
213 $tagEndPos = $startPos +
strlen( '<onlyinclude>' ); // past-the-end
214 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i ) );
216 $findOnlyinclude = false;
219 if ( $fakeLineStart ) {
220 $found = 'line-start';
223 # Find next opening brace, closing brace or pipe
224 $search = $searchBase;
225 if ( $stack->top
=== false ) {
226 $currentClosing = '';
228 $currentClosing = $stack->top
->close
;
229 $search .= $currentClosing;
235 // First equals will be for the template
239 # Output literal section, advance input counter
240 $literalLength = strcspn( $text, $search, $i );
241 if ( $literalLength > 0 ) {
242 $accum->addLiteral( substr( $text, $i, $literalLength ) );
243 $i +
= $literalLength;
245 if ( $i >= $lengthText ) {
246 if ( $currentClosing == "\n" ) {
247 // Do a past-the-end run to finish off the heading
255 $curChar = $text[$i];
256 if ( $curChar == '|' ) {
258 } elseif ( $curChar == '=' ) {
260 } elseif ( $curChar == '<' ) {
262 } elseif ( $curChar == "\n" ) {
266 $found = 'line-start';
268 } elseif ( $curChar == $currentClosing ) {
270 } elseif ( isset( $rules[$curChar] ) ) {
272 $rule = $rules[$curChar];
274 # Some versions of PHP have a strcspn which stops on null characters
275 # Ignore and continue
282 if ( $found == 'angle' ) {
284 // Handle </onlyinclude>
285 if ( $enableOnlyinclude
286 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
288 $findOnlyinclude = true;
292 // Determine element name
293 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i +
1 ) ) {
294 // Element name missing or not listed
295 $accum->addLiteral( '<' );
300 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
302 // To avoid leaving blank lines, when a sequence of
303 // space-separated comments is both preceded and followed by
304 // a newline (ignoring spaces), then
305 // trim leading and trailing spaces and the trailing newline.
308 $endPos = strpos( $text, '-->', $i +
4 );
309 if ( $endPos === false ) {
310 // Unclosed comment in input, runs to end
311 $inner = substr( $text, $i );
312 $accum->addNodeWithText( 'comment', $inner );
315 // Search backwards for leading whitespace
316 $wsStart = $i ?
( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
318 // Search forwards for trailing whitespace
319 // $wsEnd will be the position of the last space (or the '>' if there's none)
320 $wsEnd = $endPos +
2 +
strspn( $text, " \t", $endPos +
3 );
322 // Keep looking forward as long as we're finding more
324 $comments = array( array( $wsStart, $wsEnd ) );
325 while ( substr( $text, $wsEnd +
1, 4 ) == '<!--' ) {
326 $c = strpos( $text, '-->', $wsEnd +
4 );
327 if ( $c === false ) {
330 $c = $c +
2 +
strspn( $text, " \t", $c +
3 );
331 $comments[] = array( $wsEnd +
1, $c );
335 // Eat the line if possible
336 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
337 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
338 // it's a possible beneficial b/c break.
339 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
340 && substr( $text, $wsEnd +
1, 1 ) == "\n"
342 // Remove leading whitespace from the end of the accumulator
343 // Sanity check first though
344 $wsLength = $i - $wsStart;
346 && $accum->lastNode
instanceof PPNode_Hash_Text
347 && strspn( $accum->lastNode
->value
, " \t", -$wsLength ) === $wsLength
349 $accum->lastNode
->value
= substr( $accum->lastNode
->value
, 0, -$wsLength );
352 // Dump all but the last comment to the accumulator
353 foreach ( $comments as $j => $com ) {
355 $endPos = $com[1] +
1;
356 if ( $j == ( count( $comments ) - 1 ) ) {
359 $inner = substr( $text, $startPos, $endPos - $startPos );
360 $accum->addNodeWithText( 'comment', $inner );
363 // Do a line-start run next time to look for headings after the comment
364 $fakeLineStart = true;
366 // No line to eat, just take the comment itself
372 $part = $stack->top
->getCurrentPart();
373 if ( !( isset( $part->commentEnd
) && $part->commentEnd
== $wsStart - 1 ) ) {
374 $part->visualEnd
= $wsStart;
376 // Else comments abutting, no change in visual end
377 $part->commentEnd
= $endPos;
380 $inner = substr( $text, $startPos, $endPos - $startPos +
1 );
381 $accum->addNodeWithText( 'comment', $inner );
386 $lowerName = strtolower( $name );
387 $attrStart = $i +
strlen( $name ) +
1;
390 $tagEndPos = $noMoreGT ?
false : strpos( $text, '>', $attrStart );
391 if ( $tagEndPos === false ) {
392 // Infinite backtrack
393 // Disable tag search to prevent worst-case O(N^2) performance
395 $accum->addLiteral( '<' );
400 // Handle ignored tags
401 if ( in_array( $lowerName, $ignoredTags ) ) {
402 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i +
1 ) );
408 if ( $text[$tagEndPos - 1] == '/' ) {
410 $attrEnd = $tagEndPos - 1;
415 $attrEnd = $tagEndPos;
417 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
418 $text, $matches, PREG_OFFSET_CAPTURE
, $tagEndPos +
1 )
420 $inner = substr( $text, $tagEndPos +
1, $matches[0][1] - $tagEndPos - 1 );
421 $i = $matches[0][1] +
strlen( $matches[0][0] );
422 $close = $matches[0][0];
424 // No end tag -- let it run out to the end of the text.
425 $inner = substr( $text, $tagEndPos +
1 );
430 // <includeonly> and <noinclude> just become <ignore> tags
431 if ( in_array( $lowerName, $ignoredElements ) ) {
432 $accum->addNodeWithText( 'ignore', substr( $text, $tagStartPos, $i - $tagStartPos ) );
436 if ( $attrEnd <= $attrStart ) {
439 // Note that the attr element contains the whitespace between name and attribute,
440 // this is necessary for precise reconstruction during pre-save transform.
441 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
444 $extNode = new PPNode_Hash_Tree( 'ext' );
445 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'name', $name ) );
446 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'attr', $attr ) );
447 if ( $inner !== null ) {
448 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'inner', $inner ) );
450 if ( $close !== null ) {
451 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'close', $close ) );
453 $accum->addNode( $extNode );
454 } elseif ( $found == 'line-start' ) {
455 // Is this the start of a heading?
456 // Line break belongs before the heading element in any case
457 if ( $fakeLineStart ) {
458 $fakeLineStart = false;
460 $accum->addLiteral( $curChar );
464 $count = strspn( $text, '=', $i, 6 );
465 if ( $count == 1 && $findEquals ) {
466 // DWIM: This looks kind of like a name/value separator.
467 // Let's let the equals handler have it and break the potential
468 // heading. This is heuristic, but AFAICT the methods for
469 // completely correct disambiguation are very complex.
470 } elseif ( $count > 0 ) {
474 'parts' => array( new PPDPart_Hash( str_repeat( '=', $count ) ) ),
477 $stack->push( $piece );
478 $accum =& $stack->getAccum();
479 extract( $stack->getFlags() );
482 } elseif ( $found == 'line-end' ) {
483 $piece = $stack->top
;
484 // A heading must be open, otherwise \n wouldn't have been in the search list
485 assert( '$piece->open == "\n"' );
486 $part = $piece->getCurrentPart();
487 // Search back through the input to see if it has a proper close.
488 // Do this using the reversed string since the other solutions
489 // (end anchor, etc.) are inefficient.
490 $wsLength = strspn( $revText, " \t", $lengthText - $i );
491 $searchStart = $i - $wsLength;
492 if ( isset( $part->commentEnd
) && $searchStart - 1 == $part->commentEnd
) {
493 // Comment found at line end
494 // Search for equals signs before the comment
495 $searchStart = $part->visualEnd
;
496 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
498 $count = $piece->count
;
499 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
500 if ( $equalsLength > 0 ) {
501 if ( $searchStart - $equalsLength == $piece->startPos
) {
502 // This is just a single string of equals signs on its own line
503 // Replicate the doHeadings behavior /={count}(.+)={count}/
504 // First find out how many equals signs there really are (don't stop at 6)
505 $count = $equalsLength;
509 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
512 $count = min( $equalsLength, $count );
515 // Normal match, output <h>
516 $element = new PPNode_Hash_Tree( 'possible-h' );
517 $element->addChild( new PPNode_Hash_Attr( 'level', $count ) );
518 $element->addChild( new PPNode_Hash_Attr( 'i', $headingIndex++
) );
519 $element->lastChild
->nextSibling
= $accum->firstNode
;
520 $element->lastChild
= $accum->lastNode
;
522 // Single equals sign on its own line, count=0
526 // No match, no <h>, just pass down the inner text
531 $accum =& $stack->getAccum();
532 extract( $stack->getFlags() );
534 // Append the result to the enclosing accumulator
535 if ( $element instanceof PPNode
) {
536 $accum->addNode( $element );
538 $accum->addAccum( $element );
540 // Note that we do NOT increment the input pointer.
541 // This is because the closing linebreak could be the opening linebreak of
542 // another heading. Infinite loops are avoided because the next iteration MUST
543 // hit the heading open case above, which unconditionally increments the
545 } elseif ( $found == 'open' ) {
546 # count opening brace characters
547 $count = strspn( $text, $curChar, $i );
549 # we need to add to stack only if opening brace count is enough for one of the rules
550 if ( $count >= $rule['min'] ) {
551 # Add it to the stack
554 'close' => $rule['end'],
556 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
559 $stack->push( $piece );
560 $accum =& $stack->getAccum();
561 extract( $stack->getFlags() );
563 # Add literal brace(s)
564 $accum->addLiteral( str_repeat( $curChar, $count ) );
567 } elseif ( $found == 'close' ) {
568 $piece = $stack->top
;
569 # lets check if there are enough characters for closing brace
570 $maxCount = $piece->count
;
571 $count = strspn( $text, $curChar, $i, $maxCount );
573 # check for maximum matching characters (if there are 5 closing
574 # characters, we will probably need only 3 - depending on the rules)
575 $rule = $rules[$piece->open
];
576 if ( $count > $rule['max'] ) {
577 # The specified maximum exists in the callback array, unless the caller
579 $matchingCount = $rule['max'];
581 # Count is less than the maximum
582 # Skip any gaps in the callback array to find the true largest match
583 # Need to use array_key_exists not isset because the callback can be null
584 $matchingCount = $count;
585 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
590 if ( $matchingCount <= 0 ) {
591 # No matching element found in callback array
592 # Output a literal closing brace and continue
593 $accum->addLiteral( str_repeat( $curChar, $count ) );
597 $name = $rule['names'][$matchingCount];
598 if ( $name === null ) {
599 // No element, just literal text
600 $element = $piece->breakSyntax( $matchingCount );
601 $element->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
604 # Note: $parts is already XML, does not need to be encoded further
605 $parts = $piece->parts
;
606 $titleAccum = $parts[0]->out
;
609 $element = new PPNode_Hash_Tree( $name );
611 # The invocation is at the start of the line if lineStart is set in
612 # the stack, and all opening brackets are used up.
613 if ( $maxCount == $matchingCount && !empty( $piece->lineStart
) ) {
614 $element->addChild( new PPNode_Hash_Attr( 'lineStart', 1 ) );
616 $titleNode = new PPNode_Hash_Tree( 'title' );
617 $titleNode->firstChild
= $titleAccum->firstNode
;
618 $titleNode->lastChild
= $titleAccum->lastNode
;
619 $element->addChild( $titleNode );
621 foreach ( $parts as $part ) {
622 if ( isset( $part->eqpos
) ) {
625 for ( $node = $part->out
->firstNode
; $node; $node = $node->nextSibling
) {
626 if ( $node === $part->eqpos
) {
634 throw new MWException( __METHOD__
. ': eqpos not found' );
636 if ( $node->name
!== 'equals' ) {
639 throw new MWException( __METHOD__
. ': eqpos is not equals' );
643 // Construct name node
644 $nameNode = new PPNode_Hash_Tree( 'name' );
645 if ( $lastNode !== false ) {
646 $lastNode->nextSibling
= false;
647 $nameNode->firstChild
= $part->out
->firstNode
;
648 $nameNode->lastChild
= $lastNode;
651 // Construct value node
652 $valueNode = new PPNode_Hash_Tree( 'value' );
653 if ( $equalsNode->nextSibling
!== false ) {
654 $valueNode->firstChild
= $equalsNode->nextSibling
;
655 $valueNode->lastChild
= $part->out
->lastNode
;
657 $partNode = new PPNode_Hash_Tree( 'part' );
658 $partNode->addChild( $nameNode );
659 $partNode->addChild( $equalsNode->firstChild
);
660 $partNode->addChild( $valueNode );
661 $element->addChild( $partNode );
663 $partNode = new PPNode_Hash_Tree( 'part' );
664 $nameNode = new PPNode_Hash_Tree( 'name' );
665 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $argIndex++
) );
666 $valueNode = new PPNode_Hash_Tree( 'value' );
667 $valueNode->firstChild
= $part->out
->firstNode
;
668 $valueNode->lastChild
= $part->out
->lastNode
;
669 $partNode->addChild( $nameNode );
670 $partNode->addChild( $valueNode );
671 $element->addChild( $partNode );
676 # Advance input pointer
677 $i +
= $matchingCount;
681 $accum =& $stack->getAccum();
683 # Re-add the old stack element if it still has unmatched opening characters remaining
684 if ( $matchingCount < $piece->count
) {
685 $piece->parts
= array( new PPDPart_Hash
);
686 $piece->count
-= $matchingCount;
687 # do we still qualify for any callback with remaining count?
688 $min = $rules[$piece->open
]['min'];
689 if ( $piece->count
>= $min ) {
690 $stack->push( $piece );
691 $accum =& $stack->getAccum();
693 $accum->addLiteral( str_repeat( $piece->open
, $piece->count
) );
697 extract( $stack->getFlags() );
699 # Add XML element to the enclosing accumulator
700 if ( $element instanceof PPNode
) {
701 $accum->addNode( $element );
703 $accum->addAccum( $element );
705 } elseif ( $found == 'pipe' ) {
706 $findEquals = true; // shortcut for getFlags()
708 $accum =& $stack->getAccum();
710 } elseif ( $found == 'equals' ) {
711 $findEquals = false; // shortcut for getFlags()
712 $accum->addNodeWithText( 'equals', '=' );
713 $stack->getCurrentPart()->eqpos
= $accum->lastNode
;
718 # Output any remaining unclosed brackets
719 foreach ( $stack->stack
as $piece ) {
720 $stack->rootAccum
->addAccum( $piece->breakSyntax() );
723 # Enable top-level headings
724 for ( $node = $stack->rootAccum
->firstNode
; $node; $node = $node->nextSibling
) {
725 if ( isset( $node->name
) && $node->name
=== 'possible-h' ) {
730 $rootNode = new PPNode_Hash_Tree( 'root' );
731 $rootNode->firstChild
= $stack->rootAccum
->firstNode
;
732 $rootNode->lastChild
= $stack->rootAccum
->lastNode
;
736 $cacheValue = sprintf( "%08d", self
::CACHE_VERSION
) . serialize( $rootNode );
737 $wgMemc->set( $cacheKey, $cacheValue, 86400 );
738 wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
746 * Stack class to help Preprocessor::preprocessToObj()
748 * @codingStandardsIgnoreStart
750 class PPDStack_Hash
extends PPDStack
{
751 // @codingStandardsIgnoreEnd
753 public function __construct() {
754 $this->elementClass
= 'PPDStackElement_Hash';
755 parent
::__construct();
756 $this->rootAccum
= new PPDAccum_Hash
;
762 * @codingStandardsIgnoreStart
764 class PPDStackElement_Hash
extends PPDStackElement
{
765 // @codingStandardsIgnoreENd
767 public function __construct( $data = array() ) {
768 $this->partClass
= 'PPDPart_Hash';
769 parent
::__construct( $data );
773 * Get the accumulator that would result if the close is not found.
775 * @param int|bool $openingCount
776 * @return PPDAccum_Hash
778 public function breakSyntax( $openingCount = false ) {
779 if ( $this->open
== "\n" ) {
780 $accum = $this->parts
[0]->out
;
782 if ( $openingCount === false ) {
783 $openingCount = $this->count
;
785 $accum = new PPDAccum_Hash
;
786 $accum->addLiteral( str_repeat( $this->open
, $openingCount ) );
788 foreach ( $this->parts
as $part ) {
792 $accum->addLiteral( '|' );
794 $accum->addAccum( $part->out
);
803 * @codingStandardsIgnoreStart
805 class PPDPart_Hash
extends PPDPart
{
806 // @codingStandardsIgnoreEnd
808 public function __construct( $out = '' ) {
809 $accum = new PPDAccum_Hash
;
811 $accum->addLiteral( $out );
813 parent
::__construct( $accum );
819 * @codingStandardsIgnoreStart
821 class PPDAccum_Hash
{
822 // @codingStandardsIgnoreEnd
824 public $firstNode, $lastNode;
826 public function __construct() {
827 $this->firstNode
= $this->lastNode
= false;
831 * Append a string literal
834 public function addLiteral( $s ) {
835 if ( $this->lastNode
=== false ) {
836 $this->firstNode
= $this->lastNode
= new PPNode_Hash_Text( $s );
837 } elseif ( $this->lastNode
instanceof PPNode_Hash_Text
) {
838 $this->lastNode
->value
.= $s;
840 $this->lastNode
->nextSibling
= new PPNode_Hash_Text( $s );
841 $this->lastNode
= $this->lastNode
->nextSibling
;
847 * @param PPNode $node
849 public function addNode( PPNode
$node ) {
850 if ( $this->lastNode
=== false ) {
851 $this->firstNode
= $this->lastNode
= $node;
853 $this->lastNode
->nextSibling
= $node;
854 $this->lastNode
= $node;
859 * Append a tree node with text contents
860 * @param string $name
861 * @param string $value
863 public function addNodeWithText( $name, $value ) {
864 $node = PPNode_Hash_Tree
::newWithText( $name, $value );
865 $this->addNode( $node );
869 * Append a PPDAccum_Hash
870 * Takes over ownership of the nodes in the source argument. These nodes may
871 * subsequently be modified, especially nextSibling.
872 * @param PPDAccum_Hash $accum
874 public function addAccum( $accum ) {
875 if ( $accum->lastNode
=== false ) {
877 } elseif ( $this->lastNode
=== false ) {
878 $this->firstNode
= $accum->firstNode
;
879 $this->lastNode
= $accum->lastNode
;
881 $this->lastNode
->nextSibling
= $accum->firstNode
;
882 $this->lastNode
= $accum->lastNode
;
888 * An expansion frame, used as a context to expand the result of preprocessToObj()
890 * @codingStandardsIgnoreStart
892 class PPFrame_Hash
implements PPFrame
{
893 // @codingStandardsIgnoreEnd
903 public $preprocessor;
912 * Hashtable listing templates which are disallowed for expansion in this frame,
913 * having been encountered previously in parent frames.
915 public $loopCheckHash;
918 * Recursion depth of this frame, top = 0
919 * Note that this is NOT the same as expansion depth in expand()
923 private $volatile = false;
929 protected $childExpansionCache;
932 * Construct a new preprocessor frame.
933 * @param Preprocessor $preprocessor The parent preprocessor
935 public function __construct( $preprocessor ) {
936 $this->preprocessor
= $preprocessor;
937 $this->parser
= $preprocessor->parser
;
938 $this->title
= $this->parser
->mTitle
;
939 $this->titleCache
= array( $this->title ?
$this->title
->getPrefixedDBkey() : false );
940 $this->loopCheckHash
= array();
942 $this->childExpansionCache
= array();
946 * Create a new child frame
947 * $args is optionally a multi-root PPNode or array containing the template arguments
949 * @param array|bool|PPNode_Hash_Array $args
950 * @param Title|bool $title
951 * @param int $indexOffset
952 * @throws MWException
953 * @return PPTemplateFrame_Hash
955 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
956 $namedArgs = array();
957 $numberedArgs = array();
958 if ( $title === false ) {
959 $title = $this->title
;
961 if ( $args !== false ) {
962 if ( $args instanceof PPNode_Hash_Array
) {
963 $args = $args->value
;
964 } elseif ( !is_array( $args ) ) {
965 throw new MWException( __METHOD__
. ': $args must be array or PPNode_Hash_Array' );
967 foreach ( $args as $arg ) {
968 $bits = $arg->splitArg();
969 if ( $bits['index'] !== '' ) {
970 // Numbered parameter
971 $index = $bits['index'] - $indexOffset;
972 if ( isset( $namedArgs[$index] ) ||
isset( $numberedArgs[$index] ) ) {
973 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
974 wfEscapeWikiText( $this->title
),
975 wfEscapeWikiText( $title ),
976 wfEscapeWikiText( $index ) )->text() );
977 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
979 $numberedArgs[$index] = $bits['value'];
980 unset( $namedArgs[$index] );
983 $name = trim( $this->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
984 if ( isset( $namedArgs[$name] ) ||
isset( $numberedArgs[$name] ) ) {
985 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
986 wfEscapeWikiText( $this->title
),
987 wfEscapeWikiText( $title ),
988 wfEscapeWikiText( $name ) )->text() );
989 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
991 $namedArgs[$name] = $bits['value'];
992 unset( $numberedArgs[$name] );
996 return new PPTemplateFrame_Hash( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
1000 * @throws MWException
1001 * @param string|int $key
1002 * @param string|PPNode $root
1006 public function cachedExpand( $key, $root, $flags = 0 ) {
1007 // we don't have a parent, so we don't have a cache
1008 return $this->expand( $root, $flags );
1012 * @throws MWException
1013 * @param string|PPNode $root
1017 public function expand( $root, $flags = 0 ) {
1018 static $expansionDepth = 0;
1019 if ( is_string( $root ) ) {
1023 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
1024 $this->parser
->limitationWarn( 'node-count-exceeded',
1025 $this->parser
->mPPNodeCount
,
1026 $this->parser
->mOptions
->getMaxPPNodeCount()
1028 return '<span class="error">Node-count limit exceeded</span>';
1030 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
1031 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
1033 $this->parser
->mOptions
->getMaxPPExpandDepth()
1035 return '<span class="error">Expansion depth limit exceeded</span>';
1038 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
1039 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
1042 $outStack = array( '', '' );
1043 $iteratorStack = array( false, $root );
1044 $indexStack = array( 0, 0 );
1046 while ( count( $iteratorStack ) > 1 ) {
1047 $level = count( $outStack ) - 1;
1048 $iteratorNode =& $iteratorStack[$level];
1049 $out =& $outStack[$level];
1050 $index =& $indexStack[$level];
1052 if ( is_array( $iteratorNode ) ) {
1053 if ( $index >= count( $iteratorNode ) ) {
1054 // All done with this iterator
1055 $iteratorStack[$level] = false;
1056 $contextNode = false;
1058 $contextNode = $iteratorNode[$index];
1061 } elseif ( $iteratorNode instanceof PPNode_Hash_Array
) {
1062 if ( $index >= $iteratorNode->getLength() ) {
1063 // All done with this iterator
1064 $iteratorStack[$level] = false;
1065 $contextNode = false;
1067 $contextNode = $iteratorNode->item( $index );
1071 // Copy to $contextNode and then delete from iterator stack,
1072 // because this is not an iterator but we do have to execute it once
1073 $contextNode = $iteratorStack[$level];
1074 $iteratorStack[$level] = false;
1077 $newIterator = false;
1079 if ( $contextNode === false ) {
1081 } elseif ( is_string( $contextNode ) ) {
1082 $out .= $contextNode;
1083 } elseif ( is_array( $contextNode ) ||
$contextNode instanceof PPNode_Hash_Array
) {
1084 $newIterator = $contextNode;
1085 } elseif ( $contextNode instanceof PPNode_Hash_Attr
) {
1087 } elseif ( $contextNode instanceof PPNode_Hash_Text
) {
1088 $out .= $contextNode->value
;
1089 } elseif ( $contextNode instanceof PPNode_Hash_Tree
) {
1090 if ( $contextNode->name
== 'template' ) {
1091 # Double-brace expansion
1092 $bits = $contextNode->splitTemplate();
1093 if ( $flags & PPFrame
::NO_TEMPLATES
) {
1094 $newIterator = $this->virtualBracketedImplode(
1100 $ret = $this->parser
->braceSubstitution( $bits, $this );
1101 if ( isset( $ret['object'] ) ) {
1102 $newIterator = $ret['object'];
1104 $out .= $ret['text'];
1107 } elseif ( $contextNode->name
== 'tplarg' ) {
1108 # Triple-brace expansion
1109 $bits = $contextNode->splitTemplate();
1110 if ( $flags & PPFrame
::NO_ARGS
) {
1111 $newIterator = $this->virtualBracketedImplode(
1117 $ret = $this->parser
->argSubstitution( $bits, $this );
1118 if ( isset( $ret['object'] ) ) {
1119 $newIterator = $ret['object'];
1121 $out .= $ret['text'];
1124 } elseif ( $contextNode->name
== 'comment' ) {
1125 # HTML-style comment
1126 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1127 if ( $this->parser
->ot
['html']
1128 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
1129 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
1132 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
1133 # Add a strip marker in PST mode so that pstPass2() can
1134 # run some old-fashioned regexes on the result.
1135 # Not in RECOVER_COMMENTS mode (extractSections) though.
1136 $out .= $this->parser
->insertStripItem( $contextNode->firstChild
->value
);
1138 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1139 $out .= $contextNode->firstChild
->value
;
1141 } elseif ( $contextNode->name
== 'ignore' ) {
1142 # Output suppression used by <includeonly> etc.
1143 # OT_WIKI will only respect <ignore> in substed templates.
1144 # The other output types respect it unless NO_IGNORE is set.
1145 # extractSections() sets NO_IGNORE and so never respects it.
1146 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] )
1147 ||
( $flags & PPFrame
::NO_IGNORE
)
1149 $out .= $contextNode->firstChild
->value
;
1153 } elseif ( $contextNode->name
== 'ext' ) {
1155 $bits = $contextNode->splitExt() +
array( 'attr' => null, 'inner' => null, 'close' => null );
1156 if ( $flags & PPFrame
::NO_TAGS
) {
1157 $s = '<' . $bits['name']->firstChild
->value
;
1158 if ( $bits['attr'] ) {
1159 $s .= $bits['attr']->firstChild
->value
;
1161 if ( $bits['inner'] ) {
1162 $s .= '>' . $bits['inner']->firstChild
->value
;
1163 if ( $bits['close'] ) {
1164 $s .= $bits['close']->firstChild
->value
;
1171 $out .= $this->parser
->extensionSubstitution( $bits, $this );
1173 } elseif ( $contextNode->name
== 'h' ) {
1175 if ( $this->parser
->ot
['html'] ) {
1176 # Expand immediately and insert heading index marker
1178 for ( $node = $contextNode->firstChild
; $node; $node = $node->nextSibling
) {
1179 $s .= $this->expand( $node, $flags );
1182 $bits = $contextNode->splitHeading();
1183 $titleText = $this->title
->getPrefixedDBkey();
1184 $this->parser
->mHeadings
[] = array( $titleText, $bits['i'] );
1185 $serial = count( $this->parser
->mHeadings
) - 1;
1186 $marker = Parser
::MARKER_PREFIX
. "-h-$serial-" . Parser
::MARKER_SUFFIX
;
1187 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1188 $this->parser
->mStripState
->addGeneral( $marker, '' );
1191 # Expand in virtual stack
1192 $newIterator = $contextNode->getChildren();
1195 # Generic recursive expansion
1196 $newIterator = $contextNode->getChildren();
1199 throw new MWException( __METHOD__
. ': Invalid parameter type' );
1202 if ( $newIterator !== false ) {
1204 $iteratorStack[] = $newIterator;
1206 } elseif ( $iteratorStack[$level] === false ) {
1207 // Return accumulated value to parent
1208 // With tail recursion
1209 while ( $iteratorStack[$level] === false && $level > 0 ) {
1210 $outStack[$level - 1] .= $out;
1211 array_pop( $outStack );
1212 array_pop( $iteratorStack );
1213 array_pop( $indexStack );
1219 return $outStack[0];
1223 * @param string $sep
1225 * @param string|PPNode $args,...
1228 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1229 $args = array_slice( func_get_args(), 2 );
1233 foreach ( $args as $root ) {
1234 if ( $root instanceof PPNode_Hash_Array
) {
1235 $root = $root->value
;
1237 if ( !is_array( $root ) ) {
1238 $root = array( $root );
1240 foreach ( $root as $node ) {
1246 $s .= $this->expand( $node, $flags );
1253 * Implode with no flags specified
1254 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1255 * @param string $sep
1256 * @param string|PPNode $args,...
1259 public function implode( $sep /*, ... */ ) {
1260 $args = array_slice( func_get_args(), 1 );
1264 foreach ( $args as $root ) {
1265 if ( $root instanceof PPNode_Hash_Array
) {
1266 $root = $root->value
;
1268 if ( !is_array( $root ) ) {
1269 $root = array( $root );
1271 foreach ( $root as $node ) {
1277 $s .= $this->expand( $node );
1284 * Makes an object that, when expand()ed, will be the same as one obtained
1287 * @param string $sep
1288 * @param string|PPNode $args,...
1289 * @return PPNode_Hash_Array
1291 public function virtualImplode( $sep /*, ... */ ) {
1292 $args = array_slice( func_get_args(), 1 );
1296 foreach ( $args as $root ) {
1297 if ( $root instanceof PPNode_Hash_Array
) {
1298 $root = $root->value
;
1300 if ( !is_array( $root ) ) {
1301 $root = array( $root );
1303 foreach ( $root as $node ) {
1312 return new PPNode_Hash_Array( $out );
1316 * Virtual implode with brackets
1318 * @param string $start
1319 * @param string $sep
1320 * @param string $end
1321 * @param string|PPNode $args,...
1322 * @return PPNode_Hash_Array
1324 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1325 $args = array_slice( func_get_args(), 3 );
1326 $out = array( $start );
1329 foreach ( $args as $root ) {
1330 if ( $root instanceof PPNode_Hash_Array
) {
1331 $root = $root->value
;
1333 if ( !is_array( $root ) ) {
1334 $root = array( $root );
1336 foreach ( $root as $node ) {
1346 return new PPNode_Hash_Array( $out );
1349 public function __toString() {
1354 * @param bool $level
1355 * @return array|bool|string
1357 public function getPDBK( $level = false ) {
1358 if ( $level === false ) {
1359 return $this->title
->getPrefixedDBkey();
1361 return isset( $this->titleCache
[$level] ) ?
$this->titleCache
[$level] : false;
1368 public function getArguments() {
1375 public function getNumberedArguments() {
1382 public function getNamedArguments() {
1387 * Returns true if there are no arguments in this frame
1391 public function isEmpty() {
1396 * @param string $name
1399 public function getArgument( $name ) {
1404 * Returns true if the infinite loop check is OK, false if a loop is detected
1406 * @param Title $title
1410 public function loopCheck( $title ) {
1411 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
1415 * Return true if the frame is a template frame
1419 public function isTemplate() {
1424 * Get a title of frame
1428 public function getTitle() {
1429 return $this->title
;
1433 * Set the volatile flag
1437 public function setVolatile( $flag = true ) {
1438 $this->volatile
= $flag;
1442 * Get the volatile flag
1446 public function isVolatile() {
1447 return $this->volatile
;
1455 public function setTTL( $ttl ) {
1456 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
1466 public function getTTL() {
1472 * Expansion frame with template arguments
1474 * @codingStandardsIgnoreStart
1476 class PPTemplateFrame_Hash
extends PPFrame_Hash
{
1477 // @codingStandardsIgnoreEnd
1479 public $numberedArgs, $namedArgs, $parent;
1480 public $numberedExpansionCache, $namedExpansionCache;
1483 * @param Preprocessor $preprocessor
1484 * @param bool|PPFrame $parent
1485 * @param array $numberedArgs
1486 * @param array $namedArgs
1487 * @param bool|Title $title
1489 public function __construct( $preprocessor, $parent = false, $numberedArgs = array(),
1490 $namedArgs = array(), $title = false
1492 parent
::__construct( $preprocessor );
1494 $this->parent
= $parent;
1495 $this->numberedArgs
= $numberedArgs;
1496 $this->namedArgs
= $namedArgs;
1497 $this->title
= $title;
1498 $pdbk = $title ?
$title->getPrefixedDBkey() : false;
1499 $this->titleCache
= $parent->titleCache
;
1500 $this->titleCache
[] = $pdbk;
1501 $this->loopCheckHash
= /*clone*/ $parent->loopCheckHash
;
1502 if ( $pdbk !== false ) {
1503 $this->loopCheckHash
[$pdbk] = true;
1505 $this->depth
= $parent->depth +
1;
1506 $this->numberedExpansionCache
= $this->namedExpansionCache
= array();
1509 public function __toString() {
1512 $args = $this->numberedArgs +
$this->namedArgs
;
1513 foreach ( $args as $name => $value ) {
1519 $s .= "\"$name\":\"" .
1520 str_replace( '"', '\\"', $value->__toString() ) . '"';
1527 * @throws MWException
1528 * @param string|int $key
1529 * @param string|PPNode $root
1533 public function cachedExpand( $key, $root, $flags = 0 ) {
1534 if ( isset( $this->parent
->childExpansionCache
[$key] ) ) {
1535 return $this->parent
->childExpansionCache
[$key];
1537 $retval = $this->expand( $root, $flags );
1538 if ( !$this->isVolatile() ) {
1539 $this->parent
->childExpansionCache
[$key] = $retval;
1545 * Returns true if there are no arguments in this frame
1549 public function isEmpty() {
1550 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
1556 public function getArguments() {
1557 $arguments = array();
1558 foreach ( array_merge(
1559 array_keys( $this->numberedArgs
),
1560 array_keys( $this->namedArgs
) ) as $key ) {
1561 $arguments[$key] = $this->getArgument( $key );
1569 public function getNumberedArguments() {
1570 $arguments = array();
1571 foreach ( array_keys( $this->numberedArgs
) as $key ) {
1572 $arguments[$key] = $this->getArgument( $key );
1580 public function getNamedArguments() {
1581 $arguments = array();
1582 foreach ( array_keys( $this->namedArgs
) as $key ) {
1583 $arguments[$key] = $this->getArgument( $key );
1590 * @return array|bool
1592 public function getNumberedArgument( $index ) {
1593 if ( !isset( $this->numberedArgs
[$index] ) ) {
1596 if ( !isset( $this->numberedExpansionCache
[$index] ) ) {
1597 # No trimming for unnamed arguments
1598 $this->numberedExpansionCache
[$index] = $this->parent
->expand(
1599 $this->numberedArgs
[$index],
1600 PPFrame
::STRIP_COMMENTS
1603 return $this->numberedExpansionCache
[$index];
1607 * @param string $name
1610 public function getNamedArgument( $name ) {
1611 if ( !isset( $this->namedArgs
[$name] ) ) {
1614 if ( !isset( $this->namedExpansionCache
[$name] ) ) {
1615 # Trim named arguments post-expand, for backwards compatibility
1616 $this->namedExpansionCache
[$name] = trim(
1617 $this->parent
->expand( $this->namedArgs
[$name], PPFrame
::STRIP_COMMENTS
) );
1619 return $this->namedExpansionCache
[$name];
1623 * @param string $name
1624 * @return array|bool
1626 public function getArgument( $name ) {
1627 $text = $this->getNumberedArgument( $name );
1628 if ( $text === false ) {
1629 $text = $this->getNamedArgument( $name );
1635 * Return true if the frame is a template frame
1639 public function isTemplate() {
1643 public function setVolatile( $flag = true ) {
1644 parent
::setVolatile( $flag );
1645 $this->parent
->setVolatile( $flag );
1648 public function setTTL( $ttl ) {
1649 parent
::setTTL( $ttl );
1650 $this->parent
->setTTL( $ttl );
1655 * Expansion frame with custom arguments
1657 * @codingStandardsIgnoreStart
1659 class PPCustomFrame_Hash
extends PPFrame_Hash
{
1660 // @codingStandardsIgnoreEnd
1664 public function __construct( $preprocessor, $args ) {
1665 parent
::__construct( $preprocessor );
1666 $this->args
= $args;
1669 public function __toString() {
1672 foreach ( $this->args
as $name => $value ) {
1678 $s .= "\"$name\":\"" .
1679 str_replace( '"', '\\"', $value->__toString() ) . '"';
1688 public function isEmpty() {
1689 return !count( $this->args
);
1696 public function getArgument( $index ) {
1697 if ( !isset( $this->args
[$index] ) ) {
1700 return $this->args
[$index];
1703 public function getArguments() {
1710 * @codingStandardsIgnoreStart
1712 class PPNode_Hash_Tree
implements PPNode
{
1713 // @codingStandardsIgnoreEnd
1715 public $name, $firstChild, $lastChild, $nextSibling;
1717 public function __construct( $name ) {
1718 $this->name
= $name;
1719 $this->firstChild
= $this->lastChild
= $this->nextSibling
= false;
1722 public function __toString() {
1725 for ( $node = $this->firstChild
; $node; $node = $node->nextSibling
) {
1726 if ( $node instanceof PPNode_Hash_Attr
) {
1727 $attribs .= ' ' . $node->name
. '="' . htmlspecialchars( $node->value
) . '"';
1729 $inner .= $node->__toString();
1732 if ( $inner === '' ) {
1733 return "<{$this->name}$attribs/>";
1735 return "<{$this->name}$attribs>$inner</{$this->name}>";
1740 * @param string $name
1741 * @param string $text
1742 * @return PPNode_Hash_Tree
1744 public static function newWithText( $name, $text ) {
1745 $obj = new self( $name );
1746 $obj->addChild( new PPNode_Hash_Text( $text ) );
1750 public function addChild( $node ) {
1751 if ( $this->lastChild
=== false ) {
1752 $this->firstChild
= $this->lastChild
= $node;
1754 $this->lastChild
->nextSibling
= $node;
1755 $this->lastChild
= $node;
1760 * @return PPNode_Hash_Array
1762 public function getChildren() {
1763 $children = array();
1764 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1765 $children[] = $child;
1767 return new PPNode_Hash_Array( $children );
1770 public function getFirstChild() {
1771 return $this->firstChild
;
1774 public function getNextSibling() {
1775 return $this->nextSibling
;
1778 public function getChildrenOfType( $name ) {
1779 $children = array();
1780 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1781 if ( isset( $child->name
) && $child->name
=== $name ) {
1782 $children[] = $child;
1791 public function getLength() {
1799 public function item( $i ) {
1806 public function getName() {
1811 * Split a "<part>" node into an associative array containing:
1812 * - name PPNode name
1813 * - index String index
1814 * - value PPNode value
1816 * @throws MWException
1819 public function splitArg() {
1821 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1822 if ( !isset( $child->name
) ) {
1825 if ( $child->name
=== 'name' ) {
1826 $bits['name'] = $child;
1827 if ( $child->firstChild
instanceof PPNode_Hash_Attr
1828 && $child->firstChild
->name
=== 'index'
1830 $bits['index'] = $child->firstChild
->value
;
1832 } elseif ( $child->name
=== 'value' ) {
1833 $bits['value'] = $child;
1837 if ( !isset( $bits['name'] ) ) {
1838 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
1840 if ( !isset( $bits['index'] ) ) {
1841 $bits['index'] = '';
1847 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1848 * All values in the resulting array are PPNodes. Inner and close are optional.
1850 * @throws MWException
1853 public function splitExt() {
1855 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1856 if ( !isset( $child->name
) ) {
1859 if ( $child->name
== 'name' ) {
1860 $bits['name'] = $child;
1861 } elseif ( $child->name
== 'attr' ) {
1862 $bits['attr'] = $child;
1863 } elseif ( $child->name
== 'inner' ) {
1864 $bits['inner'] = $child;
1865 } elseif ( $child->name
== 'close' ) {
1866 $bits['close'] = $child;
1869 if ( !isset( $bits['name'] ) ) {
1870 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
1876 * Split an "<h>" node
1878 * @throws MWException
1881 public function splitHeading() {
1882 if ( $this->name
!== 'h' ) {
1883 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1886 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1887 if ( !isset( $child->name
) ) {
1890 if ( $child->name
== 'i' ) {
1891 $bits['i'] = $child->value
;
1892 } elseif ( $child->name
== 'level' ) {
1893 $bits['level'] = $child->value
;
1896 if ( !isset( $bits['i'] ) ) {
1897 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1903 * Split a "<template>" or "<tplarg>" node
1905 * @throws MWException
1908 public function splitTemplate() {
1910 $bits = array( 'lineStart' => '' );
1911 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1912 if ( !isset( $child->name
) ) {
1915 if ( $child->name
== 'title' ) {
1916 $bits['title'] = $child;
1918 if ( $child->name
== 'part' ) {
1921 if ( $child->name
== 'lineStart' ) {
1922 $bits['lineStart'] = '1';
1925 if ( !isset( $bits['title'] ) ) {
1926 throw new MWException( 'Invalid node passed to ' . __METHOD__
);
1928 $bits['parts'] = new PPNode_Hash_Array( $parts );
1935 * @codingStandardsIgnoreStart
1937 class PPNode_Hash_Text
implements PPNode
{
1938 // @codingStandardsIgnoreEnd
1940 public $value, $nextSibling;
1942 public function __construct( $value ) {
1943 if ( is_object( $value ) ) {
1944 throw new MWException( __CLASS__
. ' given object instead of string' );
1946 $this->value
= $value;
1949 public function __toString() {
1950 return htmlspecialchars( $this->value
);
1953 public function getNextSibling() {
1954 return $this->nextSibling
;
1957 public function getChildren() {
1961 public function getFirstChild() {
1965 public function getChildrenOfType( $name ) {
1969 public function getLength() {
1973 public function item( $i ) {
1977 public function getName() {
1981 public function splitArg() {
1982 throw new MWException( __METHOD__
. ': not supported' );
1985 public function splitExt() {
1986 throw new MWException( __METHOD__
. ': not supported' );
1989 public function splitHeading() {
1990 throw new MWException( __METHOD__
. ': not supported' );
1996 * @codingStandardsIgnoreStart
1998 class PPNode_Hash_Array
implements PPNode
{
1999 // @codingStandardsIgnoreEnd
2001 public $value, $nextSibling;
2003 public function __construct( $value ) {
2004 $this->value
= $value;
2007 public function __toString() {
2008 return var_export( $this, true );
2011 public function getLength() {
2012 return count( $this->value
);
2015 public function item( $i ) {
2016 return $this->value
[$i];
2019 public function getName() {
2023 public function getNextSibling() {
2024 return $this->nextSibling
;
2027 public function getChildren() {
2031 public function getFirstChild() {
2035 public function getChildrenOfType( $name ) {
2039 public function splitArg() {
2040 throw new MWException( __METHOD__
. ': not supported' );
2043 public function splitExt() {
2044 throw new MWException( __METHOD__
. ': not supported' );
2047 public function splitHeading() {
2048 throw new MWException( __METHOD__
. ': not supported' );
2054 * @codingStandardsIgnoreStart
2056 class PPNode_Hash_Attr
implements PPNode
{
2057 // @codingStandardsIgnoreEnd
2059 public $name, $value, $nextSibling;
2061 public function __construct( $name, $value ) {
2062 $this->name
= $name;
2063 $this->value
= $value;
2066 public function __toString() {
2067 return "<@{$this->name}>" . htmlspecialchars( $this->value
) . "</@{$this->name}>";
2070 public function getName() {
2074 public function getNextSibling() {
2075 return $this->nextSibling
;
2078 public function getChildren() {
2082 public function getFirstChild() {
2086 public function getChildrenOfType( $name ) {
2090 public function getLength() {
2094 public function item( $i ) {
2098 public function splitArg() {
2099 throw new MWException( __METHOD__
. ': not supported' );
2102 public function splitExt() {
2103 throw new MWException( __METHOD__
. ': not supported' );
2106 public function splitHeading() {
2107 throw new MWException( __METHOD__
. ': not supported' );