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 Maintenance
{
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',
43 public function execute() {
44 global $wgChangeTagsSchemaMigrationStage;
45 $this->lbFactory
= MediaWiki\MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
46 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
48 $this->countDown( 5 );
49 if ( $wgChangeTagsSchemaMigrationStage < MIGRATION_NEW
) {
50 $this->updateCountTag();
51 $this->backpopulateChangeTagId();
53 $this->updateCountTagId();
57 // $this->cleanZeroCountRows();
60 private function updateCountTagId() {
61 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
63 // This query can be pretty expensive, don't run it on master
66 [ 'ct_tag_id', 'hitcount' => 'count(*)' ],
69 [ 'GROUP BY' => 'ct_tag_id' ]
72 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
74 foreach ( $res as $row ) {
75 if ( !$row->ct_tag_id
) {
79 if ( $this->hasOption( 'dry-run' ) ) {
80 $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
86 [ 'ctd_count' => $row->hitcount
],
87 [ 'ctd_id' => $row->ct_tag_id
],
91 $this->lbFactory
->waitForReplication();
94 private function updateCountTag() {
95 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
97 // This query can be pretty expensive, don't run it on master
100 [ 'ct_tag', 'hitcount' => 'count(*)' ],
103 [ 'GROUP BY' => 'ct_tag' ]
106 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
108 foreach ( $res as $row ) {
110 if ( !$row->ct_tag
) {
114 if ( $this->hasOption( 'dry-run' ) ) {
115 $this->output( 'This row will be updated: ' . $row->ct_tag
. $row->hitcount
. "\n" );
122 'ctd_name' => $row->ct_tag
,
123 'ctd_user_defined' => 0,
124 'ctd_count' => $row->hitcount
127 [ 'ctd_count' => $row->hitcount
],
131 $this->lbFactory
->waitForReplication();
134 private function backpopulateChangeTagId() {
135 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
136 $changeTagDefs = $dbr->select(
138 [ 'ctd_name', 'ctd_id' ],
141 [ 'ORDER BY' => 'ctd_id' ]
144 foreach ( $changeTagDefs as $row ) {
145 $this->backpopulateChangeTagPerTag( $row->ctd_name
, $row->ctd_id
);
149 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
150 $dbr = $this->lbFactory
->getMainLB()->getConnection( DB_REPLICA
);
151 $dbw = $this->lbFactory
->getMainLB()->getConnection( DB_MASTER
);
152 $sleep = (int)$this->getOption( 'sleep', 10 );
154 $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
156 // Given that indexes might not be there, it's better to use replica
157 $ids = $dbr->selectFieldValues(
160 [ 'ct_tag' => $tagName, 'ct_tag_id' => null, 'ct_id > ' . $lastId ],
162 [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => 'ct_id' ]
168 $lastId = end( $ids );
170 if ( $this->hasOption( 'dry-run' ) ) {
172 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
176 $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
181 [ 'ct_tag_id' => $tagId ],
186 $this->lbFactory
->waitForReplication();
192 $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
197 $maintClass = PopulateChangeTagDef
::class;
198 require_once RUN_MAINTENANCE_IF_MAIN
;