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