MediaWiki  1.28.1
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() {
44  global $wgDebugToolbar, $wgUseFileCache, $wgReadOnly;
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  $wgReadOnly = 'Building cache'; // avoid DB writes (like enotif/counters)
54 
55  parent::finalSetup();
56  }
57 
58  public function execute() {
60 
61  if ( !$this->enabled ) {
62  $this->error( "Nothing to do -- \$wgUseFileCache is disabled.", true );
63  }
64 
65  $start = $this->getOption( 'start', "0" );
66  if ( !ctype_digit( $start ) ) {
67  $this->error( "Invalid value for start parameter.", true );
68  }
69  $start = intval( $start );
70 
71  $end = $this->getOption( 'end', "0" );
72  if ( !ctype_digit( $end ) ) {
73  $this->error( "Invalid value for end parameter.", true );
74  }
75  $end = intval( $end );
76 
77  $this->output( "Building content page file cache from page {$start}!\n" );
78 
79  $dbr = $this->getDB( DB_REPLICA );
80  $overwrite = $this->getOption( 'overwrite', false );
81  $start = ( $start > 0 )
82  ? $start
83  : $dbr->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
84  $end = ( $end > 0 )
85  ? $end
86  : $dbr->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
87  if ( !$start ) {
88  $this->error( "Nothing to do.", true );
89  }
90 
91  $_SERVER['HTTP_ACCEPT_ENCODING'] = 'bgzip'; // hack, no real client
92 
93  # Do remaining chunk
94  $end += $this->mBatchSize - 1;
95  $blockStart = $start;
96  $blockEnd = $start + $this->mBatchSize - 1;
97 
98  $dbw = $this->getDB( DB_MASTER );
99  // Go through each page and save the output
100  while ( $blockEnd <= $end ) {
101  // Get the pages
102  $res = $dbr->select( 'page',
103  [ 'page_namespace', 'page_title', 'page_id' ],
104  [ 'page_namespace' => MWNamespace::getContentNamespaces(),
105  "page_id BETWEEN $blockStart AND $blockEnd" ],
106  __METHOD__,
107  [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
108  );
109 
110  $this->beginTransaction( $dbw, __METHOD__ ); // for any changes
111  foreach ( $res as $row ) {
112  $rebuilt = false;
113 
114  $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
115  if ( null == $title ) {
116  $this->output( "Page {$row->page_id} has bad title\n" );
117  continue; // broken title?
118  }
119 
120  $context = new RequestContext();
121  $context->setTitle( $title );
123  $context->setWikiPage( $article->getPage() );
124 
125  // If the article is cacheable, then load it
126  if ( $article->isFileCacheable( HTMLFileCache::MODE_REBUILD ) ) {
127  $viewCache = new HTMLFileCache( $title, 'view' );
128  $historyCache = new HTMLFileCache( $title, 'history' );
129  if ( $viewCache->isCacheGood() && $historyCache->isCacheGood() ) {
130  if ( $overwrite ) {
131  $rebuilt = true;
132  } else {
133  $this->output( "Page '$title' (id {$row->page_id}) already cached\n" );
134  continue; // done already!
135  }
136  }
137 
138  MediaWiki\suppressWarnings(); // header notices
139  // Cache ?action=view
140  $wgRequestTime = microtime( true ); # bug 22852
141  ob_start();
142  $article->view();
143  $context->getOutput()->output();
144  $context->getOutput()->clearHTML();
145  $viewHtml = ob_get_clean();
146  $viewCache->saveToFileCache( $viewHtml );
147  // Cache ?action=history
148  $wgRequestTime = microtime( true ); # bug 22852
149  ob_start();
150  Action::factory( 'history', $article, $context )->show();
151  $context->getOutput()->output();
152  $context->getOutput()->clearHTML();
153  $historyHtml = ob_get_clean();
154  $historyCache->saveToFileCache( $historyHtml );
155  MediaWiki\restoreWarnings();
156 
157  if ( $rebuilt ) {
158  $this->output( "Re-cached page '$title' (id {$row->page_id})..." );
159  } else {
160  $this->output( "Cached page '$title' (id {$row->page_id})..." );
161  }
162  $this->output( "[view: " . strlen( $viewHtml ) . " bytes; " .
163  "history: " . strlen( $historyHtml ) . " bytes]\n" );
164  } else {
165  $this->output( "Page '$title' (id {$row->page_id}) not cacheable\n" );
166  }
167  }
168  $this->commitTransaction( $dbw, __METHOD__ ); // commit any changes (just for sanity)
169 
170  $blockStart += $this->mBatchSize;
171  $blockEnd += $this->mBatchSize;
172  }
173  $this->output( "Done!\n" );
174  }
175 }
176 
177 $maintClass = "RebuildFileCache";
178 require_once RUN_MAINTENANCE_IF_MAIN;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Group all the pieces relevant to the context of a request into one instance.
$context
Definition: load.php:50
Maintenance script that builds file cache for content pages.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
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
Page view caching in the file system.
getDB($db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
const DB_MASTER
Definition: defines.php:23
static factory($action, Page $page, IContextSource $context=null)
Get an appropriate Action subclass for the given action.
Definition: Action.php:95
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
$wgUseFileCache
This will cache static pages for non-logged-in users to reduce database traffic on public sites...
$res
Definition: database.txt:21
static newFromTitle($title, IContextSource $context)
Create an Article object of the appropriate class for the given page.
Definition: Article.php:112
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:535
static getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
addDescription($text)
Set the description text.
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
getOption($name, $default=null)
Get an option, or return the default.
output($out, $channel=null)
Throw some output to the user.
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
int $mBatchSize
Batch size.
Definition: Maintenance.php:98
error($err, $die=0)
Throw an error to the user.
const DB_REPLICA
Definition: defines.php:22
setBatchSize($s=0)
Set the batch size.
float $wgRequestTime
Request start time as fractional seconds since epoch.
Definition: WebStart.php:43
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.