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