28 parent::__construct();
29 $this->
addDescription(
'Populate and improve accuracy of change_tag_def statistics' );
30 $this->
addOption(
'dry-run',
'Print debug info instead of actually deleting' );
34 'Sleep time (in seconds) between every batch, defaults to zero',
38 $this->
addOption(
'populate-only',
'Do not update change_tag_def table' );
39 $this->
addOption(
'set-user-tags-only',
'Only update ctd_user_defined from valid_tag table' );
46 if ( $dbw->fieldExists(
52 if ( $this->
hasOption(
'set-user-tags-only' ) ) {
53 $this->setUserDefinedTags();
56 if ( !$this->
hasOption(
'populate-only' ) ) {
57 $this->updateCountTag();
59 $this->backpopulateChangeTagId();
60 $this->setUserDefinedTags();
62 $this->updateCountTagId();
71 private function setUserDefinedTags() {
75 if ( $dbw->tableExists(
'valid_tag', __METHOD__ ) ) {
76 $userTags = $dbw->newSelectQueryBuilder()
79 ->caller( __METHOD__ )->fetchFieldValues();
83 $this->
output(
"No user defined tags to set, moving on...\n" );
89 'These tags will have ctd_user_defined=1 : ' . implode(
', ', $userTags ) .
"\n"
94 $dbw->newUpdateQueryBuilder()
95 ->update(
'change_tag_def' )
96 ->set( [
'ctd_user_defined' => 1 ] )
97 ->where( [
'ctd_name' => $userTags ] )
98 ->caller( __METHOD__ )
101 $this->
output(
"Finished setting user defined tags in change_tag_def table\n" );
104 private function updateCountTagId() {
108 $res = $dbr->newSelectQueryBuilder()
109 ->select( [
'ct_tag_id',
'hitcount' =>
'count(*)' ] )
110 ->from(
'change_tag' )
111 ->groupBy(
'ct_tag_id' )
112 ->caller( __METHOD__ )->fetchResultSet();
116 foreach ( $res as $row ) {
117 if ( !$row->ct_tag_id ) {
122 $this->
output(
'This row will be updated: ' . implode(
', ', $row ) .
"\n" );
126 $dbw->newUpdateQueryBuilder()
127 ->update(
'change_tag_def' )
128 ->set( [
'ctd_count' => $row->hitcount ] )
129 ->where( [
'ctd_id' => $row->ct_tag_id ] )
130 ->caller( __METHOD__ )
136 private function updateCountTag() {
140 $res = $dbr->newSelectQueryBuilder()
141 ->select( [
'ct_tag',
'hitcount' =>
'count(*)' ] )
142 ->from(
'change_tag' )
143 ->groupBy(
'ct_tag' )
144 ->caller( __METHOD__ )->fetchResultSet();
148 foreach ( $res as $row ) {
150 if ( !$row->ct_tag ) {
155 $this->
output(
'This row will be updated: ' . $row->ct_tag . $row->hitcount .
"\n" );
158 $dbw->newInsertQueryBuilder()
159 ->insertInto(
'change_tag_def' )
161 'ctd_name' => $row->ct_tag,
162 'ctd_user_defined' => 0,
163 'ctd_count' => $row->hitcount
165 ->onDuplicateKeyUpdate()
166 ->uniqueIndexFields( [
'ctd_name' ] )
167 ->set( [
'ctd_count' => $row->hitcount ] )
168 ->caller( __METHOD__ )->execute();
173 private function backpopulateChangeTagId() {
175 $changeTagDefs = $dbr->newSelectQueryBuilder()
176 ->select( [
'ctd_name',
'ctd_id' ] )
177 ->from(
'change_tag_def' )
178 ->orderBy(
'ctd_id' )
179 ->caller( __METHOD__ )->fetchResultSet();
181 foreach ( $changeTagDefs as $row ) {
182 $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
186 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
189 $sleep = (int)$this->
getOption(
'sleep', 0 );
191 $this->
output(
"Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
194 $ids = $dbr->newSelectQueryBuilder()
196 ->from(
'change_tag' )
197 ->where( [
'ct_tag' => $tagName,
'ct_tag_id' =>
null,
'ct_id > ' . $lastId ] )
200 ->caller( __METHOD__ )->fetchFieldValues();
205 $lastId = end( $ids );
209 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode(
', ', $ids ) .
"\n"
213 $this->
output(
"Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
216 $dbw->newUpdateQueryBuilder()
217 ->update(
'change_tag' )
218 ->set( [
'ct_tag_id' => $tagId ] )
219 ->where( [
'ct_id' => $ids ] )
220 ->caller( __METHOD__ )
229 $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...
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.