Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateSpecialPages
0.00% covered (danger)
0.00%
0 / 90
0.00% covered (danger)
0.00%
0 / 4
702
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
240
 reopenAndWaitForReplicas
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 doSpecialPageCacheUpdates
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
72
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
25require_once __DIR__ . '/Maintenance.php';
26
27use MediaWiki\MainConfigNames;
28use MediaWiki\SpecialPage\QueryPage;
29
30/**
31 * Maintenance script to update cached special pages.
32 *
33 * @ingroup Maintenance
34 */
35class UpdateSpecialPages extends Maintenance {
36    public function __construct() {
37        parent::__construct();
38        $this->addOption( 'list', 'List special page names' );
39        $this->addOption( 'only', 'Only update "page"; case sensitive, ' .
40            'check correct case by calling this script with --list. ' .
41            'Ex: --only=BrokenRedirects', false, true );
42        $this->addOption( 'override', 'Also update pages that have updates disabled' );
43    }
44
45    public function execute() {
46        $dbw = $this->getPrimaryDB();
47        $config = $this->getConfig();
48        $specialPageFactory = $this->getServiceContainer()->getSpecialPageFactory();
49
50        $this->doSpecialPageCacheUpdates( $dbw );
51
52        $queryCacheLimit = (int)$config->get( MainConfigNames::QueryCacheLimit );
53        $disabledQueryPages = QueryPage::getDisabledQueryPages( $config );
54        foreach ( QueryPage::getPages() as $page ) {
55            [ , $special ] = $page;
56            $limit = $page[2] ?? $queryCacheLimit;
57
58            # --list : just show the name of pages
59            if ( $this->hasOption( 'list' ) ) {
60                $this->output( "$special [QueryPage]\n" );
61                continue;
62            }
63
64            if ( !$this->hasOption( 'override' )
65                && isset( $disabledQueryPages[$special] )
66            ) {
67                $this->output( sprintf( "%-30s [QueryPage] disabled\n", $special ) );
68                continue;
69            }
70
71            $specialObj = $specialPageFactory->getPage( $special );
72            if ( !$specialObj ) {
73                $this->output( "No such special page: $special\n" );
74                return;
75            }
76            if ( $specialObj instanceof QueryPage ) {
77                $queryPage = $specialObj;
78            } else {
79                $class = get_class( $specialObj );
80                $this->fatalError( "$class is not an instance of QueryPage.\n" );
81            }
82
83            if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $queryPage->getName() ) {
84                $this->output( sprintf( '%-30s [QueryPage] ', $special ) );
85                if ( $queryPage->isExpensive() ) {
86                    $t1 = microtime( true );
87                    # Do the query
88                    $num = $queryPage->recache( $limit );
89                    $t2 = microtime( true );
90                    if ( $num === false ) {
91                        $this->output( "FAILED: database error\n" );
92                    } else {
93                        $this->output( "got $num rows in " );
94
95                        $elapsed = $t2 - $t1;
96                        $hours = intval( $elapsed / 3600 );
97                        $minutes = intval( (int)$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                    $this->reopenAndWaitForReplicas();
109                } else {
110                    // Check if this page was expensive before and now cheap
111                    $cached = $queryPage->getCachedTimestamp();
112                    if ( $cached ) {
113                        $queryPage->deleteAllCachedData();
114                        $this->reopenAndWaitForReplicas();
115                        $this->output( "cheap, but deleted cached data\n" );
116                    } else {
117                        $this->output( "cheap, skipped\n" );
118                    }
119                }
120                if ( $this->hasOption( 'only' ) ) {
121                    break;
122                }
123            }
124        }
125    }
126
127    /**
128     * Re-open any closed db connection, and wait for replicas
129     *
130     * Queries that take a really long time, might cause the
131     * mysql connection to "go away"
132     */
133    private function reopenAndWaitForReplicas() {
134        $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
135        $lb = $lbFactory->getMainLB();
136        if ( !$lb->pingAll() ) {
137            $this->output( "\n" );
138            do {
139                $this->error( "Connection failed, reconnecting in 10 seconds..." );
140                sleep( 10 );
141                $this->waitForReplication();
142            } while ( !$lb->pingAll() );
143            $this->output( "Reconnected\n\n" );
144        }
145        $this->waitForReplication();
146    }
147
148    public function doSpecialPageCacheUpdates( $dbw ) {
149        foreach ( $this->getConfig()->get( MainConfigNames::SpecialPageCacheUpdates ) as $special => $call ) {
150            # --list : just show the name of pages
151            if ( $this->hasOption( 'list' ) ) {
152                $this->output( "$special [callback]\n" );
153                continue;
154            }
155
156            if ( !$this->hasOption( 'only' ) || $this->getOption( 'only' ) === $special ) {
157                if ( !is_callable( $call ) ) {
158                    $this->error( "Uncallable function $call!" );
159                    continue;
160                }
161                $this->output( sprintf( '%-30s [callback] ', $special ) );
162                $t1 = microtime( true );
163                $call( $dbw );
164                $t2 = microtime( true );
165
166                $this->output( "completed in " );
167                $elapsed = $t2 - $t1;
168                $hours = intval( $elapsed / 3600 );
169                $minutes = intval( (int)$elapsed % 3600 / 60 );
170                $seconds = $elapsed - $hours * 3600 - $minutes * 60;
171                if ( $hours ) {
172                    $this->output( $hours . 'h ' );
173                }
174                if ( $minutes ) {
175                    $this->output( $minutes . 'm ' );
176                }
177                $this->output( sprintf( "%.2fs\n", $seconds ) );
178                # Wait for the replica DB to catch up
179                $this->reopenAndWaitForReplicas();
180            }
181        }
182    }
183}
184
185$maintClass = UpdateSpecialPages::class;
186require_once RUN_MAINTENANCE_IF_MAIN;