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