Allow cleanupSpam.php optionally delete offending pages
[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->addOption( 'start', 'Page_id to start from', false, true );
30 $this->addOption( 'end', 'Page_id to end on', false, true );
31 $this->addOption( 'overwrite', 'Refresh page cache' );
32 $this->setBatchSize( 100 );
33 }
34
35 public function finalSetup() {
36 global $wgDebugToolbar;
37
38 // Debug toolbar makes content uncacheable so we disable it.
39 // Has to be done before Setup.php initialize MWDebug
40 $wgDebugToolbar = false;
41 parent::finalSetup();
42 }
43
44 public function execute() {
45 global $wgUseFileCache, $wgReadOnly, $wgContentNamespaces, $wgRequestTime;
46 global $wgTitle, $wgOut;
47 if ( !$wgUseFileCache ) {
48 $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
49 }
50
51 $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
52
53 $start = $this->getOption( 'start', "0" );
54 if ( !ctype_digit( $start ) ) {
55 $this->error( "Invalid value for start parameter.", true );
56 }
57 $start = intval( $start );
58
59 $end = $this->getOption( 'end', "0" );
60 if ( !ctype_digit( $end ) ) {
61 $this->error( "Invalid value for end parameter.", true );
62 }
63 $end = intval( $end );
64
65 $this->output( "Building content page file cache from page {$start}!\n" );
66
67 $dbr = wfGetDB( DB_SLAVE );
68 $overwrite = $this->getOption( 'overwrite', false );
69 $start = ( $start > 0 )
70 ? $start
71 : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
72 $end = ( $end > 0 )
73 ? $end
74 : $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
75 if ( !$start ) {
76 $this->error( "Nothing to do.", true );
77 }
78
79 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
80
81 # Do remaining chunk
82 $end += $this->mBatchSize - 1;
83 $blockStart = $start;
84 $blockEnd = $start + $this->mBatchSize - 1;
85
86 $dbw = wfGetDB( DB_MASTER );
87 // Go through each page and save the output
88 while ( $blockEnd <= $end ) {
89 // Get the pages
90 $res = $dbr->select( 'page', array( 'page_namespace', 'page_title', 'page_id' ),
91 array( 'page_namespace' => $wgContentNamespaces,
92 "page_id BETWEEN $blockStart AND $blockEnd" ),
93 array( 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' )
94 );
95
96 $dbw->begin( __METHOD__ ); // for any changes
97 foreach ( $res as $row ) {
98 $rebuilt = false;
99 $wgRequestTime = microtime( true ); # bug 22852
100
101 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
102 if ( null == $wgTitle ) {
103 $this->output( "Page {$row->page_id} has bad title\n" );
104 continue; // broken title?
105 }
106
107 $context = new RequestContext;
108 $context->setTitle( $wgTitle );
109 $article = Article::newFromTitle( $wgTitle, $context );
110 $context->setWikiPage( $article->getPage() );
111
112 $wgOut = $context->getOutput(); // set display title
113
114 // If the article is cacheable, then load it
115 if ( $article->isFileCacheable() ) {
116 $cache = HTMLFileCache::newFromTitle( $wgTitle, 'view' );
117 if ( $cache->isCacheGood() ) {
118 if ( $overwrite ) {
119 $rebuilt = true;
120 } else {
121 $this->output( "Page {$row->page_id} already cached\n" );
122 continue; // done already!
123 }
124 }
125 ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
126 $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
127 $article->view();
128 wfSuppressWarnings(); // header notices
129 $wgOut->output();
130 wfRestoreWarnings();
131 $wgUseFileCache = true;
132 ob_end_clean(); // clear buffer
133 if ( $rebuilt ) {
134 $this->output( "Re-cached page {$row->page_id}\n" );
135 } else {
136 $this->output( "Cached page {$row->page_id}\n" );
137 }
138 } else {
139 $this->output( "Page {$row->page_id} not cacheable\n" );
140 }
141 }
142 $dbw->commit( __METHOD__ ); // commit any changes (just for sanity)
143
144 $blockStart += $this->mBatchSize;
145 $blockEnd += $this->mBatchSize;
146 }
147 $this->output( "Done!\n" );
148
149 // Remove these to be safe
150 if ( isset( $wgTitle ) )
151 unset( $wgTitle );
152 }
153 }
154
155 $maintClass = "RebuildFileCache";
156 require_once( RUN_MAINTENANCE_IF_MAIN );