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