Index: trunk/extensions/StableVersion/StableVersion.php |
— | — | @@ -24,6 +24,8 @@ |
25 | 25 | define( 'SV_TYPE_STABLE_CANDIDATE', 2 ); |
26 | 26 | /**@-*/ |
27 | 27 | |
| 28 | +# Evil variables |
| 29 | +$wgStableVersionCaching = false ; |
28 | 30 | |
29 | 31 | $wgExtensionCredits['StableVersion'][] = array( |
30 | 32 | 'name' => 'Stable version', |
— | — | @@ -36,6 +38,8 @@ |
37 | 39 | $wgHooks['ArticleViewHeader'][] = 'wfStableVersionHeaderHook' ; |
38 | 40 | $wgHooks['ArticlePageDataBefore'][] = 'wfStableVersionArticlePageDataBeforeHook' ; |
39 | 41 | $wgHooks['ArticlePageDataAfter'][] = 'wfStableVersionArticlePageDataAfterHook' ; |
| 42 | +$wgHooks['ParserBeforeInternalParse'][] = 'wfStableVersionParseBeforeInternalParseHook' ; |
| 43 | +$wgHooks['ArticleAfterFetchContent'][] = 'wfStableVersionArticleAfterFetchContentHook' ; |
40 | 44 | |
41 | 45 | # BEGIN logging functions |
42 | 46 | $wgHooks['LogPageValidTypes'][] = 'wfStableVersionAddLogType'; |
— | — | @@ -97,7 +101,7 @@ |
98 | 102 | * @param $article The article |
99 | 103 | * @param $fields Query result object |
100 | 104 | */ |
101 | | -function wfStableVersionArticlePageDataAfterHook ( &$article , $fields ) { |
| 105 | +function wfStableVersionArticlePageDataAfterHook ( &$article , &$fields ) { |
102 | 106 | $dbr =& wfGetDB( DB_SLAVE ); |
103 | 107 | $fname = "wfStableVersionArticlePageDataAfterHook" ; |
104 | 108 | $title = $article->getTitle() ; |
— | — | @@ -117,11 +121,15 @@ |
118 | 122 | $article->mLastStable = 0 ; |
119 | 123 | while ( $o = $dbr->fetchObject( $res ) ) { |
120 | 124 | 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 |
123 | 127 | $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 |
125 | 132 | $article->mLastStable = $o->sv_page_rev ; |
| 133 | + } |
126 | 134 | } |
127 | 135 | } |
128 | 136 | $dbr->freeResult( $res ); |
— | — | @@ -183,6 +191,32 @@ |
184 | 192 | return true ; |
185 | 193 | } |
186 | 194 | |
| 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 | + |
187 | 221 | # The special page |
188 | 222 | function wfStableVersion() { |
189 | 223 | global $IP, $wgMessageCache; |
— | — | @@ -200,8 +234,47 @@ |
201 | 235 | SpecialPage::SpecialPage( 'StableVersion' ); |
202 | 236 | $this->includable( true ); |
203 | 237 | } |
| 238 | + |
| 239 | + |
| 240 | + function fixNoWiki( &$state ) { |
| 241 | + if ( !is_array( $state ) ) { |
| 242 | + return ; |
| 243 | + } |
204 | 244 | |
| 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 | + |
205 | 253 | /** |
| 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 | + /** |
206 | 279 | * main() |
207 | 280 | */ |
208 | 281 | function execute( $par = null ) { |
— | — | @@ -229,19 +302,18 @@ |
230 | 303 | $act = wfMsg ( 'stableversion_reset_log' ) ; |
231 | 304 | } |
232 | 305 | |
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 |
240 | 309 | $oldstable = isset ( $wgArticle->mLastStable ) ? $wgArticle->mLastStable : 0 ; |
241 | 310 | if ( $oldstable == 0 ) $before = wfMsg ( 'stableversion_before_no' ) ; |
242 | 311 | else $before = wfMsg ( 'stableversion_before_yes' , $oldstable ) ; |
243 | 312 | $act .= " " . $before ; |
244 | 313 | |
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 ) ; |
246 | 318 | |
247 | 319 | $dbw =& wfGetDB( DB_MASTER ); |
248 | 320 | $dbw->begin () ; |
— | — | @@ -256,7 +328,7 @@ |
257 | 329 | 'sv_type' => $type, |
258 | 330 | 'sv_user' => $wgUser->getID(), |
259 | 331 | 'sv_date' => "12345678123456" , |
260 | | - 'sv_cache' => "", |
| 332 | + 'sv_cache' => $cache, |
261 | 333 | ) ; |
262 | 334 | $dbw->insert( 'stableversions', |
263 | 335 | $values , |