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' );
45 $this->setBatchSize( $this->getOption(
'batch-size', $this->getBatchSize() ) );
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();
73 private function setUserDefinedTags() {
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();
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();
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" );
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" );