Merge maintenance-work branch:
[lhc/web/wiklou.git] / maintenance / rebuildFileCache.php
1 <?php
2 /**
3 * Build file cache for content pages
4 *
5 * @file
6 * @ingroup Maintenance
7 */
8
9 require_once( "Maintenance.php" );
10
11 class RebuildFileCache extends Maintenance {
12 public function __construct() {
13 parent::__construct();
14 $this->mDescription = "Build file cache for content pages";
15 $this->addArgs( array( 'start', 'overwrite' ) );
16 }
17
18 public function execute() {
19 global $wgUseFileCache, $wgDisableCounters, $wgTitle, $wgArticle, $wgOut;
20 if( !$wgUseFileCache ) {
21 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.\n", true );
22 }
23 $wgDisableCounters = false;
24 $start = intval( $this->getArg( 0, 0 ) );
25 $overwrite = $this->hasArg(1) && $this->getArg(1) === 'overwrite';
26 $this->output( "Building content page file cache from page {$start}!\n" );
27
28 $dbr = wfGetDB( DB_SLAVE );
29 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
30 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
31 if( !$start ) {
32 $this->error( "Nothing to do.\n", true );
33 }
34
35 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
36 OutputPage::setEncodings(); # Not really used yet
37
38 $BATCH_SIZE = 100;
39 # Do remaining chunk
40 $end += $BATCH_SIZE - 1;
41 $blockStart = $start;
42 $blockEnd = $start + $BATCH_SIZE - 1;
43
44 $dbw = wfGetDB( DB_MASTER );
45 // Go through each page and save the output
46 while( $blockEnd <= $end ) {
47 // Get the pages
48 $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
49 array('page_namespace' => $wgContentNamespaces,
50 "page_id BETWEEN $blockStart AND $blockEnd" ),
51 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
52 );
53 while( $row = $dbr->fetchObject( $res ) ) {
54 $rebuilt = false;
55 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
56 if( null == $wgTitle ) {
57 $this->output( "Page {$row->page_id} bad title\n" );
58 continue; // broken title?
59 }
60 $wgArticle = new Article( $wgTitle );
61 // If the article is cacheable, then load it
62 if( $wgArticle->isFileCacheable() ) {
63 $cache = new HTMLFileCache( $wgTitle );
64 if( $cache->isFileCacheGood() ) {
65 if( $overwrite ) {
66 $rebuilt = true;
67 } else {
68 $this->output( "Page {$row->page_id} already cached\n" );
69 continue; // done already!
70 }
71 }
72 ob_start( array(&$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
73 $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
74 $wgArticle->view();
75 @$wgOut->output(); // header notices
76 $wgUseFileCache = true;
77 ob_end_clean(); // clear buffer
78 $wgOut = new OutputPage(); // empty out any output page garbage
79 if( $rebuilt )
80 $this->output( "Re-cached page {$row->page_id}\n" );
81 else
82 $this->output( "Cached page {$row->page_id}\n" );
83 } else {
84 $this->output( "Page {$row->page_id} not cacheable\n" );
85 }
86 $dbw->commit(); // commit any changes
87 }
88 $blockStart += $BATCH_SIZE;
89 $blockEnd += $BATCH_SIZE;
90 wfWaitForSlaves( 5 );
91 }
92 $this->output( "Done!\n" );
93
94 // Remove these to be safe
95 if( isset($wgTitle) )
96 unset($wgTitle);
97 if( isset($wgArticle) )
98 unset($wgArticle);
99 }
100 }
101 require_once( "commandLine.inc" );