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' );
51 if ( $dbw->fieldExists(
57 if ( $this->
hasOption(
'set-user-tags-only' ) ) {
58 $this->setUserDefinedTags();
61 if ( !$this->
hasOption(
'populate-only' ) ) {
62 $this->updateCountTag();
64 $this->backpopulateChangeTagId();
65 $this->setUserDefinedTags();
67 $this->updateCountTagId();
76 private function setUserDefinedTags() {
80 if ( $dbw->tableExists(
'valid_tag', __METHOD__ ) ) {
81 $userTags = $dbw->newSelectQueryBuilder()
84 ->caller( __METHOD__ )->fetchFieldValues();
88 $this->
output(
"No user defined tags to set, moving on...\n" );
94 'These tags will have ctd_user_defined=1 : ' . implode(
', ', $userTags ) .
"\n"
99 $dbw->newUpdateQueryBuilder()
100 ->update(
'change_tag_def' )
101 ->set( [
'ctd_user_defined' => 1 ] )
102 ->where( [
'ctd_name' => $userTags ] )
103 ->caller( __METHOD__ )
106 $this->
output(
"Finished setting user defined tags in change_tag_def table\n" );
109 private function updateCountTagId() {
113 $res = $dbr->newSelectQueryBuilder()
114 ->select( [
'ct_tag_id',
'hitcount' =>
'count(*)' ] )
115 ->from(
'change_tag' )
116 ->groupBy(
'ct_tag_id' )
117 ->caller( __METHOD__ )->fetchResultSet();
121 foreach ( $res as $row ) {
122 if ( !$row->ct_tag_id ) {
127 $this->
output(
'This row will be updated: ' . implode(
', ', $row ) .
"\n" );
131 $dbw->newUpdateQueryBuilder()
132 ->update(
'change_tag_def' )
133 ->set( [
'ctd_count' => $row->hitcount ] )
134 ->where( [
'ctd_id' => $row->ct_tag_id ] )
135 ->caller( __METHOD__ )
141 private function updateCountTag() {
145 $res = $dbr->newSelectQueryBuilder()
146 ->select( [
'ct_tag',
'hitcount' =>
'count(*)' ] )
147 ->from(
'change_tag' )
148 ->groupBy(
'ct_tag' )
149 ->caller( __METHOD__ )->fetchResultSet();
153 foreach ( $res as $row ) {
155 if ( !$row->ct_tag ) {
160 $this->
output(
'This row will be updated: ' . $row->ct_tag . $row->hitcount .
"\n" );
163 $dbw->newInsertQueryBuilder()
164 ->insertInto(
'change_tag_def' )
166 'ctd_name' => $row->ct_tag,
167 'ctd_user_defined' => 0,
168 'ctd_count' => $row->hitcount
170 ->onDuplicateKeyUpdate()
171 ->uniqueIndexFields( [
'ctd_name' ] )
172 ->set( [
'ctd_count' => $row->hitcount ] )
173 ->caller( __METHOD__ )->execute();
178 private function backpopulateChangeTagId() {
180 $changeTagDefs = $dbr->newSelectQueryBuilder()
181 ->select( [
'ctd_name',
'ctd_id' ] )
182 ->from(
'change_tag_def' )
183 ->orderBy(
'ctd_id' )
184 ->caller( __METHOD__ )->fetchResultSet();
186 foreach ( $changeTagDefs as $row ) {
187 $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
191 private function backpopulateChangeTagPerTag(
string $tagName,
int $tagId ) {
194 $sleep = (int)$this->
getOption(
'sleep', 0 );
196 $this->
output(
"Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
199 $ids = $dbr->newSelectQueryBuilder()
201 ->from(
'change_tag' )
202 ->where( [
'ct_tag' => $tagName,
'ct_tag_id' =>
null, $dbr->expr(
'ct_id',
'>', $lastId ) ] )
205 ->caller( __METHOD__ )->fetchFieldValues();
210 $lastId = end( $ids );
214 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode(
', ', $ids ) .
"\n"
218 $this->
output(
"Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
221 $dbw->newUpdateQueryBuilder()
222 ->update(
'change_tag' )
223 ->set( [
'ct_tag_id' => $tagId ] )
224 ->where( [
'ct_id' => $ids ] )
225 ->caller( __METHOD__ )
234 $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.