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