MediaWiki  1.27.1
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->addDescription( '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() {
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 = $this->getDB( 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 = $this->getDB( DB_MASTER );
93  // Go through each page and save the output
94  while ( $blockEnd <= $end ) {
95  // Get the pages
96  $res = $dbr->select( 'page', [ 'page_namespace', 'page_title', 'page_id' ],
97  [ 'page_namespace' => $wgContentNamespaces,
98  "page_id BETWEEN $blockStart AND $blockEnd" ],
99  [ 'ORDER BY' => 'page_id ASC', 'USE INDEX' => 'PRIMARY' ]
100  );
101 
102  $this->beginTransaction( $dbw, __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 );
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() ) {
122  $cache = HTMLFileCache::newFromTitle( $title, 'view' );
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( [ &$cache, 'saveToFileCache' ] ); // save on ob_end_clean()
132  $wgUseFileCache = false; // hack, we don't want $article fiddling with filecache
133  $article->view();
134  MediaWiki\suppressWarnings(); // header notices
135  $wgOut->output();
136  MediaWiki\restoreWarnings();
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  $this->commitTransaction( $dbw, __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;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for slaves to catch up.
Group all the pieces relevant to the context of a request into one instance.
$context
Definition: load.php:44
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
static newFromTitle($title, $action)
Construct an ObjectFileCache from a Title and an action.
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
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
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
$cache
Definition: mcc.php:33
static newFromTitle($title, IContextSource $context)
Create an Article object of the appropriate class for the given page.
Definition: Article.php:114
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:548
const DB_SLAVE
Definition: Defines.php:46
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
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.
$wgContentNamespaces
Array of namespaces which can be deemed to contain valid "content", as far as the site statistics are...
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:99
error($err, $die=0)
Throw an error to the user.
const DB_MASTER
Definition: Defines.php:47
$wgOut
Definition: Setup.php:804
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.