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 
44  public function setForce( $forced = true ) {
45  $this->mOptions['force'] = $forced;
46  }
47 
48  protected function getUpdateKey() {
49  return __CLASS__;
50  }
51 
52  protected function doDBUpdates() {
53  $batchSize = $this->getBatchSize();
54 
55  $dbw = $this->getDB( DB_MASTER );
56  if ( !$dbw->fieldExists( 'image', 'img_description_id', __METHOD__ ) ) {
57  $this->output( "Run update.php to create img_description_id.\n" );
58  return false;
59  }
60  if ( !$dbw->tableExists( 'image_comment_temp', __METHOD__ ) ) {
61  $this->output( "image_comment_temp does not exist, so nothing to do.\n" );
62  return true;
63  }
64 
65  $this->output( "Merging image_comment_temp into the image table...\n" );
66  $conds = [];
67  $updated = 0;
68  $deleted = 0;
69  while ( true ) {
70  $this->beginTransaction( $dbw, __METHOD__ );
71 
72  $res = $dbw->select(
73  [ 'image_comment_temp', 'image' ],
74  [
75  'name' => 'imgcomment_name',
76  'oldid' => 'imgcomment_description_id',
77  'newid' => 'img_description_id',
78  ],
79  $conds,
80  __METHOD__,
81  [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'name' ] ],
82  [ 'image' => [ 'JOIN', 'img_name = imgcomment_name' ] ]
83  );
84  $numRows = $res->numRows();
85 
86  $toDelete = [];
87  $last = null;
88  foreach ( $res as $row ) {
89  $last = $row->name;
90  $toDelete[] = $row->name;
91  if ( !$row->newid ) {
92  $dbw->update(
93  'image',
94  [ 'img_description_id' => $row->oldid ],
95  [ 'img_name' => $row->name ],
96  __METHOD__
97  );
98  $updated++;
99  } elseif ( $row->newid !== $row->oldid ) {
100  $this->error(
101  "Image \"$row->name\" has img_description_id = $row->newid and "
102  . "imgcomment_description_id = $row->oldid. Ignoring the latter."
103  );
104  }
105  }
106  if ( $toDelete ) {
107  $dbw->delete( 'image_comment_temp', [ 'imgcomment_name' => $toDelete ], __METHOD__ );
108  $deleted += count( $toDelete );
109  }
110 
111  $this->commitTransaction( $dbw, __METHOD__ );
112 
113  if ( $numRows < $batchSize ) {
114  // We must have reached the end
115  break;
116  }
117 
118  $this->output( "... $last, updated $updated, deleted $deleted\n" );
119  $conds = [ 'imgcomment_name > ' . $dbw->addQuotes( $last ) ];
120  }
121 
122  // This should be 0, so it should be very fast
123  $count = (int)$dbw->selectField( 'image_comment_temp', 'COUNT(*)', [], __METHOD__ );
124  if ( $count !== 0 ) {
125  $this->error( "Ignoring $count orphaned image_comment_temp row(s)." );
126  }
127 
128  $this->output(
129  "Completed merge of image_comment_temp into the image table, "
130  . "$updated image rows updated, $deleted image_comment_temp rows deleted.\n"
131  );
132 
133  return true;
134  }
135 }
136 
137 $maintClass = MigrateImageCommentTemp::class;
138 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
$res
$res
Definition: testCompression.php:57
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1397
MigrateImageCommentTemp\setForce
setForce( $forced=true)
Sets whether a run of this maintenance script has the force parameter set.
Definition: migrateImageCommentTemp.php:44
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:52
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: migrateImageCommentTemp.php:137
MigrateImageCommentTemp\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: migrateImageCommentTemp.php:48
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1412
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
MigrateImageCommentTemp\__construct
__construct()
Default constructor.
Definition: migrateImageCommentTemp.php:33
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:366
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
MigrateImageCommentTemp
Maintenance script that merges image_comment_temp into the image table.
Definition: migrateImageCommentTemp.php:32