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, defaults to zero',
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 protected function doDBUpdates() {
46 $this->lbFactory
= MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
47 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
49 if ( $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
)->fieldExists(
55 if ( $this->hasOption( 'set-user-tags-only' ) ) {
56 $this->setUserDefinedTags();
59 if ( !$this->hasOption( 'populate-only' ) ) {
60 $this->updateCountTag();
62 $this->backpopulateChangeTagId();
63 $this->setUserDefinedTags();
65 $this->updateCountTagId();
69 // $this->cleanZeroCountRows();
74 private function setUserDefinedTags() {
75 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
77 $userTags = $dbr->selectFieldValues(
84 if ( empty( $userTags ) ) {
85 $this->output( "No user defined tags to set, moving on...\n" );
89 if ( $this->hasOption( 'dry-run' ) ) {
91 'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n"
96 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
100 [ 'ctd_user_defined' => 1 ],
101 [ 'ctd_name' => $userTags ],
104 $this->lbFactory
->waitForReplication();
105 $this->output( "Finished setting user defined tags in change_tag_def table\n" );
108 private function updateCountTagId() {
109 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
111 // This query can be pretty expensive, don't run it on master
114 [ 'ct_tag_id', 'hitcount' => 'count(*)' ],
117 [ 'GROUP BY' => 'ct_tag_id' ]
120 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
122 foreach ( $res as $row ) {
123 if ( !$row->ct_tag_id
) {
127 if ( $this->hasOption( 'dry-run' ) ) {
128 $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
134 [ 'ctd_count' => $row->hitcount
],
135 [ 'ctd_id' => $row->ct_tag_id
],
139 $this->lbFactory
->waitForReplication();
142 private function updateCountTag() {
143 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
145 // This query can be pretty expensive, don't run it on master
148 [ 'ct_tag', 'hitcount' => 'count(*)' ],
151 [ 'GROUP BY' => 'ct_tag' ]
154 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
156 foreach ( $res as $row ) {
158 if ( !$row->ct_tag
) {
162 if ( $this->hasOption( 'dry-run' ) ) {
163 $this->output( 'This row will be updated: ' . $row->ct_tag
. $row->hitcount
. "\n" );
170 'ctd_name' => $row->ct_tag
,
171 'ctd_user_defined' => 0,
172 'ctd_count' => $row->hitcount
175 [ 'ctd_count' => $row->hitcount
],
179 $this->lbFactory
->waitForReplication();
182 private function backpopulateChangeTagId() {
183 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
184 $changeTagDefs = $dbr->select(
186 [ 'ctd_name', 'ctd_id' ],
189 [ 'ORDER BY' => 'ctd_id' ]
192 foreach ( $changeTagDefs as $row ) {
193 $this->backpopulateChangeTagPerTag( $row->ctd_name
, $row->ctd_id
);
197 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
198 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
199 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
200 $sleep = (int)$this->getOption( 'sleep', 0 );
202 $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
204 // Given that indexes might not be there, it's better to use replica
205 $ids = $dbr->selectFieldValues(
208 [ 'ct_tag' => $tagName, 'ct_tag_id' => null, 'ct_id > ' . $lastId ],
210 [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => 'ct_id' ]
216 $lastId = end( $ids );
218 if ( $this->hasOption( 'dry-run' ) ) {
220 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
224 $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
229 [ 'ct_tag_id' => $tagId ],
234 $this->lbFactory
->waitForReplication();
240 $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
243 protected function getUpdateKey() {
248 $maintClass = PopulateChangeTagDef
::class;
249 require_once RUN_MAINTENANCE_IF_MAIN
;