resources: Collapse all jQuery UI modules into one deprecated mega-module
[lhc/web/wiklou.git] / maintenance / migrateImageCommentTemp.php
1 <?php
2 /**
3 * Merge `image_comment_temp` into the `image` table
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script that merges `image_comment_temp` into the `image` table
28 *
29 * @ingroup Maintenance
30 * @since 1.32
31 */
32 class MigrateImageCommentTemp extends LoggedUpdateMaintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription(
36 'Merges image_comment_temp into the image table'
37 );
38 }
39
40 /**
41 * Sets whether a run of this maintenance script has the force parameter set
42 * @param bool $forced
43 */
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;