19 require_once __DIR__ .
'/Maintenance.php';
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' );
37 'Sleep time (in seconds) between every batch, defaults to zero',
41 $this->
addOption(
'populate-only',
'Do not update change_tag_def table' );
42 $this->
addOption(
'set-user-tags-only',
'Only update ctd_user_defined from valid_tag table' );
49 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
50 if ( $dbr->fieldExists(
56 if ( $this->
hasOption(
'set-user-tags-only' ) ) {
57 $this->setUserDefinedTags();
60 if ( !$this->
hasOption(
'populate-only' ) ) {
61 $this->updateCountTag();
63 $this->backpopulateChangeTagId();
64 $this->setUserDefinedTags();
66 $this->updateCountTagId();
75 private function setUserDefinedTags() {
76 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
79 if ( $dbr->tableExists(
'valid_tag', __METHOD__ ) ) {
80 $userTags = $dbr->newSelectQueryBuilder()
83 ->caller( __METHOD__ )->fetchFieldValues();
87 $this->
output(
"No user defined tags to set, moving on...\n" );
93 'These tags will have ctd_user_defined=1 : ' . implode(
', ', $userTags ) .
"\n"
98 $dbw = $this->lbFactory->getMainLB()->getConnectionRef(
DB_PRIMARY );
102 [
'ctd_user_defined' => 1 ],
103 [
'ctd_name' => $userTags ],
106 $this->lbFactory->waitForReplication();
107 $this->
output(
"Finished setting user defined tags in change_tag_def table\n" );
110 private function updateCountTagId() {
111 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
114 $res = $dbr->newSelectQueryBuilder()
115 ->select( [
'ct_tag_id',
'hitcount' =>
'count(*)' ] )
116 ->from(
'change_tag' )
117 ->groupBy(
'ct_tag_id' )
118 ->caller( __METHOD__ )->fetchResultSet();
120 $dbw = $this->lbFactory->getMainLB()->getConnectionRef(
DB_PRIMARY );
122 foreach ( $res as $row ) {
123 if ( !$row->ct_tag_id ) {
128 $this->
output(
'This row will be updated: ' . implode(
', ', $row ) .
"\n" );
134 [
'ctd_count' => $row->hitcount ],
135 [
'ctd_id' => $row->ct_tag_id ],
139 $this->lbFactory->waitForReplication();
142 private function updateCountTag() {
143 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
146 $res = $dbr->newSelectQueryBuilder()
147 ->select( [
'ct_tag',
'hitcount' =>
'count(*)' ] )
148 ->from(
'change_tag' )
149 ->groupBy(
'ct_tag' )
150 ->caller( __METHOD__ )->fetchResultSet();
152 $dbw = $this->lbFactory->getMainLB()->getConnectionRef(
DB_PRIMARY );
154 foreach ( $res as $row ) {
156 if ( !$row->ct_tag ) {
161 $this->
output(
'This row will be updated: ' . $row->ct_tag . $row->hitcount .
"\n" );
164 $dbw->newInsertQueryBuilder()
165 ->insertInto(
'change_tag_def' )
167 'ctd_name' => $row->ct_tag,
168 'ctd_user_defined' => 0,
169 'ctd_count' => $row->hitcount
171 ->onDuplicateKeyUpdate()
172 ->uniqueIndexFields( [
'ctd_name' ] )
173 ->set( [
'ctd_count' => $row->hitcount ] )
174 ->caller( __METHOD__ )->execute();
176 $this->lbFactory->waitForReplication();
179 private function backpopulateChangeTagId() {
180 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
181 $changeTagDefs = $dbr->newSelectQueryBuilder()
182 ->select( [
'ctd_name',
'ctd_id' ] )
183 ->from(
'change_tag_def' )
184 ->orderBy(
'ctd_id' )
185 ->caller( __METHOD__ )->fetchResultSet();
187 foreach ( $changeTagDefs as $row ) {
188 $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
192 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
193 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
194 $dbw = $this->lbFactory->getMainLB()->getConnectionRef(
DB_PRIMARY );
195 $sleep = (int)$this->
getOption(
'sleep', 0 );
197 $this->
output(
"Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
200 $ids = $dbr->newSelectQueryBuilder()
202 ->from(
'change_tag' )
203 ->where( [
'ct_tag' => $tagName,
'ct_tag_id' =>
null,
'ct_id > ' . $lastId ] )
206 ->caller( __METHOD__ )->fetchFieldValues();
211 $lastId = end( $ids );
215 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode(
', ', $ids ) .
"\n"
219 $this->
output(
"Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
224 [
'ct_tag_id' => $tagId ],
229 $this->lbFactory->waitForReplication();
235 $this->
output(
"Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
244 require_once RUN_MAINTENANCE_IF_MAIN;
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
getUpdateKey()
Get the update key name to go in the update log table.
__construct()
Default constructor.
Wikimedia Rdbms ILBFactory $lbFactory
doDBUpdates()
Do the actual work.