Some more verbose output and fix infinite loop if you make the block size small :)
[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 ) {
12 echo "Nothing to do -- \$wgUseFileCache is disabled.\n";
13 exit(0);
14 }
15
16 $start = isset($args[0]) ? intval($args[0]) : 0;
17 echo "Building content page file cache from page {$start}!\n";
18
19 $dbr = wfGetDB( DB_SLAVE );
20 $start = $start > 0 ? $start : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
21 $end = $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
22 if( !$start ) {
23 die("Nothing to do.\n");
24 }
25
26 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
27 OutputPage::setEncodings(); # Not really used yet
28
29 $BATCH_SIZE = 100;
30 # Do remaining chunk
31 $end += $BATCH_SIZE - 1;
32 $blockStart = $start;
33 $blockEnd = $start + $BATCH_SIZE - 1;
34
35 // Go through each page and save the output
36 while( $blockEnd <= $end ) {
37 // Get the pages
38 $res = $dbr->select( 'page', array('page_namespace','page_title','page_id'),
39 array('page_namespace' => $wgContentNamespaces,
40 "page_id BETWEEN $blockStart AND $blockEnd" ),
41 array('ORDER BY' => 'page_id ASC','USE INDEX' => 'PRIMARY')
42 );
43 while( $row = $dbr->fetchObject( $res ) ) {
44 $wgTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
45 if( null == $wgTitle ) {
46 echo "Page {$row->page_id} bad title\n";
47 continue; // broken title?
48 }
49 $wgArticle = new Article( $wgTitle );
50 // If the article is cacheable, then load it
51 if( $wgArticle->isFileCacheable() ) {
52 $cache = new HTMLFileCache( $wgTitle );
53 if( $cache->isFileCacheGood() ) {
54 echo "Page {$row->page_id} already cached\n";
55 continue; // done already!
56 } else {
57 echo "Page {$row->page_id} not cached\n";
58 }
59 ob_start( array(&$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
60 $wgUseFileCache = false; // hack, we don't want $wgArticle fiddling with filecache
61 $wgArticle->view();
62 @$wgOut->output(); // header notices
63 $wgUseFileCache = true;
64 ob_end_clean(); // clear buffer
65 $wgOut = new OutputPage(); // empty out any output page garbage
66 echo "Cached page {$row->page_id}\n";
67 } else {
68 echo "Page {$row->page_id} not cacheable\n";
69 }
70 }
71 $blockStart += $BATCH_SIZE;
72 $blockEnd += $BATCH_SIZE;
73 wfWaitForSlaves( 5 );
74 }
75 echo "Done!\n";
76
77 // Remove these to be safe
78 if( isset($wgTitle) )
79 unset($wgTitle);
80 if( isset($wgArticle) )
81 unset($wgArticle);