48 $oldLayout = $this->
getOption(
'oldlayout' );
49 if ( !in_array( $oldLayout, [
'name',
'sha1' ] ) ) {
52 $newLayout = $this->
getOption(
'newlayout' );
53 if ( !in_array( $newLayout, [
'name',
'sha1' ] ) ) {
60 $be = $repo->getBackend();
63 $be = $be->getInternalBackend();
66 $dbw = $repo->getPrimaryDB();
68 $origBase = $be->getContainerStoragePath(
"{$repo->getName()}-original" );
74 $conds[] = $dbw->expr(
'img_timestamp',
'>=', $dbw->timestamp( $since ) );
81 $res = $dbw->newSelectQueryBuilder()
82 ->select( [
'img_name',
'img_sha1' ] )
84 ->where( $dbw->expr(
'img_name',
'>', $lastName ) )
86 ->orderBy(
'img_name' )
88 ->caller( __METHOD__ )->fetchResultSet();
90 foreach ( $res as $row ) {
91 $lastName = $row->img_name;
93 $file = $repo->newFile( $row->img_name );
95 $sha1 = strlen( $row->img_sha1 ) ? $row->img_sha1 : $file->getSha1();
97 if ( !strlen( $sha1 ) ) {
98 $this->
error(
"Image SHA-1 not known for {$row->img_name}." );
100 if ( $oldLayout ===
'sha1' ) {
101 $spath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
103 $spath = $file->getPath();
106 if ( $newLayout ===
'sha1' ) {
107 $dpath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
109 $dpath = $file->getPath();
112 $status = $be->prepare( [
113 'dir' => dirname( $dpath ),
'bypassReadOnly' =>
true ] );
114 if ( !$status->isOK() ) {
115 $this->
error( $status );
118 $batch[] = [
'op' =>
'copy',
'overwrite' =>
true,
119 'src' => $spath,
'dst' => $dpath,
'img' => $row->img_name ];
122 foreach ( $file->getHistory() as $ofile ) {
123 $sha1 = $ofile->getSha1();
124 if ( !strlen( $sha1 ) ) {
125 $this->
error(
"Image SHA-1 not set for {$ofile->getArchiveName()}." );
129 if ( $oldLayout ===
'sha1' ) {
130 $spath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
131 } elseif ( $ofile->isDeleted( File::DELETED_FILE ) ) {
132 $spath = $be->getContainerStoragePath(
"{$repo->getName()}-deleted" ) .
133 '/' . $repo->getDeletedHashPath( $sha1 ) .
134 $sha1 .
'.' . $ofile->getExtension();
136 $spath = $ofile->getPath();
139 if ( $newLayout ===
'sha1' ) {
140 $dpath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
142 $dpath = $ofile->getPath();
145 $status = $be->prepare( [
146 'dir' => dirname( $dpath ),
'bypassReadOnly' =>
true ] );
147 if ( !$status->isOK() ) {
148 $this->
error( $status );
150 $batch[] = [
'op' =>
'copy',
'overwrite' =>
true,
151 'src' => $spath,
'dst' => $dpath,
'img' => $ofile->getArchiveName() ];
154 if ( count( $batch ) >= $batchSize ) {
159 }
while ( $res->numRows() );
161 if ( count( $batch ) ) {
168 $conds[] = $dbw->expr(
'fa_deleted_timestamp',
'>=', $dbw->timestamp( $since ) );
174 $res = $dbw->newSelectQueryBuilder()
175 ->select( [
'fa_storage_key',
'fa_id',
'fa_name' ] )
176 ->from(
'filearchive' )
177 ->where( $dbw->expr(
'fa_id',
'>', $lastId ) )
180 ->limit( $batchSize )
181 ->caller( __METHOD__ )->fetchResultSet();
183 foreach ( $res as $row ) {
184 $lastId = $row->fa_id;
185 $sha1Key = $row->fa_storage_key;
186 if ( !strlen( $sha1Key ) ) {
187 $this->
error(
"Image SHA-1 not set for file #{$row->fa_id} (deleted)." );
190 $sha1 = substr( $sha1Key, 0, strpos( $sha1Key,
'.' ) );
192 if ( $oldLayout ===
'sha1' ) {
193 $spath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
195 $spath = $be->getContainerStoragePath(
"{$repo->getName()}-deleted" ) .
196 '/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
199 if ( $newLayout ===
'sha1' ) {
200 $dpath =
"{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
202 $dpath = $be->getContainerStoragePath(
"{$repo->getName()}-deleted" ) .
203 '/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
206 $status = $be->prepare( [
207 'dir' => dirname( $dpath ),
'bypassReadOnly' =>
true ] );
208 if ( !$status->isOK() ) {
209 $this->
error( $status );
212 $batch[] = [
'op' =>
'copy',
'src' => $spath,
'dst' => $dpath,
213 'overwriteSame' =>
true,
'img' =>
"(ID {$row->fa_id}) {$row->fa_name}" ];
215 if ( count( $batch ) >= $batchSize ) {
220 }
while ( $res->numRows() );
222 if ( count( $batch ) ) {
226 $this->
output(
"Done (started $startTime)\n" );