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 // @codeCoverageIgnoreStart
20 require_once __DIR__
. '/Maintenance.php';
21 // @codeCoverageIgnoreEnd
24 * Populate and improve accuracy of change_tag_def statistics.
26 * @ingroup Maintenance
28 class PopulateChangeTagDef
extends LoggedUpdateMaintenance
{
29 public function __construct() {
30 parent
::__construct();
31 $this->addDescription( 'Populate and improve accuracy of change_tag_def statistics' );
32 $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
33 $this->setBatchSize( 1000 );
36 'Sleep time (in seconds) between every batch, defaults to zero',
40 $this->addOption( 'populate-only', 'Do not update change_tag_def table' );
41 $this->addOption( 'set-user-tags-only', 'Only update ctd_user_defined from valid_tag table' );
44 protected function doDBUpdates() {
45 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
47 $dbw = $this->getDB( DB_PRIMARY
);
48 if ( $dbw->fieldExists(
54 if ( $this->hasOption( 'set-user-tags-only' ) ) {
55 $this->setUserDefinedTags();
58 if ( !$this->hasOption( 'populate-only' ) ) {
59 $this->updateCountTag();
61 $this->backpopulateChangeTagId();
62 $this->setUserDefinedTags();
64 $this->updateCountTagId();
68 // $this->cleanZeroCountRows();
73 private function setUserDefinedTags() {
74 $dbw = $this->getDB( DB_PRIMARY
);
77 if ( $dbw->tableExists( 'valid_tag', __METHOD__
) ) {
78 $userTags = $dbw->newSelectQueryBuilder()
81 ->caller( __METHOD__
)->fetchFieldValues();
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->newUpdateQueryBuilder()
97 ->update( 'change_tag_def' )
98 ->set( [ 'ctd_user_defined' => 1 ] )
99 ->where( [ 'ctd_name' => $userTags ] )
100 ->caller( __METHOD__
)
102 $this->waitForReplication();
103 $this->output( "Finished setting user defined tags in change_tag_def table\n" );
106 private function updateCountTagId() {
107 $dbr = $this->getReplicaDB();
109 // This query can be pretty expensive, don't run it on master
110 $res = $dbr->newSelectQueryBuilder()
111 ->select( [ 'ct_tag_id', 'hitcount' => 'count(*)' ] )
112 ->from( 'change_tag' )
113 ->groupBy( 'ct_tag_id' )
114 ->caller( __METHOD__
)->fetchResultSet();
116 $dbw = $this->getPrimaryDB();
118 foreach ( $res as $row ) {
119 if ( !$row->ct_tag_id
) {
123 if ( $this->hasOption( 'dry-run' ) ) {
124 $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
128 $dbw->newUpdateQueryBuilder()
129 ->update( 'change_tag_def' )
130 ->set( [ 'ctd_count' => $row->hitcount
] )
131 ->where( [ 'ctd_id' => $row->ct_tag_id
] )
132 ->caller( __METHOD__
)
135 $this->waitForReplication();
138 private function updateCountTag() {
139 $dbr = $this->getReplicaDB();
141 // This query can be pretty expensive, don't run it on master
142 $res = $dbr->newSelectQueryBuilder()
143 ->select( [ 'ct_tag', 'hitcount' => 'count(*)' ] )
144 ->from( 'change_tag' )
145 ->groupBy( 'ct_tag' )
146 ->caller( __METHOD__
)->fetchResultSet();
148 $dbw = $this->getPrimaryDB();
150 foreach ( $res as $row ) {
152 if ( !$row->ct_tag
) {
156 if ( $this->hasOption( 'dry-run' ) ) {
157 $this->output( 'This row will be updated: ' . $row->ct_tag
. $row->hitcount
. "\n" );
160 $dbw->newInsertQueryBuilder()
161 ->insertInto( 'change_tag_def' )
163 'ctd_name' => $row->ct_tag
,
164 'ctd_user_defined' => 0,
165 'ctd_count' => $row->hitcount
167 ->onDuplicateKeyUpdate()
168 ->uniqueIndexFields( [ 'ctd_name' ] )
169 ->set( [ 'ctd_count' => $row->hitcount
] )
170 ->caller( __METHOD__
)->execute();
172 $this->waitForReplication();
175 private function backpopulateChangeTagId() {
176 $dbr = $this->getReplicaDB();
177 $changeTagDefs = $dbr->newSelectQueryBuilder()
178 ->select( [ 'ctd_name', 'ctd_id' ] )
179 ->from( 'change_tag_def' )
180 ->orderBy( 'ctd_id' )
181 ->caller( __METHOD__
)->fetchResultSet();
183 foreach ( $changeTagDefs as $row ) {
184 $this->backpopulateChangeTagPerTag( $row->ctd_name
, $row->ctd_id
);
188 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
189 $dbr = $this->getReplicaDB();
190 $dbw = $this->getPrimaryDB();
191 $sleep = (int)$this->getOption( 'sleep', 0 );
193 $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
195 // Given that indexes might not be there, it's better to use replica
196 $ids = $dbr->newSelectQueryBuilder()
198 ->from( 'change_tag' )
199 ->where( [ 'ct_tag' => $tagName, 'ct_tag_id' => null, $dbr->expr( 'ct_id', '>', $lastId ) ] )
201 ->limit( $this->getBatchSize() )
202 ->caller( __METHOD__
)->fetchFieldValues();
207 $lastId = end( $ids );
209 if ( $this->hasOption( 'dry-run' ) ) {
211 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
215 $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
218 $dbw->newUpdateQueryBuilder()
219 ->update( 'change_tag' )
220 ->set( [ 'ct_tag_id' => $tagId ] )
221 ->where( [ 'ct_id' => $ids ] )
222 ->caller( __METHOD__
)
225 $this->waitForReplication();
231 $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
234 protected function getUpdateKey() {
239 // @codeCoverageIgnoreStart
240 $maintClass = PopulateChangeTagDef
::class;
241 require_once RUN_MAINTENANCE_IF_MAIN
;
242 // @codeCoverageIgnoreEnd