Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 74 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
TableCleanup | |
0.00% |
0 / 74 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
init | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
progress | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
6 | |||
runTable | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
56 | |||
hexChar | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Generic class to cleanup a database table. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Maintenance |
22 | */ |
23 | |
24 | use MediaWiki\Maintenance\Maintenance; |
25 | use MediaWiki\WikiMap\WikiMap; |
26 | |
27 | // @codeCoverageIgnoreStart |
28 | require_once __DIR__ . '/Maintenance.php'; |
29 | // @codeCoverageIgnoreEnd |
30 | |
31 | /** |
32 | * Generic class to cleanup a database table. Already subclasses Maintenance. |
33 | * |
34 | * @ingroup Maintenance |
35 | */ |
36 | class TableCleanup extends Maintenance { |
37 | /** @var array */ |
38 | protected $defaultParams = [ |
39 | 'table' => 'page', |
40 | 'conds' => [], |
41 | 'index' => 'page_id', |
42 | 'callback' => 'processRow', |
43 | ]; |
44 | |
45 | /** @var bool */ |
46 | protected $dryrun = false; |
47 | /** @var int */ |
48 | protected $reportInterval = 100; |
49 | |
50 | protected int $processed; |
51 | protected int $updated; |
52 | protected int $count; |
53 | protected float $startTime; |
54 | protected string $table; |
55 | |
56 | public function __construct() { |
57 | parent::__construct(); |
58 | $this->addOption( 'dry-run', 'Perform a dry run' ); |
59 | $this->addOption( 'reporting-interval', 'How often to print status line' ); |
60 | $this->setBatchSize( 100 ); |
61 | } |
62 | |
63 | public function execute() { |
64 | $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval ); |
65 | |
66 | $this->dryrun = $this->hasOption( 'dry-run' ); |
67 | |
68 | if ( $this->dryrun ) { |
69 | $this->output( "Checking for bad titles...\n" ); |
70 | } else { |
71 | $this->output( "Checking and fixing bad titles...\n" ); |
72 | } |
73 | |
74 | $this->runTable( $this->defaultParams ); |
75 | } |
76 | |
77 | protected function init( $count, $table ) { |
78 | $this->processed = 0; |
79 | $this->updated = 0; |
80 | $this->count = $count; |
81 | $this->startTime = microtime( true ); |
82 | $this->table = $table; |
83 | } |
84 | |
85 | /** |
86 | * @param int $updated |
87 | */ |
88 | protected function progress( $updated ) { |
89 | $this->updated += $updated; |
90 | $this->processed++; |
91 | if ( $this->processed % $this->reportInterval != 0 ) { |
92 | return; |
93 | } |
94 | $portion = $this->processed / $this->count; |
95 | $updateRate = $this->updated / $this->processed; |
96 | |
97 | $now = microtime( true ); |
98 | $delta = $now - $this->startTime; |
99 | $estimatedTotalTime = $delta / $portion; |
100 | $eta = $this->startTime + $estimatedTotalTime; |
101 | |
102 | $this->output( |
103 | sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n", |
104 | WikiMap::getCurrentWikiDbDomain()->getId(), |
105 | wfTimestamp( TS_DB, intval( $now ) ), |
106 | $portion * 100.0, |
107 | $this->table, |
108 | wfTimestamp( TS_DB, intval( $eta ) ), |
109 | $this->processed, |
110 | $this->count, |
111 | $this->processed / $delta, |
112 | $updateRate * 100.0 |
113 | ) |
114 | ); |
115 | flush(); |
116 | } |
117 | |
118 | /** |
119 | * @param array $params |
120 | */ |
121 | public function runTable( $params ) { |
122 | $dbr = $this->getReplicaDB(); |
123 | |
124 | if ( array_diff( array_keys( $params ), |
125 | [ 'table', 'conds', 'index', 'callback' ] ) |
126 | ) { |
127 | $this->fatalError( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) ); |
128 | } |
129 | |
130 | $table = $params['table']; |
131 | // count(*) would melt the DB for huge tables, we can estimate here |
132 | $count = $dbr->newSelectQueryBuilder() |
133 | ->table( $table ) |
134 | ->caller( __METHOD__ ) |
135 | ->estimateRowCount(); |
136 | $this->init( $count, $table ); |
137 | $this->output( "Processing $table...\n" ); |
138 | |
139 | $index = (array)$params['index']; |
140 | $indexConds = []; |
141 | $callback = [ $this, $params['callback'] ]; |
142 | |
143 | while ( true ) { |
144 | $conds = array_merge( $params['conds'], $indexConds ); |
145 | $res = $dbr->newSelectQueryBuilder() |
146 | ->select( '*' ) |
147 | ->from( $table ) |
148 | ->where( $conds ) |
149 | ->orderBy( implode( ',', $index ) ) |
150 | ->limit( $this->getBatchSize() ) |
151 | ->caller( __METHOD__ )->fetchResultSet(); |
152 | if ( !$res->numRows() ) { |
153 | // Done |
154 | break; |
155 | } |
156 | |
157 | foreach ( $res as $row ) { |
158 | call_user_func( $callback, $row ); |
159 | } |
160 | |
161 | if ( $res->numRows() < $this->getBatchSize() ) { |
162 | // Done |
163 | break; |
164 | } |
165 | |
166 | // Update the conditions to select the next batch. |
167 | $conds = []; |
168 | foreach ( $index as $field ) { |
169 | // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item |
170 | $conds[ $field ] = $row->$field; |
171 | } |
172 | $indexConds = [ $dbr->buildComparison( '>', $conds ) ]; |
173 | } |
174 | |
175 | $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" ); |
176 | } |
177 | |
178 | /** |
179 | * @param string[] $matches |
180 | * @return string |
181 | */ |
182 | protected function hexChar( $matches ) { |
183 | return sprintf( "\\x%02x", ord( $matches[1] ) ); |
184 | } |
185 | } |