31 parent::__construct();
33 $this->
addDescription(
'Script to migrate from image/oldimage tables to file/filerevision' );
36 $this->
addOption(
'start',
'Name of file to start with',
false,
true );
37 $this->
addOption(
'end',
'Name of file to end with',
false,
true );
40 'Time to sleep between each batch (in seconds). Default: 0',
48 $start = $this->
getOption(
'start',
false );
49 $sleep = (int)$this->
getOption(
'sleep', 0 );
69 'img_description_text' =>
'comment_img_description.comment_text',
70 'img_description_data' =>
'comment_img_description.comment_data',
71 'img_description_cid' =>
'comment_img_description.comment_id'
77 'comment_img_description',
78 'comment_img_description.comment_id = img_description_id'
80 $totalRowsInserted = 0;
83 if ( $batchSize <= 0 ) {
84 $this->
fatalError(
"Batch size is too low...", 12 );
87 if ( $end !==
false ) {
88 $queryBuilderTemplate->andWhere(
$dbw->
expr(
'img_name',
'<=', $end ) );
91 ->orderBy(
'img_name', SelectQueryBuilder::SORT_ASC )
92 ->limit( $batchSize );
96 if ( $start !==
false ) {
97 $batchCondition[] =
$dbw->
expr(
'img_name',
'>=', $start );
100 $queryBuilder = clone $queryBuilderTemplate;
101 $res = $queryBuilder->andWhere( $batchCondition )
102 ->caller( __METHOD__ )->fetchResultSet();
103 if ( $res->numRows() > 0 ) {
104 $row1 = $res->current();
105 $this->
output(
"Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
109 foreach ( $res as $row ) {
110 $rowsInserted = $this->handleFile( $row );
112 $totalRowsInserted += $rowsInserted;
114 $this->
output(
"Migrated File:{$row->img_name}. Inserted $rowsInserted rows.\n" );
116 if ( $res->numRows() > 0 ) {
118 $batchCondition = [
$dbw->
expr(
'img_name',
'>', $row->img_name ) ];
124 }
while ( $res->numRows() === $batchSize );
126 $this->
output(
"\nFinished migration for $filesHandled files. "
127 .
"$totalRowsInserted rows have been inserted into filerevision table.\n" );
130 private function handleFile( stdClass $row ): int {
132 ->newCustomLocalRepo();
137 $imgDescriptionId = $row->img_description_id;
138 unset( $row->img_description_id );
140 $file = $repo->newFileFromRow( $row );
143 $file->acquireFileLock();
149 ->where( [
'img_name' => $row->img_name ] )
150 ->caller( __METHOD__ )->fetchRow();
155 ->where( [
'oi_name' => $row->img_name ] )
156 ->orderBy(
'oi_timestamp',
'ASC' )
157 ->caller( __METHOD__ )->fetchResultSet();
162 ->where( [
'file_name' => $row->img_name ] )
163 ->caller( __METHOD__ )->fetchRow();
166 $fileId = $file->acquireFileIdFromName();
170 ->from(
'filerevision' )
171 ->where( [
'fr_file' => $fileId ] )
172 ->caller( __METHOD__ )->fetchResultSet();
175 foreach ( $oldimageRows as $oldimageRow ) {
176 $timestamp = $oldimageRow->oi_timestamp;
177 $sha1 = $oldimageRow->oi_sha1;
179 $alreadyDone =
false;
180 foreach ( $fileRevisionRows as $fileRevisionRow ) {
182 $timestamp === $fileRevisionRow->fr_timestamp &&
183 $sha1 === $fileRevisionRow->fr_sha1
194 if ( $alreadyDone ) {
199 ->insertInto(
'filerevision' )
202 'fr_file' => $fileId,
203 'fr_size' => $oldimageRow->oi_size,
204 'fr_width' => $oldimageRow->oi_width,
205 'fr_height' => $oldimageRow->oi_height,
206 'fr_metadata' => $oldimageRow->oi_metadata,
207 'fr_bits' => $oldimageRow->oi_bits,
208 'fr_description_id' => $oldimageRow->oi_description_id,
209 'fr_actor' => $oldimageRow->oi_actor,
210 'fr_timestamp' => $oldimageRow->oi_timestamp,
211 'fr_sha1' => $oldimageRow->oi_sha1,
212 'fr_archive_name' => $oldimageRow->oi_archive_name,
213 'fr_deleted' => $oldimageRow->oi_deleted,
216 ->caller( __METHOD__ )->execute();
221 $timestamp = $row->img_timestamp;
222 $sha1 = $row->img_sha1;
224 $alreadyDone =
false;
225 foreach ( $fileRevisionRows as $fileRevisionRow ) {
227 $timestamp === $fileRevisionRow->fr_timestamp &&
228 $sha1 === $fileRevisionRow->fr_sha1
235 if ( !$alreadyDone ) {
237 ->insertInto(
'filerevision' )
240 'fr_file' => $fileId,
241 'fr_size' => $row->img_size,
242 'fr_width' => $row->img_width,
243 'fr_height' => $row->img_height,
244 'fr_metadata' => $row->img_metadata,
245 'fr_bits' => $row->img_bits,
246 'fr_description_id' => $imgDescriptionId,
247 'fr_actor' => $row->img_actor,
248 'fr_timestamp' => $row->img_timestamp,
249 'fr_sha1' => $row->img_sha1,
250 'fr_archive_name' =>
'',
254 ->caller( __METHOD__ )->execute();
261 ->from(
'filerevision' )
262 ->where( [
'fr_file' => $fileId ] )
263 ->orderBy(
'fr_timestamp',
'DESC' )
264 ->caller( __METHOD__ )->fetchField();
267 ->set( [
'file_latest' => $latestFrId ] )
268 ->where( [
'file_id' => $fileId ] )
269 ->caller( __METHOD__ )->execute();
272 $file->releaseFileLock();
273 return $rowsInserted;
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.