using .append() instead of innerHTML+=. The latter replaces the html and causes all...
[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, $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 ( $specialObj instanceof QueryPage ) {
87 $queryPage = $specialObj;
88 } else {
89 if ( !class_exists( $class ) ) {
90 $file = $specialObj->getFile();
91 require_once( $file );
92 }
93 $queryPage = new $class;
94 }
95
96 if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) == $queryPage->getName() ) {
97 $this->output( sprintf( '%-30s ', $special ) );
98 if ( $queryPage->isExpensive() ) {
99 $t1 = explode( ' ', microtime() );
100 # Do the query
101 $num = $queryPage->recache( $limit === null ? $wgQueryCacheLimit : $limit );
102 $t2 = explode( ' ', microtime() );
103 if ( $num === false ) {
104 $this->output( "FAILED: database error\n" );
105 } else {
106 $this->output( "got $num rows in " );
107
108 $elapsed = ( $t2[0] - $t1[0] ) + ( $t2[1] - $t1[1] );
109 $hours = intval( $elapsed / 3600 );
110 $minutes = intval( $elapsed % 3600 / 60 );
111 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
112 if ( $hours ) {
113 $this->output( $hours . 'h ' );
114 }
115 if ( $minutes ) {
116 $this->output( $minutes . 'm ' );
117 }
118 $this->output( sprintf( "%.2fs\n", $seconds ) );
119 }
120 # Reopen any connections that have closed
121 if ( !wfGetLB()->pingAll() ) {
122 $this->output( "\n" );
123 do {
124 $this->error( "Connection failed, reconnecting in 10 seconds..." );
125 sleep( 10 );
126 } while ( !wfGetLB()->pingAll() );
127 $this->output( "Reconnected\n\n" );
128 } else {
129 # Commit the results
130 $dbw->commit();
131 }
132 # Wait for the slave to catch up
133 wfWaitForSlaves( 5 );
134 } else {
135 $this->output( "cheap, skipped\n" );
136 }
137 }
138 }
139 }
140 }
141
142 $maintClass = "UpdateSpecialPages";
143 require_once( DO_MAINTENANCE );