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