Merge "Make the spelling and pluralization of "Active filters" consistent"
[lhc/web/wiklou.git] / maintenance / populateChangeTagDef.php
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 require_once __DIR__ . '/Maintenance.php';
20
21 /**
22 * Populate and improve accuracy of change_tag_def statistics.
23 *
24 * @ingroup Maintenance
25 */
26 class PopulateChangeTagDef extends LoggedUpdateMaintenance {
27 /** @var Wikimedia\Rdbms\ILBFactory */
28 protected $lbFactory;
29
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Populate and improve accuracy of change_tag_def statistics' );
33 $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
34 $this->setBatchSize( 1000 );
35 $this->addOption(
36 'sleep',
37 'Sleep time (in seconds) between every batch',
38 false,
39 true
40 );
41 $this->addOption( 'populate-only', 'Do not update change_tag_def table' );
42 }
43
44 public function execute() {
45 global $wgChangeTagsSchemaMigrationStage;
46 if ( $wgChangeTagsSchemaMigrationStage === MIGRATION_OLD ) {
47 // Return "success", but don't flag it as done so the next run will retry
48 $this->output( '... Not run, $wgChangeTagsSchemaMigrationStage === MIGRATION_OLD' . "\n" );
49 return true;
50 }
51 return parent::execute();
52 }
53
54 protected function doDBUpdates() {
55 $this->lbFactory = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
56 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
57
58 if ( $this->lbFactory->getMainLB()->getConnection( DB_REPLICA )->fieldExists(
59 'change_tag',
60 'ct_tag',
61 __METHOD__
62 )
63 ) {
64 if ( !$this->hasOption( 'populate-only' ) ) {
65 $this->updateCountTag();
66 }
67 $this->backpopulateChangeTagId();
68 } else {
69 $this->updateCountTagId();
70 }
71
72 // TODO: Implement
73 // $this->cleanZeroCountRows();
74
75 return true;
76 }
77
78 private function updateCountTagId() {
79 $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
80
81 // This query can be pretty expensive, don't run it on master
82 $res = $dbr->select(
83 'change_tag',
84 [ 'ct_tag_id', 'hitcount' => 'count(*)' ],
85 [],
86 __METHOD__,
87 [ 'GROUP BY' => 'ct_tag_id' ]
88 );
89
90 $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
91
92 foreach ( $res as $row ) {
93 if ( !$row->ct_tag_id ) {
94 continue;
95 }
96
97 if ( $this->hasOption( 'dry-run' ) ) {
98 $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
99 continue;
100 }
101
102 $dbw->update(
103 'change_tag_def',
104 [ 'ctd_count' => $row->hitcount ],
105 [ 'ctd_id' => $row->ct_tag_id ],
106 __METHOD__
107 );
108 }
109 $this->lbFactory->waitForReplication();
110 }
111
112 private function updateCountTag() {
113 $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
114
115 // This query can be pretty expensive, don't run it on master
116 $res = $dbr->select(
117 'change_tag',
118 [ 'ct_tag', 'hitcount' => 'count(*)' ],
119 [],
120 __METHOD__,
121 [ 'GROUP BY' => 'ct_tag' ]
122 );
123
124 $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
125
126 foreach ( $res as $row ) {
127 // Hygiene check
128 if ( !$row->ct_tag ) {
129 continue;
130 }
131
132 if ( $this->hasOption( 'dry-run' ) ) {
133 $this->output( 'This row will be updated: ' . $row->ct_tag . $row->hitcount . "\n" );
134 continue;
135 }
136
137 $dbw->upsert(
138 'change_tag_def',
139 [
140 'ctd_name' => $row->ct_tag,
141 'ctd_user_defined' => 0,
142 'ctd_count' => $row->hitcount
143 ],
144 [ 'ctd_name' ],
145 [ 'ctd_count' => $row->hitcount ],
146 __METHOD__
147 );
148 }
149 $this->lbFactory->waitForReplication();
150 }
151
152 private function backpopulateChangeTagId() {
153 $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
154 $changeTagDefs = $dbr->select(
155 'change_tag_def',
156 [ 'ctd_name', 'ctd_id' ],
157 [],
158 __METHOD__,
159 [ 'ORDER BY' => 'ctd_id' ]
160 );
161
162 foreach ( $changeTagDefs as $row ) {
163 $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
164 }
165 }
166
167 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
168 $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
169 $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
170 $sleep = (int)$this->getOption( 'sleep', 10 );
171 $lastId = 0;
172 $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
173 while ( true ) {
174 // Given that indexes might not be there, it's better to use replica
175 $ids = $dbr->selectFieldValues(
176 'change_tag',
177 'ct_id',
178 [ 'ct_tag' => $tagName, 'ct_tag_id' => null, 'ct_id > ' . $lastId ],
179 __METHOD__,
180 [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => 'ct_id' ]
181 );
182
183 if ( !$ids ) {
184 break;
185 }
186 $lastId = end( $ids );
187
188 if ( $this->hasOption( 'dry-run' ) ) {
189 $this->output(
190 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
191 );
192 continue;
193 } else {
194 $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
195 }
196
197 $dbw->update(
198 'change_tag',
199 [ 'ct_tag_id' => $tagId ],
200 [ 'ct_id' => $ids ],
201 __METHOD__
202 );
203
204 $this->lbFactory->waitForReplication();
205 if ( $sleep > 0 ) {
206 sleep( $sleep );
207 }
208 }
209
210 $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
211 }
212
213 protected function getUpdateKey() {
214 return __CLASS__;
215 }
216 }
217
218 $maintClass = PopulateChangeTagDef::class;
219 require_once RUN_MAINTENANCE_IF_MAIN;