split getHooksFromDoc() function
[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 = wfTime(); # bug 22852
90 $context = new RequestContext;
91 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
92 $context->setTitle( $wgTitle );
93 if ( null == $wgTitle ) {
94 $this->output( "Page {$row->page_id} has bad title\n" );
95 continue; // broken title?
96 }
97 $wgOut = $context->getOutput(); // set display title
98 $article = new Article( $wgTitle );
99 // If the article is cacheable, then load it
100 if ( $article->isFileCacheable() ) {
101 $cache = HTMLFileCache::newFromTitle( $wgTitle, 'view' );
102 if ( $cache->isCacheGood() ) {
103 if ( $overwrite ) {
104 $rebuilt = true;
105 } else {
106 $this->output( "Page {$row->page_id} already cached\n" );
107 continue; // done already!
108 }
109 }
110 ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
111 $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
112 $article->view();
113 wfSuppressWarnings(); // header notices
114 $wgOut->output();
115 wfRestoreWarnings();
116 $wgUseFileCache = true;
117 ob_end_clean(); // clear buffer
118 if ( $rebuilt ) {
119 $this->output( "Re-cached page {$row->page_id}\n" );
120 } else {
121 $this->output( "Cached page {$row->page_id}\n" );
122 }
123 } else {
124 $this->output( "Page {$row->page_id} not cacheable\n" );
125 }
126 }
127 $dbw->commit(); // commit any changes (just for sanity)
128
129 $blockStart += $this->mBatchSize;
130 $blockEnd += $this->mBatchSize;
131 }
132 $this->output( "Done!\n" );
133
134 // Remove these to be safe
135 if ( isset( $wgTitle ) )
136 unset( $wgTitle );
137 }
138 }
139
140 $maintClass = "RebuildFileCache";
141 require_once( RUN_MAINTENANCE_IF_MAIN );