MediaWiki REL1_39
migrateImageCommentTemp.php
Go to the documentation of this file.
1<?php
24require_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 // @phan-suppress-next-line PhanTypeSuspiciousStringExpression last is not-null when used
111 $this->output( "... $last, updated $updated, deleted $deleted\n" );
112 $conds = [ 'imgcomment_name > ' . $dbw->addQuotes( $last ) ];
113 }
114
115 // This should be 0, so it should be very fast
116 $count = (int)$dbw->selectField( 'image_comment_temp', 'COUNT(*)', [], __METHOD__ );
117 if ( $count !== 0 ) {
118 $this->error( "Ignoring $count orphaned image_comment_temp row(s)." );
119 }
120
121 $this->output(
122 "Completed merge of image_comment_temp into the image table, "
123 . "$updated image rows updated, $deleted image_comment_temp rows deleted.\n"
124 );
125
126 return true;
127 }
128}
129
130$maintClass = MigrateImageCommentTemp::class;
131require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
error( $err, $die=0)
Throw an error to the user.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
Maintenance script that merges image_comment_temp into the image table.
__construct()
Default constructor.
getUpdateKey()
Get the update key name to go in the update log table.
const DB_PRIMARY
Definition defines.php:28