3 * Merge `image_comment_temp` into the `image` table
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script that merges `image_comment_temp` into the `image` table
29 * @ingroup Maintenance
32 class MigrateImageCommentTemp
extends LoggedUpdateMaintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->addDescription(
36 'Merges image_comment_temp into the image table'
41 * Sets whether a run of this maintenance script has the force parameter set
44 public function setForce( $forced = true ) {
45 $this->mOptions
['force'] = $forced;
48 protected function getUpdateKey() {
52 protected function doDBUpdates() {
53 $batchSize = $this->getBatchSize();
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" );
60 if ( !$dbw->tableExists( 'image_comment_temp', __METHOD__
) ) {
61 $this->output( "image_comment_temp does not exist, so nothing to do.\n" );
65 $this->output( "Merging image_comment_temp into the image table...\n" );
70 $this->beginTransaction( $dbw, __METHOD__
);
73 [ 'image_comment_temp', 'image' ],
75 'name' => 'imgcomment_name',
76 'oldid' => 'imgcomment_description_id',
77 'newid' => 'img_description_id',
81 [ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'name' ] ],
82 [ 'image' => [ 'JOIN', 'img_name = imgcomment_name' ] ]
84 $numRows = $res->numRows();
88 foreach ( $res as $row ) {
90 $toDelete[] = $row->name
;
94 [ 'img_description_id' => $row->oldid
],
95 [ 'img_name' => $row->name
],
99 } elseif ( $row->newid
!== $row->oldid
) {
101 "Image \"$row->name\" has img_description_id = $row->newid and "
102 . "imgcomment_description_id = $row->oldid. Ignoring the latter."
107 $dbw->delete( 'image_comment_temp', [ 'imgcomment_name' => $toDelete ], __METHOD__
);
108 $deleted +
= count( $toDelete );
111 $this->commitTransaction( $dbw, __METHOD__
);
113 if ( $numRows < $batchSize ) {
114 // We must have reached the end
118 $this->output( "... $last, updated $updated, deleted $deleted\n" );
119 $conds = [ 'imgcomment_name > ' . $dbw->addQuotes( $last ) ];
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)." );
129 "Completed merge of image_comment_temp into the image table, "
130 . "$updated image rows updated, $deleted image_comment_temp rows deleted.\n"
137 $maintClass = MigrateImageCommentTemp
::class;
138 require_once RUN_MAINTENANCE_IF_MAIN
;