707eb29f75833c10a24b9b4c84b696624734eae1
[lhc/web/wiklou.git] / maintenance / populateChangeTagDef.php
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.
7 *
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.
12 *
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
17 */
18
19 require_once __DIR__ . '/Maintenance.php';
20
21 /**
22 * Populate and improve accuracy of change_tag_def statistics.
23 *
24 * @ingroup Maintenance
25 */
26 class PopulateChangeTagDef extends Maintenance {
27 /** @var Wikimedia\Rdbms\ILBFactory */
28 protected $lbFactory;
29
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 );
35 $this->addOption(
36 'sleep',
37 'Sleep time (in seconds) between every batch',
38 false,
39 true
40 );
41 }
42
43 public function execute() {
44 global $wgChangeTagsSchemaMigrationStage;
45 $this->lbFactory = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
46 $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
47
48 $this->countDown( 5 );
49 if ( $wgChangeTagsSchemaMigrationStage < MIGRATION_NEW ) {
50 $this->updateCountTag();
51 $this->backpopulateChangeTagId();
52 } else {
53 $this->updateCountTagId();
54 }
55
56 // TODO: Implement
57 // $this->cleanZeroCountRows();
58 }
59
60 private function updateCountTagId() {
61 $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
62
63 // This query can be pretty expensive, don't run it on master
64 $res = $dbr->select(
65 'change_tag',
66 [ 'ct_tag_id', 'hitcount' => 'count(*)' ],
67 [],
68 __METHOD__,
69 [ 'GROUP BY' => 'ct_tag_id' ]
70 );
71
72 $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
73
74 foreach ( $res as $row ) {
75 if ( !$row->ct_tag_id ) {
76 continue;
77 }
78
79 if ( $this->hasOption( 'dry-run' ) ) {
80 $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
81 continue;
82 }
83
84 $dbw->update(
85 'change_tag_def',
86 [ 'ctd_count' => $row->hitcount ],
87 [ 'ctd_id' => $row->ct_tag_id ],
88 __METHOD__
89 );
90 }
91 $this->lbFactory->waitForReplication();
92 }
93
94 private function updateCountTag() {
95 $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
96
97 // This query can be pretty expensive, don't run it on master
98 $res = $dbr->select(
99 'change_tag',
100 [ 'ct_tag', 'hitcount' => 'count(*)' ],
101 [],
102 __METHOD__,
103 [ 'GROUP BY' => 'ct_tag' ]
104 );
105
106 $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
107
108 foreach ( $res as $row ) {
109 // Hygiene check
110 if ( !$row->ct_tag ) {
111 continue;
112 }
113
114 if ( $this->hasOption( 'dry-run' ) ) {
115 $this->output( 'This row will be updated: ' . $row->ct_tag . $row->hitcount . "\n" );
116 continue;
117 }
118
119 $dbw->upsert(
120 'change_tag_def',
121 [
122 'ctd_name' => $row->ct_tag,
123 'ctd_user_defined' => 0,
124 'ctd_count' => $row->hitcount
125 ],
126 [ 'ctd_name' ],
127 [ 'ctd_count' => $row->hitcount ],
128 __METHOD__
129 );
130 }
131 $this->lbFactory->waitForReplication();
132 }
133
134 private function backpopulateChangeTagId() {
135 $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
136 $changeTagDefs = $dbr->select(
137 'change_tag_def',
138 [ 'ctd_name', 'ctd_id' ],
139 [],
140 __METHOD__,
141 [ 'ORDER BY' => 'ctd_id' ]
142 );
143
144 foreach ( $changeTagDefs as $row ) {
145 $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
146 }
147 }
148
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 );
153 $lastId = 0;
154 $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
155 while ( true ) {
156 // Given that indexes might not be there, it's better to use replica
157 $ids = $dbr->selectFieldValues(
158 'change_tag',
159 'ct_id',
160 [ 'ct_tag' => $tagName, 'ct_tag_id' => null, 'ct_id > ' . $lastId ],
161 __METHOD__,
162 [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => 'ct_id' ]
163 );
164
165 if ( !$ids ) {
166 break;
167 }
168 $lastId = end( $ids );
169
170 if ( $this->hasOption( 'dry-run' ) ) {
171 $this->output(
172 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
173 );
174 continue;
175 } else {
176 $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
177 }
178
179 $dbw->update(
180 'change_tag',
181 [ 'ct_tag_id' => $tagId ],
182 [ 'ct_id' => $ids ],
183 __METHOD__
184 );
185
186 $this->lbFactory->waitForReplication();
187 if ( $sleep > 0 ) {
188 sleep( $sleep );
189 }
190 }
191
192 $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
193 }
194
195 }
196
197 $maintClass = PopulateChangeTagDef::class;
198 require_once RUN_MAINTENANCE_IF_MAIN;