9require_once __DIR__ .
'/Maintenance.php';
19 parent::__construct();
20 $this->
addDescription(
'Populate and improve accuracy of change_tag_def statistics' );
21 $this->
addOption(
'dry-run',
'Print debug info instead of actually deleting' );
25 'Sleep time (in seconds) between every batch, defaults to zero',
29 $this->
addOption(
'populate-only',
'Do not update change_tag_def table' );
30 $this->
addOption(
'set-user-tags-only',
'Only update ctd_user_defined from valid_tag table' );
38 if ( $dbw->fieldExists(
44 if ( $this->
hasOption(
'set-user-tags-only' ) ) {
45 $this->setUserDefinedTags();
48 if ( !$this->
hasOption(
'populate-only' ) ) {
49 $this->updateCountTag();
51 $this->backpopulateChangeTagId();
52 $this->setUserDefinedTags();
54 $this->updateCountTagId();
63 private function setUserDefinedTags() {
67 if ( $dbw->tableExists(
'valid_tag', __METHOD__ ) ) {
68 $userTags = $dbw->newSelectQueryBuilder()
71 ->caller( __METHOD__ )->fetchFieldValues();
75 $this->
output(
"No user defined tags to set, moving on...\n" );
81 'These tags will have ctd_user_defined=1 : ' . implode(
', ', $userTags ) .
"\n"
86 $dbw->newUpdateQueryBuilder()
87 ->update(
'change_tag_def' )
88 ->set( [
'ctd_user_defined' => 1 ] )
89 ->where( [
'ctd_name' => $userTags ] )
90 ->caller( __METHOD__ )
93 $this->
output(
"Finished setting user defined tags in change_tag_def table\n" );
96 private function updateCountTagId() {
100 $res = $dbr->newSelectQueryBuilder()
101 ->select( [
'ct_tag_id',
'hitcount' =>
'count(*)' ] )
102 ->from(
'change_tag' )
103 ->groupBy(
'ct_tag_id' )
104 ->caller( __METHOD__ )->fetchResultSet();
108 foreach ( $res as $row ) {
109 if ( !$row->ct_tag_id ) {
114 $this->
output(
'This row will be updated: id ' . $row->ct_tag_id .
', ' . $row->hitcount .
" hits\n" );
118 $dbw->newUpdateQueryBuilder()
119 ->update(
'change_tag_def' )
120 ->set( [
'ctd_count' => $row->hitcount ] )
121 ->where( [
'ctd_id' => $row->ct_tag_id ] )
122 ->caller( __METHOD__ )
128 private function updateCountTag() {
132 $res = $dbr->newSelectQueryBuilder()
133 ->select( [
'ct_tag',
'hitcount' =>
'count(*)' ] )
134 ->from(
'change_tag' )
135 ->groupBy(
'ct_tag' )
136 ->caller( __METHOD__ )->fetchResultSet();
140 foreach ( $res as $row ) {
142 if ( !$row->ct_tag ) {
147 $this->
output(
'This row will be updated: ' . $row->ct_tag . $row->hitcount .
"\n" );
150 $dbw->newInsertQueryBuilder()
151 ->insertInto(
'change_tag_def' )
153 'ctd_name' => $row->ct_tag,
154 'ctd_user_defined' => 0,
155 'ctd_count' => $row->hitcount
157 ->onDuplicateKeyUpdate()
158 ->uniqueIndexFields( [
'ctd_name' ] )
159 ->set( [
'ctd_count' => $row->hitcount ] )
160 ->caller( __METHOD__ )->execute();
165 private function backpopulateChangeTagId() {
167 $changeTagDefs = $dbr->newSelectQueryBuilder()
168 ->select( [
'ctd_name',
'ctd_id' ] )
169 ->from(
'change_tag_def' )
170 ->orderBy(
'ctd_id' )
171 ->caller( __METHOD__ )->fetchResultSet();
173 foreach ( $changeTagDefs as $row ) {
174 $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
178 private function backpopulateChangeTagPerTag(
string $tagName,
int $tagId ) {
181 $sleep = (int)$this->
getOption(
'sleep', 0 );
183 $this->
output(
"Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
186 $ids = $dbr->newSelectQueryBuilder()
188 ->from(
'change_tag' )
189 ->where( [
'ct_tag' => $tagName,
'ct_tag_id' =>
null, $dbr->expr(
'ct_id',
'>', $lastId ) ] )
192 ->caller( __METHOD__ )->fetchFieldValues();
197 $lastId = end( $ids );
201 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode(
', ', $ids ) .
"\n"
205 $this->
output(
"Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
208 $dbw->newUpdateQueryBuilder()
209 ->update(
'change_tag' )
210 ->set( [
'ct_tag_id' => $tagId ] )
211 ->where( [
'ct_id' => $ids ] )
212 ->caller( __METHOD__ )
221 $this->
output(
"Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
232require_once RUN_MAINTENANCE_IF_MAIN;
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
Populate and improve accuracy of change_tag_def statistics.
getUpdateKey()
Get the update key name to go in the update log table.string
__construct()
Default constructor.
doDBUpdates()
Do the actual work.All child classes will need to implement this. Return true to log the update as do...