2015-03-10 13:26:14 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Copy all files in FileRepo to an originals container using SHA1 paths.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2024-05-19 13:40:05 +00:00
|
|
|
use Wikimedia\FileBackend\FileBackend;
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2015-03-10 13:26:14 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-03-10 13:26:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copy all files in FileRepo to an originals container using SHA1 paths.
|
|
|
|
|
*
|
|
|
|
|
* This script should be run while the repo is still set to the old layout.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
class MigrateFileRepoLayout extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Copy files in repo to a different layout.' );
|
2015-03-10 13:26:14 +00:00
|
|
|
$this->addOption( 'oldlayout', "Old layout; one of 'name' or 'sha1'", true, true );
|
|
|
|
|
$this->addOption( 'newlayout', "New layout; one of 'name' or 'sha1'", true, true );
|
|
|
|
|
$this->addOption( 'since', "Copy only files from after this timestamp", false, true );
|
|
|
|
|
$this->setBatchSize( 50 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$oldLayout = $this->getOption( 'oldlayout' );
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( !in_array( $oldLayout, [ 'name', 'sha1' ] ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Invalid old layout." );
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
$newLayout = $this->getOption( 'newlayout' );
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( !in_array( $newLayout, [ 'name', 'sha1' ] ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Invalid new layout." );
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
$since = $this->getOption( 'since' );
|
|
|
|
|
|
|
|
|
|
$repo = $this->getRepo();
|
|
|
|
|
|
|
|
|
|
$be = $repo->getBackend();
|
|
|
|
|
if ( $be instanceof FileBackendDBRepoWrapper ) {
|
2020-05-19 22:10:27 +00:00
|
|
|
// avoid path translations for this script
|
|
|
|
|
$be = $be->getInternalBackend();
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-19 01:32:42 +00:00
|
|
|
$dbw = $repo->getPrimaryDB();
|
2015-03-10 13:26:14 +00:00
|
|
|
|
|
|
|
|
$origBase = $be->getContainerStoragePath( "{$repo->getName()}-original" );
|
|
|
|
|
$startTime = wfTimestampNow();
|
|
|
|
|
|
|
|
|
|
// Do current and archived versions...
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [];
|
2015-03-10 13:26:14 +00:00
|
|
|
if ( $since ) {
|
2024-01-17 17:48:40 +00:00
|
|
|
$conds[] = $dbw->expr( 'img_timestamp', '>=', $dbw->timestamp( $since ) );
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-05 08:09:51 +00:00
|
|
|
$batchSize = $this->getBatchSize();
|
2016-02-17 09:09:32 +00:00
|
|
|
$batch = [];
|
2015-03-10 13:26:14 +00:00
|
|
|
$lastName = '';
|
|
|
|
|
do {
|
2023-09-21 11:54:38 +00:00
|
|
|
$res = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'img_name', 'img_sha1' ] )
|
|
|
|
|
->from( 'image' )
|
2024-01-17 17:48:40 +00:00
|
|
|
->where( $dbw->expr( 'img_name', '>', $lastName ) )
|
2023-09-21 11:54:38 +00:00
|
|
|
->andWhere( $conds )
|
|
|
|
|
->orderBy( 'img_name' )
|
|
|
|
|
->limit( $batchSize )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2015-03-10 13:26:14 +00:00
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$lastName = $row->img_name;
|
2015-10-05 04:45:25 +00:00
|
|
|
/** @var LocalFile $file */
|
|
|
|
|
$file = $repo->newFile( $row->img_name );
|
|
|
|
|
// Check in case SHA1 rows are not populated for some files
|
|
|
|
|
$sha1 = strlen( $row->img_sha1 ) ? $row->img_sha1 : $file->getSha1();
|
|
|
|
|
|
2015-03-10 13:26:14 +00:00
|
|
|
if ( !strlen( $sha1 ) ) {
|
2015-10-05 04:45:25 +00:00
|
|
|
$this->error( "Image SHA-1 not known for {$row->img_name}." );
|
2015-03-10 13:26:14 +00:00
|
|
|
} else {
|
|
|
|
|
if ( $oldLayout === 'sha1' ) {
|
|
|
|
|
$spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
|
|
|
|
|
} else {
|
|
|
|
|
$spath = $file->getPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $newLayout === 'sha1' ) {
|
|
|
|
|
$dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
|
|
|
|
|
} else {
|
|
|
|
|
$dpath = $file->getPath();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$status = $be->prepare( [
|
2022-03-07 23:49:13 +00:00
|
|
|
'dir' => dirname( $dpath ), 'bypassReadOnly' => true ] );
|
2015-03-10 13:26:14 +00:00
|
|
|
if ( !$status->isOK() ) {
|
Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.
Thoughts on the previously commonly used methods:
- $status->getMessage( false, false, 'en' )->text()
Almost the same as the new output, but it allows on-wiki message
overrides, and if there is more than one error, it prefixes each
line with a '*' (like a wikitext list).
- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
As above, but these forms do not expand parser functions
such as {{GENDER:}}.
- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
These forms output the message keys instead of the message text,
which is not very human-readable.
The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.
Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-06 23:50:00 +00:00
|
|
|
$this->error( $status );
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$batch[] = [ 'op' => 'copy', 'overwrite' => true,
|
|
|
|
|
'src' => $spath, 'dst' => $dpath, 'img' => $row->img_name ];
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $file->getHistory() as $ofile ) {
|
|
|
|
|
$sha1 = $ofile->getSha1();
|
|
|
|
|
if ( !strlen( $sha1 ) ) {
|
|
|
|
|
$this->error( "Image SHA-1 not set for {$ofile->getArchiveName()}." );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $oldLayout === 'sha1' ) {
|
|
|
|
|
$spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
|
|
|
|
|
} elseif ( $ofile->isDeleted( File::DELETED_FILE ) ) {
|
|
|
|
|
$spath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
|
|
|
|
|
'/' . $repo->getDeletedHashPath( $sha1 ) .
|
|
|
|
|
$sha1 . '.' . $ofile->getExtension();
|
|
|
|
|
} else {
|
|
|
|
|
$spath = $ofile->getPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $newLayout === 'sha1' ) {
|
|
|
|
|
$dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
|
|
|
|
|
} else {
|
|
|
|
|
$dpath = $ofile->getPath();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$status = $be->prepare( [
|
2022-03-07 23:49:13 +00:00
|
|
|
'dir' => dirname( $dpath ), 'bypassReadOnly' => true ] );
|
2015-03-10 13:26:14 +00:00
|
|
|
if ( !$status->isOK() ) {
|
Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.
Thoughts on the previously commonly used methods:
- $status->getMessage( false, false, 'en' )->text()
Almost the same as the new output, but it allows on-wiki message
overrides, and if there is more than one error, it prefixes each
line with a '*' (like a wikitext list).
- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
As above, but these forms do not expand parser functions
such as {{GENDER:}}.
- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
These forms output the message keys instead of the message text,
which is not very human-readable.
The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.
Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-06 23:50:00 +00:00
|
|
|
$this->error( $status );
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$batch[] = [ 'op' => 'copy', 'overwrite' => true,
|
|
|
|
|
'src' => $spath, 'dst' => $dpath, 'img' => $ofile->getArchiveName() ];
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-05 08:09:51 +00:00
|
|
|
if ( count( $batch ) >= $batchSize ) {
|
2015-03-10 13:26:14 +00:00
|
|
|
$this->runBatch( $batch, $be );
|
2016-02-17 09:09:32 +00:00
|
|
|
$batch = [];
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while ( $res->numRows() );
|
|
|
|
|
|
|
|
|
|
if ( count( $batch ) ) {
|
|
|
|
|
$this->runBatch( $batch, $be );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do deleted versions...
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [];
|
2015-03-10 13:26:14 +00:00
|
|
|
if ( $since ) {
|
2024-01-17 17:48:40 +00:00
|
|
|
$conds[] = $dbw->expr( 'fa_deleted_timestamp', '>=', $dbw->timestamp( $since ) );
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$batch = [];
|
2015-03-10 13:26:14 +00:00
|
|
|
$lastId = 0;
|
|
|
|
|
do {
|
2023-09-21 11:54:38 +00:00
|
|
|
$res = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( [ 'fa_storage_key', 'fa_id', 'fa_name' ] )
|
|
|
|
|
->from( 'filearchive' )
|
2024-01-17 17:48:40 +00:00
|
|
|
->where( $dbw->expr( 'fa_id', '>', $lastId ) )
|
2023-09-21 11:54:38 +00:00
|
|
|
->andWhere( $conds )
|
|
|
|
|
->orderBy( 'fa_id' )
|
|
|
|
|
->limit( $batchSize )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2015-03-10 13:26:14 +00:00
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$lastId = $row->fa_id;
|
|
|
|
|
$sha1Key = $row->fa_storage_key;
|
|
|
|
|
if ( !strlen( $sha1Key ) ) {
|
|
|
|
|
$this->error( "Image SHA-1 not set for file #{$row->fa_id} (deleted)." );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$sha1 = substr( $sha1Key, 0, strpos( $sha1Key, '.' ) );
|
|
|
|
|
|
|
|
|
|
if ( $oldLayout === 'sha1' ) {
|
|
|
|
|
$spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
|
|
|
|
|
} else {
|
|
|
|
|
$spath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
|
|
|
|
|
'/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $newLayout === 'sha1' ) {
|
|
|
|
|
$dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
|
|
|
|
|
} else {
|
|
|
|
|
$dpath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
|
|
|
|
|
'/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$status = $be->prepare( [
|
2022-03-07 23:49:13 +00:00
|
|
|
'dir' => dirname( $dpath ), 'bypassReadOnly' => true ] );
|
2015-03-10 13:26:14 +00:00
|
|
|
if ( !$status->isOK() ) {
|
Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.
Thoughts on the previously commonly used methods:
- $status->getMessage( false, false, 'en' )->text()
Almost the same as the new output, but it allows on-wiki message
overrides, and if there is more than one error, it prefixes each
line with a '*' (like a wikitext list).
- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
As above, but these forms do not expand parser functions
such as {{GENDER:}}.
- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
These forms output the message keys instead of the message text,
which is not very human-readable.
The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.
Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-06 23:50:00 +00:00
|
|
|
$this->error( $status );
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$batch[] = [ 'op' => 'copy', 'src' => $spath, 'dst' => $dpath,
|
|
|
|
|
'overwriteSame' => true, 'img' => "(ID {$row->fa_id}) {$row->fa_name}" ];
|
2015-03-10 13:26:14 +00:00
|
|
|
|
2017-11-05 08:09:51 +00:00
|
|
|
if ( count( $batch ) >= $batchSize ) {
|
2015-03-10 13:26:14 +00:00
|
|
|
$this->runBatch( $batch, $be );
|
2016-02-17 09:09:32 +00:00
|
|
|
$batch = [];
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while ( $res->numRows() );
|
|
|
|
|
|
|
|
|
|
if ( count( $batch ) ) {
|
|
|
|
|
$this->runBatch( $batch, $be );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( "Done (started $startTime)\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getRepo() {
|
2023-08-31 09:21:12 +00:00
|
|
|
return $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-11 14:45:54 +00:00
|
|
|
/**
|
|
|
|
|
* @param array[] $ops
|
|
|
|
|
* @param FileBackend $be
|
|
|
|
|
*/
|
2015-03-10 13:26:14 +00:00
|
|
|
protected function runBatch( array $ops, FileBackend $be ) {
|
|
|
|
|
$this->output( "Migrating file batch:\n" );
|
|
|
|
|
foreach ( $ops as $op ) {
|
|
|
|
|
$this->output( "\"{$op['img']}\" (dest: {$op['dst']})\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 23:49:13 +00:00
|
|
|
$status = $be->doOperations( $ops, [ 'bypassReadOnly' => true ] );
|
2015-03-10 13:26:14 +00:00
|
|
|
if ( !$status->isOK() ) {
|
Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.
Thoughts on the previously commonly used methods:
- $status->getMessage( false, false, 'en' )->text()
Almost the same as the new output, but it allows on-wiki message
overrides, and if there is more than one error, it prefixes each
line with a '*' (like a wikitext list).
- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
As above, but these forms do not expand parser functions
such as {{GENDER:}}.
- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
These forms output the message keys instead of the message text,
which is not very human-readable.
The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.
Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-06 23:50:00 +00:00
|
|
|
$this->error( $status );
|
2015-03-10 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( "Batch done\n\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = MigrateFileRepoLayout::class;
|
2015-03-10 13:26:14 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|