Merge "Go search to consider fragment only title invalid"
[lhc/web/wiklou.git] / maintenance / populateChangeTagDef.php
index 3b32c00..b11b95f 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,16 +38,32 @@ class PopulateChangeTagDef extends Maintenance {
                        false,
                        true
                );
+               $this->addOption( 'populate-only', 'Do not update change_tag_def 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( 'populate-only' ) ) {
+                               $this->updateCountTag();
+                       }
                        $this->backpopulateChangeTagId();
                } else {
                        $this->updateCountTagId();
@@ -55,6 +71,8 @@ class PopulateChangeTagDef extends Maintenance {
 
                // TODO: Implement
                // $this->cleanZeroCountRows();
+
+               return true;
        }
 
        private function updateCountTagId() {
@@ -137,7 +155,8 @@ class PopulateChangeTagDef extends Maintenance {
                        'change_tag_def',
                        [ 'ctd_name', 'ctd_id' ],
                        [],
-                       __METHOD__
+                       __METHOD__,
+                       [ 'ORDER BY' => 'ctd_id' ]
                );
 
                foreach ( $changeTagDefs as $row ) {
@@ -150,6 +169,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}\n" );
                while ( true ) {
                        // Given that indexes might not be there, it's better to use replica
                        $ids = $dbr->selectFieldValues(
@@ -157,7 +177,7 @@ class PopulateChangeTagDef extends Maintenance {
                                'ct_id',
                                [ 'ct_tag' => $tagName, 'ct_tag_id' => null, 'ct_id > ' . $lastId ],
                                __METHOD__,
-                               [ 'LIMIT' => $this->getBatchSize() ]
+                               [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => 'ct_id' ]
                        );
 
                        if ( !$ids ) {
@@ -170,6 +190,8 @@ class PopulateChangeTagDef extends Maintenance {
                                        "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
                                );
                                continue;
+                       } else {
+                               $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
                        }
 
                        $dbw->update(
@@ -184,8 +206,13 @@ class PopulateChangeTagDef extends Maintenance {
                                sleep( $sleep );
                        }
                }
+
+               $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
        }
 
+       protected function getUpdateKey() {
+               return __CLASS__;
+       }
 }
 
 $maintClass = PopulateChangeTagDef::class;