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 use MediaWiki\Maintenance\LoggedUpdateMaintenance
;
21 // @codeCoverageIgnoreStart
22 require_once __DIR__
. '/Maintenance.php';
23 // @codeCoverageIgnoreEnd
26 * Populate and improve accuracy of change_tag_def statistics.
28 * @ingroup Maintenance
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 );
38 'Sleep time (in seconds) between every batch, defaults to zero',
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' );
46 protected function doDBUpdates() {
47 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
49 $dbw = $this->getDB( DB_PRIMARY
);
50 if ( $dbw->fieldExists(
56 if ( $this->hasOption( 'set-user-tags-only' ) ) {
57 $this->setUserDefinedTags();
60 if ( !$this->hasOption( 'populate-only' ) ) {
61 $this->updateCountTag();
63 $this->backpopulateChangeTagId();
64 $this->setUserDefinedTags();
66 $this->updateCountTagId();
70 // $this->cleanZeroCountRows();
75 private function setUserDefinedTags() {
76 $dbw = $this->getDB( DB_PRIMARY
);
79 if ( $dbw->tableExists( 'valid_tag', __METHOD__
) ) {
80 $userTags = $dbw->newSelectQueryBuilder()
83 ->caller( __METHOD__
)->fetchFieldValues();
87 $this->output( "No user defined tags to set, moving on...\n" );
91 if ( $this->hasOption( 'dry-run' ) ) {
93 'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n"
98 $dbw->newUpdateQueryBuilder()
99 ->update( 'change_tag_def' )
100 ->set( [ 'ctd_user_defined' => 1 ] )
101 ->where( [ 'ctd_name' => $userTags ] )
102 ->caller( __METHOD__
)
104 $this->waitForReplication();
105 $this->output( "Finished setting user defined tags in change_tag_def table\n" );
108 private function updateCountTagId() {
109 $dbr = $this->getReplicaDB();
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();
118 $dbw = $this->getPrimaryDB();
120 foreach ( $res as $row ) {
121 if ( !$row->ct_tag_id
) {
125 if ( $this->hasOption( 'dry-run' ) ) {
126 $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
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__
)
137 $this->waitForReplication();
140 private function updateCountTag() {
141 $dbr = $this->getReplicaDB();
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();
150 $dbw = $this->getPrimaryDB();
152 foreach ( $res as $row ) {
154 if ( !$row->ct_tag
) {
158 if ( $this->hasOption( 'dry-run' ) ) {
159 $this->output( 'This row will be updated: ' . $row->ct_tag
. $row->hitcount
. "\n" );
162 $dbw->newInsertQueryBuilder()
163 ->insertInto( 'change_tag_def' )
165 'ctd_name' => $row->ct_tag
,
166 'ctd_user_defined' => 0,
167 'ctd_count' => $row->hitcount
169 ->onDuplicateKeyUpdate()
170 ->uniqueIndexFields( [ 'ctd_name' ] )
171 ->set( [ 'ctd_count' => $row->hitcount
] )
172 ->caller( __METHOD__
)->execute();
174 $this->waitForReplication();
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();
185 foreach ( $changeTagDefs as $row ) {
186 $this->backpopulateChangeTagPerTag( $row->ctd_name
, $row->ctd_id
);
190 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
191 $dbr = $this->getReplicaDB();
192 $dbw = $this->getPrimaryDB();
193 $sleep = (int)$this->getOption( 'sleep', 0 );
195 $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
197 // Given that indexes might not be there, it's better to use replica
198 $ids = $dbr->newSelectQueryBuilder()
200 ->from( 'change_tag' )
201 ->where( [ 'ct_tag' => $tagName, 'ct_tag_id' => null, $dbr->expr( 'ct_id', '>', $lastId ) ] )
203 ->limit( $this->getBatchSize() )
204 ->caller( __METHOD__
)->fetchFieldValues();
209 $lastId = end( $ids );
211 if ( $this->hasOption( 'dry-run' ) ) {
213 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
217 $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
220 $dbw->newUpdateQueryBuilder()
221 ->update( 'change_tag' )
222 ->set( [ 'ct_tag_id' => $tagId ] )
223 ->where( [ 'ct_id' => $ids ] )
224 ->caller( __METHOD__
)
227 $this->waitForReplication();
233 $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
236 protected function getUpdateKey() {
241 // @codeCoverageIgnoreStart
242 $maintClass = PopulateChangeTagDef
::class;
243 require_once RUN_MAINTENANCE_IF_MAIN
;
244 // @codeCoverageIgnoreEnd