Merge "Use statslib for metrics emitted by HtmlInputTransformHelper, HtmlToContentTra...
[mediawiki.git] / maintenance / populateChangeTagDef.php
blob7c618fcf359bd9d216b1df3c641bfe146626a104
1 <?php
2 /**
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
23 /**
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 );
34 $this->addOption(
35 'sleep',
36 'Sleep time (in seconds) between every batch, defaults to zero',
37 false,
38 true
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(
49 'change_tag',
50 'ct_tag',
51 __METHOD__
53 ) {
54 if ( $this->hasOption( 'set-user-tags-only' ) ) {
55 $this->setUserDefinedTags();
56 return true;
58 if ( !$this->hasOption( 'populate-only' ) ) {
59 $this->updateCountTag();
61 $this->backpopulateChangeTagId();
62 $this->setUserDefinedTags();
63 } else {
64 $this->updateCountTagId();
67 // TODO: Implement
68 // $this->cleanZeroCountRows();
70 return true;
73 private function setUserDefinedTags() {
74 $dbw = $this->getDB( DB_PRIMARY );
76 $userTags = null;
77 if ( $dbw->tableExists( 'valid_tag', __METHOD__ ) ) {
78 $userTags = $dbw->newSelectQueryBuilder()
79 ->select( 'vt_tag' )
80 ->from( 'valid_tag' )
81 ->caller( __METHOD__ )->fetchFieldValues();
84 if ( !$userTags ) {
85 $this->output( "No user defined tags to set, moving on...\n" );
86 return;
89 if ( $this->hasOption( 'dry-run' ) ) {
90 $this->output(
91 'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n"
93 return;
96 $dbw->newUpdateQueryBuilder()
97 ->update( 'change_tag_def' )
98 ->set( [ 'ctd_user_defined' => 1 ] )
99 ->where( [ 'ctd_name' => $userTags ] )
100 ->caller( __METHOD__ )
101 ->execute();
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 ) {
120 continue;
123 if ( $this->hasOption( 'dry-run' ) ) {
124 $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
125 continue;
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__ )
133 ->execute();
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 ) {
151 // Hygiene check
152 if ( !$row->ct_tag ) {
153 continue;
156 if ( $this->hasOption( 'dry-run' ) ) {
157 $this->output( 'This row will be updated: ' . $row->ct_tag . $row->hitcount . "\n" );
158 continue;
160 $dbw->newInsertQueryBuilder()
161 ->insertInto( 'change_tag_def' )
162 ->row( [
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 );
192 $lastId = 0;
193 $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
194 while ( true ) {
195 // Given that indexes might not be there, it's better to use replica
196 $ids = $dbr->newSelectQueryBuilder()
197 ->select( 'ct_id' )
198 ->from( 'change_tag' )
199 ->where( [ 'ct_tag' => $tagName, 'ct_tag_id' => null, $dbr->expr( 'ct_id', '>', $lastId ) ] )
200 ->orderBy( 'ct_id' )
201 ->limit( $this->getBatchSize() )
202 ->caller( __METHOD__ )->fetchFieldValues();
204 if ( !$ids ) {
205 break;
207 $lastId = end( $ids );
209 if ( $this->hasOption( 'dry-run' ) ) {
210 $this->output(
211 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
213 continue;
214 } else {
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__ )
223 ->execute();
225 $this->waitForReplication();
226 if ( $sleep > 0 ) {
227 sleep( $sleep );
231 $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
234 protected function getUpdateKey() {
235 return __CLASS__;
239 // @codeCoverageIgnoreStart
240 $maintClass = PopulateChangeTagDef::class;
241 require_once RUN_MAINTENANCE_IF_MAIN;
242 // @codeCoverageIgnoreEnd