3 * Preprocessor using PHP arrays
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Differences from DOM schema:
26 * * attribute nodes are children
27 * * "<h>" nodes that aren't at the top are replaced with <possible-h>
30 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
31 class Preprocessor_Hash
extends Preprocessor
{
32 // @codingStandardsIgnoreEnd
39 const CACHE_PREFIX
= 'preprocess-hash';
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 );
93 * Preprocess some wikitext and return the document tree.
94 * This is the ghost of Parser::replace_variables().
96 * @param string $text The text to parse
97 * @param int $flags Bitwise combination of:
98 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<includeonly>" as if the text is being
99 * included. Default is to assume a direct page view.
101 * The generated DOM tree must depend only on the input text and the flags.
102 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
104 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
105 * change in the DOM tree for a given text, must be passed through the section identifier
106 * in the section edit link and thus back to extractSections().
108 * The output of this function is currently only cached in process memory, but a persistent
109 * cache may be implemented at a later date which takes further advantage of these strict
110 * dependency requirements.
112 * @throws MWException
113 * @return PPNode_Hash_Tree
115 public function preprocessToObj( $text, $flags = 0 ) {
116 $tree = $this->cacheGetTree( $text, $flags );
117 if ( $tree !== false ) {
118 return unserialize( $tree );
133 'names' => array( 2 => null ),
139 $forInclusion = $flags & Parser
::PTD_FOR_INCLUSION
;
141 $xmlishElements = $this->parser
->getStripList();
142 $enableOnlyinclude = false;
143 if ( $forInclusion ) {
144 $ignoredTags = array( 'includeonly', '/includeonly' );
145 $ignoredElements = array( 'noinclude' );
146 $xmlishElements[] = 'noinclude';
147 if ( strpos( $text, '<onlyinclude>' ) !== false
148 && strpos( $text, '</onlyinclude>' ) !== false
150 $enableOnlyinclude = true;
153 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
154 $ignoredElements = array( 'includeonly' );
155 $xmlishElements[] = 'includeonly';
157 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
159 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
160 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
162 $stack = new PPDStack_Hash
;
164 $searchBase = "[{<\n";
165 // For fast reverse searches
166 $revText = strrev( $text );
167 $lengthText = strlen( $text );
169 // Input pointer, starts out pointing to a pseudo-newline before the start
171 // Current accumulator
172 $accum =& $stack->getAccum();
173 // True to find equals signs in arguments
175 // True to take notice of pipe characters
178 // True if $i is inside a possible heading
180 // True if there are no more greater-than (>) signs right of $i
182 // True to ignore all input up to the next <onlyinclude>
183 $findOnlyinclude = $enableOnlyinclude;
184 // Do a line-start run without outputting an LF character
185 $fakeLineStart = true;
188 // $this->memCheck();
190 if ( $findOnlyinclude ) {
191 // Ignore all input up to the next <onlyinclude>
192 $startPos = strpos( $text, '<onlyinclude>', $i );
193 if ( $startPos === false ) {
194 // Ignored section runs to the end
195 $accum->addNodeWithText( 'ignore', substr( $text, $i ) );
198 $tagEndPos = $startPos +
strlen( '<onlyinclude>' ); // past-the-end
199 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i ) );
201 $findOnlyinclude = false;
204 if ( $fakeLineStart ) {
205 $found = 'line-start';
208 # Find next opening brace, closing brace or pipe
209 $search = $searchBase;
210 if ( $stack->top
=== false ) {
211 $currentClosing = '';
213 $currentClosing = $stack->top
->close
;
214 $search .= $currentClosing;
220 // First equals will be for the template
224 # Output literal section, advance input counter
225 $literalLength = strcspn( $text, $search, $i );
226 if ( $literalLength > 0 ) {
227 $accum->addLiteral( substr( $text, $i, $literalLength ) );
228 $i +
= $literalLength;
230 if ( $i >= $lengthText ) {
231 if ( $currentClosing == "\n" ) {
232 // Do a past-the-end run to finish off the heading
240 $curChar = $text[$i];
241 if ( $curChar == '|' ) {
243 } elseif ( $curChar == '=' ) {
245 } elseif ( $curChar == '<' ) {
247 } elseif ( $curChar == "\n" ) {
251 $found = 'line-start';
253 } elseif ( $curChar == $currentClosing ) {
255 } elseif ( isset( $rules[$curChar] ) ) {
257 $rule = $rules[$curChar];
259 # Some versions of PHP have a strcspn which stops on null characters
260 # Ignore and continue
267 if ( $found == 'angle' ) {
269 // Handle </onlyinclude>
270 if ( $enableOnlyinclude
271 && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
273 $findOnlyinclude = true;
277 // Determine element name
278 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i +
1 ) ) {
279 // Element name missing or not listed
280 $accum->addLiteral( '<' );
285 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
287 // To avoid leaving blank lines, when a sequence of
288 // space-separated comments is both preceded and followed by
289 // a newline (ignoring spaces), then
290 // trim leading and trailing spaces and the trailing newline.
293 $endPos = strpos( $text, '-->', $i +
4 );
294 if ( $endPos === false ) {
295 // Unclosed comment in input, runs to end
296 $inner = substr( $text, $i );
297 $accum->addNodeWithText( 'comment', $inner );
300 // Search backwards for leading whitespace
301 $wsStart = $i ?
( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
303 // Search forwards for trailing whitespace
304 // $wsEnd will be the position of the last space (or the '>' if there's none)
305 $wsEnd = $endPos +
2 +
strspn( $text, " \t", $endPos +
3 );
307 // Keep looking forward as long as we're finding more
309 $comments = array( array( $wsStart, $wsEnd ) );
310 while ( substr( $text, $wsEnd +
1, 4 ) == '<!--' ) {
311 $c = strpos( $text, '-->', $wsEnd +
4 );
312 if ( $c === false ) {
315 $c = $c +
2 +
strspn( $text, " \t", $c +
3 );
316 $comments[] = array( $wsEnd +
1, $c );
320 // Eat the line if possible
321 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
322 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
323 // it's a possible beneficial b/c break.
324 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
325 && substr( $text, $wsEnd +
1, 1 ) == "\n"
327 // Remove leading whitespace from the end of the accumulator
328 // Sanity check first though
329 $wsLength = $i - $wsStart;
331 && $accum->lastNode
instanceof PPNode_Hash_Text
332 && strspn( $accum->lastNode
->value
, " \t", -$wsLength ) === $wsLength
334 $accum->lastNode
->value
= substr( $accum->lastNode
->value
, 0, -$wsLength );
337 // Dump all but the last comment to the accumulator
338 foreach ( $comments as $j => $com ) {
340 $endPos = $com[1] +
1;
341 if ( $j == ( count( $comments ) - 1 ) ) {
344 $inner = substr( $text, $startPos, $endPos - $startPos );
345 $accum->addNodeWithText( 'comment', $inner );
348 // Do a line-start run next time to look for headings after the comment
349 $fakeLineStart = true;
351 // No line to eat, just take the comment itself
357 $part = $stack->top
->getCurrentPart();
358 if ( !( isset( $part->commentEnd
) && $part->commentEnd
== $wsStart - 1 ) ) {
359 $part->visualEnd
= $wsStart;
361 // Else comments abutting, no change in visual end
362 $part->commentEnd
= $endPos;
365 $inner = substr( $text, $startPos, $endPos - $startPos +
1 );
366 $accum->addNodeWithText( 'comment', $inner );
371 $lowerName = strtolower( $name );
372 $attrStart = $i +
strlen( $name ) +
1;
375 $tagEndPos = $noMoreGT ?
false : strpos( $text, '>', $attrStart );
376 if ( $tagEndPos === false ) {
377 // Infinite backtrack
378 // Disable tag search to prevent worst-case O(N^2) performance
380 $accum->addLiteral( '<' );
385 // Handle ignored tags
386 if ( in_array( $lowerName, $ignoredTags ) ) {
387 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i +
1 ) );
393 if ( $text[$tagEndPos - 1] == '/' ) {
395 $attrEnd = $tagEndPos - 1;
400 $attrEnd = $tagEndPos;
402 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
403 $text, $matches, PREG_OFFSET_CAPTURE
, $tagEndPos +
1 )
405 $inner = substr( $text, $tagEndPos +
1, $matches[0][1] - $tagEndPos - 1 );
406 $i = $matches[0][1] +
strlen( $matches[0][0] );
407 $close = $matches[0][0];
409 // No end tag -- let it run out to the end of the text.
410 $inner = substr( $text, $tagEndPos +
1 );
415 // <includeonly> and <noinclude> just become <ignore> tags
416 if ( in_array( $lowerName, $ignoredElements ) ) {
417 $accum->addNodeWithText( 'ignore', substr( $text, $tagStartPos, $i - $tagStartPos ) );
421 if ( $attrEnd <= $attrStart ) {
424 // Note that the attr element contains the whitespace between name and attribute,
425 // this is necessary for precise reconstruction during pre-save transform.
426 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
429 $extNode = new PPNode_Hash_Tree( 'ext' );
430 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'name', $name ) );
431 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'attr', $attr ) );
432 if ( $inner !== null ) {
433 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'inner', $inner ) );
435 if ( $close !== null ) {
436 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'close', $close ) );
438 $accum->addNode( $extNode );
439 } elseif ( $found == 'line-start' ) {
440 // Is this the start of a heading?
441 // Line break belongs before the heading element in any case
442 if ( $fakeLineStart ) {
443 $fakeLineStart = false;
445 $accum->addLiteral( $curChar );
449 $count = strspn( $text, '=', $i, 6 );
450 if ( $count == 1 && $findEquals ) {
451 // DWIM: This looks kind of like a name/value separator.
452 // Let's let the equals handler have it and break the potential
453 // heading. This is heuristic, but AFAICT the methods for
454 // completely correct disambiguation are very complex.
455 } elseif ( $count > 0 ) {
459 'parts' => array( new PPDPart_Hash( str_repeat( '=', $count ) ) ),
462 $stack->push( $piece );
463 $accum =& $stack->getAccum();
464 extract( $stack->getFlags() );
467 } elseif ( $found == 'line-end' ) {
468 $piece = $stack->top
;
469 // A heading must be open, otherwise \n wouldn't have been in the search list
470 assert( '$piece->open == "\n"' );
471 $part = $piece->getCurrentPart();
472 // Search back through the input to see if it has a proper close.
473 // Do this using the reversed string since the other solutions
474 // (end anchor, etc.) are inefficient.
475 $wsLength = strspn( $revText, " \t", $lengthText - $i );
476 $searchStart = $i - $wsLength;
477 if ( isset( $part->commentEnd
) && $searchStart - 1 == $part->commentEnd
) {
478 // Comment found at line end
479 // Search for equals signs before the comment
480 $searchStart = $part->visualEnd
;
481 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
483 $count = $piece->count
;
484 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
485 if ( $equalsLength > 0 ) {
486 if ( $searchStart - $equalsLength == $piece->startPos
) {
487 // This is just a single string of equals signs on its own line
488 // Replicate the doHeadings behavior /={count}(.+)={count}/
489 // First find out how many equals signs there really are (don't stop at 6)
490 $count = $equalsLength;
494 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
497 $count = min( $equalsLength, $count );
500 // Normal match, output <h>
501 $element = new PPNode_Hash_Tree( 'possible-h' );
502 $element->addChild( new PPNode_Hash_Attr( 'level', $count ) );
503 $element->addChild( new PPNode_Hash_Attr( 'i', $headingIndex++
) );
504 $element->lastChild
->nextSibling
= $accum->firstNode
;
505 $element->lastChild
= $accum->lastNode
;
507 // Single equals sign on its own line, count=0
511 // No match, no <h>, just pass down the inner text
516 $accum =& $stack->getAccum();
517 extract( $stack->getFlags() );
519 // Append the result to the enclosing accumulator
520 if ( $element instanceof PPNode
) {
521 $accum->addNode( $element );
523 $accum->addAccum( $element );
525 // Note that we do NOT increment the input pointer.
526 // This is because the closing linebreak could be the opening linebreak of
527 // another heading. Infinite loops are avoided because the next iteration MUST
528 // hit the heading open case above, which unconditionally increments the
530 } elseif ( $found == 'open' ) {
531 # count opening brace characters
532 $count = strspn( $text, $curChar, $i );
534 # we need to add to stack only if opening brace count is enough for one of the rules
535 if ( $count >= $rule['min'] ) {
536 # Add it to the stack
539 'close' => $rule['end'],
541 'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
544 $stack->push( $piece );
545 $accum =& $stack->getAccum();
546 extract( $stack->getFlags() );
548 # Add literal brace(s)
549 $accum->addLiteral( str_repeat( $curChar, $count ) );
552 } elseif ( $found == 'close' ) {
553 $piece = $stack->top
;
554 # lets check if there are enough characters for closing brace
555 $maxCount = $piece->count
;
556 $count = strspn( $text, $curChar, $i, $maxCount );
558 # check for maximum matching characters (if there are 5 closing
559 # characters, we will probably need only 3 - depending on the rules)
560 $rule = $rules[$piece->open
];
561 if ( $count > $rule['max'] ) {
562 # The specified maximum exists in the callback array, unless the caller
564 $matchingCount = $rule['max'];
566 # Count is less than the maximum
567 # Skip any gaps in the callback array to find the true largest match
568 # Need to use array_key_exists not isset because the callback can be null
569 $matchingCount = $count;
570 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
575 if ( $matchingCount <= 0 ) {
576 # No matching element found in callback array
577 # Output a literal closing brace and continue
578 $accum->addLiteral( str_repeat( $curChar, $count ) );
582 $name = $rule['names'][$matchingCount];
583 if ( $name === null ) {
584 // No element, just literal text
585 $element = $piece->breakSyntax( $matchingCount );
586 $element->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
589 # Note: $parts is already XML, does not need to be encoded further
590 $parts = $piece->parts
;
591 $titleAccum = $parts[0]->out
;
594 $element = new PPNode_Hash_Tree( $name );
596 # The invocation is at the start of the line if lineStart is set in
597 # the stack, and all opening brackets are used up.
598 if ( $maxCount == $matchingCount && !empty( $piece->lineStart
) ) {
599 $element->addChild( new PPNode_Hash_Attr( 'lineStart', 1 ) );
601 $titleNode = new PPNode_Hash_Tree( 'title' );
602 $titleNode->firstChild
= $titleAccum->firstNode
;
603 $titleNode->lastChild
= $titleAccum->lastNode
;
604 $element->addChild( $titleNode );
606 foreach ( $parts as $part ) {
607 if ( isset( $part->eqpos
) ) {
610 for ( $node = $part->out
->firstNode
; $node; $node = $node->nextSibling
) {
611 if ( $node === $part->eqpos
) {
617 // if ( $cacheable ) { ... }
618 throw new MWException( __METHOD__
. ': eqpos not found' );
620 if ( $node->name
!== 'equals' ) {
621 // if ( $cacheable ) { ... }
622 throw new MWException( __METHOD__
. ': eqpos is not equals' );
626 // Construct name node
627 $nameNode = new PPNode_Hash_Tree( 'name' );
628 if ( $lastNode !== false ) {
629 $lastNode->nextSibling
= false;
630 $nameNode->firstChild
= $part->out
->firstNode
;
631 $nameNode->lastChild
= $lastNode;
634 // Construct value node
635 $valueNode = new PPNode_Hash_Tree( 'value' );
636 if ( $equalsNode->nextSibling
!== false ) {
637 $valueNode->firstChild
= $equalsNode->nextSibling
;
638 $valueNode->lastChild
= $part->out
->lastNode
;
640 $partNode = new PPNode_Hash_Tree( 'part' );
641 $partNode->addChild( $nameNode );
642 $partNode->addChild( $equalsNode->firstChild
);
643 $partNode->addChild( $valueNode );
644 $element->addChild( $partNode );
646 $partNode = new PPNode_Hash_Tree( 'part' );
647 $nameNode = new PPNode_Hash_Tree( 'name' );
648 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $argIndex++
) );
649 $valueNode = new PPNode_Hash_Tree( 'value' );
650 $valueNode->firstChild
= $part->out
->firstNode
;
651 $valueNode->lastChild
= $part->out
->lastNode
;
652 $partNode->addChild( $nameNode );
653 $partNode->addChild( $valueNode );
654 $element->addChild( $partNode );
659 # Advance input pointer
660 $i +
= $matchingCount;
664 $accum =& $stack->getAccum();
666 # Re-add the old stack element if it still has unmatched opening characters remaining
667 if ( $matchingCount < $piece->count
) {
668 $piece->parts
= array( new PPDPart_Hash
);
669 $piece->count
-= $matchingCount;
670 # do we still qualify for any callback with remaining count?
671 $min = $rules[$piece->open
]['min'];
672 if ( $piece->count
>= $min ) {
673 $stack->push( $piece );
674 $accum =& $stack->getAccum();
676 $accum->addLiteral( str_repeat( $piece->open
, $piece->count
) );
680 extract( $stack->getFlags() );
682 # Add XML element to the enclosing accumulator
683 if ( $element instanceof PPNode
) {
684 $accum->addNode( $element );
686 $accum->addAccum( $element );
688 } elseif ( $found == 'pipe' ) {
689 $findEquals = true; // shortcut for getFlags()
691 $accum =& $stack->getAccum();
693 } elseif ( $found == 'equals' ) {
694 $findEquals = false; // shortcut for getFlags()
695 $accum->addNodeWithText( 'equals', '=' );
696 $stack->getCurrentPart()->eqpos
= $accum->lastNode
;
701 # Output any remaining unclosed brackets
702 foreach ( $stack->stack
as $piece ) {
703 $stack->rootAccum
->addAccum( $piece->breakSyntax() );
706 # Enable top-level headings
707 for ( $node = $stack->rootAccum
->firstNode
; $node; $node = $node->nextSibling
) {
708 if ( isset( $node->name
) && $node->name
=== 'possible-h' ) {
713 $rootNode = new PPNode_Hash_Tree( 'root' );
714 $rootNode->firstChild
= $stack->rootAccum
->firstNode
;
715 $rootNode->lastChild
= $stack->rootAccum
->lastNode
;
718 $this->cacheSetTree( $text, $flags, serialize( $rootNode ) );
725 * Stack class to help Preprocessor::preprocessToObj()
728 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
729 class PPDStack_Hash
extends PPDStack
{
730 // @codingStandardsIgnoreEnd
732 public function __construct() {
733 $this->elementClass
= 'PPDStackElement_Hash';
734 parent
::__construct();
735 $this->rootAccum
= new PPDAccum_Hash
;
742 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
743 class PPDStackElement_Hash
extends PPDStackElement
{
744 // @codingStandardsIgnoreENd
746 public function __construct( $data = array() ) {
747 $this->partClass
= 'PPDPart_Hash';
748 parent
::__construct( $data );
752 * Get the accumulator that would result if the close is not found.
754 * @param int|bool $openingCount
755 * @return PPDAccum_Hash
757 public function breakSyntax( $openingCount = false ) {
758 if ( $this->open
== "\n" ) {
759 $accum = $this->parts
[0]->out
;
761 if ( $openingCount === false ) {
762 $openingCount = $this->count
;
764 $accum = new PPDAccum_Hash
;
765 $accum->addLiteral( str_repeat( $this->open
, $openingCount ) );
767 foreach ( $this->parts
as $part ) {
771 $accum->addLiteral( '|' );
773 $accum->addAccum( $part->out
);
783 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
784 class PPDPart_Hash
extends PPDPart
{
785 // @codingStandardsIgnoreEnd
787 public function __construct( $out = '' ) {
788 $accum = new PPDAccum_Hash
;
790 $accum->addLiteral( $out );
792 parent
::__construct( $accum );
799 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
800 class PPDAccum_Hash
{
801 // @codingStandardsIgnoreEnd
803 public $firstNode, $lastNode;
805 public function __construct() {
806 $this->firstNode
= $this->lastNode
= false;
810 * Append a string literal
813 public function addLiteral( $s ) {
814 if ( $this->lastNode
=== false ) {
815 $this->firstNode
= $this->lastNode
= new PPNode_Hash_Text( $s );
816 } elseif ( $this->lastNode
instanceof PPNode_Hash_Text
) {
817 $this->lastNode
->value
.= $s;
819 $this->lastNode
->nextSibling
= new PPNode_Hash_Text( $s );
820 $this->lastNode
= $this->lastNode
->nextSibling
;
826 * @param PPNode $node
828 public function addNode( PPNode
$node ) {
829 if ( $this->lastNode
=== false ) {
830 $this->firstNode
= $this->lastNode
= $node;
832 $this->lastNode
->nextSibling
= $node;
833 $this->lastNode
= $node;
838 * Append a tree node with text contents
839 * @param string $name
840 * @param string $value
842 public function addNodeWithText( $name, $value ) {
843 $node = PPNode_Hash_Tree
::newWithText( $name, $value );
844 $this->addNode( $node );
848 * Append a PPDAccum_Hash
849 * Takes over ownership of the nodes in the source argument. These nodes may
850 * subsequently be modified, especially nextSibling.
851 * @param PPDAccum_Hash $accum
853 public function addAccum( $accum ) {
854 if ( $accum->lastNode
=== false ) {
856 } elseif ( $this->lastNode
=== false ) {
857 $this->firstNode
= $accum->firstNode
;
858 $this->lastNode
= $accum->lastNode
;
860 $this->lastNode
->nextSibling
= $accum->firstNode
;
861 $this->lastNode
= $accum->lastNode
;
867 * An expansion frame, used as a context to expand the result of preprocessToObj()
870 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
871 class PPFrame_Hash
implements PPFrame
{
872 // @codingStandardsIgnoreEnd
882 public $preprocessor;
891 * Hashtable listing templates which are disallowed for expansion in this frame,
892 * having been encountered previously in parent frames.
894 public $loopCheckHash;
897 * Recursion depth of this frame, top = 0
898 * Note that this is NOT the same as expansion depth in expand()
902 private $volatile = false;
908 protected $childExpansionCache;
911 * Construct a new preprocessor frame.
912 * @param Preprocessor $preprocessor The parent preprocessor
914 public function __construct( $preprocessor ) {
915 $this->preprocessor
= $preprocessor;
916 $this->parser
= $preprocessor->parser
;
917 $this->title
= $this->parser
->mTitle
;
918 $this->titleCache
= array( $this->title ?
$this->title
->getPrefixedDBkey() : false );
919 $this->loopCheckHash
= array();
921 $this->childExpansionCache
= array();
925 * Create a new child frame
926 * $args is optionally a multi-root PPNode or array containing the template arguments
928 * @param array|bool|PPNode_Hash_Array $args
929 * @param Title|bool $title
930 * @param int $indexOffset
931 * @throws MWException
932 * @return PPTemplateFrame_Hash
934 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
935 $namedArgs = array();
936 $numberedArgs = array();
937 if ( $title === false ) {
938 $title = $this->title
;
940 if ( $args !== false ) {
941 if ( $args instanceof PPNode_Hash_Array
) {
942 $args = $args->value
;
943 } elseif ( !is_array( $args ) ) {
944 throw new MWException( __METHOD__
. ': $args must be array or PPNode_Hash_Array' );
946 foreach ( $args as $arg ) {
947 $bits = $arg->splitArg();
948 if ( $bits['index'] !== '' ) {
949 // Numbered parameter
950 $index = $bits['index'] - $indexOffset;
951 if ( isset( $namedArgs[$index] ) ||
isset( $numberedArgs[$index] ) ) {
952 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
953 wfEscapeWikiText( $this->title
),
954 wfEscapeWikiText( $title ),
955 wfEscapeWikiText( $index ) )->text() );
956 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
958 $numberedArgs[$index] = $bits['value'];
959 unset( $namedArgs[$index] );
962 $name = trim( $this->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
963 if ( isset( $namedArgs[$name] ) ||
isset( $numberedArgs[$name] ) ) {
964 $this->parser
->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
965 wfEscapeWikiText( $this->title
),
966 wfEscapeWikiText( $title ),
967 wfEscapeWikiText( $name ) )->text() );
968 $this->parser
->addTrackingCategory( 'duplicate-args-category' );
970 $namedArgs[$name] = $bits['value'];
971 unset( $numberedArgs[$name] );
975 return new PPTemplateFrame_Hash( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
979 * @throws MWException
980 * @param string|int $key
981 * @param string|PPNode $root
985 public function cachedExpand( $key, $root, $flags = 0 ) {
986 // we don't have a parent, so we don't have a cache
987 return $this->expand( $root, $flags );
991 * @throws MWException
992 * @param string|PPNode $root
996 public function expand( $root, $flags = 0 ) {
997 static $expansionDepth = 0;
998 if ( is_string( $root ) ) {
1002 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
1003 $this->parser
->limitationWarn( 'node-count-exceeded',
1004 $this->parser
->mPPNodeCount
,
1005 $this->parser
->mOptions
->getMaxPPNodeCount()
1007 return '<span class="error">Node-count limit exceeded</span>';
1009 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
1010 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
1012 $this->parser
->mOptions
->getMaxPPExpandDepth()
1014 return '<span class="error">Expansion depth limit exceeded</span>';
1017 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
1018 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
1021 $outStack = array( '', '' );
1022 $iteratorStack = array( false, $root );
1023 $indexStack = array( 0, 0 );
1025 while ( count( $iteratorStack ) > 1 ) {
1026 $level = count( $outStack ) - 1;
1027 $iteratorNode =& $iteratorStack[$level];
1028 $out =& $outStack[$level];
1029 $index =& $indexStack[$level];
1031 if ( is_array( $iteratorNode ) ) {
1032 if ( $index >= count( $iteratorNode ) ) {
1033 // All done with this iterator
1034 $iteratorStack[$level] = false;
1035 $contextNode = false;
1037 $contextNode = $iteratorNode[$index];
1040 } elseif ( $iteratorNode instanceof PPNode_Hash_Array
) {
1041 if ( $index >= $iteratorNode->getLength() ) {
1042 // All done with this iterator
1043 $iteratorStack[$level] = false;
1044 $contextNode = false;
1046 $contextNode = $iteratorNode->item( $index );
1050 // Copy to $contextNode and then delete from iterator stack,
1051 // because this is not an iterator but we do have to execute it once
1052 $contextNode = $iteratorStack[$level];
1053 $iteratorStack[$level] = false;
1056 $newIterator = false;
1058 if ( $contextNode === false ) {
1060 } elseif ( is_string( $contextNode ) ) {
1061 $out .= $contextNode;
1062 } elseif ( is_array( $contextNode ) ||
$contextNode instanceof PPNode_Hash_Array
) {
1063 $newIterator = $contextNode;
1064 } elseif ( $contextNode instanceof PPNode_Hash_Attr
) {
1066 } elseif ( $contextNode instanceof PPNode_Hash_Text
) {
1067 $out .= $contextNode->value
;
1068 } elseif ( $contextNode instanceof PPNode_Hash_Tree
) {
1069 if ( $contextNode->name
== 'template' ) {
1070 # Double-brace expansion
1071 $bits = $contextNode->splitTemplate();
1072 if ( $flags & PPFrame
::NO_TEMPLATES
) {
1073 $newIterator = $this->virtualBracketedImplode(
1079 $ret = $this->parser
->braceSubstitution( $bits, $this );
1080 if ( isset( $ret['object'] ) ) {
1081 $newIterator = $ret['object'];
1083 $out .= $ret['text'];
1086 } elseif ( $contextNode->name
== 'tplarg' ) {
1087 # Triple-brace expansion
1088 $bits = $contextNode->splitTemplate();
1089 if ( $flags & PPFrame
::NO_ARGS
) {
1090 $newIterator = $this->virtualBracketedImplode(
1096 $ret = $this->parser
->argSubstitution( $bits, $this );
1097 if ( isset( $ret['object'] ) ) {
1098 $newIterator = $ret['object'];
1100 $out .= $ret['text'];
1103 } elseif ( $contextNode->name
== 'comment' ) {
1104 # HTML-style comment
1105 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1106 # Not in RECOVER_COMMENTS mode (msgnw) though.
1107 if ( ( $this->parser
->ot
['html']
1108 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
1109 ||
( $flags & PPFrame
::STRIP_COMMENTS
)
1110 ) && !( $flags & PPFrame
::RECOVER_COMMENTS
)
1113 } elseif ( $this->parser
->ot
['wiki'] && !( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
1114 # Add a strip marker in PST mode so that pstPass2() can
1115 # run some old-fashioned regexes on the result.
1116 # Not in RECOVER_COMMENTS mode (extractSections) though.
1117 $out .= $this->parser
->insertStripItem( $contextNode->firstChild
->value
);
1119 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1120 $out .= $contextNode->firstChild
->value
;
1122 } elseif ( $contextNode->name
== 'ignore' ) {
1123 # Output suppression used by <includeonly> etc.
1124 # OT_WIKI will only respect <ignore> in substed templates.
1125 # The other output types respect it unless NO_IGNORE is set.
1126 # extractSections() sets NO_IGNORE and so never respects it.
1127 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] )
1128 ||
( $flags & PPFrame
::NO_IGNORE
)
1130 $out .= $contextNode->firstChild
->value
;
1134 } elseif ( $contextNode->name
== 'ext' ) {
1136 $bits = $contextNode->splitExt() +
array( 'attr' => null, 'inner' => null, 'close' => null );
1137 if ( $flags & PPFrame
::NO_TAGS
) {
1138 $s = '<' . $bits['name']->firstChild
->value
;
1139 if ( $bits['attr'] ) {
1140 $s .= $bits['attr']->firstChild
->value
;
1142 if ( $bits['inner'] ) {
1143 $s .= '>' . $bits['inner']->firstChild
->value
;
1144 if ( $bits['close'] ) {
1145 $s .= $bits['close']->firstChild
->value
;
1152 $out .= $this->parser
->extensionSubstitution( $bits, $this );
1154 } elseif ( $contextNode->name
== 'h' ) {
1156 if ( $this->parser
->ot
['html'] ) {
1157 # Expand immediately and insert heading index marker
1159 for ( $node = $contextNode->firstChild
; $node; $node = $node->nextSibling
) {
1160 $s .= $this->expand( $node, $flags );
1163 $bits = $contextNode->splitHeading();
1164 $titleText = $this->title
->getPrefixedDBkey();
1165 $this->parser
->mHeadings
[] = array( $titleText, $bits['i'] );
1166 $serial = count( $this->parser
->mHeadings
) - 1;
1167 $marker = Parser
::MARKER_PREFIX
. "-h-$serial-" . Parser
::MARKER_SUFFIX
;
1168 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1169 $this->parser
->mStripState
->addGeneral( $marker, '' );
1172 # Expand in virtual stack
1173 $newIterator = $contextNode->getChildren();
1176 # Generic recursive expansion
1177 $newIterator = $contextNode->getChildren();
1180 throw new MWException( __METHOD__
. ': Invalid parameter type' );
1183 if ( $newIterator !== false ) {
1185 $iteratorStack[] = $newIterator;
1187 } elseif ( $iteratorStack[$level] === false ) {
1188 // Return accumulated value to parent
1189 // With tail recursion
1190 while ( $iteratorStack[$level] === false && $level > 0 ) {
1191 $outStack[$level - 1] .= $out;
1192 array_pop( $outStack );
1193 array_pop( $iteratorStack );
1194 array_pop( $indexStack );
1200 return $outStack[0];
1204 * @param string $sep
1206 * @param string|PPNode $args,...
1209 public function implodeWithFlags( $sep, $flags /*, ... */ ) {
1210 $args = array_slice( func_get_args(), 2 );
1214 foreach ( $args as $root ) {
1215 if ( $root instanceof PPNode_Hash_Array
) {
1216 $root = $root->value
;
1218 if ( !is_array( $root ) ) {
1219 $root = array( $root );
1221 foreach ( $root as $node ) {
1227 $s .= $this->expand( $node, $flags );
1234 * Implode with no flags specified
1235 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1236 * @param string $sep
1237 * @param string|PPNode $args,...
1240 public function implode( $sep /*, ... */ ) {
1241 $args = array_slice( func_get_args(), 1 );
1245 foreach ( $args as $root ) {
1246 if ( $root instanceof PPNode_Hash_Array
) {
1247 $root = $root->value
;
1249 if ( !is_array( $root ) ) {
1250 $root = array( $root );
1252 foreach ( $root as $node ) {
1258 $s .= $this->expand( $node );
1265 * Makes an object that, when expand()ed, will be the same as one obtained
1268 * @param string $sep
1269 * @param string|PPNode $args,...
1270 * @return PPNode_Hash_Array
1272 public function virtualImplode( $sep /*, ... */ ) {
1273 $args = array_slice( func_get_args(), 1 );
1277 foreach ( $args as $root ) {
1278 if ( $root instanceof PPNode_Hash_Array
) {
1279 $root = $root->value
;
1281 if ( !is_array( $root ) ) {
1282 $root = array( $root );
1284 foreach ( $root as $node ) {
1293 return new PPNode_Hash_Array( $out );
1297 * Virtual implode with brackets
1299 * @param string $start
1300 * @param string $sep
1301 * @param string $end
1302 * @param string|PPNode $args,...
1303 * @return PPNode_Hash_Array
1305 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1306 $args = array_slice( func_get_args(), 3 );
1307 $out = array( $start );
1310 foreach ( $args as $root ) {
1311 if ( $root instanceof PPNode_Hash_Array
) {
1312 $root = $root->value
;
1314 if ( !is_array( $root ) ) {
1315 $root = array( $root );
1317 foreach ( $root as $node ) {
1327 return new PPNode_Hash_Array( $out );
1330 public function __toString() {
1335 * @param bool $level
1336 * @return array|bool|string
1338 public function getPDBK( $level = false ) {
1339 if ( $level === false ) {
1340 return $this->title
->getPrefixedDBkey();
1342 return isset( $this->titleCache
[$level] ) ?
$this->titleCache
[$level] : false;
1349 public function getArguments() {
1356 public function getNumberedArguments() {
1363 public function getNamedArguments() {
1368 * Returns true if there are no arguments in this frame
1372 public function isEmpty() {
1377 * @param string $name
1380 public function getArgument( $name ) {
1385 * Returns true if the infinite loop check is OK, false if a loop is detected
1387 * @param Title $title
1391 public function loopCheck( $title ) {
1392 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
1396 * Return true if the frame is a template frame
1400 public function isTemplate() {
1405 * Get a title of frame
1409 public function getTitle() {
1410 return $this->title
;
1414 * Set the volatile flag
1418 public function setVolatile( $flag = true ) {
1419 $this->volatile
= $flag;
1423 * Get the volatile flag
1427 public function isVolatile() {
1428 return $this->volatile
;
1436 public function setTTL( $ttl ) {
1437 if ( $ttl !== null && ( $this->ttl
=== null ||
$ttl < $this->ttl
) ) {
1447 public function getTTL() {
1453 * Expansion frame with template arguments
1456 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1457 class PPTemplateFrame_Hash
extends PPFrame_Hash
{
1458 // @codingStandardsIgnoreEnd
1460 public $numberedArgs, $namedArgs, $parent;
1461 public $numberedExpansionCache, $namedExpansionCache;
1464 * @param Preprocessor $preprocessor
1465 * @param bool|PPFrame $parent
1466 * @param array $numberedArgs
1467 * @param array $namedArgs
1468 * @param bool|Title $title
1470 public function __construct( $preprocessor, $parent = false, $numberedArgs = array(),
1471 $namedArgs = array(), $title = false
1473 parent
::__construct( $preprocessor );
1475 $this->parent
= $parent;
1476 $this->numberedArgs
= $numberedArgs;
1477 $this->namedArgs
= $namedArgs;
1478 $this->title
= $title;
1479 $pdbk = $title ?
$title->getPrefixedDBkey() : false;
1480 $this->titleCache
= $parent->titleCache
;
1481 $this->titleCache
[] = $pdbk;
1482 $this->loopCheckHash
= /*clone*/ $parent->loopCheckHash
;
1483 if ( $pdbk !== false ) {
1484 $this->loopCheckHash
[$pdbk] = true;
1486 $this->depth
= $parent->depth +
1;
1487 $this->numberedExpansionCache
= $this->namedExpansionCache
= array();
1490 public function __toString() {
1493 $args = $this->numberedArgs +
$this->namedArgs
;
1494 foreach ( $args as $name => $value ) {
1500 $s .= "\"$name\":\"" .
1501 str_replace( '"', '\\"', $value->__toString() ) . '"';
1508 * @throws MWException
1509 * @param string|int $key
1510 * @param string|PPNode $root
1514 public function cachedExpand( $key, $root, $flags = 0 ) {
1515 if ( isset( $this->parent
->childExpansionCache
[$key] ) ) {
1516 return $this->parent
->childExpansionCache
[$key];
1518 $retval = $this->expand( $root, $flags );
1519 if ( !$this->isVolatile() ) {
1520 $this->parent
->childExpansionCache
[$key] = $retval;
1526 * Returns true if there are no arguments in this frame
1530 public function isEmpty() {
1531 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
1537 public function getArguments() {
1538 $arguments = array();
1539 foreach ( array_merge(
1540 array_keys( $this->numberedArgs
),
1541 array_keys( $this->namedArgs
) ) as $key ) {
1542 $arguments[$key] = $this->getArgument( $key );
1550 public function getNumberedArguments() {
1551 $arguments = array();
1552 foreach ( array_keys( $this->numberedArgs
) as $key ) {
1553 $arguments[$key] = $this->getArgument( $key );
1561 public function getNamedArguments() {
1562 $arguments = array();
1563 foreach ( array_keys( $this->namedArgs
) as $key ) {
1564 $arguments[$key] = $this->getArgument( $key );
1571 * @return array|bool
1573 public function getNumberedArgument( $index ) {
1574 if ( !isset( $this->numberedArgs
[$index] ) ) {
1577 if ( !isset( $this->numberedExpansionCache
[$index] ) ) {
1578 # No trimming for unnamed arguments
1579 $this->numberedExpansionCache
[$index] = $this->parent
->expand(
1580 $this->numberedArgs
[$index],
1581 PPFrame
::STRIP_COMMENTS
1584 return $this->numberedExpansionCache
[$index];
1588 * @param string $name
1591 public function getNamedArgument( $name ) {
1592 if ( !isset( $this->namedArgs
[$name] ) ) {
1595 if ( !isset( $this->namedExpansionCache
[$name] ) ) {
1596 # Trim named arguments post-expand, for backwards compatibility
1597 $this->namedExpansionCache
[$name] = trim(
1598 $this->parent
->expand( $this->namedArgs
[$name], PPFrame
::STRIP_COMMENTS
) );
1600 return $this->namedExpansionCache
[$name];
1604 * @param string $name
1605 * @return array|bool
1607 public function getArgument( $name ) {
1608 $text = $this->getNumberedArgument( $name );
1609 if ( $text === false ) {
1610 $text = $this->getNamedArgument( $name );
1616 * Return true if the frame is a template frame
1620 public function isTemplate() {
1624 public function setVolatile( $flag = true ) {
1625 parent
::setVolatile( $flag );
1626 $this->parent
->setVolatile( $flag );
1629 public function setTTL( $ttl ) {
1630 parent
::setTTL( $ttl );
1631 $this->parent
->setTTL( $ttl );
1636 * Expansion frame with custom arguments
1639 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1640 class PPCustomFrame_Hash
extends PPFrame_Hash
{
1641 // @codingStandardsIgnoreEnd
1645 public function __construct( $preprocessor, $args ) {
1646 parent
::__construct( $preprocessor );
1647 $this->args
= $args;
1650 public function __toString() {
1653 foreach ( $this->args
as $name => $value ) {
1659 $s .= "\"$name\":\"" .
1660 str_replace( '"', '\\"', $value->__toString() ) . '"';
1669 public function isEmpty() {
1670 return !count( $this->args
);
1677 public function getArgument( $index ) {
1678 if ( !isset( $this->args
[$index] ) ) {
1681 return $this->args
[$index];
1684 public function getArguments() {
1692 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1693 class PPNode_Hash_Tree
implements PPNode
{
1694 // @codingStandardsIgnoreEnd
1696 public $name, $firstChild, $lastChild, $nextSibling;
1698 public function __construct( $name ) {
1699 $this->name
= $name;
1700 $this->firstChild
= $this->lastChild
= $this->nextSibling
= false;
1703 public function __toString() {
1706 for ( $node = $this->firstChild
; $node; $node = $node->nextSibling
) {
1707 if ( $node instanceof PPNode_Hash_Attr
) {
1708 $attribs .= ' ' . $node->name
. '="' . htmlspecialchars( $node->value
) . '"';
1710 $inner .= $node->__toString();
1713 if ( $inner === '' ) {
1714 return "<{$this->name}$attribs/>";
1716 return "<{$this->name}$attribs>$inner</{$this->name}>";
1721 * @param string $name
1722 * @param string $text
1723 * @return PPNode_Hash_Tree
1725 public static function newWithText( $name, $text ) {
1726 $obj = new self( $name );
1727 $obj->addChild( new PPNode_Hash_Text( $text ) );
1731 public function addChild( $node ) {
1732 if ( $this->lastChild
=== false ) {
1733 $this->firstChild
= $this->lastChild
= $node;
1735 $this->lastChild
->nextSibling
= $node;
1736 $this->lastChild
= $node;
1741 * @return PPNode_Hash_Array
1743 public function getChildren() {
1744 $children = array();
1745 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1746 $children[] = $child;
1748 return new PPNode_Hash_Array( $children );
1751 public function getFirstChild() {
1752 return $this->firstChild
;
1755 public function getNextSibling() {
1756 return $this->nextSibling
;
1759 public function getChildrenOfType( $name ) {
1760 $children = array();
1761 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1762 if ( isset( $child->name
) && $child->name
=== $name ) {
1763 $children[] = $child;
1772 public function getLength() {
1780 public function item( $i ) {
1787 public function getName() {
1792 * Split a "<part>" node into an associative array containing:
1793 * - name PPNode name
1794 * - index String index
1795 * - value PPNode value
1797 * @throws MWException
1800 public function splitArg() {
1802 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1803 if ( !isset( $child->name
) ) {
1806 if ( $child->name
=== 'name' ) {
1807 $bits['name'] = $child;
1808 if ( $child->firstChild
instanceof PPNode_Hash_Attr
1809 && $child->firstChild
->name
=== 'index'
1811 $bits['index'] = $child->firstChild
->value
;
1813 } elseif ( $child->name
=== 'value' ) {
1814 $bits['value'] = $child;
1818 if ( !isset( $bits['name'] ) ) {
1819 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
1821 if ( !isset( $bits['index'] ) ) {
1822 $bits['index'] = '';
1828 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1829 * All values in the resulting array are PPNodes. Inner and close are optional.
1831 * @throws MWException
1834 public function splitExt() {
1836 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1837 if ( !isset( $child->name
) ) {
1840 if ( $child->name
== 'name' ) {
1841 $bits['name'] = $child;
1842 } elseif ( $child->name
== 'attr' ) {
1843 $bits['attr'] = $child;
1844 } elseif ( $child->name
== 'inner' ) {
1845 $bits['inner'] = $child;
1846 } elseif ( $child->name
== 'close' ) {
1847 $bits['close'] = $child;
1850 if ( !isset( $bits['name'] ) ) {
1851 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
1857 * Split an "<h>" node
1859 * @throws MWException
1862 public function splitHeading() {
1863 if ( $this->name
!== 'h' ) {
1864 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1867 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1868 if ( !isset( $child->name
) ) {
1871 if ( $child->name
== 'i' ) {
1872 $bits['i'] = $child->value
;
1873 } elseif ( $child->name
== 'level' ) {
1874 $bits['level'] = $child->value
;
1877 if ( !isset( $bits['i'] ) ) {
1878 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1884 * Split a "<template>" or "<tplarg>" node
1886 * @throws MWException
1889 public function splitTemplate() {
1891 $bits = array( 'lineStart' => '' );
1892 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1893 if ( !isset( $child->name
) ) {
1896 if ( $child->name
== 'title' ) {
1897 $bits['title'] = $child;
1899 if ( $child->name
== 'part' ) {
1902 if ( $child->name
== 'lineStart' ) {
1903 $bits['lineStart'] = '1';
1906 if ( !isset( $bits['title'] ) ) {
1907 throw new MWException( 'Invalid node passed to ' . __METHOD__
);
1909 $bits['parts'] = new PPNode_Hash_Array( $parts );
1917 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1918 class PPNode_Hash_Text
implements PPNode
{
1919 // @codingStandardsIgnoreEnd
1921 public $value, $nextSibling;
1923 public function __construct( $value ) {
1924 if ( is_object( $value ) ) {
1925 throw new MWException( __CLASS__
. ' given object instead of string' );
1927 $this->value
= $value;
1930 public function __toString() {
1931 return htmlspecialchars( $this->value
);
1934 public function getNextSibling() {
1935 return $this->nextSibling
;
1938 public function getChildren() {
1942 public function getFirstChild() {
1946 public function getChildrenOfType( $name ) {
1950 public function getLength() {
1954 public function item( $i ) {
1958 public function getName() {
1962 public function splitArg() {
1963 throw new MWException( __METHOD__
. ': not supported' );
1966 public function splitExt() {
1967 throw new MWException( __METHOD__
. ': not supported' );
1970 public function splitHeading() {
1971 throw new MWException( __METHOD__
. ': not supported' );
1978 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
1979 class PPNode_Hash_Array
implements PPNode
{
1980 // @codingStandardsIgnoreEnd
1982 public $value, $nextSibling;
1984 public function __construct( $value ) {
1985 $this->value
= $value;
1988 public function __toString() {
1989 return var_export( $this, true );
1992 public function getLength() {
1993 return count( $this->value
);
1996 public function item( $i ) {
1997 return $this->value
[$i];
2000 public function getName() {
2004 public function getNextSibling() {
2005 return $this->nextSibling
;
2008 public function getChildren() {
2012 public function getFirstChild() {
2016 public function getChildrenOfType( $name ) {
2020 public function splitArg() {
2021 throw new MWException( __METHOD__
. ': not supported' );
2024 public function splitExt() {
2025 throw new MWException( __METHOD__
. ': not supported' );
2028 public function splitHeading() {
2029 throw new MWException( __METHOD__
. ': not supported' );
2036 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
2037 class PPNode_Hash_Attr
implements PPNode
{
2038 // @codingStandardsIgnoreEnd
2040 public $name, $value, $nextSibling;
2042 public function __construct( $name, $value ) {
2043 $this->name
= $name;
2044 $this->value
= $value;
2047 public function __toString() {
2048 return "<@{$this->name}>" . htmlspecialchars( $this->value
) . "</@{$this->name}>";
2051 public function getName() {
2055 public function getNextSibling() {
2056 return $this->nextSibling
;
2059 public function getChildren() {
2063 public function getFirstChild() {
2067 public function getChildrenOfType( $name ) {
2071 public function getLength() {
2075 public function item( $i ) {
2079 public function splitArg() {
2080 throw new MWException( __METHOD__
. ': not supported' );
2083 public function splitExt() {
2084 throw new MWException( __METHOD__
. ': not supported' );
2087 public function splitHeading() {
2088 throw new MWException( __METHOD__
. ': not supported' );