aboutsummaryrefslogtreecommitdiffstats
path: root/maintenance/migrateImageCommentTemp.php
blob: f8b45058db1da6fdcfaa4b17fe4992d88eeac75b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * Merge `image_comment_temp` into the `image` table
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

require_once __DIR__ . '/Maintenance.php';

/**
 * Maintenance script that merges `image_comment_temp` into the `image` table
 *
 * @ingroup Maintenance
 * @since 1.32
 */
class MigrateImageCommentTemp extends LoggedUpdateMaintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription(
			'Merges image_comment_temp into the image table'
		);
	}

	protected function getUpdateKey() {
		return __CLASS__;
	}

	protected function doDBUpdates() {
		$batchSize = $this->getBatchSize();

		$dbw = $this->getDB( DB_PRIMARY );
		if ( !$dbw->fieldExists( 'image', 'img_description_id', __METHOD__ ) ) {
			$this->output( "Run update.php to create img_description_id.\n" );
			return false;
		}
		if ( !$dbw->tableExists( 'image_comment_temp', __METHOD__ ) ) {
			$this->output( "image_comment_temp does not exist, so nothing to do.\n" );
			return true;
		}

		$this->output( "Merging image_comment_temp into the image table...\n" );
		$conds = [];
		$updated = 0;
		$deleted = 0;
		while ( true ) {
			$this->beginTransaction( $dbw, __METHOD__ );

			$res = $dbw->select(
				[ 'image_comment_temp', 'image' ],
				[
					'name' => 'imgcomment_name',
					'oldid' => 'imgcomment_description_id',
					'newid' => 'img_description_id',
				],
				$conds,
				__METHOD__,
				[ 'LIMIT' => $batchSize, 'ORDER BY' => [ 'name' ] ],
				[ 'image' => [ 'JOIN', 'img_name = imgcomment_name' ] ]
			);
			$numRows = $res->numRows();

			$toDelete = [];
			$last = null;
			foreach ( $res as $row ) {
				$last = $row->name;
				$toDelete[] = $row->name;
				if ( !$row->newid ) {
					$dbw->update(
						'image',
						[ 'img_description_id' => $row->oldid ],
						[ 'img_name' => $row->name ],
						__METHOD__
					);
					$updated++;
				} elseif ( $row->newid !== $row->oldid ) {
					$this->error(
						"Image \"$row->name\" has img_description_id = $row->newid and "
						. "imgcomment_description_id = $row->oldid. Ignoring the latter."
					);
				}
			}
			if ( $toDelete ) {
				$dbw->delete( 'image_comment_temp', [ 'imgcomment_name' => $toDelete ], __METHOD__ );
				$deleted += count( $toDelete );
			}

			$this->commitTransaction( $dbw, __METHOD__ );

			if ( $numRows < $batchSize ) {
				// We must have reached the end
				break;
			}

			$this->output( "... $last, updated $updated, deleted $deleted\n" );
			$conds = [ 'imgcomment_name > ' . $dbw->addQuotes( $last ) ];
		}

		// This should be 0, so it should be very fast
		$count = (int)$dbw->selectField( 'image_comment_temp', 'COUNT(*)', [], __METHOD__ );
		if ( $count !== 0 ) {
			$this->error( "Ignoring $count orphaned image_comment_temp row(s)." );
		}

		$this->output(
			"Completed merge of image_comment_temp into the image table, "
			. "$updated image rows updated, $deleted image_comment_temp rows deleted.\n"
		);

		return true;
	}
}

$maintClass = MigrateImageCommentTemp::class;
require_once RUN_MAINTENANCE_IF_MAIN;