]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/updateSpecialPages.php
MediaWiki 1.17.1-scripts
[autoinstalls/mediawiki.git] / maintenance / updateSpecialPages.php
1 <?php
2 /**
3  * Run this script periodically if you have miser mode enabled, to refresh the
4  * caches
5  *
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.
10  *
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.
15  *
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
20  *
21  * @file
22  * @ingroup Maintenance
23  */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 class UpdateSpecialPages extends Maintenance {
28         public function __construct() {
29                 parent::__construct();
30                 $this->addOption( 'list', 'List special page names' );
31                 $this->addOption( 'only', 'Only update "page". Ex: --only=BrokenRedirects', false, true );
32                 $this->addOption( 'override', 'Also update pages that have updates disabled' );
33         }
34
35         public function execute() {
36                 global $IP, $wgOut, $wgSpecialPageCacheUpdates, $wgQueryPages, $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
37                 $wgOut->disable();
38                 $dbw = wfGetDB( DB_MASTER );
39
40                 foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
41                         if ( !is_callable( $call ) ) {
42                                 $this->error( "Uncallable function $call!" );
43                                 continue;
44                         }
45                         $t1 = explode( ' ', microtime() );
46                         call_user_func( $call, $dbw );
47                         $t2 = explode( ' ', microtime() );
48                         $this->output( sprintf( '%-30s ', $special ) );
49                         $elapsed = ( $t2[0] - $t1[0] ) + ( $t2[1] - $t1[1] );
50                         $hours = intval( $elapsed / 3600 );
51                         $minutes = intval( $elapsed % 3600 / 60 );
52                         $seconds = $elapsed - $hours * 3600 - $minutes * 60;
53                         if ( $hours ) {
54                                 $this->output( $hours . 'h ' );
55                         }
56                         if ( $minutes ) {
57                                 $this->output( $minutes . 'm ' );
58                         }
59                         $this->output( sprintf( "completed in %.2fs\n", $seconds ) );
60                         # Wait for the slave to catch up
61                         wfWaitForSlaves( 5 );
62                 }
63
64                 // This is needed to initialise $wgQueryPages
65                 require_once( "$IP/includes/QueryPage.php" );
66
67                 foreach ( $wgQueryPages as $page ) {
68                         @list( $class, $special, $limit ) = $page;
69
70                         # --list : just show the name of pages
71                         if ( $this->hasOption( 'list' ) ) {
72                                 $this->output( "$special\n" );
73                                 continue;
74                         }
75
76                         if ( !$this->hasOption( 'override' ) && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate ) ) {
77                                 $this->output( sprintf( "%-30s disabled\n", $special ) );
78                                 continue;
79                         }
80
81                         $specialObj = SpecialPage::getPage( $special );
82                         if ( !$specialObj ) {
83                                 $this->output( "No such special page: $special\n" );
84                                 exit;
85                         }
86                         if ( !class_exists( $class ) ) {
87                                 $file = $specialObj->getFile();
88                                 require_once( $file );
89                         }
90                         $queryPage = new $class;
91
92                         if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $queryPage->getName() ) {
93                                 $this->output( sprintf( '%-30s ',  $special ) );
94                                 if ( $queryPage->isExpensive() ) {
95                                         $t1 = explode( ' ', microtime() );
96                                         # Do the query
97                                         $num = $queryPage->recache( $limit === null ? $wgQueryCacheLimit : $limit );
98                                         $t2 = explode( ' ', microtime() );
99                                         if ( $num === false ) {
100                                                 $this->output( "FAILED: database error\n" );
101                                         } else {
102                                                 $this->output( "got $num rows in " );
103
104                                                 $elapsed = ( $t2[0] - $t1[0] ) + ( $t2[1] - $t1[1] );
105                                                 $hours = intval( $elapsed / 3600 );
106                                                 $minutes = intval( $elapsed % 3600 / 60 );
107                                                 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
108                                                 if ( $hours ) {
109                                                         $this->output( $hours . 'h ' );
110                                                 }
111                                                 if ( $minutes ) {
112                                                         $this->output( $minutes . 'm ' );
113                                                 }
114                                                 $this->output( sprintf( "%.2fs\n", $seconds ) );
115                                         }
116                                         # Reopen any connections that have closed
117                                         if ( !wfGetLB()->pingAll() )  {
118                                                 $this->output( "\n" );
119                                                 do {
120                                                         $this->error( "Connection failed, reconnecting in 10 seconds..." );
121                                                         sleep( 10 );
122                                                 } while ( !wfGetLB()->pingAll() );
123                                                 $this->output( "Reconnected\n\n" );
124                                         } else {
125                                                 # Commit the results
126                                                 $dbw->commit();
127                                         }
128                                         # Wait for the slave to catch up
129                                         wfWaitForSlaves( 5 );
130                                 } else {
131                                         $this->output( "cheap, skipped\n" );
132                                 }
133                         }
134                 }
135         }
136 }
137
138 $maintClass = "UpdateSpecialPages";
139 require_once( RUN_MAINTENANCE_IF_MAIN );