Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 137 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
PopulateChangeTagDef | |
0.00% |
0 / 137 |
|
0.00% |
0 / 8 |
650 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
doDBUpdates | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
setUserDefinedTags | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 | |||
updateCountTagId | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
20 | |||
updateCountTag | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
20 | |||
backpopulateChangeTagId | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
backpopulateChangeTagPerTag | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
30 | |||
getUpdateKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | */ |
18 | |
19 | use MediaWiki\Maintenance\LoggedUpdateMaintenance; |
20 | |
21 | // @codeCoverageIgnoreStart |
22 | require_once __DIR__ . '/Maintenance.php'; |
23 | // @codeCoverageIgnoreEnd |
24 | |
25 | /** |
26 | * Populate and improve accuracy of change_tag_def statistics. |
27 | * |
28 | * @ingroup Maintenance |
29 | */ |
30 | class PopulateChangeTagDef extends LoggedUpdateMaintenance { |
31 | public function __construct() { |
32 | parent::__construct(); |
33 | $this->addDescription( 'Populate and improve accuracy of change_tag_def statistics' ); |
34 | $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' ); |
35 | $this->setBatchSize( 1000 ); |
36 | $this->addOption( |
37 | 'sleep', |
38 | 'Sleep time (in seconds) between every batch, defaults to zero', |
39 | false, |
40 | true |
41 | ); |
42 | $this->addOption( 'populate-only', 'Do not update change_tag_def table' ); |
43 | $this->addOption( 'set-user-tags-only', 'Only update ctd_user_defined from valid_tag table' ); |
44 | } |
45 | |
46 | protected function doDBUpdates() { |
47 | $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) ); |
48 | |
49 | $dbw = $this->getDB( DB_PRIMARY ); |
50 | if ( $dbw->fieldExists( |
51 | 'change_tag', |
52 | 'ct_tag', |
53 | __METHOD__ |
54 | ) |
55 | ) { |
56 | if ( $this->hasOption( 'set-user-tags-only' ) ) { |
57 | $this->setUserDefinedTags(); |
58 | return true; |
59 | } |
60 | if ( !$this->hasOption( 'populate-only' ) ) { |
61 | $this->updateCountTag(); |
62 | } |
63 | $this->backpopulateChangeTagId(); |
64 | $this->setUserDefinedTags(); |
65 | } else { |
66 | $this->updateCountTagId(); |
67 | } |
68 | |
69 | // TODO: Implement |
70 | // $this->cleanZeroCountRows(); |
71 | |
72 | return true; |
73 | } |
74 | |
75 | private function setUserDefinedTags() { |
76 | $dbw = $this->getDB( DB_PRIMARY ); |
77 | |
78 | $userTags = null; |
79 | if ( $dbw->tableExists( 'valid_tag', __METHOD__ ) ) { |
80 | $userTags = $dbw->newSelectQueryBuilder() |
81 | ->select( 'vt_tag' ) |
82 | ->from( 'valid_tag' ) |
83 | ->caller( __METHOD__ )->fetchFieldValues(); |
84 | } |
85 | |
86 | if ( !$userTags ) { |
87 | $this->output( "No user defined tags to set, moving on...\n" ); |
88 | return; |
89 | } |
90 | |
91 | if ( $this->hasOption( 'dry-run' ) ) { |
92 | $this->output( |
93 | 'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n" |
94 | ); |
95 | return; |
96 | } |
97 | |
98 | $dbw->newUpdateQueryBuilder() |
99 | ->update( 'change_tag_def' ) |
100 | ->set( [ 'ctd_user_defined' => 1 ] ) |
101 | ->where( [ 'ctd_name' => $userTags ] ) |
102 | ->caller( __METHOD__ ) |
103 | ->execute(); |
104 | $this->waitForReplication(); |
105 | $this->output( "Finished setting user defined tags in change_tag_def table\n" ); |
106 | } |
107 | |
108 | private function updateCountTagId() { |
109 | $dbr = $this->getReplicaDB(); |
110 | |
111 | // This query can be pretty expensive, don't run it on master |
112 | $res = $dbr->newSelectQueryBuilder() |
113 | ->select( [ 'ct_tag_id', 'hitcount' => 'count(*)' ] ) |
114 | ->from( 'change_tag' ) |
115 | ->groupBy( 'ct_tag_id' ) |
116 | ->caller( __METHOD__ )->fetchResultSet(); |
117 | |
118 | $dbw = $this->getPrimaryDB(); |
119 | |
120 | foreach ( $res as $row ) { |
121 | if ( !$row->ct_tag_id ) { |
122 | continue; |
123 | } |
124 | |
125 | if ( $this->hasOption( 'dry-run' ) ) { |
126 | $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" ); |
127 | continue; |
128 | } |
129 | |
130 | $dbw->newUpdateQueryBuilder() |
131 | ->update( 'change_tag_def' ) |
132 | ->set( [ 'ctd_count' => $row->hitcount ] ) |
133 | ->where( [ 'ctd_id' => $row->ct_tag_id ] ) |
134 | ->caller( __METHOD__ ) |
135 | ->execute(); |
136 | } |
137 | $this->waitForReplication(); |
138 | } |
139 | |
140 | private function updateCountTag() { |
141 | $dbr = $this->getReplicaDB(); |
142 | |
143 | // This query can be pretty expensive, don't run it on master |
144 | $res = $dbr->newSelectQueryBuilder() |
145 | ->select( [ 'ct_tag', 'hitcount' => 'count(*)' ] ) |
146 | ->from( 'change_tag' ) |
147 | ->groupBy( 'ct_tag' ) |
148 | ->caller( __METHOD__ )->fetchResultSet(); |
149 | |
150 | $dbw = $this->getPrimaryDB(); |
151 | |
152 | foreach ( $res as $row ) { |
153 | // Hygiene check |
154 | if ( !$row->ct_tag ) { |
155 | continue; |
156 | } |
157 | |
158 | if ( $this->hasOption( 'dry-run' ) ) { |
159 | $this->output( 'This row will be updated: ' . $row->ct_tag . $row->hitcount . "\n" ); |
160 | continue; |
161 | } |
162 | $dbw->newInsertQueryBuilder() |
163 | ->insertInto( 'change_tag_def' ) |
164 | ->row( [ |
165 | 'ctd_name' => $row->ct_tag, |
166 | 'ctd_user_defined' => 0, |
167 | 'ctd_count' => $row->hitcount |
168 | ] ) |
169 | ->onDuplicateKeyUpdate() |
170 | ->uniqueIndexFields( [ 'ctd_name' ] ) |
171 | ->set( [ 'ctd_count' => $row->hitcount ] ) |
172 | ->caller( __METHOD__ )->execute(); |
173 | } |
174 | $this->waitForReplication(); |
175 | } |
176 | |
177 | private function backpopulateChangeTagId() { |
178 | $dbr = $this->getReplicaDB(); |
179 | $changeTagDefs = $dbr->newSelectQueryBuilder() |
180 | ->select( [ 'ctd_name', 'ctd_id' ] ) |
181 | ->from( 'change_tag_def' ) |
182 | ->orderBy( 'ctd_id' ) |
183 | ->caller( __METHOD__ )->fetchResultSet(); |
184 | |
185 | foreach ( $changeTagDefs as $row ) { |
186 | $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id ); |
187 | } |
188 | } |
189 | |
190 | private function backpopulateChangeTagPerTag( $tagName, $tagId ) { |
191 | $dbr = $this->getReplicaDB(); |
192 | $dbw = $this->getPrimaryDB(); |
193 | $sleep = (int)$this->getOption( 'sleep', 0 ); |
194 | $lastId = 0; |
195 | $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" ); |
196 | while ( true ) { |
197 | // Given that indexes might not be there, it's better to use replica |
198 | $ids = $dbr->newSelectQueryBuilder() |
199 | ->select( 'ct_id' ) |
200 | ->from( 'change_tag' ) |
201 | ->where( [ 'ct_tag' => $tagName, 'ct_tag_id' => null, $dbr->expr( 'ct_id', '>', $lastId ) ] ) |
202 | ->orderBy( 'ct_id' ) |
203 | ->limit( $this->getBatchSize() ) |
204 | ->caller( __METHOD__ )->fetchFieldValues(); |
205 | |
206 | if ( !$ids ) { |
207 | break; |
208 | } |
209 | $lastId = end( $ids ); |
210 | |
211 | if ( $this->hasOption( 'dry-run' ) ) { |
212 | $this->output( |
213 | "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n" |
214 | ); |
215 | continue; |
216 | } else { |
217 | $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" ); |
218 | } |
219 | |
220 | $dbw->newUpdateQueryBuilder() |
221 | ->update( 'change_tag' ) |
222 | ->set( [ 'ct_tag_id' => $tagId ] ) |
223 | ->where( [ 'ct_id' => $ids ] ) |
224 | ->caller( __METHOD__ ) |
225 | ->execute(); |
226 | |
227 | $this->waitForReplication(); |
228 | if ( $sleep > 0 ) { |
229 | sleep( $sleep ); |
230 | } |
231 | } |
232 | |
233 | $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" ); |
234 | } |
235 | |
236 | protected function getUpdateKey() { |
237 | return __CLASS__; |
238 | } |
239 | } |
240 | |
241 | // @codeCoverageIgnoreStart |
242 | $maintClass = PopulateChangeTagDef::class; |
243 | require_once RUN_MAINTENANCE_IF_MAIN; |
244 | // @codeCoverageIgnoreEnd |