MediaWiki
REL1_20
|
00001 <?php 00027 class Preprocessor_DOM implements Preprocessor { 00028 00032 var $parser; 00033 00034 var $memoryLimit; 00035 00036 const CACHE_VERSION = 1; 00037 00038 function __construct( $parser ) { 00039 $this->parser = $parser; 00040 $mem = ini_get( 'memory_limit' ); 00041 $this->memoryLimit = false; 00042 if ( strval( $mem ) !== '' && $mem != -1 ) { 00043 if ( preg_match( '/^\d+$/', $mem ) ) { 00044 $this->memoryLimit = $mem; 00045 } elseif ( preg_match( '/^(\d+)M$/i', $mem, $m ) ) { 00046 $this->memoryLimit = $m[1] * 1048576; 00047 } 00048 } 00049 } 00050 00054 function newFrame() { 00055 return new PPFrame_DOM( $this ); 00056 } 00057 00062 function newCustomFrame( $args ) { 00063 return new PPCustomFrame_DOM( $this, $args ); 00064 } 00065 00070 function newPartNodeArray( $values ) { 00071 //NOTE: DOM manipulation is slower than building & parsing XML! (or so Tim sais) 00072 $xml = "<list>"; 00073 00074 foreach ( $values as $k => $val ) { 00075 00076 if ( is_int( $k ) ) { 00077 $xml .= "<part><name index=\"$k\"/><value>" . htmlspecialchars( $val ) ."</value></part>"; 00078 } else { 00079 $xml .= "<part><name>" . htmlspecialchars( $k ) . "</name>=<value>" . htmlspecialchars( $val ) . "</value></part>"; 00080 } 00081 } 00082 00083 $xml .= "</list>"; 00084 00085 $dom = new DOMDocument(); 00086 $dom->loadXML( $xml ); 00087 $root = $dom->documentElement; 00088 00089 $node = new PPNode_DOM( $root->childNodes ); 00090 return $node; 00091 } 00092 00097 function memCheck() { 00098 if ( $this->memoryLimit === false ) { 00099 return true; 00100 } 00101 $usage = memory_get_usage(); 00102 if ( $usage > $this->memoryLimit * 0.9 ) { 00103 $limit = intval( $this->memoryLimit * 0.9 / 1048576 + 0.5 ); 00104 throw new MWException( "Preprocessor hit 90% memory limit ($limit MB)" ); 00105 } 00106 return $usage <= $this->memoryLimit * 0.8; 00107 } 00108 00131 function preprocessToObj( $text, $flags = 0 ) { 00132 wfProfileIn( __METHOD__ ); 00133 global $wgMemc, $wgPreprocessorCacheThreshold; 00134 00135 $xml = false; 00136 $cacheable = ( $wgPreprocessorCacheThreshold !== false 00137 && strlen( $text ) > $wgPreprocessorCacheThreshold ); 00138 if ( $cacheable ) { 00139 wfProfileIn( __METHOD__.'-cacheable' ); 00140 00141 $cacheKey = wfMemcKey( 'preprocess-xml', md5($text), $flags ); 00142 $cacheValue = $wgMemc->get( $cacheKey ); 00143 if ( $cacheValue ) { 00144 $version = substr( $cacheValue, 0, 8 ); 00145 if ( intval( $version ) == self::CACHE_VERSION ) { 00146 $xml = substr( $cacheValue, 8 ); 00147 // From the cache 00148 wfDebugLog( "Preprocessor", "Loaded preprocessor XML from memcached (key $cacheKey)" ); 00149 } 00150 } 00151 } 00152 if ( $xml === false ) { 00153 if ( $cacheable ) { 00154 wfProfileIn( __METHOD__.'-cache-miss' ); 00155 $xml = $this->preprocessToXml( $text, $flags ); 00156 $cacheValue = sprintf( "%08d", self::CACHE_VERSION ) . $xml; 00157 $wgMemc->set( $cacheKey, $cacheValue, 86400 ); 00158 wfProfileOut( __METHOD__.'-cache-miss' ); 00159 wfDebugLog( "Preprocessor", "Saved preprocessor XML to memcached (key $cacheKey)" ); 00160 } else { 00161 $xml = $this->preprocessToXml( $text, $flags ); 00162 } 00163 00164 } 00165 00166 // Fail if the number of elements exceeds acceptable limits 00167 // Do not attempt to generate the DOM 00168 $this->parser->mGeneratedPPNodeCount += substr_count( $xml, '<' ); 00169 $max = $this->parser->mOptions->getMaxGeneratedPPNodeCount(); 00170 if ( $this->parser->mGeneratedPPNodeCount > $max ) { 00171 throw new MWException( __METHOD__.': generated node count limit exceeded' ); 00172 } 00173 00174 wfProfileIn( __METHOD__.'-loadXML' ); 00175 $dom = new DOMDocument; 00176 wfSuppressWarnings(); 00177 $result = $dom->loadXML( $xml ); 00178 wfRestoreWarnings(); 00179 if ( !$result ) { 00180 // Try running the XML through UtfNormal to get rid of invalid characters 00181 $xml = UtfNormal::cleanUp( $xml ); 00182 // 1 << 19 == XML_PARSE_HUGE, needed so newer versions of libxml2 don't barf when the XML is >256 levels deep 00183 $result = $dom->loadXML( $xml, 1 << 19 ); 00184 if ( !$result ) { 00185 throw new MWException( __METHOD__.' generated invalid XML' ); 00186 } 00187 } 00188 $obj = new PPNode_DOM( $dom->documentElement ); 00189 wfProfileOut( __METHOD__.'-loadXML' ); 00190 if ( $cacheable ) { 00191 wfProfileOut( __METHOD__.'-cacheable' ); 00192 } 00193 wfProfileOut( __METHOD__ ); 00194 return $obj; 00195 } 00196 00202 function preprocessToXml( $text, $flags = 0 ) { 00203 wfProfileIn( __METHOD__ ); 00204 $rules = array( 00205 '{' => array( 00206 'end' => '}', 00207 'names' => array( 00208 2 => 'template', 00209 3 => 'tplarg', 00210 ), 00211 'min' => 2, 00212 'max' => 3, 00213 ), 00214 '[' => array( 00215 'end' => ']', 00216 'names' => array( 2 => null ), 00217 'min' => 2, 00218 'max' => 2, 00219 ) 00220 ); 00221 00222 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION; 00223 00224 $xmlishElements = $this->parser->getStripList(); 00225 $enableOnlyinclude = false; 00226 if ( $forInclusion ) { 00227 $ignoredTags = array( 'includeonly', '/includeonly' ); 00228 $ignoredElements = array( 'noinclude' ); 00229 $xmlishElements[] = 'noinclude'; 00230 if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) { 00231 $enableOnlyinclude = true; 00232 } 00233 } else { 00234 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ); 00235 $ignoredElements = array( 'includeonly' ); 00236 $xmlishElements[] = 'includeonly'; 00237 } 00238 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) ); 00239 00240 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset 00241 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA"; 00242 00243 $stack = new PPDStack; 00244 00245 $searchBase = "[{<\n"; #} 00246 $revText = strrev( $text ); // For fast reverse searches 00247 $lengthText = strlen( $text ); 00248 00249 $i = 0; # Input pointer, starts out pointing to a pseudo-newline before the start 00250 $accum =& $stack->getAccum(); # Current accumulator 00251 $accum = '<root>'; 00252 $findEquals = false; # True to find equals signs in arguments 00253 $findPipe = false; # True to take notice of pipe characters 00254 $headingIndex = 1; 00255 $inHeading = false; # True if $i is inside a possible heading 00256 $noMoreGT = false; # True if there are no more greater-than (>) signs right of $i 00257 $findOnlyinclude = $enableOnlyinclude; # True to ignore all input up to the next <onlyinclude> 00258 $fakeLineStart = true; # Do a line-start run without outputting an LF character 00259 00260 while ( true ) { 00261 //$this->memCheck(); 00262 00263 if ( $findOnlyinclude ) { 00264 // Ignore all input up to the next <onlyinclude> 00265 $startPos = strpos( $text, '<onlyinclude>', $i ); 00266 if ( $startPos === false ) { 00267 // Ignored section runs to the end 00268 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i ) ) . '</ignore>'; 00269 break; 00270 } 00271 $tagEndPos = $startPos + strlen( '<onlyinclude>' ); // past-the-end 00272 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i ) ) . '</ignore>'; 00273 $i = $tagEndPos; 00274 $findOnlyinclude = false; 00275 } 00276 00277 if ( $fakeLineStart ) { 00278 $found = 'line-start'; 00279 $curChar = ''; 00280 } else { 00281 # Find next opening brace, closing brace or pipe 00282 $search = $searchBase; 00283 if ( $stack->top === false ) { 00284 $currentClosing = ''; 00285 } else { 00286 $currentClosing = $stack->top->close; 00287 $search .= $currentClosing; 00288 } 00289 if ( $findPipe ) { 00290 $search .= '|'; 00291 } 00292 if ( $findEquals ) { 00293 // First equals will be for the template 00294 $search .= '='; 00295 } 00296 $rule = null; 00297 # Output literal section, advance input counter 00298 $literalLength = strcspn( $text, $search, $i ); 00299 if ( $literalLength > 0 ) { 00300 $accum .= htmlspecialchars( substr( $text, $i, $literalLength ) ); 00301 $i += $literalLength; 00302 } 00303 if ( $i >= $lengthText ) { 00304 if ( $currentClosing == "\n" ) { 00305 // Do a past-the-end run to finish off the heading 00306 $curChar = ''; 00307 $found = 'line-end'; 00308 } else { 00309 # All done 00310 break; 00311 } 00312 } else { 00313 $curChar = $text[$i]; 00314 if ( $curChar == '|' ) { 00315 $found = 'pipe'; 00316 } elseif ( $curChar == '=' ) { 00317 $found = 'equals'; 00318 } elseif ( $curChar == '<' ) { 00319 $found = 'angle'; 00320 } elseif ( $curChar == "\n" ) { 00321 if ( $inHeading ) { 00322 $found = 'line-end'; 00323 } else { 00324 $found = 'line-start'; 00325 } 00326 } elseif ( $curChar == $currentClosing ) { 00327 $found = 'close'; 00328 } elseif ( isset( $rules[$curChar] ) ) { 00329 $found = 'open'; 00330 $rule = $rules[$curChar]; 00331 } else { 00332 # Some versions of PHP have a strcspn which stops on null characters 00333 # Ignore and continue 00334 ++$i; 00335 continue; 00336 } 00337 } 00338 } 00339 00340 if ( $found == 'angle' ) { 00341 $matches = false; 00342 // Handle </onlyinclude> 00343 if ( $enableOnlyinclude && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>' ) { 00344 $findOnlyinclude = true; 00345 continue; 00346 } 00347 00348 // Determine element name 00349 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) { 00350 // Element name missing or not listed 00351 $accum .= '<'; 00352 ++$i; 00353 continue; 00354 } 00355 // Handle comments 00356 if ( isset( $matches[2] ) && $matches[2] == '!--' ) { 00357 // To avoid leaving blank lines, when a comment is both preceded 00358 // and followed by a newline (ignoring spaces), trim leading and 00359 // trailing spaces and one of the newlines. 00360 00361 // Find the end 00362 $endPos = strpos( $text, '-->', $i + 4 ); 00363 if ( $endPos === false ) { 00364 // Unclosed comment in input, runs to end 00365 $inner = substr( $text, $i ); 00366 $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>'; 00367 $i = $lengthText; 00368 } else { 00369 // Search backwards for leading whitespace 00370 $wsStart = $i ? ( $i - strspn( $revText, ' ', $lengthText - $i ) ) : 0; 00371 // Search forwards for trailing whitespace 00372 // $wsEnd will be the position of the last space (or the '>' if there's none) 00373 $wsEnd = $endPos + 2 + strspn( $text, ' ', $endPos + 3 ); 00374 // Eat the line if possible 00375 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at 00376 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but 00377 // it's a possible beneficial b/c break. 00378 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n" 00379 && substr( $text, $wsEnd + 1, 1 ) == "\n" ) 00380 { 00381 $startPos = $wsStart; 00382 $endPos = $wsEnd + 1; 00383 // Remove leading whitespace from the end of the accumulator 00384 // Sanity check first though 00385 $wsLength = $i - $wsStart; 00386 if ( $wsLength > 0 && substr( $accum, -$wsLength ) === str_repeat( ' ', $wsLength ) ) { 00387 $accum = substr( $accum, 0, -$wsLength ); 00388 } 00389 // Do a line-start run next time to look for headings after the comment 00390 $fakeLineStart = true; 00391 } else { 00392 // No line to eat, just take the comment itself 00393 $startPos = $i; 00394 $endPos += 2; 00395 } 00396 00397 if ( $stack->top ) { 00398 $part = $stack->top->getCurrentPart(); 00399 if ( ! (isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 )) { 00400 $part->visualEnd = $wsStart; 00401 } 00402 // Else comments abutting, no change in visual end 00403 $part->commentEnd = $endPos; 00404 } 00405 $i = $endPos + 1; 00406 $inner = substr( $text, $startPos, $endPos - $startPos + 1 ); 00407 $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>'; 00408 } 00409 continue; 00410 } 00411 $name = $matches[1]; 00412 $lowerName = strtolower( $name ); 00413 $attrStart = $i + strlen( $name ) + 1; 00414 00415 // Find end of tag 00416 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart ); 00417 if ( $tagEndPos === false ) { 00418 // Infinite backtrack 00419 // Disable tag search to prevent worst-case O(N^2) performance 00420 $noMoreGT = true; 00421 $accum .= '<'; 00422 ++$i; 00423 continue; 00424 } 00425 00426 // Handle ignored tags 00427 if ( in_array( $lowerName, $ignoredTags ) ) { 00428 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i + 1 ) ) . '</ignore>'; 00429 $i = $tagEndPos + 1; 00430 continue; 00431 } 00432 00433 $tagStartPos = $i; 00434 if ( $text[$tagEndPos-1] == '/' ) { 00435 $attrEnd = $tagEndPos - 1; 00436 $inner = null; 00437 $i = $tagEndPos + 1; 00438 $close = ''; 00439 } else { 00440 $attrEnd = $tagEndPos; 00441 // Find closing tag 00442 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i", 00443 $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) ) 00444 { 00445 $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 ); 00446 $i = $matches[0][1] + strlen( $matches[0][0] ); 00447 $close = '<close>' . htmlspecialchars( $matches[0][0] ) . '</close>'; 00448 } else { 00449 // No end tag -- let it run out to the end of the text. 00450 $inner = substr( $text, $tagEndPos + 1 ); 00451 $i = $lengthText; 00452 $close = ''; 00453 } 00454 } 00455 // <includeonly> and <noinclude> just become <ignore> tags 00456 if ( in_array( $lowerName, $ignoredElements ) ) { 00457 $accum .= '<ignore>' . htmlspecialchars( substr( $text, $tagStartPos, $i - $tagStartPos ) ) 00458 . '</ignore>'; 00459 continue; 00460 } 00461 00462 $accum .= '<ext>'; 00463 if ( $attrEnd <= $attrStart ) { 00464 $attr = ''; 00465 } else { 00466 $attr = substr( $text, $attrStart, $attrEnd - $attrStart ); 00467 } 00468 $accum .= '<name>' . htmlspecialchars( $name ) . '</name>' . 00469 // Note that the attr element contains the whitespace between name and attribute, 00470 // this is necessary for precise reconstruction during pre-save transform. 00471 '<attr>' . htmlspecialchars( $attr ) . '</attr>'; 00472 if ( $inner !== null ) { 00473 $accum .= '<inner>' . htmlspecialchars( $inner ) . '</inner>'; 00474 } 00475 $accum .= $close . '</ext>'; 00476 } elseif ( $found == 'line-start' ) { 00477 // Is this the start of a heading? 00478 // Line break belongs before the heading element in any case 00479 if ( $fakeLineStart ) { 00480 $fakeLineStart = false; 00481 } else { 00482 $accum .= $curChar; 00483 $i++; 00484 } 00485 00486 $count = strspn( $text, '=', $i, 6 ); 00487 if ( $count == 1 && $findEquals ) { 00488 // DWIM: This looks kind of like a name/value separator 00489 // Let's let the equals handler have it and break the potential heading 00490 // This is heuristic, but AFAICT the methods for completely correct disambiguation are very complex. 00491 } elseif ( $count > 0 ) { 00492 $piece = array( 00493 'open' => "\n", 00494 'close' => "\n", 00495 'parts' => array( new PPDPart( str_repeat( '=', $count ) ) ), 00496 'startPos' => $i, 00497 'count' => $count ); 00498 $stack->push( $piece ); 00499 $accum =& $stack->getAccum(); 00500 $flags = $stack->getFlags(); 00501 extract( $flags ); 00502 $i += $count; 00503 } 00504 } elseif ( $found == 'line-end' ) { 00505 $piece = $stack->top; 00506 // A heading must be open, otherwise \n wouldn't have been in the search list 00507 assert( '$piece->open == "\n"' ); 00508 $part = $piece->getCurrentPart(); 00509 // Search back through the input to see if it has a proper close 00510 // Do this using the reversed string since the other solutions (end anchor, etc.) are inefficient 00511 $wsLength = strspn( $revText, " \t", $lengthText - $i ); 00512 $searchStart = $i - $wsLength; 00513 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) { 00514 // Comment found at line end 00515 // Search for equals signs before the comment 00516 $searchStart = $part->visualEnd; 00517 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart ); 00518 } 00519 $count = $piece->count; 00520 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart ); 00521 if ( $equalsLength > 0 ) { 00522 if ( $searchStart - $equalsLength == $piece->startPos ) { 00523 // This is just a single string of equals signs on its own line 00524 // Replicate the doHeadings behaviour /={count}(.+)={count}/ 00525 // First find out how many equals signs there really are (don't stop at 6) 00526 $count = $equalsLength; 00527 if ( $count < 3 ) { 00528 $count = 0; 00529 } else { 00530 $count = min( 6, intval( ( $count - 1 ) / 2 ) ); 00531 } 00532 } else { 00533 $count = min( $equalsLength, $count ); 00534 } 00535 if ( $count > 0 ) { 00536 // Normal match, output <h> 00537 $element = "<h level=\"$count\" i=\"$headingIndex\">$accum</h>"; 00538 $headingIndex++; 00539 } else { 00540 // Single equals sign on its own line, count=0 00541 $element = $accum; 00542 } 00543 } else { 00544 // No match, no <h>, just pass down the inner text 00545 $element = $accum; 00546 } 00547 // Unwind the stack 00548 $stack->pop(); 00549 $accum =& $stack->getAccum(); 00550 $flags = $stack->getFlags(); 00551 extract( $flags ); 00552 00553 // Append the result to the enclosing accumulator 00554 $accum .= $element; 00555 // Note that we do NOT increment the input pointer. 00556 // This is because the closing linebreak could be the opening linebreak of 00557 // another heading. Infinite loops are avoided because the next iteration MUST 00558 // hit the heading open case above, which unconditionally increments the 00559 // input pointer. 00560 } elseif ( $found == 'open' ) { 00561 # count opening brace characters 00562 $count = strspn( $text, $curChar, $i ); 00563 00564 # we need to add to stack only if opening brace count is enough for one of the rules 00565 if ( $count >= $rule['min'] ) { 00566 # Add it to the stack 00567 $piece = array( 00568 'open' => $curChar, 00569 'close' => $rule['end'], 00570 'count' => $count, 00571 'lineStart' => ($i > 0 && $text[$i-1] == "\n"), 00572 ); 00573 00574 $stack->push( $piece ); 00575 $accum =& $stack->getAccum(); 00576 $flags = $stack->getFlags(); 00577 extract( $flags ); 00578 } else { 00579 # Add literal brace(s) 00580 $accum .= htmlspecialchars( str_repeat( $curChar, $count ) ); 00581 } 00582 $i += $count; 00583 } elseif ( $found == 'close' ) { 00584 $piece = $stack->top; 00585 # lets check if there are enough characters for closing brace 00586 $maxCount = $piece->count; 00587 $count = strspn( $text, $curChar, $i, $maxCount ); 00588 00589 # check for maximum matching characters (if there are 5 closing 00590 # characters, we will probably need only 3 - depending on the rules) 00591 $rule = $rules[$piece->open]; 00592 if ( $count > $rule['max'] ) { 00593 # The specified maximum exists in the callback array, unless the caller 00594 # has made an error 00595 $matchingCount = $rule['max']; 00596 } else { 00597 # Count is less than the maximum 00598 # Skip any gaps in the callback array to find the true largest match 00599 # Need to use array_key_exists not isset because the callback can be null 00600 $matchingCount = $count; 00601 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) { 00602 --$matchingCount; 00603 } 00604 } 00605 00606 if ( $matchingCount <= 0 ) { 00607 # No matching element found in callback array 00608 # Output a literal closing brace and continue 00609 $accum .= htmlspecialchars( str_repeat( $curChar, $count ) ); 00610 $i += $count; 00611 continue; 00612 } 00613 $name = $rule['names'][$matchingCount]; 00614 if ( $name === null ) { 00615 // No element, just literal text 00616 $element = $piece->breakSyntax( $matchingCount ) . str_repeat( $rule['end'], $matchingCount ); 00617 } else { 00618 # Create XML element 00619 # Note: $parts is already XML, does not need to be encoded further 00620 $parts = $piece->parts; 00621 $title = $parts[0]->out; 00622 unset( $parts[0] ); 00623 00624 # The invocation is at the start of the line if lineStart is set in 00625 # the stack, and all opening brackets are used up. 00626 if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) { 00627 $attr = ' lineStart="1"'; 00628 } else { 00629 $attr = ''; 00630 } 00631 00632 $element = "<$name$attr>"; 00633 $element .= "<title>$title</title>"; 00634 $argIndex = 1; 00635 foreach ( $parts as $part ) { 00636 if ( isset( $part->eqpos ) ) { 00637 $argName = substr( $part->out, 0, $part->eqpos ); 00638 $argValue = substr( $part->out, $part->eqpos + 1 ); 00639 $element .= "<part><name>$argName</name>=<value>$argValue</value></part>"; 00640 } else { 00641 $element .= "<part><name index=\"$argIndex\" /><value>{$part->out}</value></part>"; 00642 $argIndex++; 00643 } 00644 } 00645 $element .= "</$name>"; 00646 } 00647 00648 # Advance input pointer 00649 $i += $matchingCount; 00650 00651 # Unwind the stack 00652 $stack->pop(); 00653 $accum =& $stack->getAccum(); 00654 00655 # Re-add the old stack element if it still has unmatched opening characters remaining 00656 if ( $matchingCount < $piece->count ) { 00657 $piece->parts = array( new PPDPart ); 00658 $piece->count -= $matchingCount; 00659 # do we still qualify for any callback with remaining count? 00660 $names = $rules[$piece->open]['names']; 00661 $skippedBraces = 0; 00662 $enclosingAccum =& $accum; 00663 while ( $piece->count ) { 00664 if ( array_key_exists( $piece->count, $names ) ) { 00665 $stack->push( $piece ); 00666 $accum =& $stack->getAccum(); 00667 break; 00668 } 00669 --$piece->count; 00670 $skippedBraces ++; 00671 } 00672 $enclosingAccum .= str_repeat( $piece->open, $skippedBraces ); 00673 } 00674 $flags = $stack->getFlags(); 00675 extract( $flags ); 00676 00677 # Add XML element to the enclosing accumulator 00678 $accum .= $element; 00679 } elseif ( $found == 'pipe' ) { 00680 $findEquals = true; // shortcut for getFlags() 00681 $stack->addPart(); 00682 $accum =& $stack->getAccum(); 00683 ++$i; 00684 } elseif ( $found == 'equals' ) { 00685 $findEquals = false; // shortcut for getFlags() 00686 $stack->getCurrentPart()->eqpos = strlen( $accum ); 00687 $accum .= '='; 00688 ++$i; 00689 } 00690 } 00691 00692 # Output any remaining unclosed brackets 00693 foreach ( $stack->stack as $piece ) { 00694 $stack->rootAccum .= $piece->breakSyntax(); 00695 } 00696 $stack->rootAccum .= '</root>'; 00697 $xml = $stack->rootAccum; 00698 00699 wfProfileOut( __METHOD__ ); 00700 00701 return $xml; 00702 } 00703 } 00704 00709 class PPDStack { 00710 var $stack, $rootAccum; 00711 00715 var $top; 00716 var $out; 00717 var $elementClass = 'PPDStackElement'; 00718 00719 static $false = false; 00720 00721 function __construct() { 00722 $this->stack = array(); 00723 $this->top = false; 00724 $this->rootAccum = ''; 00725 $this->accum =& $this->rootAccum; 00726 } 00727 00731 function count() { 00732 return count( $this->stack ); 00733 } 00734 00735 function &getAccum() { 00736 return $this->accum; 00737 } 00738 00739 function getCurrentPart() { 00740 if ( $this->top === false ) { 00741 return false; 00742 } else { 00743 return $this->top->getCurrentPart(); 00744 } 00745 } 00746 00747 function push( $data ) { 00748 if ( $data instanceof $this->elementClass ) { 00749 $this->stack[] = $data; 00750 } else { 00751 $class = $this->elementClass; 00752 $this->stack[] = new $class( $data ); 00753 } 00754 $this->top = $this->stack[ count( $this->stack ) - 1 ]; 00755 $this->accum =& $this->top->getAccum(); 00756 } 00757 00758 function pop() { 00759 if ( !count( $this->stack ) ) { 00760 throw new MWException( __METHOD__.': no elements remaining' ); 00761 } 00762 $temp = array_pop( $this->stack ); 00763 00764 if ( count( $this->stack ) ) { 00765 $this->top = $this->stack[ count( $this->stack ) - 1 ]; 00766 $this->accum =& $this->top->getAccum(); 00767 } else { 00768 $this->top = self::$false; 00769 $this->accum =& $this->rootAccum; 00770 } 00771 return $temp; 00772 } 00773 00774 function addPart( $s = '' ) { 00775 $this->top->addPart( $s ); 00776 $this->accum =& $this->top->getAccum(); 00777 } 00778 00782 function getFlags() { 00783 if ( !count( $this->stack ) ) { 00784 return array( 00785 'findEquals' => false, 00786 'findPipe' => false, 00787 'inHeading' => false, 00788 ); 00789 } else { 00790 return $this->top->getFlags(); 00791 } 00792 } 00793 } 00794 00798 class PPDStackElement { 00799 var $open, // Opening character (\n for heading) 00800 $close, // Matching closing character 00801 $count, // Number of opening characters found (number of "=" for heading) 00802 $parts, // Array of PPDPart objects describing pipe-separated parts. 00803 $lineStart; // True if the open char appeared at the start of the input line. Not set for headings. 00804 00805 var $partClass = 'PPDPart'; 00806 00807 function __construct( $data = array() ) { 00808 $class = $this->partClass; 00809 $this->parts = array( new $class ); 00810 00811 foreach ( $data as $name => $value ) { 00812 $this->$name = $value; 00813 } 00814 } 00815 00816 function &getAccum() { 00817 return $this->parts[count($this->parts) - 1]->out; 00818 } 00819 00820 function addPart( $s = '' ) { 00821 $class = $this->partClass; 00822 $this->parts[] = new $class( $s ); 00823 } 00824 00825 function getCurrentPart() { 00826 return $this->parts[count($this->parts) - 1]; 00827 } 00828 00832 function getFlags() { 00833 $partCount = count( $this->parts ); 00834 $findPipe = $this->open != "\n" && $this->open != '['; 00835 return array( 00836 'findPipe' => $findPipe, 00837 'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts[$partCount - 1]->eqpos ), 00838 'inHeading' => $this->open == "\n", 00839 ); 00840 } 00841 00847 function breakSyntax( $openingCount = false ) { 00848 if ( $this->open == "\n" ) { 00849 $s = $this->parts[0]->out; 00850 } else { 00851 if ( $openingCount === false ) { 00852 $openingCount = $this->count; 00853 } 00854 $s = str_repeat( $this->open, $openingCount ); 00855 $first = true; 00856 foreach ( $this->parts as $part ) { 00857 if ( $first ) { 00858 $first = false; 00859 } else { 00860 $s .= '|'; 00861 } 00862 $s .= $part->out; 00863 } 00864 } 00865 return $s; 00866 } 00867 } 00868 00872 class PPDPart { 00873 var $out; // Output accumulator string 00874 00875 // Optional member variables: 00876 // eqpos Position of equals sign in output accumulator 00877 // commentEnd Past-the-end input pointer for the last comment encountered 00878 // visualEnd Past-the-end input pointer for the end of the accumulator minus comments 00879 00880 function __construct( $out = '' ) { 00881 $this->out = $out; 00882 } 00883 } 00884 00889 class PPFrame_DOM implements PPFrame { 00890 00894 var $preprocessor; 00895 00899 var $parser; 00900 00904 var $title; 00905 var $titleCache; 00906 00911 var $loopCheckHash; 00912 00917 var $depth; 00918 00919 00924 function __construct( $preprocessor ) { 00925 $this->preprocessor = $preprocessor; 00926 $this->parser = $preprocessor->parser; 00927 $this->title = $this->parser->mTitle; 00928 $this->titleCache = array( $this->title ? $this->title->getPrefixedDBkey() : false ); 00929 $this->loopCheckHash = array(); 00930 $this->depth = 0; 00931 } 00932 00939 function newChild( $args = false, $title = false, $indexOffset = 0 ) { 00940 $namedArgs = array(); 00941 $numberedArgs = array(); 00942 if ( $title === false ) { 00943 $title = $this->title; 00944 } 00945 if ( $args !== false ) { 00946 $xpath = false; 00947 if ( $args instanceof PPNode ) { 00948 $args = $args->node; 00949 } 00950 foreach ( $args as $arg ) { 00951 if ( $arg instanceof PPNode ) { 00952 $arg = $arg->node; 00953 } 00954 if ( !$xpath ) { 00955 $xpath = new DOMXPath( $arg->ownerDocument ); 00956 } 00957 00958 $nameNodes = $xpath->query( 'name', $arg ); 00959 $value = $xpath->query( 'value', $arg ); 00960 if ( $nameNodes->item( 0 )->hasAttributes() ) { 00961 // Numbered parameter 00962 $index = $nameNodes->item( 0 )->attributes->getNamedItem( 'index' )->textContent; 00963 $index = $index - $indexOffset; 00964 $numberedArgs[$index] = $value->item( 0 ); 00965 unset( $namedArgs[$index] ); 00966 } else { 00967 // Named parameter 00968 $name = trim( $this->expand( $nameNodes->item( 0 ), PPFrame::STRIP_COMMENTS ) ); 00969 $namedArgs[$name] = $value->item( 0 ); 00970 unset( $numberedArgs[$name] ); 00971 } 00972 } 00973 } 00974 return new PPTemplateFrame_DOM( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title ); 00975 } 00976 00983 function expand( $root, $flags = 0 ) { 00984 static $expansionDepth = 0; 00985 if ( is_string( $root ) ) { 00986 return $root; 00987 } 00988 00989 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) { 00990 $this->parser->limitationWarn( 'node-count-exceeded', 00991 $this->parser->mPPNodeCount, 00992 $this->parser->mOptions->getMaxPPNodeCount() 00993 ); 00994 return '<span class="error">Node-count limit exceeded</span>'; 00995 } 00996 00997 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) { 00998 $this->parser->limitationWarn( 'expansion-depth-exceeded', 00999 $expansionDepth, 01000 $this->parser->mOptions->getMaxPPExpandDepth() 01001 ); 01002 return '<span class="error">Expansion depth limit exceeded</span>'; 01003 } 01004 wfProfileIn( __METHOD__ ); 01005 ++$expansionDepth; 01006 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) { 01007 $this->parser->mHighestExpansionDepth = $expansionDepth; 01008 } 01009 01010 if ( $root instanceof PPNode_DOM ) { 01011 $root = $root->node; 01012 } 01013 if ( $root instanceof DOMDocument ) { 01014 $root = $root->documentElement; 01015 } 01016 01017 $outStack = array( '', '' ); 01018 $iteratorStack = array( false, $root ); 01019 $indexStack = array( 0, 0 ); 01020 01021 while ( count( $iteratorStack ) > 1 ) { 01022 $level = count( $outStack ) - 1; 01023 $iteratorNode =& $iteratorStack[ $level ]; 01024 $out =& $outStack[$level]; 01025 $index =& $indexStack[$level]; 01026 01027 if ( $iteratorNode instanceof PPNode_DOM ) $iteratorNode = $iteratorNode->node; 01028 01029 if ( is_array( $iteratorNode ) ) { 01030 if ( $index >= count( $iteratorNode ) ) { 01031 // All done with this iterator 01032 $iteratorStack[$level] = false; 01033 $contextNode = false; 01034 } else { 01035 $contextNode = $iteratorNode[$index]; 01036 $index++; 01037 } 01038 } elseif ( $iteratorNode instanceof DOMNodeList ) { 01039 if ( $index >= $iteratorNode->length ) { 01040 // All done with this iterator 01041 $iteratorStack[$level] = false; 01042 $contextNode = false; 01043 } else { 01044 $contextNode = $iteratorNode->item( $index ); 01045 $index++; 01046 } 01047 } else { 01048 // Copy to $contextNode and then delete from iterator stack, 01049 // because this is not an iterator but we do have to execute it once 01050 $contextNode = $iteratorStack[$level]; 01051 $iteratorStack[$level] = false; 01052 } 01053 01054 if ( $contextNode instanceof PPNode_DOM ) { 01055 $contextNode = $contextNode->node; 01056 } 01057 01058 $newIterator = false; 01059 01060 if ( $contextNode === false ) { 01061 // nothing to do 01062 } elseif ( is_string( $contextNode ) ) { 01063 $out .= $contextNode; 01064 } elseif ( is_array( $contextNode ) || $contextNode instanceof DOMNodeList ) { 01065 $newIterator = $contextNode; 01066 } elseif ( $contextNode instanceof DOMNode ) { 01067 if ( $contextNode->nodeType == XML_TEXT_NODE ) { 01068 $out .= $contextNode->nodeValue; 01069 } elseif ( $contextNode->nodeName == 'template' ) { 01070 # Double-brace expansion 01071 $xpath = new DOMXPath( $contextNode->ownerDocument ); 01072 $titles = $xpath->query( 'title', $contextNode ); 01073 $title = $titles->item( 0 ); 01074 $parts = $xpath->query( 'part', $contextNode ); 01075 if ( $flags & PPFrame::NO_TEMPLATES ) { 01076 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $title, $parts ); 01077 } else { 01078 $lineStart = $contextNode->getAttribute( 'lineStart' ); 01079 $params = array( 01080 'title' => new PPNode_DOM( $title ), 01081 'parts' => new PPNode_DOM( $parts ), 01082 'lineStart' => $lineStart ); 01083 $ret = $this->parser->braceSubstitution( $params, $this ); 01084 if ( isset( $ret['object'] ) ) { 01085 $newIterator = $ret['object']; 01086 } else { 01087 $out .= $ret['text']; 01088 } 01089 } 01090 } elseif ( $contextNode->nodeName == 'tplarg' ) { 01091 # Triple-brace expansion 01092 $xpath = new DOMXPath( $contextNode->ownerDocument ); 01093 $titles = $xpath->query( 'title', $contextNode ); 01094 $title = $titles->item( 0 ); 01095 $parts = $xpath->query( 'part', $contextNode ); 01096 if ( $flags & PPFrame::NO_ARGS ) { 01097 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $title, $parts ); 01098 } else { 01099 $params = array( 01100 'title' => new PPNode_DOM( $title ), 01101 'parts' => new PPNode_DOM( $parts ) ); 01102 $ret = $this->parser->argSubstitution( $params, $this ); 01103 if ( isset( $ret['object'] ) ) { 01104 $newIterator = $ret['object']; 01105 } else { 01106 $out .= $ret['text']; 01107 } 01108 } 01109 } elseif ( $contextNode->nodeName == 'comment' ) { 01110 # HTML-style comment 01111 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes 01112 if ( $this->parser->ot['html'] 01113 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() ) 01114 || ( $flags & PPFrame::STRIP_COMMENTS ) ) 01115 { 01116 $out .= ''; 01117 } 01118 # Add a strip marker in PST mode so that pstPass2() can run some old-fashioned regexes on the result 01119 # Not in RECOVER_COMMENTS mode (extractSections) though 01120 elseif ( $this->parser->ot['wiki'] && ! ( $flags & PPFrame::RECOVER_COMMENTS ) ) { 01121 $out .= $this->parser->insertStripItem( $contextNode->textContent ); 01122 } 01123 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove 01124 else { 01125 $out .= $contextNode->textContent; 01126 } 01127 } elseif ( $contextNode->nodeName == 'ignore' ) { 01128 # Output suppression used by <includeonly> etc. 01129 # OT_WIKI will only respect <ignore> in substed templates. 01130 # The other output types respect it unless NO_IGNORE is set. 01131 # extractSections() sets NO_IGNORE and so never respects it. 01132 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] ) || ( $flags & PPFrame::NO_IGNORE ) ) { 01133 $out .= $contextNode->textContent; 01134 } else { 01135 $out .= ''; 01136 } 01137 } elseif ( $contextNode->nodeName == 'ext' ) { 01138 # Extension tag 01139 $xpath = new DOMXPath( $contextNode->ownerDocument ); 01140 $names = $xpath->query( 'name', $contextNode ); 01141 $attrs = $xpath->query( 'attr', $contextNode ); 01142 $inners = $xpath->query( 'inner', $contextNode ); 01143 $closes = $xpath->query( 'close', $contextNode ); 01144 $params = array( 01145 'name' => new PPNode_DOM( $names->item( 0 ) ), 01146 'attr' => $attrs->length > 0 ? new PPNode_DOM( $attrs->item( 0 ) ) : null, 01147 'inner' => $inners->length > 0 ? new PPNode_DOM( $inners->item( 0 ) ) : null, 01148 'close' => $closes->length > 0 ? new PPNode_DOM( $closes->item( 0 ) ) : null, 01149 ); 01150 $out .= $this->parser->extensionSubstitution( $params, $this ); 01151 } elseif ( $contextNode->nodeName == 'h' ) { 01152 # Heading 01153 $s = $this->expand( $contextNode->childNodes, $flags ); 01154 01155 # Insert a heading marker only for <h> children of <root> 01156 # This is to stop extractSections from going over multiple tree levels 01157 if ( $contextNode->parentNode->nodeName == 'root' 01158 && $this->parser->ot['html'] ) 01159 { 01160 # Insert heading index marker 01161 $headingIndex = $contextNode->getAttribute( 'i' ); 01162 $titleText = $this->title->getPrefixedDBkey(); 01163 $this->parser->mHeadings[] = array( $titleText, $headingIndex ); 01164 $serial = count( $this->parser->mHeadings ) - 1; 01165 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser::MARKER_SUFFIX; 01166 $count = $contextNode->getAttribute( 'level' ); 01167 $s = substr( $s, 0, $count ) . $marker . substr( $s, $count ); 01168 $this->parser->mStripState->addGeneral( $marker, '' ); 01169 } 01170 $out .= $s; 01171 } else { 01172 # Generic recursive expansion 01173 $newIterator = $contextNode->childNodes; 01174 } 01175 } else { 01176 wfProfileOut( __METHOD__ ); 01177 throw new MWException( __METHOD__.': Invalid parameter type' ); 01178 } 01179 01180 if ( $newIterator !== false ) { 01181 if ( $newIterator instanceof PPNode_DOM ) { 01182 $newIterator = $newIterator->node; 01183 } 01184 $outStack[] = ''; 01185 $iteratorStack[] = $newIterator; 01186 $indexStack[] = 0; 01187 } elseif ( $iteratorStack[$level] === false ) { 01188 // Return accumulated value to parent 01189 // With tail recursion 01190 while ( $iteratorStack[$level] === false && $level > 0 ) { 01191 $outStack[$level - 1] .= $out; 01192 array_pop( $outStack ); 01193 array_pop( $iteratorStack ); 01194 array_pop( $indexStack ); 01195 $level--; 01196 } 01197 } 01198 } 01199 --$expansionDepth; 01200 wfProfileOut( __METHOD__ ); 01201 return $outStack[0]; 01202 } 01203 01209 function implodeWithFlags( $sep, $flags /*, ... */ ) { 01210 $args = array_slice( func_get_args(), 2 ); 01211 01212 $first = true; 01213 $s = ''; 01214 foreach ( $args as $root ) { 01215 if ( $root instanceof PPNode_DOM ) $root = $root->node; 01216 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) { 01217 $root = array( $root ); 01218 } 01219 foreach ( $root as $node ) { 01220 if ( $first ) { 01221 $first = false; 01222 } else { 01223 $s .= $sep; 01224 } 01225 $s .= $this->expand( $node, $flags ); 01226 } 01227 } 01228 return $s; 01229 } 01230 01237 function implode( $sep /*, ... */ ) { 01238 $args = array_slice( func_get_args(), 1 ); 01239 01240 $first = true; 01241 $s = ''; 01242 foreach ( $args as $root ) { 01243 if ( $root instanceof PPNode_DOM ) { 01244 $root = $root->node; 01245 } 01246 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) { 01247 $root = array( $root ); 01248 } 01249 foreach ( $root as $node ) { 01250 if ( $first ) { 01251 $first = false; 01252 } else { 01253 $s .= $sep; 01254 } 01255 $s .= $this->expand( $node ); 01256 } 01257 } 01258 return $s; 01259 } 01260 01267 function virtualImplode( $sep /*, ... */ ) { 01268 $args = array_slice( func_get_args(), 1 ); 01269 $out = array(); 01270 $first = true; 01271 01272 foreach ( $args as $root ) { 01273 if ( $root instanceof PPNode_DOM ) { 01274 $root = $root->node; 01275 } 01276 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) { 01277 $root = array( $root ); 01278 } 01279 foreach ( $root as $node ) { 01280 if ( $first ) { 01281 $first = false; 01282 } else { 01283 $out[] = $sep; 01284 } 01285 $out[] = $node; 01286 } 01287 } 01288 return $out; 01289 } 01290 01295 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) { 01296 $args = array_slice( func_get_args(), 3 ); 01297 $out = array( $start ); 01298 $first = true; 01299 01300 foreach ( $args as $root ) { 01301 if ( $root instanceof PPNode_DOM ) { 01302 $root = $root->node; 01303 } 01304 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) { 01305 $root = array( $root ); 01306 } 01307 foreach ( $root as $node ) { 01308 if ( $first ) { 01309 $first = false; 01310 } else { 01311 $out[] = $sep; 01312 } 01313 $out[] = $node; 01314 } 01315 } 01316 $out[] = $end; 01317 return $out; 01318 } 01319 01320 function __toString() { 01321 return 'frame{}'; 01322 } 01323 01324 function getPDBK( $level = false ) { 01325 if ( $level === false ) { 01326 return $this->title->getPrefixedDBkey(); 01327 } else { 01328 return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false; 01329 } 01330 } 01331 01335 function getArguments() { 01336 return array(); 01337 } 01338 01342 function getNumberedArguments() { 01343 return array(); 01344 } 01345 01349 function getNamedArguments() { 01350 return array(); 01351 } 01352 01358 function isEmpty() { 01359 return true; 01360 } 01361 01362 function getArgument( $name ) { 01363 return false; 01364 } 01365 01371 function loopCheck( $title ) { 01372 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] ); 01373 } 01374 01380 function isTemplate() { 01381 return false; 01382 } 01383 01389 function getTitle() { 01390 return $this->title; 01391 } 01392 } 01393 01398 class PPTemplateFrame_DOM extends PPFrame_DOM { 01399 var $numberedArgs, $namedArgs; 01400 01404 var $parent; 01405 var $numberedExpansionCache, $namedExpansionCache; 01406 01414 function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) { 01415 parent::__construct( $preprocessor ); 01416 01417 $this->parent = $parent; 01418 $this->numberedArgs = $numberedArgs; 01419 $this->namedArgs = $namedArgs; 01420 $this->title = $title; 01421 $pdbk = $title ? $title->getPrefixedDBkey() : false; 01422 $this->titleCache = $parent->titleCache; 01423 $this->titleCache[] = $pdbk; 01424 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash; 01425 if ( $pdbk !== false ) { 01426 $this->loopCheckHash[$pdbk] = true; 01427 } 01428 $this->depth = $parent->depth + 1; 01429 $this->numberedExpansionCache = $this->namedExpansionCache = array(); 01430 } 01431 01432 function __toString() { 01433 $s = 'tplframe{'; 01434 $first = true; 01435 $args = $this->numberedArgs + $this->namedArgs; 01436 foreach ( $args as $name => $value ) { 01437 if ( $first ) { 01438 $first = false; 01439 } else { 01440 $s .= ', '; 01441 } 01442 $s .= "\"$name\":\"" . 01443 str_replace( '"', '\\"', $value->ownerDocument->saveXML( $value ) ) . '"'; 01444 } 01445 $s .= '}'; 01446 return $s; 01447 } 01448 01454 function isEmpty() { 01455 return !count( $this->numberedArgs ) && !count( $this->namedArgs ); 01456 } 01457 01458 function getArguments() { 01459 $arguments = array(); 01460 foreach ( array_merge( 01461 array_keys($this->numberedArgs), 01462 array_keys($this->namedArgs)) as $key ) { 01463 $arguments[$key] = $this->getArgument($key); 01464 } 01465 return $arguments; 01466 } 01467 01468 function getNumberedArguments() { 01469 $arguments = array(); 01470 foreach ( array_keys($this->numberedArgs) as $key ) { 01471 $arguments[$key] = $this->getArgument($key); 01472 } 01473 return $arguments; 01474 } 01475 01476 function getNamedArguments() { 01477 $arguments = array(); 01478 foreach ( array_keys($this->namedArgs) as $key ) { 01479 $arguments[$key] = $this->getArgument($key); 01480 } 01481 return $arguments; 01482 } 01483 01484 function getNumberedArgument( $index ) { 01485 if ( !isset( $this->numberedArgs[$index] ) ) { 01486 return false; 01487 } 01488 if ( !isset( $this->numberedExpansionCache[$index] ) ) { 01489 # No trimming for unnamed arguments 01490 $this->numberedExpansionCache[$index] = $this->parent->expand( $this->numberedArgs[$index], PPFrame::STRIP_COMMENTS ); 01491 } 01492 return $this->numberedExpansionCache[$index]; 01493 } 01494 01495 function getNamedArgument( $name ) { 01496 if ( !isset( $this->namedArgs[$name] ) ) { 01497 return false; 01498 } 01499 if ( !isset( $this->namedExpansionCache[$name] ) ) { 01500 # Trim named arguments post-expand, for backwards compatibility 01501 $this->namedExpansionCache[$name] = trim( 01502 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) ); 01503 } 01504 return $this->namedExpansionCache[$name]; 01505 } 01506 01507 function getArgument( $name ) { 01508 $text = $this->getNumberedArgument( $name ); 01509 if ( $text === false ) { 01510 $text = $this->getNamedArgument( $name ); 01511 } 01512 return $text; 01513 } 01514 01520 function isTemplate() { 01521 return true; 01522 } 01523 } 01524 01529 class PPCustomFrame_DOM extends PPFrame_DOM { 01530 var $args; 01531 01532 function __construct( $preprocessor, $args ) { 01533 parent::__construct( $preprocessor ); 01534 $this->args = $args; 01535 } 01536 01537 function __toString() { 01538 $s = 'cstmframe{'; 01539 $first = true; 01540 foreach ( $this->args as $name => $value ) { 01541 if ( $first ) { 01542 $first = false; 01543 } else { 01544 $s .= ', '; 01545 } 01546 $s .= "\"$name\":\"" . 01547 str_replace( '"', '\\"', $value->__toString() ) . '"'; 01548 } 01549 $s .= '}'; 01550 return $s; 01551 } 01552 01556 function isEmpty() { 01557 return !count( $this->args ); 01558 } 01559 01560 function getArgument( $index ) { 01561 if ( !isset( $this->args[$index] ) ) { 01562 return false; 01563 } 01564 return $this->args[$index]; 01565 } 01566 01567 function getArguments() { 01568 return $this->args; 01569 } 01570 } 01571 01575 class PPNode_DOM implements PPNode { 01576 01580 var $node; 01581 var $xpath; 01582 01583 function __construct( $node, $xpath = false ) { 01584 $this->node = $node; 01585 } 01586 01590 function getXPath() { 01591 if ( $this->xpath === null ) { 01592 $this->xpath = new DOMXPath( $this->node->ownerDocument ); 01593 } 01594 return $this->xpath; 01595 } 01596 01597 function __toString() { 01598 if ( $this->node instanceof DOMNodeList ) { 01599 $s = ''; 01600 foreach ( $this->node as $node ) { 01601 $s .= $node->ownerDocument->saveXML( $node ); 01602 } 01603 } else { 01604 $s = $this->node->ownerDocument->saveXML( $this->node ); 01605 } 01606 return $s; 01607 } 01608 01612 function getChildren() { 01613 return $this->node->childNodes ? new self( $this->node->childNodes ) : false; 01614 } 01615 01619 function getFirstChild() { 01620 return $this->node->firstChild ? new self( $this->node->firstChild ) : false; 01621 } 01622 01626 function getNextSibling() { 01627 return $this->node->nextSibling ? new self( $this->node->nextSibling ) : false; 01628 } 01629 01635 function getChildrenOfType( $type ) { 01636 return new self( $this->getXPath()->query( $type, $this->node ) ); 01637 } 01638 01642 function getLength() { 01643 if ( $this->node instanceof DOMNodeList ) { 01644 return $this->node->length; 01645 } else { 01646 return false; 01647 } 01648 } 01649 01654 function item( $i ) { 01655 $item = $this->node->item( $i ); 01656 return $item ? new self( $item ) : false; 01657 } 01658 01662 function getName() { 01663 if ( $this->node instanceof DOMNodeList ) { 01664 return '#nodelist'; 01665 } else { 01666 return $this->node->nodeName; 01667 } 01668 } 01669 01678 function splitArg() { 01679 $xpath = $this->getXPath(); 01680 $names = $xpath->query( 'name', $this->node ); 01681 $values = $xpath->query( 'value', $this->node ); 01682 if ( !$names->length || !$values->length ) { 01683 throw new MWException( 'Invalid brace node passed to ' . __METHOD__ ); 01684 } 01685 $name = $names->item( 0 ); 01686 $index = $name->getAttribute( 'index' ); 01687 return array( 01688 'name' => new self( $name ), 01689 'index' => $index, 01690 'value' => new self( $values->item( 0 ) ) ); 01691 } 01692 01699 function splitExt() { 01700 $xpath = $this->getXPath(); 01701 $names = $xpath->query( 'name', $this->node ); 01702 $attrs = $xpath->query( 'attr', $this->node ); 01703 $inners = $xpath->query( 'inner', $this->node ); 01704 $closes = $xpath->query( 'close', $this->node ); 01705 if ( !$names->length || !$attrs->length ) { 01706 throw new MWException( 'Invalid ext node passed to ' . __METHOD__ ); 01707 } 01708 $parts = array( 01709 'name' => new self( $names->item( 0 ) ), 01710 'attr' => new self( $attrs->item( 0 ) ) ); 01711 if ( $inners->length ) { 01712 $parts['inner'] = new self( $inners->item( 0 ) ); 01713 } 01714 if ( $closes->length ) { 01715 $parts['close'] = new self( $closes->item( 0 ) ); 01716 } 01717 return $parts; 01718 } 01719 01724 function splitHeading() { 01725 if ( $this->getName() !== 'h' ) { 01726 throw new MWException( 'Invalid h node passed to ' . __METHOD__ ); 01727 } 01728 return array( 01729 'i' => $this->node->getAttribute( 'i' ), 01730 'level' => $this->node->getAttribute( 'level' ), 01731 'contents' => $this->getChildren() 01732 ); 01733 } 01734 }