MediaWiki  1.23.8
rebuildFileCache.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
32  public function __construct() {
33  parent::__construct();
34  $this->mDescription = "Build file cache for content pages";
35  $this->addOption( 'start', 'Page_id to start from', false, true );
36  $this->addOption( 'end', 'Page_id to end on', false, true );
37  $this->addOption( 'overwrite', 'Refresh page cache' );
38  $this->setBatchSize( 100 );
39  }
40 
41  public function finalSetup() {
42  global $wgDebugToolbar;
43 
44  // Debug toolbar makes content uncacheable so we disable it.
45  // Has to be done before Setup.php initialize MWDebug
46  $wgDebugToolbar = false;
47  parent::finalSetup();
48  }
49 
50  public function execute() {
51  global $wgUseFileCache, $wgReadOnly, $wgContentNamespaces, $wgRequestTime;
52  global $wgOut;
53  if ( !$wgUseFileCache ) {
54  $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
55  }
56 
57  $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
58 
59  $start = $this->getOption( 'start', "0" );
60  if ( !ctype_digit( $start ) ) {
61  $this->error( "Invalid value for start parameter.", true );
62  }
63  $start = intval( $start );
64 
65  $end = $this->getOption( 'end', "0" );
66  if ( !ctype_digit( $end ) ) {
67  $this->error( "Invalid value for end parameter.", true );
68  }
69  $end = intval( $end );
70 
71  $this->output( "Building content page file cache from page {$start}!\n" );
72 
73  $dbr = wfGetDB( DB_SLAVE );
74  $overwrite = $this->getOption( 'overwrite', false );
75  $start = ( $start > 0 )
76  ? $start
77  : $dbr->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
78  $end = ( $end > 0 )
79  ? $end
80  : $dbr->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
81  if ( !$start ) {
82  $this->error( "Nothing to do.", true );
83  }
84 
85  $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
86 
87  # Do remaining chunk
88  $end += $this->mBatchSize - 1;
89  $blockStart = $start;
90  $blockEnd = $start + $this->mBatchSize - 1;
91 
92  $dbw = wfGetDB( DB_MASTER );
93  // Go through each page and save the output
94  while ( $blockEnd <= $end ) {
95  // Get the pages
96  $res = $dbr->select( 'page', array( 'page_namespace', 'page_title', 'page_id' ),
97  array( 'page_namespace' => $wgContentNamespaces,
98  "page_id BETWEEN $blockStart AND $blockEnd" ),
99  array( 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' )
100  );
101 
102  $dbw->begin( __METHOD__ ); // for any changes
103  foreach ( $res as $row ) {
104  $rebuilt = false;
105  $wgRequestTime = microtime( true ); # bug 22852
106 
107  $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
108  if ( null == $title ) {
109  $this->output( "Page {$row->page_id} has bad title\n" );
110  continue; // broken title?
111  }
112 
113  $context = new RequestContext;
114  $context->setTitle( $title );
115  $article = Article::newFromTitle( $title, $context );
116  $context->setWikiPage( $article->getPage() );
117 
118  $wgOut = $context->getOutput(); // set display title
119 
120  // If the article is cacheable, then load it
121  if ( $article->isFileCacheable() ) {
123  if ( $cache->isCacheGood() ) {
124  if ( $overwrite ) {
125  $rebuilt = true;
126  } else {
127  $this->output( "Page {$row->page_id} already cached\n" );
128  continue; // done already!
129  }
130  }
131  ob_start( array( &$cache, 'saveToFileCache' ) ); // save on ob_end_clean()
132  $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
133  $article->view();
134  wfSuppressWarnings(); // header notices
135  $wgOut->output();
137  $wgUseFileCache = true;
138  ob_end_clean(); // clear buffer
139  if ( $rebuilt ) {
140  $this->output( "Re-cached page {$row->page_id}\n" );
141  } else {
142  $this->output( "Cached page {$row->page_id}\n" );
143  }
144  } else {
145  $this->output( "Page {$row->page_id} not cacheable\n" );
146  }
147  }
148  $dbw->commit( __METHOD__ ); // commit any changes (just for sanity)
149 
150  $blockStart += $this->mBatchSize;
151  $blockEnd += $this->mBatchSize;
152  }
153  $this->output( "Done!\n" );
154  }
155 }
156 
157 $maintClass = "RebuildFileCache";
158 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:97
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3659
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2387
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
RebuildFileCache
Maintenance script that builds file cache for content pages.
Definition: rebuildFileCache.php:31
$dbr
$dbr
Definition: testCompression.php:48
RebuildFileCache\__construct
__construct()
Default constructor.
Definition: rebuildFileCache.php:32
RequestContext\setTitle
setTitle( $t)
Set the Title object.
Definition: RequestContext.php:116
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2417
$wgRequestTime
$wgRequestTime
Definition: WebStart.php:68
$wgOut
$wgOut
Definition: Setup.php:562
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:30
$maintClass
$maintClass
Definition: rebuildFileCache.php:157
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
HTMLFileCache\newFromTitle
static newFromTitle( $title, $action)
Construct an ObjectFileCache from a Title and an action.
Definition: HTMLFileCache.php:39
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
$cache
$cache
Definition: mcc.php:32
RebuildFileCache\execute
execute()
Do the actual work.
Definition: rebuildFileCache.php:50
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$article
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function array $article
Definition: hooks.txt:78
$res
$res
Definition: database.txt:21
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:254
Article\newFromTitle
static newFromTitle( $title, IContextSource $context)
Create an Article object of the appropriate class for the given page.
Definition: Article.php:142
RebuildFileCache\finalSetup
finalSetup()
Handle some last-minute setup here.
Definition: rebuildFileCache.php:41