Add unit test for bug 32888
[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 * @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, $wgSpecialPageCacheUpdates, $wgQueryPages, $wgQueryCacheLimit, $wgDisableQueryPageUpdate;
37
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();
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 ) = $page;
69 $limit = isset( $page[2] ) ? $page[2] : null;
70
71 # --list : just show the name of pages
72 if ( $this->hasOption( 'list' ) ) {
73 $this->output( "$special\n" );
74 continue;
75 }
76
77 if ( !$this->hasOption( 'override' ) && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate ) ) {
78 $this->output( sprintf( "%-30s disabled\n", $special ) );
79 continue;
80 }
81
82 $specialObj = SpecialPageFactory::getPage( $special );
83 if ( !$specialObj ) {
84 $this->output( "No such special page: $special\n" );
85 exit;
86 }
87 if ( $specialObj instanceof QueryPage ) {
88 $queryPage = $specialObj;
89 } else {
90 if ( !class_exists( $class ) ) {
91 $file = $specialObj->getFile();
92 require_once( $file );
93 }
94 $queryPage = new $class;
95 }
96
97 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $queryPage->getName() ) {
98 $this->output( sprintf( '%-30s ', $special ) );
99 if ( $queryPage->isExpensive() ) {
100 $t1 = explode( ' ', microtime() );
101 # Do the query
102 $num = $queryPage->recache( $limit === null ? $wgQueryCacheLimit : $limit );
103 $t2 = explode( ' ', microtime() );
104 if ( $num === false ) {
105 $this->output( "FAILED: database error\n" );
106 } else {
107 $this->output( "got $num rows in " );
108
109 $elapsed = ( $t2[0] - $t1[0] ) + ( $t2[1] - $t1[1] );
110 $hours = intval( $elapsed / 3600 );
111 $minutes = intval( $elapsed % 3600 / 60 );
112 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
113 if ( $hours ) {
114 $this->output( $hours . 'h ' );
115 }
116 if ( $minutes ) {
117 $this->output( $minutes . 'm ' );
118 }
119 $this->output( sprintf( "%.2fs\n", $seconds ) );
120 }
121 # Reopen any connections that have closed
122 if ( !wfGetLB()->pingAll() ) {
123 $this->output( "\n" );
124 do {
125 $this->error( "Connection failed, reconnecting in 10 seconds..." );
126 sleep( 10 );
127 } while ( !wfGetLB()->pingAll() );
128 $this->output( "Reconnected\n\n" );
129 } else {
130 # Commit the results
131 $dbw->commit();
132 }
133 # Wait for the slave to catch up
134 wfWaitForSlaves();
135 } else {
136 $this->output( "cheap, skipped\n" );
137 }
138 }
139 }
140 }
141 }
142
143 $maintClass = "UpdateSpecialPages";
144 require_once( RUN_MAINTENANCE_IF_MAIN );