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