Add a script to filecache content pages on the wiki
[lhc/web/wiklou.git] / maintenance / rebuildFileCache.php
1 <?php
2 /**
3 * Build file cache for content pages
4 *
5 * @file
6 * @ingroup Maintenance
7 */
8
9 /** */
10 require_once( "commandLine.inc" );
11 if( !$wgUseFileCache ) exit(0);
12
13 $start = isset($args[0]) ? intval($args[0]) : 0;
14 echo "Building content page file cache from page {$start}!\n";
15
16 $dbr = wfGetDB( DB_SLAVE );
17 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
18 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
19 if( !$start ) {
20 die("Nothing to do.\n");
21 }
22
23 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
24 OutputPage::setEncodings(); # Not really used yet
25
26 $BATCH_SIZE = 100;
27 # Do remaining chunk
28 $end += $BATCH_SIZE - 1;
29 $blockStart = $start;
30 $blockEnd = $start + $BATCH_SIZE - 1;
31
32 // Go through each page and save the output
33 while( $blockEnd <= $end ) {
34 // Get the pages
35 $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
36 array('page_namespace' => $wgContentNamespaces,
37 "page_id BETWEEN $blockStart AND $blockEnd" ),
38 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
39 );
40 while( $row = $dbr->fetchObject( $res ) ) {
41 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
42 if( null == $wgTitle ) continue; // broken title?
43 $wgArticle = new Article( $wgTitle );
44 // If the article is cacheable, then load it
45 if( $wgArticle->isFileCacheable() ) {
46 $cache = new HTMLFileCache( $wgTitle );
47 if( $cache->isFileCacheGood() ) {
48 echo "Page {$row->page_id} already cached\n";
49 continue; // done already!
50 }
51 ob_start( array(&$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
52 $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
53 $wgArticle->view();
54 @$wgOut->output(); // header notices
55 $wgUseFileCache = true;
56 ob_end_clean(); // clear buffer
57 $wgOut = new OutputPage(); // empty out any output page garbage
58 echo "Cached page {$row->page_id}\n";
59 }
60 }
61 $blockStart += $BATCH_SIZE - 1;
62 $blockEnd += $BATCH_SIZE - 1;
63 wfWaitForSlaves( 5 );
64 }
65 echo "Done!\n";
66
67 // Remove these to be safe
68 if( isset($wgTitle) )
69 unset($wgTitle);
70 if( isset($wgArticle) )
71 unset($wgArticle);