From 73965b1242d9244b108f566725b18871ae059c6e Mon Sep 17 00:00:00 2001 From: Andrew Garrett Date: Tue, 3 Feb 2009 04:58:08 +0000 Subject: [PATCH] Re-implementation of r46725 (caching of Cite output). This time, I've written a Parser method called serialiseHalfParsedText, which, as the name implies, grabs some half-parsed text, and fixes up all of the strip markers, and link comments, and makes it safe to import some other time with unserialiseHalfParsedText. I tested it by live-hacking the cache key to be a constant, and then putting on a completely different page, where it worked perfectly. --- includes/parser/Parser.php | 96 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 59e0b86a43..da41348ae2 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -4764,6 +4764,102 @@ class Parser } return $out; } + + function serialiseHalfParsedText( $text ) { + $data = array(); + $data['text'] = $text; + + // First, find all strip markers, and store their + // data in an array. + $stripState = new StripState; + $pos = 0; + while( ( $start_pos = strpos( $text, $this->mUniqPrefix, $pos ) ) && ( $end_pos = strpos( $text, self::MARKER_SUFFIX, $pos ) ) ) { + $end_pos += strlen( self::MARKER_SUFFIX ); + $marker = substr( $text, $start_pos, $end_pos-$start_pos ); + + if ( !empty( $this->mStripState->general->data[$marker] ) ) { + $replaceArray = $stripState->general; + $stripText = $this->mStripState->general->data[$marker]; + } elseif ( !empty( $this->mStripState->nowiki->data[$marker] ) ) { + $replaceArray = $stripState->nowiki; + $stripText = $this->mStripState->nowiki->data[$marker]; + } else { + throw new MWException( "Hanging strip marker: '$marker'." ); + } + + $replaceArray->setPair( $marker, $stripText ); + $pos = $end_pos; + } + $data['stripstate'] = $stripState; + + // Now, find all of our links, and store THEIR + // data in an array! :) + $links = array( 'internal' => array(), 'interwiki' => array() ); + $pos = 0; + + // Internal links + while( ( $start_pos = strpos( $text, '' ) ) ); + $links['internal'][$ns][] = $this->mLinkHolders->internals[$ns][$key]; + $pos = $start_pos + strlen( "" ); + } + + $pos = 0; + + // Interwiki links + while( ( $start_pos = strpos( $text, '' ) ) ); + $links['interwiki'][] = $this->mLinkHolders->interwiki[$key]; + $pos = $start_pos + strlen( "" ); + } + + $data['linkholder'] = $links; + + return $data; + } + + function unserialiseHalfParsedText( $data, $intPrefix = null /* Unique identifying prefix */ ) { + if (!$intPrefix) + $intPrefix = $this->getRandomString(); + + // First, extract the strip state. + $stripState = $data['stripstate']; + $this->mStripState->general->merge( $stripState->general ); + $this->mStripState->nowiki->merge( $stripState->nowiki ); + + // Now, extract the text, and renumber links + $text = $data['text']; + $links = $data['linkholder']; + + // Internal... + foreach( $links['internal'] as $ns => $nsLinks ) { + foreach( $nsLinks as $key => $entry ) { + $newKey = $intPrefix . '-' . $key; + $this->mLinkHolders->internals[$ns][$newKey] = $entry; + + $text = str_replace( "", "", $text ); + } + } + + // Interwiki... + foreach( $links['interwiki'] as $key => $entry ) { + $newKey = "$intPrefix-$key"; + $this->mLinkHolders->interwikis[$newKey] = $entry; + + $text = str_replace( "", "", $text ); + } + + // Should be good to go. + return $text; + } } /** -- 2.20.1