31 parent::__construct();
32 $this->
addDescription(
'Populate and improve accuracy of change_tag_def statistics' );
33 $this->
addOption(
'dry-run',
'Print debug info instead of actually deleting' );
37 'Sleep time (in seconds) between every batch, defaults to zero',
41 $this->
addOption(
'populate-only',
'Do not update change_tag_def table' );
42 $this->
addOption(
'set-user-tags-only',
'Only update ctd_user_defined from valid_tag table' );
46 $this->lbFactory = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
49 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
50 if (
$dbr->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() {
76 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
79 if (
$dbr->tableExists(
'valid_tag', __METHOD__ ) ) {
80 $userTags =
$dbr->selectFieldValues(
88 if ( empty( $userTags ) ) {
89 $this->
output(
"No user defined tags to set, moving on...\n" );
95 'These tags will have ctd_user_defined=1 : ' . implode(
', ', $userTags ) .
"\n"
100 $dbw = $this->lbFactory->getMainLB()->getConnectionRef(
DB_PRIMARY );
104 [
'ctd_user_defined' => 1 ],
105 [
'ctd_name' => $userTags ],
108 $this->lbFactory->waitForReplication();
109 $this->
output(
"Finished setting user defined tags in change_tag_def table\n" );
112 private function updateCountTagId() {
113 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
118 [
'ct_tag_id',
'hitcount' =>
'count(*)' ],
121 [
'GROUP BY' =>
'ct_tag_id' ]
124 $dbw = $this->lbFactory->getMainLB()->getConnectionRef(
DB_PRIMARY );
126 foreach (
$res as $row ) {
127 if ( !$row->ct_tag_id ) {
132 $this->
output(
'This row will be updated: ' . implode(
', ', $row ) .
"\n" );
138 [
'ctd_count' => $row->hitcount ],
139 [
'ctd_id' => $row->ct_tag_id ],
143 $this->lbFactory->waitForReplication();
146 private function updateCountTag() {
147 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
152 [
'ct_tag',
'hitcount' =>
'count(*)' ],
155 [
'GROUP BY' =>
'ct_tag' ]
158 $dbw = $this->lbFactory->getMainLB()->getConnectionRef(
DB_PRIMARY );
160 foreach (
$res as $row ) {
162 if ( !$row->ct_tag ) {
167 $this->
output(
'This row will be updated: ' . $row->ct_tag . $row->hitcount .
"\n" );
174 'ctd_name' => $row->ct_tag,
175 'ctd_user_defined' => 0,
176 'ctd_count' => $row->hitcount
179 [
'ctd_count' => $row->hitcount ],
183 $this->lbFactory->waitForReplication();
186 private function backpopulateChangeTagId() {
187 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
188 $changeTagDefs =
$dbr->select(
190 [
'ctd_name',
'ctd_id' ],
193 [
'ORDER BY' =>
'ctd_id' ]
196 foreach ( $changeTagDefs as $row ) {
197 $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
201 private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
202 $dbr = $this->lbFactory->getMainLB()->getConnectionRef(
DB_REPLICA );
203 $dbw = $this->lbFactory->getMainLB()->getConnectionRef(
DB_PRIMARY );
204 $sleep = (int)$this->
getOption(
'sleep', 0 );
206 $this->
output(
"Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
209 $ids =
$dbr->selectFieldValues(
212 [
'ct_tag' => $tagName,
'ct_tag_id' =>
null,
'ct_id > ' . $lastId ],
214 [
'LIMIT' => $this->
getBatchSize(),
'ORDER BY' =>
'ct_id' ]
220 $lastId = end( $ids );
224 "These ids will be changed to have \"{$tagId}\" as tag id: " . implode(
', ', $ids ) .
"\n"
228 $this->
output(
"Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
233 [
'ct_tag_id' => $tagId ],
238 $this->lbFactory->waitForReplication();
244 $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.
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.