MediaWiki  REL1_21
Preprocessor_Hash.php
Go to the documentation of this file.
00001 <?php
00030 class Preprocessor_Hash implements Preprocessor {
00034         var $parser;
00035 
00036         const CACHE_VERSION = 1;
00037 
00038         function __construct( $parser ) {
00039                 $this->parser = $parser;
00040         }
00041 
00045         function newFrame() {
00046                 return new PPFrame_Hash( $this );
00047         }
00048 
00053         function newCustomFrame( $args ) {
00054                 return new PPCustomFrame_Hash( $this, $args );
00055         }
00056 
00061         function newPartNodeArray( $values ) {
00062                 $list = array();
00063 
00064                 foreach ( $values as $k => $val ) {
00065                         $partNode = new PPNode_Hash_Tree( 'part' );
00066                         $nameNode = new PPNode_Hash_Tree( 'name' );
00067 
00068                         if ( is_int( $k ) ) {
00069                                 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $k ) );
00070                                 $partNode->addChild( $nameNode );
00071                         } else {
00072                                 $nameNode->addChild( new PPNode_Hash_Text( $k ) );
00073                                 $partNode->addChild( $nameNode );
00074                                 $partNode->addChild( new PPNode_Hash_Text( '=' ) );
00075                         }
00076 
00077                         $valueNode = new PPNode_Hash_Tree( 'value' );
00078                         $valueNode->addChild( new PPNode_Hash_Text( $val ) );
00079                         $partNode->addChild( $valueNode );
00080 
00081                         $list[] = $partNode;
00082                 }
00083 
00084                 $node = new PPNode_Hash_Array( $list );
00085                 return $node;
00086         }
00087 
00111         function preprocessToObj( $text, $flags = 0 ) {
00112                 wfProfileIn( __METHOD__ );
00113 
00114                 // Check cache.
00115                 global $wgMemc, $wgPreprocessorCacheThreshold;
00116 
00117                 $cacheable = $wgPreprocessorCacheThreshold !== false && strlen( $text ) > $wgPreprocessorCacheThreshold;
00118                 if ( $cacheable ) {
00119                         wfProfileIn( __METHOD__ . '-cacheable' );
00120 
00121                         $cacheKey = wfMemcKey( 'preprocess-hash', md5( $text ), $flags );
00122                         $cacheValue = $wgMemc->get( $cacheKey );
00123                         if ( $cacheValue ) {
00124                                 $version = substr( $cacheValue, 0, 8 );
00125                                 if ( intval( $version ) == self::CACHE_VERSION ) {
00126                                         $hash = unserialize( substr( $cacheValue, 8 ) );
00127                                         // From the cache
00128                                         wfDebugLog( "Preprocessor",
00129                                                 "Loaded preprocessor hash from memcached (key $cacheKey)" );
00130                                         wfProfileOut( __METHOD__ . '-cacheable' );
00131                                         wfProfileOut( __METHOD__ );
00132                                         return $hash;
00133                                 }
00134                         }
00135                         wfProfileIn( __METHOD__ . '-cache-miss' );
00136                 }
00137 
00138                 $rules = array(
00139                         '{' => array(
00140                                 'end' => '}',
00141                                 'names' => array(
00142                                         2 => 'template',
00143                                         3 => 'tplarg',
00144                                 ),
00145                                 'min' => 2,
00146                                 'max' => 3,
00147                         ),
00148                         '[' => array(
00149                                 'end' => ']',
00150                                 'names' => array( 2 => null ),
00151                                 'min' => 2,
00152                                 'max' => 2,
00153                         )
00154                 );
00155 
00156                 $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
00157 
00158                 $xmlishElements = $this->parser->getStripList();
00159                 $enableOnlyinclude = false;
00160                 if ( $forInclusion ) {
00161                         $ignoredTags = array( 'includeonly', '/includeonly' );
00162                         $ignoredElements = array( 'noinclude' );
00163                         $xmlishElements[] = 'noinclude';
00164                         if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) {
00165                                 $enableOnlyinclude = true;
00166                         }
00167                 } else {
00168                         $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
00169                         $ignoredElements = array( 'includeonly' );
00170                         $xmlishElements[] = 'includeonly';
00171                 }
00172                 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
00173 
00174                 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
00175                 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
00176 
00177                 $stack = new PPDStack_Hash;
00178 
00179                 $searchBase = "[{<\n";
00180                 $revText = strrev( $text ); // For fast reverse searches
00181                 $lengthText = strlen( $text );
00182 
00183                 $i = 0;                     # Input pointer, starts out pointing to a pseudo-newline before the start
00184                 $accum =& $stack->getAccum();   # Current accumulator
00185                 $findEquals = false;            # True to find equals signs in arguments
00186                 $findPipe = false;              # True to take notice of pipe characters
00187                 $headingIndex = 1;
00188                 $inHeading = false;        # True if $i is inside a possible heading
00189                 $noMoreGT = false;         # True if there are no more greater-than (>) signs right of $i
00190                 $findOnlyinclude = $enableOnlyinclude; # True to ignore all input up to the next <onlyinclude>
00191                 $fakeLineStart = true;     # Do a line-start run without outputting an LF character
00192 
00193                 while ( true ) {
00194                         //$this->memCheck();
00195 
00196                         if ( $findOnlyinclude ) {
00197                                 // Ignore all input up to the next <onlyinclude>
00198                                 $startPos = strpos( $text, '<onlyinclude>', $i );
00199                                 if ( $startPos === false ) {
00200                                         // Ignored section runs to the end
00201                                         $accum->addNodeWithText( 'ignore', substr( $text, $i ) );
00202                                         break;
00203                                 }
00204                                 $tagEndPos = $startPos + strlen( '<onlyinclude>' ); // past-the-end
00205                                 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i ) );
00206                                 $i = $tagEndPos;
00207                                 $findOnlyinclude = false;
00208                         }
00209 
00210                         if ( $fakeLineStart ) {
00211                                 $found = 'line-start';
00212                                 $curChar = '';
00213                         } else {
00214                                 # Find next opening brace, closing brace or pipe
00215                                 $search = $searchBase;
00216                                 if ( $stack->top === false ) {
00217                                         $currentClosing = '';
00218                                 } else {
00219                                         $currentClosing = $stack->top->close;
00220                                         $search .= $currentClosing;
00221                                 }
00222                                 if ( $findPipe ) {
00223                                         $search .= '|';
00224                                 }
00225                                 if ( $findEquals ) {
00226                                         // First equals will be for the template
00227                                         $search .= '=';
00228                                 }
00229                                 $rule = null;
00230                                 # Output literal section, advance input counter
00231                                 $literalLength = strcspn( $text, $search, $i );
00232                                 if ( $literalLength > 0 ) {
00233                                         $accum->addLiteral( substr( $text, $i, $literalLength ) );
00234                                         $i += $literalLength;
00235                                 }
00236                                 if ( $i >= $lengthText ) {
00237                                         if ( $currentClosing == "\n" ) {
00238                                                 // Do a past-the-end run to finish off the heading
00239                                                 $curChar = '';
00240                                                 $found = 'line-end';
00241                                         } else {
00242                                                 # All done
00243                                                 break;
00244                                         }
00245                                 } else {
00246                                         $curChar = $text[$i];
00247                                         if ( $curChar == '|' ) {
00248                                                 $found = 'pipe';
00249                                         } elseif ( $curChar == '=' ) {
00250                                                 $found = 'equals';
00251                                         } elseif ( $curChar == '<' ) {
00252                                                 $found = 'angle';
00253                                         } elseif ( $curChar == "\n" ) {
00254                                                 if ( $inHeading ) {
00255                                                         $found = 'line-end';
00256                                                 } else {
00257                                                         $found = 'line-start';
00258                                                 }
00259                                         } elseif ( $curChar == $currentClosing ) {
00260                                                 $found = 'close';
00261                                         } elseif ( isset( $rules[$curChar] ) ) {
00262                                                 $found = 'open';
00263                                                 $rule = $rules[$curChar];
00264                                         } else {
00265                                                 # Some versions of PHP have a strcspn which stops on null characters
00266                                                 # Ignore and continue
00267                                                 ++$i;
00268                                                 continue;
00269                                         }
00270                                 }
00271                         }
00272 
00273                         if ( $found == 'angle' ) {
00274                                 $matches = false;
00275                                 // Handle </onlyinclude>
00276                                 if ( $enableOnlyinclude && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>' ) {
00277                                         $findOnlyinclude = true;
00278                                         continue;
00279                                 }
00280 
00281                                 // Determine element name
00282                                 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) {
00283                                         // Element name missing or not listed
00284                                         $accum->addLiteral( '<' );
00285                                         ++$i;
00286                                         continue;
00287                                 }
00288                                 // Handle comments
00289                                 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
00290                                         // To avoid leaving blank lines, when a comment is both preceded
00291                                         // and followed by a newline (ignoring spaces), trim leading and
00292                                         // trailing spaces and one of the newlines.
00293 
00294                                         // Find the end
00295                                         $endPos = strpos( $text, '-->', $i + 4 );
00296                                         if ( $endPos === false ) {
00297                                                 // Unclosed comment in input, runs to end
00298                                                 $inner = substr( $text, $i );
00299                                                 $accum->addNodeWithText( 'comment', $inner );
00300                                                 $i = $lengthText;
00301                                         } else {
00302                                                 // Search backwards for leading whitespace
00303                                                 $wsStart = $i ? ( $i - strspn( $revText, ' ', $lengthText - $i ) ) : 0;
00304                                                 // Search forwards for trailing whitespace
00305                                                 // $wsEnd will be the position of the last space (or the '>' if there's none)
00306                                                 $wsEnd = $endPos + 2 + strspn( $text, ' ', $endPos + 3 );
00307                                                 // Eat the line if possible
00308                                                 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
00309                                                 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
00310                                                 // it's a possible beneficial b/c break.
00311                                                 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
00312                                                         && substr( $text, $wsEnd + 1, 1 ) == "\n" )
00313                                                 {
00314                                                         $startPos = $wsStart;
00315                                                         $endPos = $wsEnd + 1;
00316                                                         // Remove leading whitespace from the end of the accumulator
00317                                                         // Sanity check first though
00318                                                         $wsLength = $i - $wsStart;
00319                                                         if ( $wsLength > 0
00320                                                                 && $accum->lastNode instanceof PPNode_Hash_Text
00321                                                                 && substr( $accum->lastNode->value, -$wsLength ) === str_repeat( ' ', $wsLength ) )
00322                                                         {
00323                                                                 $accum->lastNode->value = substr( $accum->lastNode->value, 0, -$wsLength );
00324                                                         }
00325                                                         // Do a line-start run next time to look for headings after the comment
00326                                                         $fakeLineStart = true;
00327                                                 } else {
00328                                                         // No line to eat, just take the comment itself
00329                                                         $startPos = $i;
00330                                                         $endPos += 2;
00331                                                 }
00332 
00333                                                 if ( $stack->top ) {
00334                                                         $part = $stack->top->getCurrentPart();
00335                                                         if ( !(isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 )) {
00336                                                                 $part->visualEnd = $wsStart;
00337                                                         }
00338                                                         // Else comments abutting, no change in visual end
00339                                                         $part->commentEnd = $endPos;
00340                                                 }
00341                                                 $i = $endPos + 1;
00342                                                 $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
00343                                                 $accum->addNodeWithText( 'comment', $inner );
00344                                         }
00345                                         continue;
00346                                 }
00347                                 $name = $matches[1];
00348                                 $lowerName = strtolower( $name );
00349                                 $attrStart = $i + strlen( $name ) + 1;
00350 
00351                                 // Find end of tag
00352                                 $tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart );
00353                                 if ( $tagEndPos === false ) {
00354                                         // Infinite backtrack
00355                                         // Disable tag search to prevent worst-case O(N^2) performance
00356                                         $noMoreGT = true;
00357                                         $accum->addLiteral( '<' );
00358                                         ++$i;
00359                                         continue;
00360                                 }
00361 
00362                                 // Handle ignored tags
00363                                 if ( in_array( $lowerName, $ignoredTags ) ) {
00364                                         $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i + 1 ) );
00365                                         $i = $tagEndPos + 1;
00366                                         continue;
00367                                 }
00368 
00369                                 $tagStartPos = $i;
00370                                 if ( $text[$tagEndPos-1] == '/' ) {
00371                                         // Short end tag
00372                                         $attrEnd = $tagEndPos - 1;
00373                                         $inner = null;
00374                                         $i = $tagEndPos + 1;
00375                                         $close = null;
00376                                 } else {
00377                                         $attrEnd = $tagEndPos;
00378                                         // Find closing tag
00379                                         if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
00380                                                         $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) )
00381                                         {
00382                                                 $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
00383                                                 $i = $matches[0][1] + strlen( $matches[0][0] );
00384                                                 $close = $matches[0][0];
00385                                         } else {
00386                                                 // No end tag -- let it run out to the end of the text.
00387                                                 $inner = substr( $text, $tagEndPos + 1 );
00388                                                 $i = $lengthText;
00389                                                 $close = null;
00390                                         }
00391                                 }
00392                                 // <includeonly> and <noinclude> just become <ignore> tags
00393                                 if ( in_array( $lowerName, $ignoredElements ) ) {
00394                                         $accum->addNodeWithText( 'ignore', substr( $text, $tagStartPos, $i - $tagStartPos ) );
00395                                         continue;
00396                                 }
00397 
00398                                 if ( $attrEnd <= $attrStart ) {
00399                                         $attr = '';
00400                                 } else {
00401                                         // Note that the attr element contains the whitespace between name and attribute,
00402                                         // this is necessary for precise reconstruction during pre-save transform.
00403                                         $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
00404                                 }
00405 
00406                                 $extNode = new PPNode_Hash_Tree( 'ext' );
00407                                 $extNode->addChild( PPNode_Hash_Tree::newWithText( 'name', $name ) );
00408                                 $extNode->addChild( PPNode_Hash_Tree::newWithText( 'attr', $attr ) );
00409                                 if ( $inner !== null ) {
00410                                         $extNode->addChild( PPNode_Hash_Tree::newWithText( 'inner', $inner ) );
00411                                 }
00412                                 if ( $close !== null ) {
00413                                         $extNode->addChild( PPNode_Hash_Tree::newWithText( 'close', $close ) );
00414                                 }
00415                                 $accum->addNode( $extNode );
00416                         }
00417 
00418                         elseif ( $found == 'line-start' ) {
00419                                 // Is this the start of a heading?
00420                                 // Line break belongs before the heading element in any case
00421                                 if ( $fakeLineStart ) {
00422                                         $fakeLineStart = false;
00423                                 } else {
00424                                         $accum->addLiteral( $curChar );
00425                                         $i++;
00426                                 }
00427 
00428                                 $count = strspn( $text, '=', $i, 6 );
00429                                 if ( $count == 1 && $findEquals ) {
00430                                         // DWIM: This looks kind of like a name/value separator
00431                                         // Let's let the equals handler have it and break the potential heading
00432                                         // This is heuristic, but AFAICT the methods for completely correct disambiguation are very complex.
00433                                 } elseif ( $count > 0 ) {
00434                                         $piece = array(
00435                                                 'open' => "\n",
00436                                                 'close' => "\n",
00437                                                 'parts' => array( new PPDPart_Hash( str_repeat( '=', $count ) ) ),
00438                                                 'startPos' => $i,
00439                                                 'count' => $count );
00440                                         $stack->push( $piece );
00441                                         $accum =& $stack->getAccum();
00442                                         extract( $stack->getFlags() );
00443                                         $i += $count;
00444                                 }
00445                         } elseif ( $found == 'line-end' ) {
00446                                 $piece = $stack->top;
00447                                 // A heading must be open, otherwise \n wouldn't have been in the search list
00448                                 assert( '$piece->open == "\n"' );
00449                                 $part = $piece->getCurrentPart();
00450                                 // Search back through the input to see if it has a proper close
00451                                 // Do this using the reversed string since the other solutions (end anchor, etc.) are inefficient
00452                                 $wsLength = strspn( $revText, " \t", $lengthText - $i );
00453                                 $searchStart = $i - $wsLength;
00454                                 if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
00455                                         // Comment found at line end
00456                                         // Search for equals signs before the comment
00457                                         $searchStart = $part->visualEnd;
00458                                         $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
00459                                 }
00460                                 $count = $piece->count;
00461                                 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
00462                                 if ( $equalsLength > 0 ) {
00463                                         if ( $searchStart - $equalsLength == $piece->startPos ) {
00464                                                 // This is just a single string of equals signs on its own line
00465                                                 // Replicate the doHeadings behavior /={count}(.+)={count}/
00466                                                 // First find out how many equals signs there really are (don't stop at 6)
00467                                                 $count = $equalsLength;
00468                                                 if ( $count < 3 ) {
00469                                                         $count = 0;
00470                                                 } else {
00471                                                         $count = min( 6, intval( ( $count - 1 ) / 2 ) );
00472                                                 }
00473                                         } else {
00474                                                 $count = min( $equalsLength, $count );
00475                                         }
00476                                         if ( $count > 0 ) {
00477                                                 // Normal match, output <h>
00478                                                 $element = new PPNode_Hash_Tree( 'possible-h' );
00479                                                 $element->addChild( new PPNode_Hash_Attr( 'level', $count ) );
00480                                                 $element->addChild( new PPNode_Hash_Attr( 'i', $headingIndex++ ) );
00481                                                 $element->lastChild->nextSibling = $accum->firstNode;
00482                                                 $element->lastChild = $accum->lastNode;
00483                                         } else {
00484                                                 // Single equals sign on its own line, count=0
00485                                                 $element = $accum;
00486                                         }
00487                                 } else {
00488                                         // No match, no <h>, just pass down the inner text
00489                                         $element = $accum;
00490                                 }
00491                                 // Unwind the stack
00492                                 $stack->pop();
00493                                 $accum =& $stack->getAccum();
00494                                 extract( $stack->getFlags() );
00495 
00496                                 // Append the result to the enclosing accumulator
00497                                 if ( $element instanceof PPNode ) {
00498                                         $accum->addNode( $element );
00499                                 } else {
00500                                         $accum->addAccum( $element );
00501                                 }
00502                                 // Note that we do NOT increment the input pointer.
00503                                 // This is because the closing linebreak could be the opening linebreak of
00504                                 // another heading. Infinite loops are avoided because the next iteration MUST
00505                                 // hit the heading open case above, which unconditionally increments the
00506                                 // input pointer.
00507                         } elseif ( $found == 'open' ) {
00508                                 # count opening brace characters
00509                                 $count = strspn( $text, $curChar, $i );
00510 
00511                                 # we need to add to stack only if opening brace count is enough for one of the rules
00512                                 if ( $count >= $rule['min'] ) {
00513                                         # Add it to the stack
00514                                         $piece = array(
00515                                                 'open' => $curChar,
00516                                                 'close' => $rule['end'],
00517                                                 'count' => $count,
00518                                                 'lineStart' => ($i > 0 && $text[$i-1] == "\n"),
00519                                         );
00520 
00521                                         $stack->push( $piece );
00522                                         $accum =& $stack->getAccum();
00523                                         extract( $stack->getFlags() );
00524                                 } else {
00525                                         # Add literal brace(s)
00526                                         $accum->addLiteral( str_repeat( $curChar, $count ) );
00527                                 }
00528                                 $i += $count;
00529                         } elseif ( $found == 'close' ) {
00530                                 $piece = $stack->top;
00531                                 # lets check if there are enough characters for closing brace
00532                                 $maxCount = $piece->count;
00533                                 $count = strspn( $text, $curChar, $i, $maxCount );
00534 
00535                                 # check for maximum matching characters (if there are 5 closing
00536                                 # characters, we will probably need only 3 - depending on the rules)
00537                                 $rule = $rules[$piece->open];
00538                                 if ( $count > $rule['max'] ) {
00539                                         # The specified maximum exists in the callback array, unless the caller
00540                                         # has made an error
00541                                         $matchingCount = $rule['max'];
00542                                 } else {
00543                                         # Count is less than the maximum
00544                                         # Skip any gaps in the callback array to find the true largest match
00545                                         # Need to use array_key_exists not isset because the callback can be null
00546                                         $matchingCount = $count;
00547                                         while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
00548                                                 --$matchingCount;
00549                                         }
00550                                 }
00551 
00552                                 if ( $matchingCount <= 0 ) {
00553                                         # No matching element found in callback array
00554                                         # Output a literal closing brace and continue
00555                                         $accum->addLiteral( str_repeat( $curChar, $count ) );
00556                                         $i += $count;
00557                                         continue;
00558                                 }
00559                                 $name = $rule['names'][$matchingCount];
00560                                 if ( $name === null ) {
00561                                         // No element, just literal text
00562                                         $element = $piece->breakSyntax( $matchingCount );
00563                                         $element->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
00564                                 } else {
00565                                         # Create XML element
00566                                         # Note: $parts is already XML, does not need to be encoded further
00567                                         $parts = $piece->parts;
00568                                         $titleAccum = $parts[0]->out;
00569                                         unset( $parts[0] );
00570 
00571                                         $element = new PPNode_Hash_Tree( $name );
00572 
00573                                         # The invocation is at the start of the line if lineStart is set in
00574                                         # the stack, and all opening brackets are used up.
00575                                         if ( $maxCount == $matchingCount && !empty( $piece->lineStart ) ) {
00576                                                 $element->addChild( new PPNode_Hash_Attr( 'lineStart', 1 ) );
00577                                         }
00578                                         $titleNode = new PPNode_Hash_Tree( 'title' );
00579                                         $titleNode->firstChild = $titleAccum->firstNode;
00580                                         $titleNode->lastChild = $titleAccum->lastNode;
00581                                         $element->addChild( $titleNode );
00582                                         $argIndex = 1;
00583                                         foreach ( $parts as $part ) {
00584                                                 if ( isset( $part->eqpos ) ) {
00585                                                         // Find equals
00586                                                         $lastNode = false;
00587                                                         for ( $node = $part->out->firstNode; $node; $node = $node->nextSibling ) {
00588                                                                 if ( $node === $part->eqpos ) {
00589                                                                         break;
00590                                                                 }
00591                                                                 $lastNode = $node;
00592                                                         }
00593                                                         if ( !$node ) {
00594                                                                 throw new MWException( __METHOD__ . ': eqpos not found' );
00595                                                         }
00596                                                         if ( $node->name !== 'equals' ) {
00597                                                                 throw new MWException( __METHOD__ . ': eqpos is not equals' );
00598                                                         }
00599                                                         $equalsNode = $node;
00600 
00601                                                         // Construct name node
00602                                                         $nameNode = new PPNode_Hash_Tree( 'name' );
00603                                                         if ( $lastNode !== false ) {
00604                                                                 $lastNode->nextSibling = false;
00605                                                                 $nameNode->firstChild = $part->out->firstNode;
00606                                                                 $nameNode->lastChild = $lastNode;
00607                                                         }
00608 
00609                                                         // Construct value node
00610                                                         $valueNode = new PPNode_Hash_Tree( 'value' );
00611                                                         if ( $equalsNode->nextSibling !== false ) {
00612                                                                 $valueNode->firstChild = $equalsNode->nextSibling;
00613                                                                 $valueNode->lastChild = $part->out->lastNode;
00614                                                         }
00615                                                         $partNode = new PPNode_Hash_Tree( 'part' );
00616                                                         $partNode->addChild( $nameNode );
00617                                                         $partNode->addChild( $equalsNode->firstChild );
00618                                                         $partNode->addChild( $valueNode );
00619                                                         $element->addChild( $partNode );
00620                                                 } else {
00621                                                         $partNode = new PPNode_Hash_Tree( 'part' );
00622                                                         $nameNode = new PPNode_Hash_Tree( 'name' );
00623                                                         $nameNode->addChild( new PPNode_Hash_Attr( 'index', $argIndex++ ) );
00624                                                         $valueNode = new PPNode_Hash_Tree( 'value' );
00625                                                         $valueNode->firstChild = $part->out->firstNode;
00626                                                         $valueNode->lastChild = $part->out->lastNode;
00627                                                         $partNode->addChild( $nameNode );
00628                                                         $partNode->addChild( $valueNode );
00629                                                         $element->addChild( $partNode );
00630                                                 }
00631                                         }
00632                                 }
00633 
00634                                 # Advance input pointer
00635                                 $i += $matchingCount;
00636 
00637                                 # Unwind the stack
00638                                 $stack->pop();
00639                                 $accum =& $stack->getAccum();
00640 
00641                                 # Re-add the old stack element if it still has unmatched opening characters remaining
00642                                 if ( $matchingCount < $piece->count ) {
00643                                         $piece->parts = array( new PPDPart_Hash );
00644                                         $piece->count -= $matchingCount;
00645                                         # do we still qualify for any callback with remaining count?
00646                                         $min = $rules[$piece->open]['min'];
00647                                         if ( $piece->count >= $min ) {
00648                                                 $stack->push( $piece );
00649                                                 $accum =& $stack->getAccum();
00650                                         } else {
00651                                                 $accum->addLiteral( str_repeat( $piece->open, $piece->count ) );
00652                                         }
00653                                 }
00654 
00655                                 extract( $stack->getFlags() );
00656 
00657                                 # Add XML element to the enclosing accumulator
00658                                 if ( $element instanceof PPNode ) {
00659                                         $accum->addNode( $element );
00660                                 } else {
00661                                         $accum->addAccum( $element );
00662                                 }
00663                         } elseif ( $found == 'pipe' ) {
00664                                 $findEquals = true; // shortcut for getFlags()
00665                                 $stack->addPart();
00666                                 $accum =& $stack->getAccum();
00667                                 ++$i;
00668                         } elseif ( $found == 'equals' ) {
00669                                 $findEquals = false; // shortcut for getFlags()
00670                                 $accum->addNodeWithText( 'equals', '=' );
00671                                 $stack->getCurrentPart()->eqpos = $accum->lastNode;
00672                                 ++$i;
00673                         }
00674                 }
00675 
00676                 # Output any remaining unclosed brackets
00677                 foreach ( $stack->stack as $piece ) {
00678                         $stack->rootAccum->addAccum( $piece->breakSyntax() );
00679                 }
00680 
00681                 # Enable top-level headings
00682                 for ( $node = $stack->rootAccum->firstNode; $node; $node = $node->nextSibling ) {
00683                         if ( isset( $node->name ) && $node->name === 'possible-h' ) {
00684                                 $node->name = 'h';
00685                         }
00686                 }
00687 
00688                 $rootNode = new PPNode_Hash_Tree( 'root' );
00689                 $rootNode->firstChild = $stack->rootAccum->firstNode;
00690                 $rootNode->lastChild = $stack->rootAccum->lastNode;
00691 
00692                 // Cache
00693                 if ( $cacheable ) {
00694                         $cacheValue = sprintf( "%08d", self::CACHE_VERSION ) . serialize( $rootNode );
00695                         $wgMemc->set( $cacheKey, $cacheValue, 86400 );
00696                         wfProfileOut( __METHOD__ . '-cache-miss' );
00697                         wfProfileOut( __METHOD__ . '-cacheable' );
00698                         wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
00699                 }
00700 
00701                 wfProfileOut( __METHOD__ );
00702                 return $rootNode;
00703         }
00704 }
00705 
00710 class PPDStack_Hash extends PPDStack {
00711         function __construct() {
00712                 $this->elementClass = 'PPDStackElement_Hash';
00713                 parent::__construct();
00714                 $this->rootAccum = new PPDAccum_Hash;
00715         }
00716 }
00717 
00721 class PPDStackElement_Hash extends PPDStackElement {
00722         function __construct( $data = array() ) {
00723                 $this->partClass = 'PPDPart_Hash';
00724                 parent::__construct( $data );
00725         }
00726 
00732         function breakSyntax( $openingCount = false ) {
00733                 if ( $this->open == "\n" ) {
00734                         $accum = $this->parts[0]->out;
00735                 } else {
00736                         if ( $openingCount === false ) {
00737                                 $openingCount = $this->count;
00738                         }
00739                         $accum = new PPDAccum_Hash;
00740                         $accum->addLiteral( str_repeat( $this->open, $openingCount ) );
00741                         $first = true;
00742                         foreach ( $this->parts as $part ) {
00743                                 if ( $first ) {
00744                                         $first = false;
00745                                 } else {
00746                                         $accum->addLiteral( '|' );
00747                                 }
00748                                 $accum->addAccum( $part->out );
00749                         }
00750                 }
00751                 return $accum;
00752         }
00753 }
00754 
00758 class PPDPart_Hash extends PPDPart {
00759         function __construct( $out = '' ) {
00760                 $accum = new PPDAccum_Hash;
00761                 if ( $out !== '' ) {
00762                         $accum->addLiteral( $out );
00763                 }
00764                 parent::__construct( $accum );
00765         }
00766 }
00767 
00771 class PPDAccum_Hash {
00772         var $firstNode, $lastNode;
00773 
00774         function __construct() {
00775                 $this->firstNode = $this->lastNode = false;
00776         }
00777 
00781         function addLiteral( $s ) {
00782                 if ( $this->lastNode === false ) {
00783                         $this->firstNode = $this->lastNode = new PPNode_Hash_Text( $s );
00784                 } elseif ( $this->lastNode instanceof PPNode_Hash_Text ) {
00785                         $this->lastNode->value .= $s;
00786                 } else {
00787                         $this->lastNode->nextSibling = new PPNode_Hash_Text( $s );
00788                         $this->lastNode = $this->lastNode->nextSibling;
00789                 }
00790         }
00791 
00795         function addNode( PPNode $node ) {
00796                 if ( $this->lastNode === false ) {
00797                         $this->firstNode = $this->lastNode = $node;
00798                 } else {
00799                         $this->lastNode->nextSibling = $node;
00800                         $this->lastNode = $node;
00801                 }
00802         }
00803 
00807         function addNodeWithText( $name, $value ) {
00808                 $node = PPNode_Hash_Tree::newWithText( $name, $value );
00809                 $this->addNode( $node );
00810         }
00811 
00817         function addAccum( $accum ) {
00818                 if ( $accum->lastNode === false ) {
00819                         // nothing to add
00820                 } elseif ( $this->lastNode === false ) {
00821                         $this->firstNode = $accum->firstNode;
00822                         $this->lastNode = $accum->lastNode;
00823                 } else {
00824                         $this->lastNode->nextSibling = $accum->firstNode;
00825                         $this->lastNode = $accum->lastNode;
00826                 }
00827         }
00828 }
00829 
00834 class PPFrame_Hash implements PPFrame {
00835 
00839         var $parser;
00840 
00844         var $preprocessor;
00845 
00849         var $title;
00850         var $titleCache;
00851 
00856         var $loopCheckHash;
00857 
00862         var $depth;
00863 
00868         function __construct( $preprocessor ) {
00869                 $this->preprocessor = $preprocessor;
00870                 $this->parser = $preprocessor->parser;
00871                 $this->title = $this->parser->mTitle;
00872                 $this->titleCache = array( $this->title ? $this->title->getPrefixedDBkey() : false );
00873                 $this->loopCheckHash = array();
00874                 $this->depth = 0;
00875         }
00876 
00888         function newChild( $args = false, $title = false, $indexOffset = 0 ) {
00889                 $namedArgs = array();
00890                 $numberedArgs = array();
00891                 if ( $title === false ) {
00892                         $title = $this->title;
00893                 }
00894                 if ( $args !== false ) {
00895                         if ( $args instanceof PPNode_Hash_Array ) {
00896                                 $args = $args->value;
00897                         } elseif ( !is_array( $args ) ) {
00898                                 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
00899                         }
00900                         foreach ( $args as $arg ) {
00901                                 $bits = $arg->splitArg();
00902                                 if ( $bits['index'] !== '' ) {
00903                                         // Numbered parameter
00904                                         $index = $bits['index'] - $indexOffset;
00905                                         $numberedArgs[$index] = $bits['value'];
00906                                         unset( $namedArgs[$index] );
00907                                 } else {
00908                                         // Named parameter
00909                                         $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
00910                                         $namedArgs[$name] = $bits['value'];
00911                                         unset( $numberedArgs[$name] );
00912                                 }
00913                         }
00914                 }
00915                 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
00916         }
00917 
00924         function expand( $root, $flags = 0 ) {
00925                 static $expansionDepth = 0;
00926                 if ( is_string( $root ) ) {
00927                         return $root;
00928                 }
00929 
00930                 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
00931                         $this->parser->limitationWarn( 'node-count-exceeded',
00932                                         $this->parser->mPPNodeCount,
00933                                         $this->parser->mOptions->getMaxPPNodeCount()
00934                         );
00935                         return '<span class="error">Node-count limit exceeded</span>';
00936                 }
00937                 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
00938                         $this->parser->limitationWarn( 'expansion-depth-exceeded',
00939                                         $expansionDepth,
00940                                         $this->parser->mOptions->getMaxPPExpandDepth()
00941                         );
00942                         return '<span class="error">Expansion depth limit exceeded</span>';
00943                 }
00944                 ++$expansionDepth;
00945                 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
00946                         $this->parser->mHighestExpansionDepth = $expansionDepth;
00947                 }
00948 
00949                 $outStack = array( '', '' );
00950                 $iteratorStack = array( false, $root );
00951                 $indexStack = array( 0, 0 );
00952 
00953                 while ( count( $iteratorStack ) > 1 ) {
00954                         $level = count( $outStack ) - 1;
00955                         $iteratorNode =& $iteratorStack[ $level ];
00956                         $out =& $outStack[$level];
00957                         $index =& $indexStack[$level];
00958 
00959                         if ( is_array( $iteratorNode ) ) {
00960                                 if ( $index >= count( $iteratorNode ) ) {
00961                                         // All done with this iterator
00962                                         $iteratorStack[$level] = false;
00963                                         $contextNode = false;
00964                                 } else {
00965                                         $contextNode = $iteratorNode[$index];
00966                                         $index++;
00967                                 }
00968                         } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
00969                                 if ( $index >= $iteratorNode->getLength() ) {
00970                                         // All done with this iterator
00971                                         $iteratorStack[$level] = false;
00972                                         $contextNode = false;
00973                                 } else {
00974                                         $contextNode = $iteratorNode->item( $index );
00975                                         $index++;
00976                                 }
00977                         } else {
00978                                 // Copy to $contextNode and then delete from iterator stack,
00979                                 // because this is not an iterator but we do have to execute it once
00980                                 $contextNode = $iteratorStack[$level];
00981                                 $iteratorStack[$level] = false;
00982                         }
00983 
00984                         $newIterator = false;
00985 
00986                         if ( $contextNode === false ) {
00987                                 // nothing to do
00988                         } elseif ( is_string( $contextNode ) ) {
00989                                 $out .= $contextNode;
00990                         } elseif ( is_array( $contextNode ) || $contextNode instanceof PPNode_Hash_Array ) {
00991                                 $newIterator = $contextNode;
00992                         } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
00993                                 // No output
00994                         } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
00995                                 $out .= $contextNode->value;
00996                         } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
00997                                 if ( $contextNode->name == 'template' ) {
00998                                         # Double-brace expansion
00999                                         $bits = $contextNode->splitTemplate();
01000                                         if ( $flags & PPFrame::NO_TEMPLATES ) {
01001                                                 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $bits['title'], $bits['parts'] );
01002                                         } else {
01003                                                 $ret = $this->parser->braceSubstitution( $bits, $this );
01004                                                 if ( isset( $ret['object'] ) ) {
01005                                                         $newIterator = $ret['object'];
01006                                                 } else {
01007                                                         $out .= $ret['text'];
01008                                                 }
01009                                         }
01010                                 } elseif ( $contextNode->name == 'tplarg' ) {
01011                                         # Triple-brace expansion
01012                                         $bits = $contextNode->splitTemplate();
01013                                         if ( $flags & PPFrame::NO_ARGS ) {
01014                                                 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $bits['title'], $bits['parts'] );
01015                                         } else {
01016                                                 $ret = $this->parser->argSubstitution( $bits, $this );
01017                                                 if ( isset( $ret['object'] ) ) {
01018                                                         $newIterator = $ret['object'];
01019                                                 } else {
01020                                                         $out .= $ret['text'];
01021                                                 }
01022                                         }
01023                                 } elseif ( $contextNode->name == 'comment' ) {
01024                                         # HTML-style comment
01025                                         # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
01026                                         if ( $this->parser->ot['html']
01027                                                 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
01028                                                 || ( $flags & PPFrame::STRIP_COMMENTS ) )
01029                                         {
01030                                                 $out .= '';
01031                                         }
01032                                         # Add a strip marker in PST mode so that pstPass2() can run some old-fashioned regexes on the result
01033                                         # Not in RECOVER_COMMENTS mode (extractSections) though
01034                                         elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
01035                                                 $out .= $this->parser->insertStripItem( $contextNode->firstChild->value );
01036                                         }
01037                                         # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
01038                                         else {
01039                                                 $out .= $contextNode->firstChild->value;
01040                                         }
01041                                 } elseif ( $contextNode->name == 'ignore' ) {
01042                                         # Output suppression used by <includeonly> etc.
01043                                         # OT_WIKI will only respect <ignore> in substed templates.
01044                                         # The other output types respect it unless NO_IGNORE is set.
01045                                         # extractSections() sets NO_IGNORE and so never respects it.
01046                                         if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] ) || ( $flags & PPFrame::NO_IGNORE ) ) {
01047                                                 $out .= $contextNode->firstChild->value;
01048                                         } else {
01049                                                 //$out .= '';
01050                                         }
01051                                 } elseif ( $contextNode->name == 'ext' ) {
01052                                         # Extension tag
01053                                         $bits = $contextNode->splitExt() + array( 'attr' => null, 'inner' => null, 'close' => null );
01054                                         $out .= $this->parser->extensionSubstitution( $bits, $this );
01055                                 } elseif ( $contextNode->name == 'h' ) {
01056                                         # Heading
01057                                         if ( $this->parser->ot['html'] ) {
01058                                                 # Expand immediately and insert heading index marker
01059                                                 $s = '';
01060                                                 for ( $node = $contextNode->firstChild; $node; $node = $node->nextSibling ) {
01061                                                         $s .= $this->expand( $node, $flags );
01062                                                 }
01063 
01064                                                 $bits = $contextNode->splitHeading();
01065                                                 $titleText = $this->title->getPrefixedDBkey();
01066                                                 $this->parser->mHeadings[] = array( $titleText, $bits['i'] );
01067                                                 $serial = count( $this->parser->mHeadings ) - 1;
01068                                                 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser::MARKER_SUFFIX;
01069                                                 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
01070                                                 $this->parser->mStripState->addGeneral( $marker, '' );
01071                                                 $out .= $s;
01072                                         } else {
01073                                                 # Expand in virtual stack
01074                                                 $newIterator = $contextNode->getChildren();
01075                                         }
01076                                 } else {
01077                                         # Generic recursive expansion
01078                                         $newIterator = $contextNode->getChildren();
01079                                 }
01080                         } else {
01081                                 throw new MWException( __METHOD__ . ': Invalid parameter type' );
01082                         }
01083 
01084                         if ( $newIterator !== false ) {
01085                                 $outStack[] = '';
01086                                 $iteratorStack[] = $newIterator;
01087                                 $indexStack[] = 0;
01088                         } elseif ( $iteratorStack[$level] === false ) {
01089                                 // Return accumulated value to parent
01090                                 // With tail recursion
01091                                 while ( $iteratorStack[$level] === false && $level > 0 ) {
01092                                         $outStack[$level - 1] .= $out;
01093                                         array_pop( $outStack );
01094                                         array_pop( $iteratorStack );
01095                                         array_pop( $indexStack );
01096                                         $level--;
01097                                 }
01098                         }
01099                 }
01100                 --$expansionDepth;
01101                 return $outStack[0];
01102         }
01103 
01109         function implodeWithFlags( $sep, $flags /*, ... */ ) {
01110                 $args = array_slice( func_get_args(), 2 );
01111 
01112                 $first = true;
01113                 $s = '';
01114                 foreach ( $args as $root ) {
01115                         if ( $root instanceof PPNode_Hash_Array ) {
01116                                 $root = $root->value;
01117                         }
01118                         if ( !is_array( $root ) ) {
01119                                 $root = array( $root );
01120                         }
01121                         foreach ( $root as $node ) {
01122                                 if ( $first ) {
01123                                         $first = false;
01124                                 } else {
01125                                         $s .= $sep;
01126                                 }
01127                                 $s .= $this->expand( $node, $flags );
01128                         }
01129                 }
01130                 return $s;
01131         }
01132 
01138         function implode( $sep /*, ... */ ) {
01139                 $args = array_slice( func_get_args(), 1 );
01140 
01141                 $first = true;
01142                 $s = '';
01143                 foreach ( $args as $root ) {
01144                         if ( $root instanceof PPNode_Hash_Array ) {
01145                                 $root = $root->value;
01146                         }
01147                         if ( !is_array( $root ) ) {
01148                                 $root = array( $root );
01149                         }
01150                         foreach ( $root as $node ) {
01151                                 if ( $first ) {
01152                                         $first = false;
01153                                 } else {
01154                                         $s .= $sep;
01155                                 }
01156                                 $s .= $this->expand( $node );
01157                         }
01158                 }
01159                 return $s;
01160         }
01161 
01168         function virtualImplode( $sep /*, ... */ ) {
01169                 $args = array_slice( func_get_args(), 1 );
01170                 $out = array();
01171                 $first = true;
01172 
01173                 foreach ( $args as $root ) {
01174                         if ( $root instanceof PPNode_Hash_Array ) {
01175                                 $root = $root->value;
01176                         }
01177                         if ( !is_array( $root ) ) {
01178                                 $root = array( $root );
01179                         }
01180                         foreach ( $root as $node ) {
01181                                 if ( $first ) {
01182                                         $first = false;
01183                                 } else {
01184                                         $out[] = $sep;
01185                                 }
01186                                 $out[] = $node;
01187                         }
01188                 }
01189                 return new PPNode_Hash_Array( $out );
01190         }
01191 
01197         function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
01198                 $args = array_slice( func_get_args(), 3 );
01199                 $out = array( $start );
01200                 $first = true;
01201 
01202                 foreach ( $args as $root ) {
01203                         if ( $root instanceof PPNode_Hash_Array ) {
01204                                 $root = $root->value;
01205                         }
01206                         if ( !is_array( $root ) ) {
01207                                 $root = array( $root );
01208                         }
01209                         foreach ( $root as $node ) {
01210                                 if ( $first ) {
01211                                         $first = false;
01212                                 } else {
01213                                         $out[] = $sep;
01214                                 }
01215                                 $out[] = $node;
01216                         }
01217                 }
01218                 $out[] = $end;
01219                 return new PPNode_Hash_Array( $out );
01220         }
01221 
01222         function __toString() {
01223                 return 'frame{}';
01224         }
01225 
01230         function getPDBK( $level = false ) {
01231                 if ( $level === false ) {
01232                         return $this->title->getPrefixedDBkey();
01233                 } else {
01234                         return isset( $this->titleCache[$level] ) ? $this->titleCache[$level] : false;
01235                 }
01236         }
01237 
01241         function getArguments() {
01242                 return array();
01243         }
01244 
01248         function getNumberedArguments() {
01249                 return array();
01250         }
01251 
01255         function getNamedArguments() {
01256                 return array();
01257         }
01258 
01264         function isEmpty() {
01265                 return true;
01266         }
01267 
01272         function getArgument( $name ) {
01273                 return false;
01274         }
01275 
01283         function loopCheck( $title ) {
01284                 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
01285         }
01286 
01292         function isTemplate() {
01293                 return false;
01294         }
01295 
01301         function getTitle() {
01302                 return $this->title;
01303         }
01304 }
01305 
01310 class PPTemplateFrame_Hash extends PPFrame_Hash {
01311         var $numberedArgs, $namedArgs, $parent;
01312         var $numberedExpansionCache, $namedExpansionCache;
01313 
01321         function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
01322                 parent::__construct( $preprocessor );
01323 
01324                 $this->parent = $parent;
01325                 $this->numberedArgs = $numberedArgs;
01326                 $this->namedArgs = $namedArgs;
01327                 $this->title = $title;
01328                 $pdbk = $title ? $title->getPrefixedDBkey() : false;
01329                 $this->titleCache = $parent->titleCache;
01330                 $this->titleCache[] = $pdbk;
01331                 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
01332                 if ( $pdbk !== false ) {
01333                         $this->loopCheckHash[$pdbk] = true;
01334                 }
01335                 $this->depth = $parent->depth + 1;
01336                 $this->numberedExpansionCache = $this->namedExpansionCache = array();
01337         }
01338 
01339         function __toString() {
01340                 $s = 'tplframe{';
01341                 $first = true;
01342                 $args = $this->numberedArgs + $this->namedArgs;
01343                 foreach ( $args as $name => $value ) {
01344                         if ( $first ) {
01345                                 $first = false;
01346                         } else {
01347                                 $s .= ', ';
01348                         }
01349                         $s .= "\"$name\":\"" .
01350                                 str_replace( '"', '\\"', $value->__toString() ) . '"';
01351                 }
01352                 $s .= '}';
01353                 return $s;
01354         }
01360         function isEmpty() {
01361                 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
01362         }
01363 
01367         function getArguments() {
01368                 $arguments = array();
01369                 foreach ( array_merge(
01370                                 array_keys( $this->numberedArgs ),
01371                                 array_keys( $this->namedArgs ) ) as $key ) {
01372                         $arguments[$key] = $this->getArgument( $key );
01373                 }
01374                 return $arguments;
01375         }
01376 
01380         function getNumberedArguments() {
01381                 $arguments = array();
01382                 foreach ( array_keys( $this->numberedArgs ) as $key ) {
01383                         $arguments[$key] = $this->getArgument( $key );
01384                 }
01385                 return $arguments;
01386         }
01387 
01391         function getNamedArguments() {
01392                 $arguments = array();
01393                 foreach ( array_keys( $this->namedArgs ) as $key ) {
01394                         $arguments[$key] = $this->getArgument( $key );
01395                 }
01396                 return $arguments;
01397         }
01398 
01403         function getNumberedArgument( $index ) {
01404                 if ( !isset( $this->numberedArgs[$index] ) ) {
01405                         return false;
01406                 }
01407                 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
01408                         # No trimming for unnamed arguments
01409                         $this->numberedExpansionCache[$index] = $this->parent->expand( $this->numberedArgs[$index], PPFrame::STRIP_COMMENTS );
01410                 }
01411                 return $this->numberedExpansionCache[$index];
01412         }
01413 
01418         function getNamedArgument( $name ) {
01419                 if ( !isset( $this->namedArgs[$name] ) ) {
01420                         return false;
01421                 }
01422                 if ( !isset( $this->namedExpansionCache[$name] ) ) {
01423                         # Trim named arguments post-expand, for backwards compatibility
01424                         $this->namedExpansionCache[$name] = trim(
01425                                 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
01426                 }
01427                 return $this->namedExpansionCache[$name];
01428         }
01429 
01434         function getArgument( $name ) {
01435                 $text = $this->getNumberedArgument( $name );
01436                 if ( $text === false ) {
01437                         $text = $this->getNamedArgument( $name );
01438                 }
01439                 return $text;
01440         }
01441 
01447         function isTemplate() {
01448                 return true;
01449         }
01450 }
01451 
01456 class PPCustomFrame_Hash extends PPFrame_Hash {
01457         var $args;
01458 
01459         function __construct( $preprocessor, $args ) {
01460                 parent::__construct( $preprocessor );
01461                 $this->args = $args;
01462         }
01463 
01464         function __toString() {
01465                 $s = 'cstmframe{';
01466                 $first = true;
01467                 foreach ( $this->args as $name => $value ) {
01468                         if ( $first ) {
01469                                 $first = false;
01470                         } else {
01471                                 $s .= ', ';
01472                         }
01473                         $s .= "\"$name\":\"" .
01474                                 str_replace( '"', '\\"', $value->__toString() ) . '"';
01475                 }
01476                 $s .= '}';
01477                 return $s;
01478         }
01479 
01483         function isEmpty() {
01484                 return !count( $this->args );
01485         }
01486 
01491         function getArgument( $index ) {
01492                 if ( !isset( $this->args[$index] ) ) {
01493                         return false;
01494                 }
01495                 return $this->args[$index];
01496         }
01497 
01498         function getArguments() {
01499                 return $this->args;
01500         }
01501 }
01502 
01506 class PPNode_Hash_Tree implements PPNode {
01507         var $name, $firstChild, $lastChild, $nextSibling;
01508 
01509         function __construct( $name ) {
01510                 $this->name = $name;
01511                 $this->firstChild = $this->lastChild = $this->nextSibling = false;
01512         }
01513 
01514         function __toString() {
01515                 $inner = '';
01516                 $attribs = '';
01517                 for ( $node = $this->firstChild; $node; $node = $node->nextSibling ) {
01518                         if ( $node instanceof PPNode_Hash_Attr ) {
01519                                 $attribs .= ' ' . $node->name . '="' . htmlspecialchars( $node->value ) . '"';
01520                         } else {
01521                                 $inner .= $node->__toString();
01522                         }
01523                 }
01524                 if ( $inner === '' ) {
01525                         return "<{$this->name}$attribs/>";
01526                 } else {
01527                         return "<{$this->name}$attribs>$inner</{$this->name}>";
01528                 }
01529         }
01530 
01536         static function newWithText( $name, $text ) {
01537                 $obj = new self( $name );
01538                 $obj->addChild( new PPNode_Hash_Text( $text ) );
01539                 return $obj;
01540         }
01541 
01542         function addChild( $node ) {
01543                 if ( $this->lastChild === false ) {
01544                         $this->firstChild = $this->lastChild = $node;
01545                 } else {
01546                         $this->lastChild->nextSibling = $node;
01547                         $this->lastChild = $node;
01548                 }
01549         }
01550 
01554         function getChildren() {
01555                 $children = array();
01556                 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
01557                         $children[] = $child;
01558                 }
01559                 return new PPNode_Hash_Array( $children );
01560         }
01561 
01562         function getFirstChild() {
01563                 return $this->firstChild;
01564         }
01565 
01566         function getNextSibling() {
01567                 return $this->nextSibling;
01568         }
01569 
01570         function getChildrenOfType( $name ) {
01571                 $children = array();
01572                 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
01573                         if ( isset( $child->name ) && $child->name === $name ) {
01574                                 $children[] = $child;
01575                         }
01576                 }
01577                 return $children;
01578         }
01579 
01583         function getLength() {
01584                 return false;
01585         }
01586 
01591         function item( $i ) {
01592                 return false;
01593         }
01594 
01598         function getName() {
01599                 return $this->name;
01600         }
01601 
01611         function splitArg() {
01612                 $bits = array();
01613                 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
01614                         if ( !isset( $child->name ) ) {
01615                                 continue;
01616                         }
01617                         if ( $child->name === 'name' ) {
01618                                 $bits['name'] = $child;
01619                                 if ( $child->firstChild instanceof PPNode_Hash_Attr
01620                                         && $child->firstChild->name === 'index' )
01621                                 {
01622                                         $bits['index'] = $child->firstChild->value;
01623                                 }
01624                         } elseif ( $child->name === 'value' ) {
01625                                 $bits['value'] = $child;
01626                         }
01627                 }
01628 
01629                 if ( !isset( $bits['name'] ) ) {
01630                         throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
01631                 }
01632                 if ( !isset( $bits['index'] ) ) {
01633                         $bits['index'] = '';
01634                 }
01635                 return $bits;
01636         }
01637 
01645         function splitExt() {
01646                 $bits = array();
01647                 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
01648                         if ( !isset( $child->name ) ) {
01649                                 continue;
01650                         }
01651                         if ( $child->name == 'name' ) {
01652                                 $bits['name'] = $child;
01653                         } elseif ( $child->name == 'attr' ) {
01654                                 $bits['attr'] = $child;
01655                         } elseif ( $child->name == 'inner' ) {
01656                                 $bits['inner'] = $child;
01657                         } elseif ( $child->name == 'close' ) {
01658                                 $bits['close'] = $child;
01659                         }
01660                 }
01661                 if ( !isset( $bits['name'] ) ) {
01662                         throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
01663                 }
01664                 return $bits;
01665         }
01666 
01673         function splitHeading() {
01674                 if ( $this->name !== 'h' ) {
01675                         throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
01676                 }
01677                 $bits = array();
01678                 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
01679                         if ( !isset( $child->name ) ) {
01680                                 continue;
01681                         }
01682                         if ( $child->name == 'i' ) {
01683                                 $bits['i'] = $child->value;
01684                         } elseif ( $child->name == 'level' ) {
01685                                 $bits['level'] = $child->value;
01686                         }
01687                 }
01688                 if ( !isset( $bits['i'] ) ) {
01689                         throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
01690                 }
01691                 return $bits;
01692         }
01693 
01700         function splitTemplate() {
01701                 $parts = array();
01702                 $bits = array( 'lineStart' => '' );
01703                 for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
01704                         if ( !isset( $child->name ) ) {
01705                                 continue;
01706                         }
01707                         if ( $child->name == 'title' ) {
01708                                 $bits['title'] = $child;
01709                         }
01710                         if ( $child->name == 'part' ) {
01711                                 $parts[] = $child;
01712                         }
01713                         if ( $child->name == 'lineStart' ) {
01714                                 $bits['lineStart'] = '1';
01715                         }
01716                 }
01717                 if ( !isset( $bits['title'] ) ) {
01718                         throw new MWException( 'Invalid node passed to ' . __METHOD__ );
01719                 }
01720                 $bits['parts'] = new PPNode_Hash_Array( $parts );
01721                 return $bits;
01722         }
01723 }
01724 
01728 class PPNode_Hash_Text implements PPNode {
01729         var $value, $nextSibling;
01730 
01731         function __construct( $value ) {
01732                 if ( is_object( $value ) ) {
01733                         throw new MWException( __CLASS__ . ' given object instead of string' );
01734                 }
01735                 $this->value = $value;
01736         }
01737 
01738         function __toString() {
01739                 return htmlspecialchars( $this->value );
01740         }
01741 
01742         function getNextSibling() {
01743                 return $this->nextSibling;
01744         }
01745 
01746         function getChildren() { return false; }
01747         function getFirstChild() { return false; }
01748         function getChildrenOfType( $name ) { return false; }
01749         function getLength() { return false; }
01750         function item( $i ) { return false; }
01751         function getName() { return '#text'; }
01752         function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
01753         function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
01754         function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
01755 }
01756 
01760 class PPNode_Hash_Array implements PPNode {
01761         var $value, $nextSibling;
01762 
01763         function __construct( $value ) {
01764                 $this->value = $value;
01765         }
01766 
01767         function __toString() {
01768                 return var_export( $this, true );
01769         }
01770 
01771         function getLength() {
01772                 return count( $this->value );
01773         }
01774 
01775         function item( $i ) {
01776                 return $this->value[$i];
01777         }
01778 
01779         function getName() { return '#nodelist'; }
01780 
01781         function getNextSibling() {
01782                 return $this->nextSibling;
01783         }
01784 
01785         function getChildren() { return false; }
01786         function getFirstChild() { return false; }
01787         function getChildrenOfType( $name ) { return false; }
01788         function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
01789         function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
01790         function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
01791 }
01792 
01796 class PPNode_Hash_Attr implements PPNode {
01797         var $name, $value, $nextSibling;
01798 
01799         function __construct( $name, $value ) {
01800                 $this->name = $name;
01801                 $this->value = $value;
01802         }
01803 
01804         function __toString() {
01805                 return "<@{$this->name}>" . htmlspecialchars( $this->value ) . "</@{$this->name}>";
01806         }
01807 
01808         function getName() {
01809                 return $this->name;
01810         }
01811 
01812         function getNextSibling() {
01813                 return $this->nextSibling;
01814         }
01815 
01816         function getChildren() { return false; }
01817         function getFirstChild() { return false; }
01818         function getChildrenOfType( $name ) { return false; }
01819         function getLength() { return false; }
01820         function item( $i ) { return false; }
01821         function splitArg() { throw new MWException( __METHOD__ . ': not supported' ); }
01822         function splitExt() { throw new MWException( __METHOD__ . ': not supported' ); }
01823         function splitHeading() { throw new MWException( __METHOD__ . ': not supported' ); }
01824 }