Argument cleanup
[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->addArgs( array( 'start' ) );
30 $this->addOption( 'overwrite', 'Refresh page cache', false );
31 $this->setBatchSize( 100 );
32 }
33
34 public function execute() {
35 global $wgUseFileCache, $wgContentNamespaces, $wgDisableCounters, $wgTitle, $wgArticle, $wgOut;
36 if( !$wgUseFileCache ) {
37 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
38 }
39 $wgDisableCounters = false;
40 $start = $this->getArg( 0, 0 );
41 if( !ctype_digit($start) ) {
42 $this->error( "Invalid value for start parameter.", true );
43 }
44 $start = intval($start);
45 $overwrite = $this->hasArg(1) && $this->getArg(1) === 'overwrite';
46 $this->output( "Building content page file cache from page {$start}!\n" );
47
48 $dbr = wfGetDB( DB_SLAVE );
49 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
50 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
51 if( !$start ) {
52 $this->error( "Nothing to do.", true );
53 }
54
55 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
56 OutputPage::setEncodings(); # Not really used yet
57
58 # Do remaining chunk
59 $end += $this->mBatchSize - 1;
60 $blockStart = $start;
61 $blockEnd = $start + $this->mBatchSize - 1;
62
63 $dbw = wfGetDB( DB_MASTER );
64 // Go through each page and save the output
65 while( $blockEnd <= $end ) {
66 // Get the pages
67 $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
68 array('page_namespace' => $wgContentNamespaces,
69 "page_id BETWEEN $blockStart AND $blockEnd" ),
70 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
71 );
72 while( $row = $dbr->fetchObject( $res ) ) {
73 $rebuilt = false;
74 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
75 if( null == $wgTitle ) {
76 $this->output( "Page {$row->page_id} bad title\n" );
77 continue; // broken title?
78 }
79 $wgArticle = new Article( $wgTitle );
80 // If the article is cacheable, then load it
81 if( $wgArticle->isFileCacheable() ) {
82 $cache = new HTMLFileCache( $wgTitle );
83 if( $cache->isFileCacheGood() ) {
84 if( $overwrite ) {
85 $rebuilt = true;
86 } else {
87 $this->output( "Page {$row->page_id} already cached\n" );
88 continue; // done already!
89 }
90 }
91 ob_start( array(&$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
92 $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
93 $wgArticle->view();
94 @$wgOut->output(); // header notices
95 $wgUseFileCache = true;
96 ob_end_clean(); // clear buffer
97 $wgOut = new OutputPage(); // empty out any output page garbage
98 if( $rebuilt )
99 $this->output( "Re-cached page {$row->page_id}\n" );
100 else
101 $this->output( "Cached page {$row->page_id}\n" );
102 } else {
103 $this->output( "Page {$row->page_id} not cacheable\n" );
104 }
105 $dbw->commit(); // commit any changes
106 }
107 $blockStart += $this->mBatchSize;
108 $blockEnd += $this->mBatchSize;
109 wfWaitForSlaves( 5 );
110 }
111 $this->output( "Done!\n" );
112
113 // Remove these to be safe
114 if( isset($wgTitle) )
115 unset($wgTitle);
116 if( isset($wgArticle) )
117 unset($wgArticle);
118 }
119 }
120
121 $maintClass = "RebuildFileCache";
122 require_once( DO_MAINTENANCE );