Merge "$wgHooks: add closure docs & admonition to register handlers early"
[lhc/web/wiklou.git] / maintenance / updateSpecialPages.php
1 <?php
2 /**
3 * Update for cached special pages.
4 * Run this script periodically if you have miser mode enabled.
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 __DIR__ . '/Maintenance.php';
26
27 /**
28 * Maintenance script to update cached special pages.
29 *
30 * @ingroup Maintenance
31 */
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' );
40 }
41
42 public function execute() {
43 global $IP, $wgSpecialPageCacheUpdates, $wgQueryPages, $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
44
45 $dbw = wfGetDB( DB_MASTER );
46
47 foreach ( $wgSpecialPageCacheUpdates as $special => $call ) {
48 if ( !is_callable( $call ) ) {
49 $this->error( "Uncallable function $call!" );
50 continue;
51 }
52 $this->output( sprintf( '%-30s ', $special ) );
53 $t1 = explode( ' ', microtime() );
54 call_user_func( $call, $dbw );
55 $t2 = explode( ' ', microtime() );
56 $elapsed = ( $t2[0] - $t1[0] ) + ( $t2[1] - $t1[1] );
57 $hours = intval( $elapsed / 3600 );
58 $minutes = intval( $elapsed % 3600 / 60 );
59 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
60 if ( $hours ) {
61 $this->output( $hours . 'h ' );
62 }
63 if ( $minutes ) {
64 $this->output( $minutes . 'm ' );
65 }
66 $this->output( sprintf( "completed in %.2fs\n", $seconds ) );
67 # Wait for the slave to catch up
68 wfWaitForSlaves();
69 }
70
71 // This is needed to initialise $wgQueryPages
72 require_once "$IP/includes/QueryPage.php";
73
74 foreach ( $wgQueryPages as $page ) {
75 list( $class, $special ) = $page;
76 $limit = isset( $page[2] ) ? $page[2] : null;
77
78 # --list : just show the name of pages
79 if ( $this->hasOption( 'list' ) ) {
80 $this->output( "$special\n" );
81 continue;
82 }
83
84 if ( !$this->hasOption( 'override' ) && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate ) ) {
85 $this->output( sprintf( "%-30s disabled\n", $special ) );
86 continue;
87 }
88
89 $specialObj = SpecialPageFactory::getPage( $special );
90 if ( !$specialObj ) {
91 $this->output( "No such special page: $special\n" );
92 exit;
93 }
94 if ( $specialObj instanceof QueryPage ) {
95 $queryPage = $specialObj;
96 } else {
97 if ( !class_exists( $class ) ) {
98 $file = $specialObj->getFile();
99 require_once $file;
100 }
101 $queryPage = new $class;
102 }
103
104 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $queryPage->getName() ) {
105 $this->output( sprintf( '%-30s ', $special ) );
106 if ( $queryPage->isExpensive() ) {
107 $t1 = explode( ' ', microtime() );
108 # Do the query
109 $num = $queryPage->recache( $limit === null ? $wgQueryCacheLimit : $limit );
110 $t2 = explode( ' ', microtime() );
111 if ( $num === false ) {
112 $this->output( "FAILED: database error\n" );
113 } else {
114 $this->output( "got $num rows in " );
115
116 $elapsed = ( $t2[0] - $t1[0] ) + ( $t2[1] - $t1[1] );
117 $hours = intval( $elapsed / 3600 );
118 $minutes = intval( $elapsed % 3600 / 60 );
119 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
120 if ( $hours ) {
121 $this->output( $hours . 'h ' );
122 }
123 if ( $minutes ) {
124 $this->output( $minutes . 'm ' );
125 }
126 $this->output( sprintf( "%.2fs\n", $seconds ) );
127 }
128 # Reopen any connections that have closed
129 if ( !wfGetLB()->pingAll() ) {
130 $this->output( "\n" );
131 do {
132 $this->error( "Connection failed, reconnecting in 10 seconds..." );
133 sleep( 10 );
134 } while ( !wfGetLB()->pingAll() );
135 $this->output( "Reconnected\n\n" );
136 } else {
137 # Commit the results
138 $dbw->commit( __METHOD__ );
139 }
140 # Wait for the slave to catch up
141 wfWaitForSlaves();
142 } else {
143 $this->output( "cheap, skipped\n" );
144 }
145 }
146 }
147 }
148 }
149
150 $maintClass = "UpdateSpecialPages";
151 require_once RUN_MAINTENANCE_IF_MAIN;