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