* (bug 22852) "Served in" comment is now the time used to cache a single page when...
[lhc/web/wiklou.git] / maintenance / rebuildFileCache.php
1 <?php
2 /**
3 * Build file cache for content pages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname(__FILE__) . '/Maintenance.php' );
24
25 class RebuildFileCache extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Build file cache for content pages";
29 $this->addArg( 'start', 'Page_id to start from', true );
30 $this->addArg( 'overwrite', 'Refresh page cache', false );
31 $this->setBatchSize( 100 );
32 }
33
34 public function execute() {
35 global $wgUseFileCache, $wgDisableCounters, $wgContentNamespaces, $wgRequestTime;
36 global $wgTitle, $wgArticle, $wgOut, $wgUser;
37 if( !$wgUseFileCache ) {
38 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
39 }
40 $wgDisableCounters = false;
41 $start = $this->getArg( 0, "0" );
42 if( !ctype_digit($start) ) {
43 $this->error( "Invalid value for start parameter.", true );
44 }
45 $start = intval($start);
46 $overwrite = $this->hasArg(1) && $this->getArg(1) === 'overwrite';
47 $this->output( "Building content page file cache from page {$start}!\n" );
48
49 $dbr = wfGetDB( DB_SLAVE );
50 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
51 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
52 if( !$start ) {
53 $this->error( "Nothing to do.", true );
54 }
55
56 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
57 OutputPage::setEncodings(); # Not really used yet
58
59 # Do remaining chunk
60 $end += $this->mBatchSize - 1;
61 $blockStart = $start;
62 $blockEnd = $start + $this->mBatchSize - 1;
63
64 $dbw = wfGetDB( DB_MASTER );
65 // Go through each page and save the output
66 while( $blockEnd <= $end ) {
67 // Get the pages
68 $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
69 array('page_namespace' => $wgContentNamespaces,
70 "page_id BETWEEN $blockStart AND $blockEnd" ),
71 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
72 );
73 foreach( $res as $row ) {
74 $rebuilt = false;
75 $wgRequestTime = wfTime(); # bug 22852
76 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
77 if( null == $wgTitle ) {
78 $this->output( "Page {$row->page_id} has bad title\n" );
79 continue; // broken title?
80 }
81 $wgOut->setTitle( $wgTitle ); // set display title
82 $wgUser->getSkin( $wgTitle ); // set skin title
83 $wgArticle = new Article( $wgTitle );
84 // If the article is cacheable, then load it
85 if( $wgArticle->isFileCacheable() ) {
86 $cache = new HTMLFileCache( $wgTitle );
87 if( $cache->isFileCacheGood() ) {
88 if( $overwrite ) {
89 $rebuilt = true;
90 } else {
91 $this->output( "Page {$row->page_id} already cached\n" );
92 continue; // done already!
93 }
94 }
95 ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
96 $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
97 $wgArticle->view();
98 @$wgOut->output(); // header notices
99 $wgUseFileCache = true;
100 ob_end_clean(); // clear buffer
101 $wgOut = new OutputPage(); // empty out any output page garbage
102 if( $rebuilt )
103 $this->output( "Re-cached page {$row->page_id}\n" );
104 else
105 $this->output( "Cached page {$row->page_id}\n" );
106 } else {
107 $this->output( "Page {$row->page_id} not cacheable\n" );
108 }
109 $dbw->commit(); // commit any changes
110 }
111 $blockStart += $this->mBatchSize;
112 $blockEnd += $this->mBatchSize;
113 wfWaitForSlaves( 5 );
114 }
115 $this->output( "Done!\n" );
116
117 // Remove these to be safe
118 if( isset($wgTitle) )
119 unset($wgTitle);
120 if( isset($wgArticle) )
121 unset($wgArticle);
122 }
123 }
124
125 $maintClass = "RebuildFileCache";
126 require_once( DO_MAINTENANCE );