r12193 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r12192‎ | r12193 | r12194 >
Date:12:01, 21 December 2005
Author:magnus_manske
Status:old
Tags:
Comment:
now caches template-safe stable wikisource
Modified paths:
  • /trunk/extensions/StableVersion/StableVersion.php (modified) (history)

Diff [purge]

Index: trunk/extensions/StableVersion/StableVersion.php
@@ -24,6 +24,8 @@
2525 define( 'SV_TYPE_STABLE_CANDIDATE', 2 );
2626 /**@-*/
2727
 28+# Evil variables
 29+$wgStableVersionCaching = false ;
2830
2931 $wgExtensionCredits['StableVersion'][] = array(
3032 'name' => 'Stable version',
@@ -36,6 +38,8 @@
3739 $wgHooks['ArticleViewHeader'][] = 'wfStableVersionHeaderHook' ;
3840 $wgHooks['ArticlePageDataBefore'][] = 'wfStableVersionArticlePageDataBeforeHook' ;
3941 $wgHooks['ArticlePageDataAfter'][] = 'wfStableVersionArticlePageDataAfterHook' ;
 42+$wgHooks['ParserBeforeInternalParse'][] = 'wfStableVersionParseBeforeInternalParseHook' ;
 43+$wgHooks['ArticleAfterFetchContent'][] = 'wfStableVersionArticleAfterFetchContentHook' ;
4044
4145 # BEGIN logging functions
4246 $wgHooks['LogPageValidTypes'][] = 'wfStableVersionAddLogType';
@@ -97,7 +101,7 @@
98102 * @param $article The article
99103 * @param $fields Query result object
100104 */
101 -function wfStableVersionArticlePageDataAfterHook ( &$article , $fields ) {
 105+function wfStableVersionArticlePageDataAfterHook ( &$article , &$fields ) {
102106 $dbr =& wfGetDB( DB_SLAVE );
103107 $fname = "wfStableVersionArticlePageDataAfterHook" ;
104108 $title = $article->getTitle() ;
@@ -117,11 +121,15 @@
118122 $article->mLastStable = 0 ;
119123 while ( $o = $dbr->fetchObject( $res ) ) {
120124 if ( $o->sv_type == SV_TYPE_STABLE ) {
121 - # Stable version
122 - if ( $o->sv_page_id == $title->getArticleID() )
 125+ if ( $o->sv_page_id == $title->getArticleID() ) {
 126+ # This is a stable version, set mark and get cache
123127 $article->mIsStable = true ;
124 - if ( $article->mLastStable == 0 )
 128+ $article->mStableCache = $o->sv_cache ;
 129+ }
 130+ if ( $article->mLastStable == 0 ) {
 131+ # The latest stable version
125132 $article->mLastStable = $o->sv_page_rev ;
 133+ }
126134 }
127135 }
128136 $dbr->freeResult( $res );
@@ -183,6 +191,32 @@
184192 return true ;
185193 }
186194
 195+
 196+/**
 197+* This is a parser hook that will terminate the parsing process after stripping
 198+*/
 199+function wfStableVersionParseBeforeInternalParseHook ( &$parser , &$text , &$x ) {
 200+ global $wgStableVersionTempText , $wgStableVersionTempX , $wgStableVersionCaching ;
 201+ if ( !$wgStableVersionCaching ) return true ; # Normal parsing, no caching
 202+
 203+ # Stop the parsing process
 204+ return false ;
 205+}
 206+
 207+/**
 208+*/
 209+function wfStableVersionArticleAfterFetchContentHook ( &$article , &$content ) {
 210+ if ( !isset ( $article->mIsStable ) ) return true ;
 211+ if ( !isset ( $article->mStableCache ) ) return true ;
 212+ if ( !$article->mIsStable ) return true ;
 213+
 214+ # This is a stable version and has a cache, so use that
 215+ $content = $article->mStableCache ;
 216+ return true ;
 217+}
 218+
 219+
 220+
187221 # The special page
188222 function wfStableVersion() {
189223 global $IP, $wgMessageCache;
@@ -200,8 +234,47 @@
201235 SpecialPage::SpecialPage( 'StableVersion' );
202236 $this->includable( true );
203237 }
 238+
 239+
 240+ function fixNoWiki( &$state ) {
 241+ if ( !is_array( $state ) ) {
 242+ return ;
 243+ }
204244
 245+ # Surround nowiki content with <nowiki> again
 246+ for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
 247+ $key = key( $state['nowiki'] ) ;
 248+ $state['nowiki'][$key] = "<nowiki>" . $content . "</nowiki>" ;
 249+ }
 250+
 251+ }
 252+
205253 /**
 254+ */
 255+ function getCacheText ( &$article ) {
 256+ global $wgStableVersionCaching , $wgUser ;
 257+ $title = $article->getTitle() ;
 258+ $article->loadContent ( true ) ; # FIXME: Do we need the "true" here? For what? Safe redirects??
 259+ $text = $article->mContent ;
 260+
 261+ $p = new Parser ;
 262+ $wgStableVersionCaching = true ;
 263+ $parserOptions = ParserOptions::newFromUser( $wgUser ); # Dummy
 264+
 265+ $text = $p->parse ( $text , $title , $parserOptions ) ;
 266+ $stripState = $p->mStripState ;
 267+ $wgStableVersionCaching = false ;
 268+ $text = $p->replaceVariables ( $text , $parse_options ) ;
 269+
 270+ $this->fixNoWiki ( $stripState ) ;
 271+ $p->mStripState = $stripState ;
 272+ $text = $p->unstrip( $text, $p->mStripState );
 273+ $text = $p->unstripNoWiki( $text, $p->mStripState );
 274+
 275+ return $text ;
 276+ }
 277+
 278+ /**
206279 * main()
207280 */
208281 function execute( $par = null ) {
@@ -229,19 +302,18 @@
230303 $act = wfMsg ( 'stableversion_reset_log' ) ;
231304 }
232305
233 - # Get old stable version
234 -/* $dbr =& wfGetDB( DB_SLAVE );
235 - $fname = "SpecialStableVersion:execute" ;
236 - $row = $dbr->selectRow( 'page', array( 'page_stable' ),
237 - array( 'page_id' => $id ), $fname );
238 - $oldstable = $row->page_stable ;
239 -*/
 306+ $article = new Article ( $t ) ;
 307+
 308+ # Old stable version
240309 $oldstable = isset ( $wgArticle->mLastStable ) ? $wgArticle->mLastStable : 0 ;
241310 if ( $oldstable == 0 ) $before = wfMsg ( 'stableversion_before_no' ) ;
242311 else $before = wfMsg ( 'stableversion_before_yes' , $oldstable ) ;
243312 $act .= " " . $before ;
244313
245 - $type = SV_TYPE_STABLE ;
 314+ $type = SV_TYPE_STABLE ; # FIXME: This should become something else once there are several "types"
 315+
 316+ # Get template-replaced cache
 317+ $cache = $this->getCacheText ( $article ) ;
246318
247319 $dbw =& wfGetDB( DB_MASTER );
248320 $dbw->begin () ;
@@ -256,7 +328,7 @@
257329 'sv_type' => $type,
258330 'sv_user' => $wgUser->getID(),
259331 'sv_date' => "12345678123456" ,
260 - 'sv_cache' => "",
 332+ 'sv_cache' => $cache,
261333 ) ;
262334 $dbw->insert( 'stableversions',
263335 $values ,

Status & tagging log