Revert r34906, r34907, r34928 -- mixing high-level data into low-level storage functi...
[lhc/web/wiklou.git] / includes / Preprocessor_Hash.php
1 <?php
2
3 /**
4 * Differences from DOM schema:
5 * * attribute nodes are children
6 * * <h> nodes that aren't at the top are replaced with <possible-h>
7 */
8
9 class Preprocessor_Hash implements Preprocessor {
10 var $parser;
11
12 function __construct( $parser ) {
13 $this->parser = $parser;
14 }
15
16 function newFrame() {
17 return new PPFrame_Hash( $this );
18 }
19
20 /**
21 * Preprocess some wikitext and return the document tree.
22 * This is the ghost of Parser::replace_variables().
23 *
24 * @param string $text The text to parse
25 * @param integer flags Bitwise combination of:
26 * Parser::PTD_FOR_INCLUSION Handle <noinclude>/<includeonly> as if the text is being
27 * included. Default is to assume a direct page view.
28 *
29 * The generated DOM tree must depend only on the input text and the flags.
30 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
31 *
32 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
33 * change in the DOM tree for a given text, must be passed through the section identifier
34 * in the section edit link and thus back to extractSections().
35 *
36 * The output of this function is currently only cached in process memory, but a persistent
37 * cache may be implemented at a later date which takes further advantage of these strict
38 * dependency requirements.
39 *
40 * @private
41 */
42 function preprocessToObj( $text, $flags = 0 ) {
43 wfProfileIn( __METHOD__ );
44
45 $rules = array(
46 '{' => array(
47 'end' => '}',
48 'names' => array(
49 2 => 'template',
50 3 => 'tplarg',
51 ),
52 'min' => 2,
53 'max' => 3,
54 ),
55 '[' => array(
56 'end' => ']',
57 'names' => array( 2 => null ),
58 'min' => 2,
59 'max' => 2,
60 )
61 );
62
63 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
64
65 $xmlishElements = $this->parser->getStripList();
66 $enableOnlyinclude = false;
67 if ( $forInclusion ) {
68 $ignoredTags = array( 'includeonly', '/includeonly' );
69 $ignoredElements = array( 'noinclude' );
70 $xmlishElements[] = 'noinclude';
71 if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) {
72 $enableOnlyinclude = true;
73 }
74 } else {
75 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
76 $ignoredElements = array( 'includeonly' );
77 $xmlishElements[] = 'includeonly';
78 }
79 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
80
81 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
82 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
83
84 $stack = new PPDStack_Hash;
85
86 $searchBase = "[{<\n";
87 $revText = strrev( $text ); // For fast reverse searches
88
89 $i = 0; # Input pointer, starts out pointing to a pseudo-newline before the start
90 $accum =& $stack->getAccum(); # Current accumulator
91 $findEquals = false; # True to find equals signs in arguments
92 $findPipe = false; # True to take notice of pipe characters
93 $headingIndex = 1;
94 $inHeading = false; # True if $i is inside a possible heading
95 $noMoreGT = false; # True if there are no more greater-than (>) signs right of $i
96 $findOnlyinclude = $enableOnlyinclude; # True to ignore all input up to the next <onlyinclude>
97 $fakeLineStart = true; # Do a line-start run without outputting an LF character
98
99 while ( true ) {
100 //$this->memCheck();
101
102 if ( $findOnlyinclude ) {
103 // Ignore all input up to the next <onlyinclude>
104 $startPos = strpos( $text, '<onlyinclude>', $i );
105 if ( $startPos === false ) {
106 // Ignored section runs to the end
107 $accum->addNodeWithText( 'ignore', substr( $text, $i ) );
108 break;
109 }
110 $tagEndPos = $startPos + strlen( '<onlyinclude>' ); // past-the-end
111 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i ) );
112 $i = $tagEndPos;
113 $findOnlyinclude = false;
114 }
115
116 if ( $fakeLineStart ) {
117 $found = 'line-start';
118 $curChar = '';
119 } else {
120 # Find next opening brace, closing brace or pipe
121 $search = $searchBase;
122 if ( $stack->top === false ) {
123 $currentClosing = '';
124 } else {
125 $currentClosing = $stack->top->close;
126 $search .= $currentClosing;
127 }
128 if ( $findPipe ) {
129 $search .= '|';
130 }
131 if ( $findEquals ) {
132 // First equals will be for the template
133 $search .= '=';
134 }
135 $rule = null;
136 # Output literal section, advance input counter
137 $literalLength = strcspn( $text, $search, $i );
138 if ( $literalLength > 0 ) {
139 $accum->addLiteral( substr( $text, $i, $literalLength ) );
140 $i += $literalLength;
141 }
142 if ( $i >= strlen( $text ) ) {
143 if ( $currentClosing == "\n" ) {
144 // Do a past-the-end run to finish off the heading
145 $curChar = '';
146 $found = 'line-end';
147 } else {
148 # All done
149 break;
150 }
151 } else {
152 $curChar = $text[$i];
153 if ( $curChar == '|' ) {
154 $found = 'pipe';
155 } elseif ( $curChar == '=' ) {
156 $found = 'equals';
157 } elseif ( $curChar == '<' ) {
158 $found = 'angle';
159 } elseif ( $curChar == "\n" ) {
160 if ( $inHeading ) {
161 $found = 'line-end';
162 } else {
163 $found = 'line-start';
164 }
165 } elseif ( $curChar == $currentClosing ) {
166 $found = 'close';
167 } elseif ( isset( $rules[$curChar] ) ) {
168 $found = 'open';
169 $rule = $rules[$curChar];
170 } else {
171 # Some versions of PHP have a strcspn which stops on null characters
172 # Ignore and continue
173 ++$i;
174 continue;
175 }
176 }
177 }
178
179 if ( $found == 'angle' ) {
180 $matches = false;
181 // Handle </onlyinclude>
182 if ( $enableOnlyinclude && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>' ) {
183 $findOnlyinclude = true;
184 continue;
185 }
186
187 // Determine element name
188 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
189 // Element name missing or not listed
190 $accum->addLiteral( '<' );
191 ++$i;
192 continue;
193 }
194 // Handle comments
195 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
196 // To avoid leaving blank lines, when a comment is both preceded
197 // and followed by a newline (ignoring spaces), trim leading and
198 // trailing spaces and one of the newlines.
199
200 // Find the end
201 $endPos = strpos( $text, '-->', $i + 4 );
202 if ( $endPos === false ) {
203 // Unclosed comment in input, runs to end
204 $inner = substr( $text, $i );
205 $accum->addNodeWithText( 'comment', $inner );
206 $i = strlen( $text );
207 } else {
208 // Search backwards for leading whitespace
209 $wsStart = $i ? ( $i - strspn( $revText, ' ', strlen( $text ) - $i ) ) : 0;
210 // Search forwards for trailing whitespace
211 // $wsEnd will be the position of the last space
212 $wsEnd = $endPos + 2 + strspn( $text, ' ', $endPos + 3 );
213 // Eat the line if possible
214 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
215 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
216 // it's a possible beneficial b/c break.
217 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
218 && substr( $text, $wsEnd + 1, 1 ) == "\n" )
219 {
220 $startPos = $wsStart;
221 $endPos = $wsEnd + 1;
222 // Remove leading whitespace from the end of the accumulator
223 // Sanity check first though
224 $wsLength = $i - $wsStart;
225 if ( $wsLength > 0
226 && $accum->lastNode instanceof PPNode_Hash_Text
227 && substr( $accum->lastNode->value, -$wsLength ) === str_repeat( ' ', $wsLength ) )
228 {
229 $accum->lastNode->value = substr( $accum->lastNode->value, 0, -$wsLength );
230 }
231 // Do a line-start run next time to look for headings after the comment
232 $fakeLineStart = true;
233 } else {
234 // No line to eat, just take the comment itself
235 $startPos = $i;
236 $endPos += 2;
237 }
238
239 if ( $stack->top ) {
240 $part = $stack->top->getCurrentPart();
241 if ( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) {
242 // Comments abutting, no change in visual end
243 $part->commentEnd = $wsEnd;
244 } else {
245 $part->visualEnd = $wsStart;
246 $part->commentEnd = $endPos;
247 }
248 }
249 $i = $endPos + 1;
250 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
251 $accum->addNodeWithText( 'comment', $inner );
252 }
253 continue;
254 }
255 $name = $matches[1];
256 $lowerName = strtolower( $name );
257 $attrStart = $i + strlen( $name ) + 1;
258
259 // Find end of tag
260 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
261 if ( $tagEndPos === false ) {
262 // Infinite backtrack
263 // Disable tag search to prevent worst-case O(N^2) performance
264 $noMoreGT = true;
265 $accum->addLiteral( '<' );
266 ++$i;
267 continue;
268 }
269
270 // Handle ignored tags
271 if ( in_array( $lowerName, $ignoredTags ) ) {
272 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i + 1 ) );
273 $i = $tagEndPos + 1;
274 continue;
275 }
276
277 $tagStartPos = $i;
278 if ( $text[$tagEndPos-1] == '/' ) {
279 // Short end tag
280 $attrEnd = $tagEndPos - 1;
281 $inner = null;
282 $i = $tagEndPos + 1;
283 $close = null;
284 } else {
285 $attrEnd = $tagEndPos;
286 // Find closing tag
287 if ( preg_match( "/<\/$name\s*>/i", $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) ) {
288 $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
289 $i = $matches[0][1] + strlen( $matches[0][0] );
290 $close = $matches[0][0];
291 } else {
292 // No end tag -- let it run out to the end of the text.
293 $inner = substr( $text, $tagEndPos + 1 );
294 $i = strlen( $text );
295 $close = null;
296 }
297 }
298 // <includeonly> and <noinclude> just become <ignore> tags
299 if ( in_array( $lowerName, $ignoredElements ) ) {
300 $accum->addNodeWithText( 'ignore', substr( $text, $tagStartPos, $i - $tagStartPos ) );
301 continue;
302 }
303
304 if ( $attrEnd <= $attrStart ) {
305 $attr = '';
306 } else {
307 // Note that the attr element contains the whitespace between name and attribute,
308 // this is necessary for precise reconstruction during pre-save transform.
309 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
310 }
311
312 $extNode = new PPNode_Hash_Tree( 'ext' );
313 $extNode->addChild( PPNode_Hash_Tree::newWithText( 'name', $name ) );
314 $extNode->addChild( PPNode_Hash_Tree::newWithText( 'attr', $attr ) );
315 if ( $inner !== null ) {
316 $extNode->addChild( PPNode_Hash_Tree::newWithText( 'inner', $inner ) );
317 }
318 if ( $close !== null ) {
319 $extNode->addChild( PPNode_Hash_Tree::newWithText( 'close', $close ) );
320 }
321 $accum->addNode( $extNode );
322 }
323
324 elseif ( $found == 'line-start' ) {
325 // Is this the start of a heading?
326 // Line break belongs before the heading element in any case
327 if ( $fakeLineStart ) {
328 $fakeLineStart = false;
329 } else {
330 $accum->addLiteral( $curChar );
331 $i++;
332 }
333
334 $count = strspn( $text, '=', $i, 6 );
335 if ( $count == 1 && $findEquals ) {
336 // DWIM: This looks kind of like a name/value separator
337 // Let's let the equals handler have it and break the potential heading
338 // This is heuristic, but AFAICT the methods for completely correct disambiguation are very complex.
339 } elseif ( $count > 0 ) {
340 $piece = array(
341 'open' => "\n",
342 'close' => "\n",
343 'parts' => array( new PPDPart_Hash( str_repeat( '=', $count ) ) ),
344 'startPos' => $i,
345 'count' => $count );
346 $stack->push( $piece );
347 $accum =& $stack->getAccum();
348 extract( $stack->getFlags() );
349 $i += $count;
350 }
351 }
352
353 elseif ( $found == 'line-end' ) {
354 $piece = $stack->top;
355 // A heading must be open, otherwise \n wouldn't have been in the search list
356 assert( $piece->open == "\n" );
357 $part = $piece->getCurrentPart();
358 // Search back through the input to see if it has a proper close
359 // Do this using the reversed string since the other solutions (end anchor, etc.) are inefficient
360 $wsLength = strspn( $revText, " \t", strlen( $text ) - $i );
361 $searchStart = $i - $wsLength;
362 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
363 // Comment found at line end
364 // Search for equals signs before the comment
365 $searchStart = $part->visualEnd;
366 $searchStart -= strspn( $revText, " \t", strlen( $text ) - $searchStart );
367 }
368 $count = $piece->count;
369 $equalsLength = strspn( $revText, '=', strlen( $text ) - $searchStart );
370 if ( $equalsLength > 0 ) {
371 if ( $i - $equalsLength == $piece->startPos ) {
372 // This is just a single string of equals signs on its own line
373 // Replicate the doHeadings behaviour /={count}(.+)={count}/
374 // First find out how many equals signs there really are (don't stop at 6)
375 $count = $equalsLength;
376 if ( $count < 3 ) {
377 $count = 0;
378 } else {
379 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
380 }
381 } else {
382 $count = min( $equalsLength, $count );
383 }
384 if ( $count > 0 ) {
385 // Normal match, output <h>
386 $element = new PPNode_Hash_Tree( 'possible-h' );
387 $element->addChild( new PPNode_Hash_Attr( 'level', $count ) );
388 $element->addChild( new PPNode_Hash_Attr( 'i', $headingIndex++ ) );
389 $element->lastChild->nextSibling = $accum->firstNode;
390 $element->lastChild = $accum->lastNode;
391 } else {
392 // Single equals sign on its own line, count=0
393 $element = $accum;
394 }
395 } else {
396 // No match, no <h>, just pass down the inner text
397 $element = $accum;
398 }
399 // Unwind the stack
400 $stack->pop();
401 $accum =& $stack->getAccum();
402 extract( $stack->getFlags() );
403
404 // Append the result to the enclosing accumulator
405 if ( $element instanceof PPNode ) {
406 $accum->addNode( $element );
407 } else {
408 $accum->addAccum( $element );
409 }
410 // Note that we do NOT increment the input pointer.
411 // This is because the closing linebreak could be the opening linebreak of
412 // another heading. Infinite loops are avoided because the next iteration MUST
413 // hit the heading open case above, which unconditionally increments the
414 // input pointer.
415 }
416
417 elseif ( $found == 'open' ) {
418 # count opening brace characters
419 $count = strspn( $text, $curChar, $i );
420
421 # we need to add to stack only if opening brace count is enough for one of the rules
422 if ( $count >= $rule['min'] ) {
423 # Add it to the stack
424 $piece = array(
425 'open' => $curChar,
426 'close' => $rule['end'],
427 'count' => $count,
428 'lineStart' => ($i > 0 && $text[$i-1] == "\n"),
429 );
430
431 $stack->push( $piece );
432 $accum =& $stack->getAccum();
433 extract( $stack->getFlags() );
434 } else {
435 # Add literal brace(s)
436 $accum->addLiteral( str_repeat( $curChar, $count ) );
437 }
438 $i += $count;
439 }
440
441 elseif ( $found == 'close' ) {
442 $piece = $stack->top;
443 # lets check if there are enough characters for closing brace
444 $maxCount = $piece->count;
445 $count = strspn( $text, $curChar, $i, $maxCount );
446
447 # check for maximum matching characters (if there are 5 closing
448 # characters, we will probably need only 3 - depending on the rules)
449 $matchingCount = 0;
450 $rule = $rules[$piece->open];
451 if ( $count > $rule['max'] ) {
452 # The specified maximum exists in the callback array, unless the caller
453 # has made an error
454 $matchingCount = $rule['max'];
455 } else {
456 # Count is less than the maximum
457 # Skip any gaps in the callback array to find the true largest match
458 # Need to use array_key_exists not isset because the callback can be null
459 $matchingCount = $count;
460 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
461 --$matchingCount;
462 }
463 }
464
465 if ($matchingCount <= 0) {
466 # No matching element found in callback array
467 # Output a literal closing brace and continue
468 $accum->addLiteral( str_repeat( $curChar, $count ) );
469 $i += $count;
470 continue;
471 }
472 $name = $rule['names'][$matchingCount];
473 if ( $name === null ) {
474 // No element, just literal text
475 $element = $piece->breakSyntax( $matchingCount );
476 $element->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
477 } else {
478 # Create XML element
479 # Note: $parts is already XML, does not need to be encoded further
480 $parts = $piece->parts;
481 $titleAccum = $parts[0]->out;
482 unset( $parts[0] );
483
484 $element = new PPNode_Hash_Tree( $name );
485
486 # The invocation is at the start of the line if lineStart is set in
487 # the stack, and all opening brackets are used up.
488 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
489 $element->addChild( new PPNode_Hash_Attr( 'lineStart', 1 ) );
490 }
491 $titleNode = new PPNode_Hash_Tree( 'title' );
492 $titleNode->firstChild = $titleAccum->firstNode;
493 $titleNode->lastChild = $titleAccum->lastNode;
494 $element->addChild( $titleNode );
495 $argIndex = 1;
496 foreach ( $parts as $partIndex => $part ) {
497 if ( isset( $part->eqpos ) ) {
498 // Find equals
499 $lastNode = false;
500 for ( $node = $part->out->firstNode; $node; $node = $node->nextSibling ) {
501 if ( $node === $part->eqpos ) {
502 break;
503 }
504 $lastNode = $node;
505 }
506 if ( !$node ) {
507 throw new MWException( __METHOD__. ': eqpos not found' );
508 }
509 if ( $node->name !== 'equals' ) {
510 throw new MWException( __METHOD__ .': eqpos is not equals' );
511 }
512 $equalsNode = $node;
513
514 // Construct name node
515 $nameNode = new PPNode_Hash_Tree( 'name' );
516 if ( $lastNode !== false ) {
517 $lastNode->nextSibling = false;
518 $nameNode->firstChild = $part->out->firstNode;
519 $nameNode->lastChild = $lastNode;
520 }
521
522 // Construct value node
523 $valueNode = new PPNode_Hash_Tree( 'value' );
524 if ( $equalsNode->nextSibling !== false ) {
525 $valueNode->firstChild = $equalsNode->nextSibling;
526 $valueNode->lastChild = $part->out->lastNode;
527 }
528 $partNode = new PPNode_Hash_Tree( 'part' );
529 $partNode->addChild( $nameNode );
530 $partNode->addChild( $equalsNode->firstChild );
531 $partNode->addChild( $valueNode );
532 $element->addChild( $partNode );
533 } else {
534 $partNode = new PPNode_Hash_Tree( 'part' );
535 $nameNode = new PPNode_Hash_Tree( 'name' );
536 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $argIndex++ ) );
537 $valueNode = new PPNode_Hash_Tree( 'value' );
538 $valueNode->firstChild = $part->out->firstNode;
539 $valueNode->lastChild = $part->out->lastNode;
540 $partNode->addChild( $nameNode );
541 $partNode->addChild( $valueNode );
542 $element->addChild( $partNode );
543 }
544 }
545 }
546
547 # Advance input pointer
548 $i += $matchingCount;
549
550 # Unwind the stack
551 $stack->pop();
552 $accum =& $stack->getAccum();
553
554 # Re-add the old stack element if it still has unmatched opening characters remaining
555 if ($matchingCount < $piece->count) {
556 $piece->parts = array( new PPDPart_Hash );
557 $piece->count -= $matchingCount;
558 # do we still qualify for any callback with remaining count?
559 $names = $rules[$piece->open]['names'];
560 $skippedBraces = 0;
561 $enclosingAccum =& $accum;
562 while ( $piece->count ) {
563 if ( array_key_exists( $piece->count, $names ) ) {
564 $stack->push( $piece );
565 $accum =& $stack->getAccum();
566 break;
567 }
568 --$piece->count;
569 $skippedBraces ++;
570 }
571 $enclosingAccum->addLiteral( str_repeat( $piece->open, $skippedBraces ) );
572 }
573
574 extract( $stack->getFlags() );
575
576 # Add XML element to the enclosing accumulator
577 if ( $element instanceof PPNode ) {
578 $accum->addNode( $element );
579 } else {
580 $accum->addAccum( $element );
581 }
582 }
583
584 elseif ( $found == 'pipe' ) {
585 $findEquals = true; // shortcut for getFlags()
586 $stack->addPart();
587 $accum =& $stack->getAccum();
588 ++$i;
589 }
590
591 elseif ( $found == 'equals' ) {
592 $findEquals = false; // shortcut for getFlags()
593 $accum->addNodeWithText( 'equals', '=' );
594 $stack->getCurrentPart()->eqpos = $accum->lastNode;
595 ++$i;
596 }
597 }
598
599 # Output any remaining unclosed brackets
600 foreach ( $stack->stack as $piece ) {
601 $stack->rootAccum->addAccum( $piece->breakSyntax() );
602 }
603
604 # Enable top-level headings
605 for ( $node = $stack->rootAccum->firstNode; $node; $node = $node->nextSibling ) {
606 if ( isset( $node->name ) && $node->name === 'possible-h' ) {
607 $node->name = 'h';
608 }
609 }
610
611 $rootNode = new PPNode_Hash_Tree( 'root' );
612 $rootNode->firstChild = $stack->rootAccum->firstNode;
613 $rootNode->lastChild = $stack->rootAccum->lastNode;
614 wfProfileOut( __METHOD__ );
615 return $rootNode;
616 }
617 }
618
619 /**
620 * Stack class to help Preprocessor::preprocessToObj()
621 */
622 class PPDStack_Hash extends PPDStack {
623 function __construct() {
624 $this->elementClass = 'PPDStackElement_Hash';
625 parent::__construct();
626 $this->rootAccum = new PPDAccum_Hash;
627 }
628 }
629
630 class PPDStackElement_Hash extends PPDStackElement {
631 function __construct( $data = array() ) {
632 $this->partClass = 'PPDPart_Hash';
633 parent::__construct( $data );
634 }
635
636 /**
637 * Get the accumulator that would result if the close is not found.
638 */
639 function breakSyntax( $openingCount = false ) {
640 if ( $this->open == "\n" ) {
641 $accum = $this->parts[0]->out;
642 } else {
643 if ( $openingCount === false ) {
644 $openingCount = $this->count;
645 }
646 $accum = new PPDAccum_Hash;
647 $accum->addLiteral( str_repeat( $this->open, $openingCount ) );
648 $first = true;
649 foreach ( $this->parts as $part ) {
650 if ( $first ) {
651 $first = false;
652 } else {
653 $accum->addLiteral( '|' );
654 }
655 $accum->addAccum( $part->out );
656 }
657 }
658 return $accum;
659 }
660 }
661
662 class PPDPart_Hash extends PPDPart {
663 function __construct( $out = '' ) {
664 $accum = new PPDAccum_Hash;
665 if ( $out !== '' ) {
666 $accum->addLiteral( $out );
667 }
668 parent::__construct( $accum );
669 }
670 }
671
672 class PPDAccum_Hash {
673 var $firstNode, $lastNode;
674
675 function __construct() {
676 $this->firstNode = $this->lastNode = false;
677 }
678
679 /**
680 * Append a string literal
681 */
682 function addLiteral( $s ) {
683 if ( $this->lastNode === false ) {
684 $this->firstNode = $this->lastNode = new PPNode_Hash_Text( $s );
685 } elseif ( $this->lastNode instanceof PPNode_Hash_Text ) {
686 $this->lastNode->value .= $s;
687 } else {
688 $this->lastNode->nextSibling = new PPNode_Hash_Text( $s );
689 $this->lastNode = $this->lastNode->nextSibling;
690 }
691 }
692
693 /**
694 * Append a PPNode
695 */
696 function addNode( PPNode $node ) {
697 if ( $this->lastNode === false ) {
698 $this->firstNode = $this->lastNode = $node;
699 } else {
700 $this->lastNode->nextSibling = $node;
701 $this->lastNode = $node;
702 }
703 }
704
705 /**
706 * Append a tree node with text contents
707 */
708 function addNodeWithText( $name, $value ) {
709 $node = PPNode_Hash_Tree::newWithText( $name, $value );
710 $this->addNode( $node );
711 }
712
713 /**
714 * Append a PPAccum_Hash
715 * Takes over ownership of the nodes in the source argument. These nodes may
716 * subsequently be modified, especially nextSibling.
717 */
718 function addAccum( $accum ) {
719 if ( $accum->lastNode === false ) {
720 // nothing to add
721 } elseif ( $this->lastNode === false ) {
722 $this->firstNode = $accum->firstNode;
723 $this->lastNode = $accum->lastNode;
724 } else {
725 $this->lastNode->nextSibling = $accum->firstNode;
726 $this->lastNode = $accum->lastNode;
727 }
728 }
729 }
730
731 /**
732 * An expansion frame, used as a context to expand the result of preprocessToObj()
733 */
734 class PPFrame_Hash implements PPFrame {
735 var $preprocessor, $parser, $title;
736 var $titleCache;
737
738 /**
739 * Hashtable listing templates which are disallowed for expansion in this frame,
740 * having been encountered previously in parent frames.
741 */
742 var $loopCheckHash;
743
744 /**
745 * Recursion depth of this frame, top = 0
746 */
747 var $depth;
748
749
750 /**
751 * Construct a new preprocessor frame.
752 * @param Preprocessor $preprocessor The parent preprocessor
753 */
754 function __construct( $preprocessor ) {
755 $this->preprocessor = $preprocessor;
756 $this->parser = $preprocessor->parser;
757 $this->title = $this->parser->mTitle;
758 $this->titleCache = array( $this->title ? $this->title->getPrefixedDBkey() : false );
759 $this->loopCheckHash = array();
760 $this->depth = 0;
761 }
762
763 /**
764 * Create a new child frame
765 * $args is optionally a multi-root PPNode or array containing the template arguments
766 */
767 function newChild( $args = false, $title = false ) {
768 $namedArgs = array();
769 $numberedArgs = array();
770 if ( $title === false ) {
771 $title = $this->title;
772 }
773 if ( $args !== false ) {
774 $xpath = false;
775 if ( $args instanceof PPNode_Hash_Array ) {
776 $args = $args->value;
777 } elseif ( !is_array( $args ) ) {
778 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
779 }
780 foreach ( $args as $arg ) {
781 $bits = $arg->splitArg();
782 if ( $bits['index'] !== '' ) {
783 // Numbered parameter
784 $numberedArgs[$bits['index']] = $bits['value'];
785 unset( $namedArgs[$bits['index']] );
786 } else {
787 // Named parameter
788 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
789 $namedArgs[$name] = $bits['value'];
790 unset( $numberedArgs[$name] );
791 }
792 }
793 }
794 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
795 }
796
797 function expand( $root, $flags = 0 ) {
798 if ( is_string( $root ) ) {
799 return $root;
800 }
801
802 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->mMaxPPNodeCount )
803 {
804 return '<span class="error">Node-count limit exceeded</span>';
805 }
806 if ( $this->depth > $this->parser->mOptions->mMaxPPExpandDepth ) {
807 return '<span class="error">Expansion depth limit exceeded</span>';
808 }
809 ++$this->depth;
810
811 $outStack = array( '', '' );
812 $iteratorStack = array( false, $root );
813 $indexStack = array( 0, 0 );
814
815 while ( count( $iteratorStack ) > 1 ) {
816 $level = count( $outStack ) - 1;
817 $iteratorNode =& $iteratorStack[ $level ];
818 $out =& $outStack[$level];
819 $index =& $indexStack[$level];
820
821 if ( is_array( $iteratorNode ) ) {
822 if ( $index >= count( $iteratorNode ) ) {
823 // All done with this iterator
824 $iteratorStack[$level] = false;
825 $contextNode = false;
826 } else {
827 $contextNode = $iteratorNode[$index];
828 $index++;
829 }
830 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
831 if ( $index >= $iteratorNode->getLength() ) {
832 // All done with this iterator
833 $iteratorStack[$level] = false;
834 $contextNode = false;
835 } else {
836 $contextNode = $iteratorNode->item( $index );
837 $index++;
838 }
839 } else {
840 // Copy to $contextNode and then delete from iterator stack,
841 // because this is not an iterator but we do have to execute it once
842 $contextNode = $iteratorStack[$level];
843 $iteratorStack[$level] = false;
844 }
845
846 $newIterator = false;
847
848 if ( $contextNode === false ) {
849 // nothing to do
850 } elseif ( is_string( $contextNode ) ) {
851 $out .= $contextNode;
852 } elseif ( is_array( $contextNode ) || $contextNode instanceof PPNode_Hash_Array ) {
853 $newIterator = $contextNode;
854 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
855 // No output
856 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
857 $out .= $contextNode->value;
858 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
859 if ( $contextNode->name == 'template' ) {
860 # Double-brace expansion
861 $bits = $contextNode->splitTemplate();
862 if ( $flags & self::NO_TEMPLATES ) {
863 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $bits['title'], $bits['parts'] );
864 } else {
865 $ret = $this->parser->braceSubstitution( $bits, $this );
866 if ( isset( $ret['object'] ) ) {
867 $newIterator = $ret['object'];
868 } else {
869 $out .= $ret['text'];
870 }
871 }
872 } elseif ( $contextNode->name == 'tplarg' ) {
873 # Triple-brace expansion
874 $bits = $contextNode->splitTemplate();
875 if ( $flags & self::NO_ARGS ) {
876 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $bits['title'], $bits['parts'] );
877 } else {
878 $ret = $this->parser->argSubstitution( $bits, $this );
879 if ( isset( $ret['object'] ) ) {
880 $newIterator = $ret['object'];
881 } else {
882 $out .= $ret['text'];
883 }
884 }
885 } elseif ( $contextNode->name == 'comment' ) {
886 # HTML-style comment
887 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
888 if ( $this->parser->ot['html']
889 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
890 || ( $flags & self::STRIP_COMMENTS ) )
891 {
892 $out .= '';
893 }
894 # Add a strip marker in PST mode so that pstPass2() can run some old-fashioned regexes on the result
895 # Not in RECOVER_COMMENTS mode (extractSections) though
896 elseif ( $this->parser->ot['wiki'] && ! ( $flags & self::RECOVER_COMMENTS ) ) {
897 $out .= $this->parser->insertStripItem( $contextNode->firstChild->value );
898 }
899 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
900 else {
901 $out .= $contextNode->firstChild->value;
902 }
903 } elseif ( $contextNode->name == 'ignore' ) {
904 # Output suppression used by <includeonly> etc.
905 # OT_WIKI will only respect <ignore> in substed templates.
906 # The other output types respect it unless NO_IGNORE is set.
907 # extractSections() sets NO_IGNORE and so never respects it.
908 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] ) || ( $flags & self::NO_IGNORE ) ) {
909 $out .= $contextNode->firstChild->value;
910 } else {
911 //$out .= '';
912 }
913 } elseif ( $contextNode->name == 'ext' ) {
914 # Extension tag
915 $bits = $contextNode->splitExt() + array( 'attr' => null, 'inner' => null, 'close' => null );
916 $out .= $this->parser->extensionSubstitution( $bits, $this );
917 } elseif ( $contextNode->name == 'h' ) {
918 # Heading
919 if ( $this->parser->ot['html'] ) {
920 # Expand immediately and insert heading index marker
921 $s = '';
922 for ( $node = $contextNode->firstChild; $node; $node = $node->nextSibling ) {
923 $s .= $this->expand( $node, $flags );
924 }
925
926 $bits = $contextNode->splitHeading();
927 $titleText = $this->title->getPrefixedDBkey();
928 $this->parser->mHeadings[] = array( $titleText, $bits['i'] );
929 $serial = count( $this->parser->mHeadings ) - 1;
930 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser::MARKER_SUFFIX;
931 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
932 $this->parser->mStripState->general->setPair( $marker, '' );
933 $out .= $s;
934 } else {
935 # Expand in virtual stack
936 $newIterator = $contextNode->getChildren();
937 }
938 } else {
939 # Generic recursive expansion
940 $newIterator = $contextNode->getChildren();
941 }
942 } else {
943 throw new MWException( __METHOD__.': Invalid parameter type' );
944 }
945
946 if ( $newIterator !== false ) {
947 $outStack[] = '';
948 $iteratorStack[] = $newIterator;
949 $indexStack[] = 0;
950 } elseif ( $iteratorStack[$level] === false ) {
951 // Return accumulated value to parent
952 // With tail recursion
953 while ( $iteratorStack[$level] === false && $level > 0 ) {
954 $outStack[$level - 1] .= $out;
955 array_pop( $outStack );
956 array_pop( $iteratorStack );
957 array_pop( $indexStack );
958 $level--;
959 }
960 }
961 }
962 --$this->depth;
963 return $outStack[0];
964 }
965
966 function implodeWithFlags( $sep, $flags /*, ... */ ) {
967 $args = array_slice( func_get_args(), 2 );
968
969 $first = true;
970 $s = '';
971 foreach ( $args as $root ) {
972 if ( $root instanceof PPNode_Hash_Array ) {
973 $root = $root->value;
974 }
975 if ( !is_array( $root ) ) {
976 $root = array( $root );
977 }
978 foreach ( $root as $node ) {
979 if ( $first ) {
980 $first = false;
981 } else {
982 $s .= $sep;
983 }
984 $s .= $this->expand( $node, $flags );
985 }
986 }
987 return $s;
988 }
989
990 /**
991 * Implode with no flags specified
992 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
993 */
994 function implode( $sep /*, ... */ ) {
995 $args = array_slice( func_get_args(), 1 );
996
997 $first = true;
998 $s = '';
999 foreach ( $args as $root ) {
1000 if ( $root instanceof PPNode_Hash_Array ) {
1001 $root = $root->value;
1002 }
1003 if ( !is_array( $root ) ) {
1004 $root = array( $root );
1005 }
1006 foreach ( $root as $node ) {
1007 if ( $first ) {
1008 $first = false;
1009 } else {
1010 $s .= $sep;
1011 }
1012 $s .= $this->expand( $node );
1013 }
1014 }
1015 return $s;
1016 }
1017
1018 /**
1019 * Makes an object that, when expand()ed, will be the same as one obtained
1020 * with implode()
1021 */
1022 function virtualImplode( $sep /*, ... */ ) {
1023 $args = array_slice( func_get_args(), 1 );
1024 $out = array();
1025 $first = true;
1026
1027 foreach ( $args as $root ) {
1028 if ( $root instanceof PPNode_Hash_Array ) {
1029 $root = $root->value;
1030 }
1031 if ( !is_array( $root ) ) {
1032 $root = array( $root );
1033 }
1034 foreach ( $root as $node ) {
1035 if ( $first ) {
1036 $first = false;
1037 } else {
1038 $out[] = $sep;
1039 }
1040 $out[] = $node;
1041 }
1042 }
1043 return new PPNode_Hash_Array( $out );
1044 }
1045
1046 /**
1047 * Virtual implode with brackets
1048 */
1049 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1050 $args = array_slice( func_get_args(), 3 );
1051 $out = array( $start );
1052 $first = true;
1053
1054 foreach ( $args as $root ) {
1055 if ( $root instanceof PPNode_Hash_Array ) {
1056 $root = $root->value;
1057 }
1058 if ( !is_array( $root ) ) {
1059 $root = array( $root );
1060 }
1061 foreach ( $root as $node ) {
1062 if ( $first ) {
1063 $first = false;
1064 } else {
1065 $out[] = $sep;
1066 }
1067 $out[] = $node;
1068 }
1069 }
1070 $out[] = $end;
1071 return new PPNode_Hash_Array( $out );
1072 }
1073
1074 function __toString() {
1075 return 'frame{}';
1076 }
1077
1078 function getPDBK( $level = false ) {
1079 if ( $level === false ) {
1080 return $this->title->getPrefixedDBkey();
1081 } else {
1082 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
1083 }
1084 }
1085
1086 /**
1087 * Returns true if there are no arguments in this frame
1088 */
1089 function isEmpty() {
1090 return true;
1091 }
1092
1093 function getArgument( $name ) {
1094 return false;
1095 }
1096
1097 /**
1098 * Returns true if the infinite loop check is OK, false if a loop is detected
1099 */
1100 function loopCheck( $title ) {
1101 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
1102 }
1103
1104 /**
1105 * Return true if the frame is a template frame
1106 */
1107 function isTemplate() {
1108 return false;
1109 }
1110 }
1111
1112 /**
1113 * Expansion frame with template arguments
1114 */
1115 class PPTemplateFrame_Hash extends PPFrame_Hash {
1116 var $numberedArgs, $namedArgs, $parent;
1117 var $numberedExpansionCache, $namedExpansionCache;
1118
1119 function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
1120 $this->preprocessor = $preprocessor;
1121 $this->parser = $preprocessor->parser;
1122 $this->parent = $parent;
1123 $this->numberedArgs = $numberedArgs;
1124 $this->namedArgs = $namedArgs;
1125 $this->title = $title;
1126 $pdbk = $title ? $title->getPrefixedDBkey() : false;
1127 $this->titleCache = $parent->titleCache;
1128 $this->titleCache[] = $pdbk;
1129 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
1130 if ( $pdbk !== false ) {
1131 $this->loopCheckHash[$pdbk] = true;
1132 }
1133 $this->depth = $parent->depth + 1;
1134 $this->numberedExpansionCache = $this->namedExpansionCache = array();
1135 }
1136
1137 function __toString() {
1138 $s = 'tplframe{';
1139 $first = true;
1140 $args = $this->numberedArgs + $this->namedArgs;
1141 foreach ( $args as $name => $value ) {
1142 if ( $first ) {
1143 $first = false;
1144 } else {
1145 $s .= ', ';
1146 }
1147 $s .= "\"$name\":\"" .
1148 str_replace( '"', '\\"', $value->__toString() ) . '"';
1149 }
1150 $s .= '}';
1151 return $s;
1152 }
1153 /**
1154 * Returns true if there are no arguments in this frame
1155 */
1156 function isEmpty() {
1157 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
1158 }
1159
1160 function getNumberedArgument( $index ) {
1161 if ( !isset( $this->numberedArgs[$index] ) ) {
1162 return false;
1163 }
1164 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
1165 # No trimming for unnamed arguments
1166 $this->numberedExpansionCache[$index] = $this->parent->expand( $this->numberedArgs[$index], self::STRIP_COMMENTS );
1167 }
1168 return $this->numberedExpansionCache[$index];
1169 }
1170
1171 function getNamedArgument( $name ) {
1172 if ( !isset( $this->namedArgs[$name] ) ) {
1173 return false;
1174 }
1175 if ( !isset( $this->namedExpansionCache[$name] ) ) {
1176 # Trim named arguments post-expand, for backwards compatibility
1177 $this->namedExpansionCache[$name] = trim(
1178 $this->parent->expand( $this->namedArgs[$name], self::STRIP_COMMENTS ) );
1179 }
1180 return $this->namedExpansionCache[$name];
1181 }
1182
1183 function getArgument( $name ) {
1184 $text = $this->getNumberedArgument( $name );
1185 if ( $text === false ) {
1186 $text = $this->getNamedArgument( $name );
1187 }
1188 return $text;
1189 }
1190
1191 /**
1192 * Return true if the frame is a template frame
1193 */
1194 function isTemplate() {
1195 return true;
1196 }
1197 }
1198
1199 class PPNode_Hash_Tree implements PPNode {
1200 var $name, $firstChild, $lastChild, $nextSibling;
1201
1202 function __construct( $name ) {
1203 $this->name = $name;
1204 $this->firstChild = $this->lastChild = $this->nextSibling = false;
1205 }
1206
1207 function __toString() {
1208 $inner = '';
1209 $attribs = '';
1210 for ( $node = $this->firstChild; $node; $node = $node->nextSibling ) {
1211 if ( $node instanceof PPNode_Hash_Attr ) {
1212 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
1213 } else {
1214 $inner .= $node->__toString();
1215 }
1216 }
1217 if ( $inner === '' ) {
1218 return "<{$this->name}$attribs/>";
1219 } else {
1220 return "<{$this->name}$attribs>$inner</{$this->name}>";
1221 }
1222 }
1223
1224 static function newWithText( $name, $text ) {
1225 $obj = new self( $name );
1226 $obj->addChild( new PPNode_Hash_Text( $text ) );
1227 return $obj;
1228 }
1229
1230 function addChild( $node ) {
1231 if ( $this->lastChild === false ) {
1232 $this->firstChild = $this->lastChild = $node;
1233 } else {
1234 $this->lastChild->nextSibling = $node;
1235 $this->lastChild = $node;
1236 }
1237 }
1238
1239 function getChildren() {
1240 $children = array();
1241 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1242 $children[] = $child;
1243 }
1244 return new PPNode_Hash_Array( $children );
1245 }
1246
1247 function getFirstChild() {
1248 return $this->firstChild;
1249 }
1250
1251 function getNextSibling() {
1252 return $this->nextSibling;
1253 }
1254
1255 function getChildrenOfType( $name ) {
1256 $children = array();
1257 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1258 if ( isset( $child->name ) && $child->name === $name ) {
1259 $children[] = $name;
1260 }
1261 }
1262 return $children;
1263 }
1264
1265 function getLength() { return false; }
1266 function item( $i ) { return false; }
1267
1268 function getName() {
1269 return $this->name;
1270 }
1271
1272 /**
1273 * Split a <part> node into an associative array containing:
1274 * name PPNode name
1275 * index String index
1276 * value PPNode value
1277 */
1278 function splitArg() {
1279 $bits = array();
1280 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1281 if ( !isset( $child->name ) ) {
1282 continue;
1283 }
1284 if ( $child->name === 'name' ) {
1285 $bits['name'] = $child;
1286 if ( $child->firstChild instanceof PPNode_Hash_Attr
1287 && $child->firstChild->name === 'index' )
1288 {
1289 $bits['index'] = $child->firstChild->value;
1290 }
1291 } elseif ( $child->name === 'value' ) {
1292 $bits['value'] = $child;
1293 }
1294 }
1295
1296 if ( !isset( $bits['name'] ) ) {
1297 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
1298 }
1299 if ( !isset( $bits['index'] ) ) {
1300 $bits['index'] = '';
1301 }
1302 return $bits;
1303 }
1304
1305 /**
1306 * Split an <ext> node into an associative array containing name, attr, inner and close
1307 * All values in the resulting array are PPNodes. Inner and close are optional.
1308 */
1309 function splitExt() {
1310 $bits = array();
1311 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1312 if ( !isset( $child->name ) ) {
1313 continue;
1314 }
1315 if ( $child->name == 'name' ) {
1316 $bits['name'] = $child;
1317 } elseif ( $child->name == 'attr' ) {
1318 $bits['attr'] = $child;
1319 } elseif ( $child->name == 'inner' ) {
1320 $bits['inner'] = $child;
1321 } elseif ( $child->name == 'close' ) {
1322 $bits['close'] = $child;
1323 }
1324 }
1325 if ( !isset( $bits['name'] ) ) {
1326 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
1327 }
1328 return $bits;
1329 }
1330
1331 /**
1332 * Split an <h> node
1333 */
1334 function splitHeading() {
1335 if ( $this->name !== 'h' ) {
1336 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1337 }
1338 $bits = array();
1339 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1340 if ( !isset( $child->name ) ) {
1341 continue;
1342 }
1343 if ( $child->name == 'i' ) {
1344 $bits['i'] = $child->value;
1345 } elseif ( $child->name == 'level' ) {
1346 $bits['level'] = $child->value;
1347 }
1348 }
1349 if ( !isset( $bits['i'] ) ) {
1350 throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
1351 }
1352 return $bits;
1353 }
1354
1355 /**
1356 * Split a <template> or <tplarg> node
1357 */
1358 function splitTemplate() {
1359 $parts = array();
1360 $bits = array( 'lineStart' => '' );
1361 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
1362 if ( !isset( $child->name ) ) {
1363 continue;
1364 }
1365 if ( $child->name == 'title' ) {
1366 $bits['title'] = $child;
1367 }
1368 if ( $child->name == 'part' ) {
1369 $parts[] = $child;
1370 }
1371 if ( $child->name == 'lineStart' ) {
1372 $bits['lineStart'] = '1';
1373 }
1374 }
1375 if ( !isset( $bits['title'] ) ) {
1376 throw new MWException( 'Invalid node passed to ' . __METHOD__ );
1377 }
1378 $bits['parts'] = new PPNode_Hash_Array( $parts );
1379 return $bits;
1380 }
1381 }
1382
1383 class PPNode_Hash_Text implements PPNode {
1384 var $value, $nextSibling;
1385
1386 function __construct( $value ) {
1387 if ( is_object( $value ) ) {
1388 throw new MWException( __CLASS__ . ' given object instead of string' );
1389 }
1390 $this->value = $value;
1391 }
1392
1393 function __toString() {
1394 return htmlspecialchars( $this->value );
1395 }
1396
1397 function getNextSibling() {
1398 return $this->nextSibling;
1399 }
1400
1401 function getChildren() { return false; }
1402 function getFirstChild() { return false; }
1403 function getChildrenOfType( $name ) { return false; }
1404 function getLength() { return false; }
1405 function item( $i ) { return false; }
1406 function getName() { return '#text'; }
1407 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1408 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1409 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1410 }
1411
1412 class PPNode_Hash_Array implements PPNode {
1413 var $value, $nextSibling;
1414
1415 function __construct( $value ) {
1416 $this->value = $value;
1417 }
1418
1419 function __toString() {
1420 return var_export( $this, true );
1421 }
1422
1423 function getLength() {
1424 return count( $this->value );
1425 }
1426
1427 function item( $i ) {
1428 return $this->value[$i];
1429 }
1430
1431 function getName() { return '#nodelist'; }
1432
1433 function getNextSibling() {
1434 return $this->nextSibling;
1435 }
1436
1437 function getChildren() { return false; }
1438 function getFirstChild() { return false; }
1439 function getChildrenOfType( $name ) { return false; }
1440 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1441 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1442 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1443 }
1444
1445 class PPNode_Hash_Attr implements PPNode {
1446 var $name, $value, $nextSibling;
1447
1448 function __construct( $name, $value ) {
1449 $this->name = $name;
1450 $this->value = $value;
1451 }
1452
1453 function __toString() {
1454 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
1455 }
1456
1457 function getName() {
1458 return $this->name;
1459 }
1460
1461 function getNextSibling() {
1462 return $this->nextSibling;
1463 }
1464
1465 function getChildren() { return false; }
1466 function getFirstChild() { return false; }
1467 function getChildrenOfType( $name ) { return false; }
1468 function getLength() { return false; }
1469 function item( $i ) { return false; }
1470 function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
1471 function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
1472 function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
1473 }