MediaWiki  1.33.0
rebuildFileCache.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
32  private $enabled = true;
33 
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( 'Build file cache for content pages' );
37  $this->addOption( 'start', 'Page_id to start from', false, true );
38  $this->addOption( 'end', 'Page_id to end on', false, true );
39  $this->addOption( 'overwrite', 'Refresh page cache' );
40  $this->setBatchSize( 100 );
41  }
42 
43  public function finalSetup() {
45 
46  $this->enabled = $wgUseFileCache;
47  // Script will handle capturing output and saving it itself
48  $wgUseFileCache = false;
49  // Debug toolbar makes content uncacheable so we disable it.
50  // Has to be done before Setup.php initialize MWDebug
51  $wgDebugToolbar = false;
52  // Avoid DB writes (like enotif/counters)
53  MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
54  ->setReason( 'Building cache' );
55 
56  parent::finalSetup();
57  }
58 
59  public function execute() {
60  if ( !$this->enabled ) {
61  $this->fatalError( "Nothing to do -- \$wgUseFileCache is disabled." );
62  }
63 
64  $start = $this->getOption( 'start', "0" );
65  if ( !ctype_digit( $start ) ) {
66  $this->fatalError( "Invalid value for start parameter." );
67  }
68  $start = intval( $start );
69 
70  $end = $this->getOption( 'end', "0" );
71  if ( !ctype_digit( $end ) ) {
72  $this->fatalError( "Invalid value for end parameter." );
73  }
74  $end = intval( $end );
75 
76  $this->output( "Building content page file cache from page {$start}!\n" );
77 
78  $dbr = $this->getDB( DB_REPLICA );
79  $batchSize = $this->getBatchSize();
80  $overwrite = $this->hasOption( 'overwrite' );
81  $start = ( $start > 0 )
82  ? $start
83  : $dbr->selectField( 'page', 'MIN(page_id)', '', __METHOD__ );
84  $end = ( $end > 0 )
85  ? $end
86  : $dbr->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
87  if ( !$start ) {
88  $this->fatalError( "Nothing to do." );
89  }
90 
91  // Mock request (hack, no real client)
92  $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip';
93 
94  # Do remaining chunk
95  $end += $batchSize - 1;
96  $blockStart = $start;
97  $blockEnd = $start + $batchSize - 1;
98 
99  $dbw = $this->getDB( DB_MASTER );
100  // Go through each page and save the output
101  while ( $blockEnd <= $end ) {
102  // Get the pages
103  $res = $dbr->select( 'page',
104  [ 'page_namespace', 'page_title', 'page_id' ],
105  [ 'page_namespace' => MWNamespace::getContentNamespaces(),
106  "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd ],
107  __METHOD__,
108  [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
109  );
110 
111  $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
112  foreach ( $res as $row ) {
113  $rebuilt = false;
114 
115  $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
116  if ( $title === null ) {
117  $this->output( "Page {$row->page_id} has bad title\n" );
118  continue; // broken title?
119  }
120 
121  $context = new RequestContext();
122  $context->setTitle( $title );
124  $context->setWikiPage( $article->getPage() );
125 
126  // Some extensions like FlaggedRevs while error out if this is unset
127  RequestContext::getMain()->setTitle( $title );
128 
129  // If the article is cacheable, then load it
130  if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
131  $viewCache = new HTMLFileCache( $title, 'view' );
132  $historyCache = new HTMLFileCache( $title, 'history' );
133  if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
134  if ( $overwrite ) {
135  $rebuilt = true;
136  } else {
137  $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
138  continue; // done already!
139  }
140  }
141 
142  Wikimedia\suppressWarnings(); // header notices
143 
144  // 1. Cache ?action=view
145  // Be sure to reset the mocked request time (T24852)
146  $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
147  ob_start();
148  $article->view();
149  $context->getOutput()->output();
150  $context->getOutput()->clearHTML();
151  $viewHtml = ob_get_clean();
152  $viewCache->saveToFileCache( $viewHtml );
153 
154  // 2. Cache ?action=history
155  // Be sure to reset the mocked request time (T24852)
156  $_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );
157  ob_start();
158  Action::factory( 'history', $article, $context )->show();
159  $context->getOutput()->output();
160  $context->getOutput()->clearHTML();
161  $historyHtml = ob_get_clean();
162  $historyCache->saveToFileCache( $historyHtml );
163 
164  Wikimedia\restoreWarnings();
165 
166  if ( $rebuilt ) {
167  $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
168  } else {
169  $this->output( "Cached page '$title' (id {$row->page_id})..." );
170  }
171  $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
172  "history: " . strlen( $historyHtml ) . " bytes]\n" );
173  } else {
174  $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
175  }
176  }
177  $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity)
178 
179  $blockStart += $batchSize;
180  $blockEnd += $batchSize;
181  }
182  $this->output( "Done!\n" );
183  }
184 }
185 
187 require_once RUN_MAINTENANCE_IF_MAIN;
$article
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers change it to an object instance and return false override the list derivative used the name of the old file & $article
Definition: hooks.txt:1476
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2636
HTMLFileCache
Page view caching in the file system.
Definition: HTMLFileCache.php:33
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:485
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
MWNamespace\getContentNamespaces
static getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
Definition: MWNamespace.php:375
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
RebuildFileCache
Maintenance script that builds file cache for content pages.
Definition: rebuildFileCache.php:31
$dbr
$dbr
Definition: testCompression.php:50
RebuildFileCache\$enabled
$enabled
Definition: rebuildFileCache.php:32
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1399
RebuildFileCache\__construct
__construct()
Default constructor.
Definition: rebuildFileCache.php:34
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
$wgUseFileCache
$wgUseFileCache
This will cache static pages for non-logged-in users to reduce database traffic on public sites.
Definition: DefaultSettings.php:2621
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:32
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: rebuildFileCache.php:186
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:124
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:604
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1414
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:430
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:283
RebuildFileCache\execute
execute()
Do the actual work.
Definition: rebuildFileCache.php:59
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:367
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
HTMLFileCache\MODE_REBUILD
const MODE_REBUILD
Definition: HTMLFileCache.php:36
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1373
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$wgDebugToolbar
$wgDebugToolbar
Display the new debugging toolbar.
Definition: DefaultSettings.php:6496
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:269
Action\factory
static factory( $action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:97
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:375
Article\newFromTitle
static newFromTitle( $title, IContextSource $context)
Create an Article object of the appropriate class for the given page.
Definition: Article.php:159
RebuildFileCache\finalSetup
finalSetup()
Handle some last-minute setup here.
Definition: rebuildFileCache.php:43