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