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' );
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' );
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" );
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__ )
103 $this->
output(
"Finished setting user defined tags in change_tag_def table\n" );
106 private function updateCountTagId() {
110 $res = $dbr->newSelectQueryBuilder()
111 ->select( [
'ct_tag_id',
'hitcount' =>
'count(*)' ] )
112 ->from(
'change_tag' )
113 ->groupBy(
'ct_tag_id' )
114 ->caller( __METHOD__ )->fetchResultSet();
118 foreach ( $res as $row ) {
119 if ( !$row->ct_tag_id ) {
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__ )
138 private function updateCountTag() {
142 $res = $dbr->newSelectQueryBuilder()
143 ->select( [
'ct_tag',
'hitcount' =>
'count(*)' ] )
144 ->from(
'change_tag' )
145 ->groupBy(
'ct_tag' )
146 ->caller( __METHOD__ )->fetchResultSet();
150 foreach ( $res as $row ) {
152 if ( !$row->ct_tag ) {
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();
175 private function backpopulateChangeTagId() {
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 ) {
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 ) ] )
202 ->caller( __METHOD__ )->fetchFieldValues();
207 $lastId = end( $ids );
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__ )
231 $this->
output(
"Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
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.