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.
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.
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
19 require_once __DIR__
. '/Maintenance.php';
22 * Populate and improve accuracy of change_tag_def statistics.
24 * @ingroup Maintenance
26 class PopulateChangeTagDef
extends LoggedUpdateMaintenance
{
27 /** @var Wikimedia\Rdbms\ILBFactory */
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 );
37 'Sleep time (in seconds) between every batch',
41 $this->addOption( 'populate-only', 'Do not update change_tag_def table' );
42 $this->addOption( 'set-user-tags-only', 'Only update ctd_user_defined from valid_tag table' );
45 public function execute() {
46 global $wgChangeTagsSchemaMigrationStage;
47 if ( $wgChangeTagsSchemaMigrationStage === MIGRATION_OLD
) {
48 // Return "success", but don't flag it as done so the next run will retry
49 $this->output( '... Not run, $wgChangeTagsSchemaMigrationStage === MIGRATION_OLD' . "\n" );
52 return parent
::execute();
55 protected function doDBUpdates() {
56 $this->lbFactory
= MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
57 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
59 if ( $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
)->fieldExists(
65 if ( $this->hasOption( 'set-user-tags-only' ) ) {
66 $this->setUserDefinedTags();
69 if ( !$this->hasOption( 'populate-only' ) ) {
70 $this->updateCountTag();
72 $this->backpopulateChangeTagId();
73 $this->setUserDefinedTags();
75 $this->updateCountTagId();
79 // $this->cleanZeroCountRows();
84 private function setUserDefinedTags() {
85 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
87 $userTags = $dbr->selectFieldValues(
94 if ( empty( $userTags ) ) {
95 $this->output( "No user defined tags to set, moving on...\n" );
99 if ( $this->hasOption( 'dry-run' ) ) {
101 'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n"
106 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
110 [ 'ctd_user_defined' => 1 ],
111 [ 'ctd_name' => $userTags ],
114 $this->lbFactory
->waitForReplication();
115 $this->output( "Finished setting user defined tags in change_tag_def table\n" );
118 private function updateCountTagId() {
119 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
121 // This query can be pretty expensive, don't run it on master
124 [ 'ct_tag_id', 'hitcount' => 'count(*)' ],
127 [ 'GROUP BY' => 'ct_tag_id' ]
130 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
132 foreach ( $res as $row ) {
133 if ( !$row->ct_tag_id
) {
137 if ( $this->hasOption( 'dry-run' ) ) {
138 $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
144 [ 'ctd_count' => $row->hitcount
],
145 [ 'ctd_id' => $row->ct_tag_id
],
149 $this->lbFactory
->waitForReplication();
152 private function updateCountTag() {
153 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
155 // This query can be pretty expensive, don't run it on master
158 [ 'ct_tag', 'hitcount' => 'count(*)' ],
161 [ 'GROUP BY' => 'ct_tag' ]
164 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
166 foreach ( $res as $row ) {
168 if ( !$row->ct_tag
) {
172 if ( $this->hasOption( 'dry-run' ) ) {
173 $this->output( 'This row will be updated: ' . $row->ct_tag
. $row->hitcount
. "\n" );
180 'ctd_name' => $row->ct_tag
,
181 'ctd_user_defined' => 0,
182 'ctd_count' => $row->hitcount
185 [ 'ctd_count' => $row->hitcount
],
189 $this->lbFactory
->waitForReplication();
192 private function backpopulateChangeTagId() {
193 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
194 $changeTagDefs = $dbr->select(
196 [ 'ctd_name', 'ctd_id' ],
199 [ 'ORDER BY' => 'ctd_id' ]
202 foreach ( $changeTagDefs as $row ) {
203 $this->backpopulateChangeTagPerTag( $row->ctd_name
, $row->ctd_id
);
207 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
208 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
209 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
210 $sleep = (int)$this->getOption( 'sleep', 10 );
212 $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
214 // Given that indexes might not be there, it's better to use replica
215 $ids = $dbr->selectFieldValues(
218 [ 'ct_tag' => $tagName, 'ct_tag_id' => null, 'ct_id > ' . $lastId ],
220 [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => 'ct_id' ]
226 $lastId = end( $ids );
228 if ( $this->hasOption( 'dry-run' ) ) {
230 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
234 $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
239 [ 'ct_tag_id' => $tagId ],
244 $this->lbFactory
->waitForReplication();
250 $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
253 protected function getUpdateKey() {
258 $maintClass = PopulateChangeTagDef
::class;
259 require_once RUN_MAINTENANCE_IF_MAIN
;