3 * Update for cached special pages.
4 * Run this script periodically if you have miser mode enabled.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
25 require_once __DIR__
. '/Maintenance.php';
28 * Maintenance script to update cached special pages.
30 * @ingroup Maintenance
32 class UpdateSpecialPages
extends Maintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->addOption( 'list', 'List special page names' );
36 $this->addOption( 'only', 'Only update "page"; case sensitive, ' .
37 'check correct case by calling this script with --list or on ' .
38 'includes/QueryPage.php. Ex: --only=BrokenRedirects', false, true );
39 $this->addOption( 'override', 'Also update pages that have updates disabled' );
42 public function execute() {
43 global $IP, $wgQueryPages, $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
45 $dbw = wfGetDB( DB_MASTER
);
47 $this->doSpecialPageCacheUpdates( $dbw );
49 // This is needed to initialise $wgQueryPages
50 require_once "$IP/includes/QueryPage.php";
52 foreach ( $wgQueryPages as $page ) {
53 list( $class, $special ) = $page;
54 $limit = isset( $page[2] ) ?
$page[2] : null;
56 # --list : just show the name of pages
57 if ( $this->hasOption( 'list' ) ) {
58 $this->output( "$special [QueryPage]\n" );
62 if ( !$this->hasOption( 'override' )
63 && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate ) )
65 $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
69 $specialObj = SpecialPageFactory
::getPage( $special );
71 $this->output( "No such special page: $special\n" );
74 if ( $specialObj instanceof QueryPage
) {
75 $queryPage = $specialObj;
77 if ( !class_exists( $class ) ) {
78 $file = $specialObj->getFile();
81 $queryPage = new $class;
84 if ( !$this->hasOption( 'only' ) ||
$this->getOption( 'only' ) == $queryPage->getName() ) {
85 $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
86 if ( $queryPage->isExpensive() ) {
87 $t1 = explode( ' ', microtime() );
89 $num = $queryPage->recache( $limit === null ?
$wgQueryCacheLimit : $limit );
90 $t2 = explode( ' ', microtime() );
91 if ( $num === false ) {
92 $this->output( "FAILED: database error\n" );
94 $this->output( "got $num rows in " );
96 $elapsed = ( $t2[0] - $t1[0] ) +
( $t2[1] - $t1[1] );
97 $hours = intval( $elapsed / 3600 );
98 $minutes = intval( $elapsed %
3600 / 60 );
99 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
101 $this->output( $hours . 'h ' );
104 $this->output( $minutes . 'm ' );
106 $this->output( sprintf( "%.2fs\n", $seconds ) );
108 # Reopen any connections that have closed
109 if ( !wfGetLB()->pingAll() ) {
110 $this->output( "\n" );
112 $this->error( "Connection failed, reconnecting in 10 seconds..." );
114 } while ( !wfGetLB()->pingAll() );
115 $this->output( "Reconnected\n\n" );
117 # Wait for the slave to catch up
120 $this->output( "cheap, skipped\n" );
122 if ( $this->hasOption( 'only' ) ) {
129 public function doSpecialPageCacheUpdates( $dbw ) {
130 global $wgSpecialPageCacheUpdates;
132 foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
133 # --list : just show the name of pages
134 if ( $this->hasOption( 'list' ) ) {
135 $this->output( "$special [callback]\n" );
139 if ( !$this->hasOption( 'only' ) ||
$this->getOption( 'only' ) == $special ) {
140 if ( !is_callable( $call ) ) {
141 $this->error( "Uncallable function $call!" );
144 $this->output( sprintf( '%-30s [callback] ', $special ) );
145 $t1 = explode( ' ', microtime() );
146 call_user_func( $call, $dbw );
147 $t2 = explode( ' ', microtime() );
149 $this->output( "completed in " );
150 $elapsed = ( $t2[0] - $t1[0] ) +
( $t2[1] - $t1[1] );
151 $hours = intval( $elapsed / 3600 );
152 $minutes = intval( $elapsed %
3600 / 60 );
153 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
155 $this->output( $hours . 'h ' );
158 $this->output( $minutes . 'm ' );
160 $this->output( sprintf( "%.2fs\n", $seconds ) );
161 # Wait for the slave to catch up
168 $maintClass = "UpdateSpecialPages";
169 require_once RUN_MAINTENANCE_IF_MAIN
;