32 parent::__construct();
33 $this->
addDescription(
'Populate and improve accuracy of change_tag_def statistics' );
34 $this->
addOption(
'dry-run',
'Print debug info instead of actually deleting' );
38 'Sleep time (in seconds) between every batch, defaults to zero',
42 $this->
addOption(
'populate-only',
'Do not update change_tag_def table' );
43 $this->
addOption(
'set-user-tags-only',
'Only update ctd_user_defined from valid_tag table' );
50 if ( $dbw->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() {
79 if ( $dbw->tableExists(
'valid_tag', __METHOD__ ) ) {
80 $userTags = $dbw->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->newUpdateQueryBuilder()
99 ->update(
'change_tag_def' )
100 ->set( [
'ctd_user_defined' => 1 ] )
101 ->where( [
'ctd_name' => $userTags ] )
102 ->caller( __METHOD__ )
105 $this->
output(
"Finished setting user defined tags in change_tag_def table\n" );
108 private function updateCountTagId() {
112 $res = $dbr->newSelectQueryBuilder()
113 ->select( [
'ct_tag_id',
'hitcount' =>
'count(*)' ] )
114 ->from(
'change_tag' )
115 ->groupBy(
'ct_tag_id' )
116 ->caller( __METHOD__ )->fetchResultSet();
120 foreach ( $res as $row ) {
121 if ( !$row->ct_tag_id ) {
126 $this->
output(
'This row will be updated: ' . implode(
', ', $row ) .
"\n" );
130 $dbw->newUpdateQueryBuilder()
131 ->update(
'change_tag_def' )
132 ->set( [
'ctd_count' => $row->hitcount ] )
133 ->where( [
'ctd_id' => $row->ct_tag_id ] )
134 ->caller( __METHOD__ )
140 private function updateCountTag() {
144 $res = $dbr->newSelectQueryBuilder()
145 ->select( [
'ct_tag',
'hitcount' =>
'count(*)' ] )
146 ->from(
'change_tag' )
147 ->groupBy(
'ct_tag' )
148 ->caller( __METHOD__ )->fetchResultSet();
152 foreach ( $res as $row ) {
154 if ( !$row->ct_tag ) {
159 $this->
output(
'This row will be updated: ' . $row->ct_tag . $row->hitcount .
"\n" );
162 $dbw->newInsertQueryBuilder()
163 ->insertInto(
'change_tag_def' )
165 'ctd_name' => $row->ct_tag,
166 'ctd_user_defined' => 0,
167 'ctd_count' => $row->hitcount
169 ->onDuplicateKeyUpdate()
170 ->uniqueIndexFields( [
'ctd_name' ] )
171 ->set( [
'ctd_count' => $row->hitcount ] )
172 ->caller( __METHOD__ )->execute();
177 private function backpopulateChangeTagId() {
179 $changeTagDefs = $dbr->newSelectQueryBuilder()
180 ->select( [
'ctd_name',
'ctd_id' ] )
181 ->from(
'change_tag_def' )
182 ->orderBy(
'ctd_id' )
183 ->caller( __METHOD__ )->fetchResultSet();
185 foreach ( $changeTagDefs as $row ) {
186 $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
190 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
193 $sleep = (int)$this->
getOption(
'sleep', 0 );
195 $this->
output(
"Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
198 $ids = $dbr->newSelectQueryBuilder()
200 ->from(
'change_tag' )
201 ->where( [
'ct_tag' => $tagName,
'ct_tag_id' =>
null, $dbr->expr(
'ct_id',
'>', $lastId ) ] )
204 ->caller( __METHOD__ )->fetchFieldValues();
209 $lastId = end( $ids );
213 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode(
', ', $ids ) .
"\n"
217 $this->
output(
"Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
220 $dbw->newUpdateQueryBuilder()
221 ->update(
'change_tag' )
222 ->set( [
'ct_tag_id' => $tagId ] )
223 ->where( [
'ct_id' => $ids ] )
224 ->caller( __METHOD__ )
233 $this->
output(
"Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.