45 parent::__construct();
47 $this->
addDescription(
'Script to migrate from image/oldimage tables to file/filerevision' );
50 $this->
addOption(
'start',
'Name of file to start with',
false,
true );
51 $this->
addOption(
'end',
'Name of file to end with',
false,
true );
54 'Time to sleep between each batch (in seconds). Default: 0',
62 $start = $this->
getOption(
'start',
false );
63 $sleep = (int)$this->
getOption(
'sleep', 0 );
83 'img_description_text' =>
'comment_img_description.comment_text',
84 'img_description_data' =>
'comment_img_description.comment_data',
85 'img_description_cid' =>
'comment_img_description.comment_id'
91 'comment_img_description',
92 'comment_img_description.comment_id = img_description_id'
94 $totalRowsInserted = 0;
97 if ( $batchSize <= 0 ) {
98 $this->
fatalError(
"Batch size is too low...", 12 );
101 if ( $end !==
false ) {
102 $queryBuilderTemplate->andWhere(
$dbw->
expr(
'img_name',
'<=', $end ) );
104 $queryBuilderTemplate
105 ->orderBy(
'img_name', SelectQueryBuilder::SORT_ASC )
106 ->limit( $batchSize );
108 $batchCondition = [];
110 if ( $start !==
false ) {
111 $batchCondition[] =
$dbw->
expr(
'img_name',
'>=', $start );
114 $queryBuilder = clone $queryBuilderTemplate;
115 $res = $queryBuilder->andWhere( $batchCondition )
116 ->caller( __METHOD__ )->fetchResultSet();
117 if ( $res->numRows() > 0 ) {
118 $row1 = $res->current();
119 $this->
output(
"Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
123 foreach ( $res as $row ) {
124 $rowsInserted = $this->handleFile( $row );
126 $totalRowsInserted += $rowsInserted;
128 $this->
output(
"Migrated File:{$row->img_name}. Inserted $rowsInserted rows.\n" );
130 if ( $res->numRows() > 0 ) {
132 $batchCondition = [
$dbw->
expr(
'img_name',
'>', $row->img_name ) ];
138 }
while ( $res->numRows() === $batchSize );
140 $this->
output(
"\nFinished migration for $filesHandled files. "
141 .
"$totalRowsInserted rows have been inserted into filerevision table.\n" );
144 private function handleFile( stdClass $row ) {
146 ->newCustomLocalRepo();
151 $imgDescriptionId = $row->img_description_id;
152 unset( $row->img_description_id );
154 $file = $repo->newFileFromRow( $row );
157 $file->acquireFileLock();
163 ->where( [
'img_name' => $row->img_name ] )
164 ->caller( __METHOD__ )->fetchRow();
169 ->where( [
'oi_name' => $row->img_name ] )
170 ->orderBy(
'oi_timestamp',
'ASC' )
171 ->caller( __METHOD__ )->fetchResultSet();
176 ->where( [
'file_name' => $row->img_name ] )
177 ->caller( __METHOD__ )->fetchRow();
180 $fileId = $file->acquireFileIdFromName();
184 ->from(
'filerevision' )
185 ->where( [
'fr_file' => $fileId ] )
186 ->caller( __METHOD__ )->fetchResultSet();
189 foreach ( $oldimageRows as $oldimageRow ) {
190 $timestamp = $oldimageRow->oi_timestamp;
191 $sha1 = $oldimageRow->oi_sha1;
193 $alreadyDone =
false;
194 foreach ( $fileRevisionRows as $fileRevisionRow ) {
196 $timestamp === $fileRevisionRow->fr_timestamp &&
197 $sha1 === $fileRevisionRow->fr_sha1
208 if ( $alreadyDone ) {
213 ->insertInto(
'filerevision' )
216 'fr_file' => $fileId,
217 'fr_size' => $oldimageRow->oi_size,
218 'fr_width' => $oldimageRow->oi_width,
219 'fr_height' => $oldimageRow->oi_height,
220 'fr_metadata' => $oldimageRow->oi_metadata,
221 'fr_bits' => $oldimageRow->oi_bits,
222 'fr_description_id' => $oldimageRow->oi_description_id,
223 'fr_actor' => $oldimageRow->oi_actor,
224 'fr_timestamp' => $oldimageRow->oi_timestamp,
225 'fr_sha1' => $oldimageRow->oi_sha1,
226 'fr_archive_name' => $oldimageRow->oi_archive_name,
227 'fr_deleted' => $oldimageRow->oi_deleted,
230 ->caller( __METHOD__ )->execute();
235 $timestamp = $row->img_timestamp;
236 $sha1 = $row->img_sha1;
238 $alreadyDone =
false;
239 foreach ( $fileRevisionRows as $fileRevisionRow ) {
241 $timestamp === $fileRevisionRow->fr_timestamp &&
242 $sha1 === $fileRevisionRow->fr_sha1
249 if ( !$alreadyDone ) {
251 ->insertInto(
'filerevision' )
254 'fr_file' => $fileId,
255 'fr_size' => $row->img_size,
256 'fr_width' => $row->img_width,
257 'fr_height' => $row->img_height,
258 'fr_metadata' => $row->img_metadata,
259 'fr_bits' => $row->img_bits,
260 'fr_description_id' => $imgDescriptionId,
261 'fr_actor' => $row->img_actor,
262 'fr_timestamp' => $row->img_timestamp,
263 'fr_sha1' => $row->img_sha1,
264 'fr_archive_name' =>
'',
268 ->caller( __METHOD__ )->execute();
275 ->from(
'filerevision' )
276 ->where( [
'fr_file' => $fileId ] )
277 ->orderBy(
'fr_timestamp',
'DESC' )
281 ->set( [
'file_latest' => $latestFrId ] )
282 ->where( [
'file_id' => $fileId ] )
283 ->caller( __METHOD__ )->execute();
286 $file->releaseFileLock();
287 return $rowsInserted;
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.