Merge maintenance-work branch:
[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 * @file
7 * @ingroup Maintenance
8 */
9
10 require_once( "Maintenance.php" );
11
12 class UpdateSpecialPages extends Maintenance {
13 public function __construct() {
14 parent::__construct();
15 $this->addParam( 'list', 'List special page names' );
16 $this->addParam( 'only', 'Only update "page". Ex: --only=BrokenRedirects', false, true );
17 $this->addParam( 'override', 'Also update pages that have updates disabled' );
18 }
19
20 public function execute() {
21 global $wgOut;
22 $wgOut->disable();
23 $dbw = wfGetDB( DB_MASTER );
24
25 foreach( $wgSpecialPageCacheUpdates as $special => $call ) {
26 if( !is_callable($call) ) {
27 $this->error( "Uncallable function $call!\n" );
28 continue;
29 }
30 $t1 = explode( ' ', microtime() );
31 call_user_func( $call, $dbw );
32 $t2 = explode( ' ', microtime() );
33 $this->output( sprintf( '%-30s ', $special ) );
34 $elapsed = ($t2[0] - $t1[0]) + ($t2[1] - $t1[1]);
35 $hours = intval( $elapsed / 3600 );
36 $minutes = intval( $elapsed % 3600 / 60 );
37 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
38 if ( $hours ) {
39 $this->output( $hours . 'h ' );
40 }
41 if ( $minutes ) {
42 $this->output( $minutes . 'm ' );
43 }
44 $this->output( sprintf( "completed in %.2fs\n", $seconds ) );
45 # Wait for the slave to catch up
46 wfWaitForSlaves( 5 );
47 }
48
49 foreach( $wgQueryPages as $page ) {
50 @list( $class, $special, $limit ) = $page;
51
52 # --list : just show the name of pages
53 if( $this->hasOption('list') ) {
54 $this->output( "$special\n" );
55 continue;
56 }
57
58 if ( $this->hasOption('override') && $wgDisableQueryPageUpdate && in_array( $special, $wgDisableQueryPageUpdate ) ) {
59 $this->output( sprintf( "%-30s disabled\n", $special ) );
60 continue;
61 }
62
63 $specialObj = SpecialPage::getPage( $special );
64 if ( !$specialObj ) {
65 $this->output( "No such special page: $special\n" );
66 exit;
67 }
68 if ( !class_exists( $class ) ) {
69 $file = $specialObj->getFile();
70 require_once( $file );
71 }
72 $queryPage = new $class;
73
74 if( !$this->hasOption('only') || $this->getOption('only') == $queryPage->getName() ) {
75 $this->output( sprintf( '%-30s ', $special ) );
76 if ( $queryPage->isExpensive() ) {
77 $t1 = explode( ' ', microtime() );
78 # Do the query
79 $num = $queryPage->recache( $limit === null ? $wgQueryCacheLimit : $limit );
80 $t2 = explode( ' ', microtime() );
81 if ( $num === false ) {
82 $this->output( "FAILED: database error\n" );
83 } else {
84 $this->output( "got $num rows in " );
85
86 $elapsed = ($t2[0] - $t1[0]) + ($t2[1] - $t1[1]);
87 $hours = intval( $elapsed / 3600 );
88 $minutes = intval( $elapsed % 3600 / 60 );
89 $seconds = $elapsed - $hours * 3600 - $minutes * 60;
90 if ( $hours ) {
91 $this->output( $hours . 'h ' );
92 }
93 if ( $minutes ) {
94 $this->output( $minutes . 'm ' );
95 }
96 $this->output( sprintf( "%.2fs\n", $seconds ) );
97 }
98 # Reopen any connections that have closed
99 if ( !wfGetLB()->pingAll()) {
100 $this->output( "\n" );
101 do {
102 $this->error( "Connection failed, reconnecting in 10 seconds...\n" );
103 sleep(10);
104 } while ( !wfGetLB()->pingAll() );
105 $this->output( "Reconnected\n\n" );
106 } else {
107 # Commit the results
108 $dbw->immediateCommit();
109 }
110 # Wait for the slave to catch up
111 wfWaitForSlaves( 5 );
112 } else {
113 $this->output( "cheap, skipped\n" );
114 }
115 }
116 }
117 }
118 }
119