Merge "Add part to update ctd_user_defined in populateChangeTagDef"
[lhc/web/wiklou.git] / maintenance / populateChangeTagDef.php
index b2e976c..7bec25a 100644 (file)
@@ -23,7 +23,7 @@ require_once __DIR__ . '/Maintenance.php';
  *
  * @ingroup Maintenance
  */
-class PopulateChangeTagDef extends Maintenance {
+class PopulateChangeTagDef extends LoggedUpdateMaintenance {
        /** @var Wikimedia\Rdbms\ILBFactory */
        protected $lbFactory;
 
@@ -38,23 +38,81 @@ class PopulateChangeTagDef extends Maintenance {
                        false,
                        true
                );
+               $this->addOption( 'populate-only', 'Do not update change_tag_def table' );
+               $this->addOption( 'set-user-tags-only', 'Only update ctd_user_defined from valid_tag table' );
        }
 
        public function execute() {
                global $wgChangeTagsSchemaMigrationStage;
+               if ( $wgChangeTagsSchemaMigrationStage === MIGRATION_OLD ) {
+                       // Return "success", but don't flag it as done so the next run will retry
+                       $this->output( '... Not run, $wgChangeTagsSchemaMigrationStage === MIGRATION_OLD' . "\n" );
+                       return true;
+               }
+               return parent::execute();
+       }
+
+       protected function doDBUpdates() {
                $this->lbFactory = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
 
-               $this->countDown( 5 );
-               if ( $wgChangeTagsSchemaMigrationStage < MIGRATION_NEW ) {
-                       $this->updateCountTag();
+               if ( $this->lbFactory->getMainLB()->getConnection( DB_REPLICA )->fieldExists(
+                               'change_tag',
+                               'ct_tag',
+                               __METHOD__
+                       )
+               ) {
+                       if ( $this->hasOption( 'set-user-tags-only' ) ) {
+                               $this->setUserDefinedTags();
+                               return true;
+                       }
+                       if ( !$this->hasOption( 'populate-only' ) ) {
+                               $this->updateCountTag();
+                       }
                        $this->backpopulateChangeTagId();
+                       $this->setUserDefinedTags();
                } else {
                        $this->updateCountTagId();
                }
 
                // TODO: Implement
                // $this->cleanZeroCountRows();
+
+               return true;
+       }
+
+       private function setUserDefinedTags() {
+               $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
+
+               $userTags = $dbr->selectFieldValues(
+                       'valid_tag',
+                       'vt_tag',
+                       [],
+                       __METHOD__
+               );
+
+               if ( empty( $userTags ) ) {
+                       $this->output( "No user defined tags to set, moving on...\n" );
+                       return;
+               }
+
+               if ( $this->hasOption( 'dry-run' ) ) {
+                       $this->output(
+                               'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n"
+                       );
+                       return;
+               }
+
+               $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
+
+               $dbw->update(
+                       'change_tag_def',
+                       [ 'ctd_user_defined' => 1 ],
+                       [ 'ctd_name' => $userTags ],
+                       __METHOD__
+               );
+               $this->lbFactory->waitForReplication();
+               $this->output( "Finished setting user defined tags in change_tag_def table\n" );
        }
 
        private function updateCountTagId() {
@@ -151,7 +209,7 @@ class PopulateChangeTagDef extends Maintenance {
                $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
                $sleep = (int)$this->getOption( 'sleep', 10 );
                $lastId = 0;
-               $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}" );
+               $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
                while ( true ) {
                        // Given that indexes might not be there, it's better to use replica
                        $ids = $dbr->selectFieldValues(
@@ -173,7 +231,7 @@ class PopulateChangeTagDef extends Maintenance {
                                );
                                continue;
                        } else {
-                               $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}" );
+                               $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
                        }
 
                        $dbw->update(
@@ -189,9 +247,12 @@ class PopulateChangeTagDef extends Maintenance {
                        }
                }
 
-               $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}" );
+               $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
        }
 
+       protected function getUpdateKey() {
+               return __CLASS__;
+       }
 }
 
 $maintClass = PopulateChangeTagDef::class;