MediaWiki  master
migrateImageCommentTemp.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
33  public function __construct() {
34  parent::__construct();
35  $this->addDescription(
36  'Merges image_comment_temp into the image table'
37  );
38  }
39 
40  protected function getUpdateKey() {
41  return __CLASS__;
42  }
43 
44  protected function doDBUpdates() {
45  $batchSize = $this->getBatchSize();
46 
47  $dbw = $this->getDB( DB_PRIMARY );
48  if ( !$dbw->fieldExists( 'image', 'img_description_id', __METHOD__ ) ) {
49  $this->output( "Run update.php to create img_description_id.\n" );
50  return false;
51  }
52  if ( !$dbw->tableExists( 'image_comment_temp', __METHOD__ ) ) {
53  $this->output( "image_comment_temp does not exist, so nothing to do.\n" );
54  return true;
55  }
56 
57  $this->output( "Merging image_comment_temp into the image table...\n" );
58  $conds = [];
59  $updated = 0;
60  $deleted = 0;
61  while ( true ) {
62  $this->beginTransaction( $dbw, __METHOD__ );
63 
64  $res = $dbw->select(
65  [ 'image_comment_temp', 'image' ],
66  [
67  'name' => 'imgcomment_name',
68  'oldid' => 'imgcomment_description_id',
69  'newid' => 'img_description_id',
70  ],
71  $conds,
72  __METHOD__,
73  [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'name' ] ],
74  [ 'image' => [ 'JOIN', 'img_name = imgcomment_name' ] ]
75  );
76  $numRows = $res->numRows();
77 
78  $toDelete = [];
79  $last = null;
80  foreach ( $res as $row ) {
81  $last = $row->name;
82  $toDelete[] = $row->name;
83  if ( !$row->newid ) {
84  $dbw->update(
85  'image',
86  [ 'img_description_id' => $row->oldid ],
87  [ 'img_name' => $row->name ],
88  __METHOD__
89  );
90  $updated++;
91  } elseif ( $row->newid !== $row->oldid ) {
92  $this->error(
93  "Image \"$row->name\" has img_description_id = $row->newid and "
94  . "imgcomment_description_id = $row->oldid. Ignoring the latter."
95  );
96  }
97  }
98  if ( $toDelete ) {
99  $dbw->delete( 'image_comment_temp', [ 'imgcomment_name' => $toDelete ], __METHOD__ );
100  $deleted += count( $toDelete );
101  }
102 
103  $this->commitTransaction( $dbw, __METHOD__ );
104 
105  if ( $numRows < $batchSize ) {
106  // We must have reached the end
107  break;
108  }
109 
110  $this->output( "... $last, updated $updated, deleted $deleted\n" );
111  $conds = [ 'imgcomment_name > ' . $dbw->addQuotes( $last ) ];
112  }
113 
114  // This should be 0, so it should be very fast
115  $count = (int)$dbw->selectField( 'image_comment_temp', 'COUNT(*)', [], __METHOD__ );
116  if ( $count !== 0 ) {
117  $this->error( "Ignoring $count orphaned image_comment_temp row(s)." );
118  }
119 
120  $this->output(
121  "Completed merge of image_comment_temp into the image table, "
122  . "$updated image rows updated, $deleted image_comment_temp rows deleted.\n"
123  );
124 
125  return true;
126  }
127 }
128 
129 $maintClass = MigrateImageCommentTemp::class;
130 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
$res
$res
Definition: testCompression.php:57
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
Definition: Maintenance.php:1406
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
MigrateImageCommentTemp\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: migrateImageCommentTemp.php:44
$maintClass
$maintClass
Definition: migrateImageCommentTemp.php:129
MigrateImageCommentTemp\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: migrateImageCommentTemp.php:40
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1421
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1375
MigrateImageCommentTemp\__construct
__construct()
Default constructor.
Definition: migrateImageCommentTemp.php:33
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:368
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
MigrateImageCommentTemp
Maintenance script that merges image_comment_temp into the image table.
Definition: migrateImageCommentTemp.php:32