44 $oldLayout = $this->
getOption(
'oldlayout' );
45 if ( !in_array( $oldLayout, [
'name',
'sha1' ] ) ) {
48 $newLayout = $this->
getOption(
'newlayout' );
49 if ( !in_array( $newLayout, [
'name',
'sha1' ] ) ) {
56 $be = $repo->getBackend();
59 $be = $be->getInternalBackend();
62 $dbw = $repo->getPrimaryDB();
64 $origBase = $be->getContainerStoragePath(
"{$repo->getName()}-original" );
70 $conds[] =
'img_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $since ) );
77 $res = $dbw->newSelectQueryBuilder()
78 ->select( [
'img_name',
'img_sha1' ] )
80 ->where(
'img_name > ' . $dbw->addQuotes( $lastName ) )
82 ->orderBy(
'img_name' )
84 ->caller( __METHOD__ )->fetchResultSet();
86 foreach ( $res as $row ) {
87 $lastName = $row->img_name;
89 $file = $repo->newFile( $row->img_name );
91 $sha1 = strlen( $row->img_sha1 ) ? $row->img_sha1 :
$file->getSha1();
93 if ( !strlen( $sha1 ) ) {
94 $this->
error(
"Image SHA-1 not known for {$row->img_name}." );
96 if ( $oldLayout ===
'sha1' ) {
97 $spath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
99 $spath =
$file->getPath();
102 if ( $newLayout ===
'sha1' ) {
103 $dpath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
105 $dpath =
$file->getPath();
108 $status = $be->prepare( [
109 'dir' => dirname( $dpath ),
'bypassReadOnly' =>
true ] );
110 if ( !$status->isOK() ) {
111 $this->
error( print_r( $status->getErrors(),
true ) );
114 $batch[] = [
'op' =>
'copy',
'overwrite' =>
true,
115 'src' => $spath,
'dst' => $dpath,
'img' => $row->img_name ];
118 foreach (
$file->getHistory() as $ofile ) {
119 $sha1 = $ofile->getSha1();
120 if ( !strlen( $sha1 ) ) {
121 $this->
error(
"Image SHA-1 not set for {$ofile->getArchiveName()}." );
125 if ( $oldLayout ===
'sha1' ) {
126 $spath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
127 } elseif ( $ofile->isDeleted( File::DELETED_FILE ) ) {
128 $spath = $be->getContainerStoragePath(
"{$repo->getName()}-deleted" ) .
129 '/' . $repo->getDeletedHashPath( $sha1 ) .
130 $sha1 .
'.' . $ofile->getExtension();
132 $spath = $ofile->getPath();
135 if ( $newLayout ===
'sha1' ) {
136 $dpath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
138 $dpath = $ofile->getPath();
141 $status = $be->prepare( [
142 'dir' => dirname( $dpath ),
'bypassReadOnly' =>
true ] );
143 if ( !$status->isOK() ) {
144 $this->
error( print_r( $status->getErrors(),
true ) );
146 $batch[] = [
'op' =>
'copy',
'overwrite' =>
true,
147 'src' => $spath,
'dst' => $dpath,
'img' => $ofile->getArchiveName() ];
150 if ( count( $batch ) >= $batchSize ) {
155 }
while ( $res->numRows() );
157 if ( count( $batch ) ) {
164 $conds[] =
'fa_deleted_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $since ) );
170 $res = $dbw->newSelectQueryBuilder()
171 ->select( [
'fa_storage_key',
'fa_id',
'fa_name' ] )
172 ->from(
'filearchive' )
173 ->where(
'fa_id > ' . $dbw->addQuotes( $lastId ) )
176 ->limit( $batchSize )
177 ->caller( __METHOD__ )->fetchResultSet();
179 foreach ( $res as $row ) {
180 $lastId = $row->fa_id;
181 $sha1Key = $row->fa_storage_key;
182 if ( !strlen( $sha1Key ) ) {
183 $this->
error(
"Image SHA-1 not set for file #{$row->fa_id} (deleted)." );
186 $sha1 = substr( $sha1Key, 0, strpos( $sha1Key,
'.' ) );
188 if ( $oldLayout ===
'sha1' ) {
189 $spath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
191 $spath = $be->getContainerStoragePath(
"{$repo->getName()}-deleted" ) .
192 '/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
195 if ( $newLayout ===
'sha1' ) {
196 $dpath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
198 $dpath = $be->getContainerStoragePath(
"{$repo->getName()}-deleted" ) .
199 '/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
202 $status = $be->prepare( [
203 'dir' => dirname( $dpath ),
'bypassReadOnly' =>
true ] );
204 if ( !$status->isOK() ) {
205 $this->
error( print_r( $status->getErrors(),
true ) );
208 $batch[] = [
'op' =>
'copy',
'src' => $spath,
'dst' => $dpath,
209 'overwriteSame' =>
true,
'img' =>
"(ID {$row->fa_id}) {$row->fa_name}" ];
211 if ( count( $batch ) >= $batchSize ) {
216 }
while ( $res->numRows() );
218 if ( count( $batch ) ) {
222 $this->
output(
"Done (started $startTime)\n" );