Merge "Remove all-caps emphasis from simpleantispam-label"
[mediawiki.git] / maintenance / migrateFileRepoLayout.php
blob78587ce5439fc7dd448e283885b7bd4d95bbed70
1 <?php
2 /**
3 * Copy all files in FileRepo to an originals container using SHA1 paths.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Copy all files in FileRepo to an originals container using SHA1 paths.
29 * This script should be run while the repo is still set to the old layout.
31 * @ingroup Maintenance
33 class MigrateFileRepoLayout extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Copy files in repo to a different layout.";
37 $this->addOption( 'oldlayout', "Old layout; one of 'name' or 'sha1'", true, true );
38 $this->addOption( 'newlayout', "New layout; one of 'name' or 'sha1'", true, true );
39 $this->addOption( 'since', "Copy only files from after this timestamp", false, true );
40 $this->setBatchSize( 50 );
43 public function execute() {
44 $oldLayout = $this->getOption( 'oldlayout' );
45 if ( !in_array( $oldLayout, array( 'name', 'sha1' ) ) ) {
46 $this->error( "Invalid old layout.", 1 );
48 $newLayout = $this->getOption( 'newlayout' );
49 if ( !in_array( $newLayout, array( 'name', 'sha1' ) ) ) {
50 $this->error( "Invalid new layout.", 1 );
52 $since = $this->getOption( 'since' );
54 $repo = $this->getRepo();
56 $be = $repo->getBackend();
57 if ( $be instanceof FileBackendDBRepoWrapper ) {
58 $be = $be->getInternalBackend(); // avoid path translations for this script
61 $dbw = $repo->getMasterDB();
63 $origBase = $be->getContainerStoragePath( "{$repo->getName()}-original" );
64 $startTime = wfTimestampNow();
66 // Do current and archived versions...
67 $conds = array();
68 if ( $since ) {
69 $conds[] = 'img_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $since ) );
72 $batch = array();
73 $lastName = '';
74 do {
75 $res = $dbw->select( 'image', array( 'img_name', 'img_sha1' ),
76 array_merge( array( 'img_name > ' . $dbw->addQuotes( $lastName ) ), $conds ),
77 __METHOD__,
78 array( 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'img_name' )
81 foreach ( $res as $row ) {
82 $lastName = $row->img_name;
83 $sha1 = $row->img_sha1;
84 if ( !strlen( $sha1 ) ) {
85 $this->error( "Image SHA-1 not set for {$row->img_name}." );
86 } else {
87 $file = $repo->newFile( $row->img_name );
89 if ( $oldLayout === 'sha1' ) {
90 $spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
91 } else {
92 $spath = $file->getPath();
95 if ( $newLayout === 'sha1' ) {
96 $dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
97 } else {
98 $dpath = $file->getPath();
101 $status = $be->prepare( array( 'dir' => dirname( $dpath ) ) );
102 if ( !$status->isOK() ) {
103 $this->error( print_r( $status->getErrorsArray(), true ) );
106 $batch[] = array( 'op' => 'copy', 'overwrite' => true,
107 'src' => $spath, 'dst' => $dpath, 'img' => $row->img_name );
110 foreach ( $file->getHistory() as $ofile ) {
111 $sha1 = $ofile->getSha1();
112 if ( !strlen( $sha1 ) ) {
113 $this->error( "Image SHA-1 not set for {$ofile->getArchiveName()}." );
114 continue;
117 if ( $oldLayout === 'sha1' ) {
118 $spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
119 } elseif ( $ofile->isDeleted( File::DELETED_FILE ) ) {
120 $spath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
121 '/' . $repo->getDeletedHashPath( $sha1 ) .
122 $sha1 . '.' . $ofile->getExtension();
123 } else {
124 $spath = $ofile->getPath();
127 if ( $newLayout === 'sha1' ) {
128 $dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
129 } else {
130 $dpath = $ofile->getPath();
133 $status = $be->prepare( array( 'dir' => dirname( $dpath ) ) );
134 if ( !$status->isOK() ) {
135 $this->error( print_r( $status->getErrorsArray(), true ) );
137 $batch[] = array( 'op' => 'copy', 'overwrite' => true,
138 'src' => $spath, 'dst' => $dpath, 'img' => $ofile->getArchiveName() );
141 if ( count( $batch ) >= $this->mBatchSize ) {
142 $this->runBatch( $batch, $be );
143 $batch = array();
146 } while ( $res->numRows() );
148 if ( count( $batch ) ) {
149 $this->runBatch( $batch, $be );
152 // Do deleted versions...
153 $conds = array();
154 if ( $since ) {
155 $conds[] = 'fa_deleted_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $since ) );
158 $batch = array();
159 $lastId = 0;
160 do {
161 $res = $dbw->select( 'filearchive', array( 'fa_storage_key', 'fa_id', 'fa_name' ),
162 array_merge( array( 'fa_id > ' . $dbw->addQuotes( $lastId ) ), $conds ),
163 __METHOD__,
164 array( 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'fa_id' )
167 foreach ( $res as $row ) {
168 $lastId = $row->fa_id;
169 $sha1Key = $row->fa_storage_key;
170 if ( !strlen( $sha1Key ) ) {
171 $this->error( "Image SHA-1 not set for file #{$row->fa_id} (deleted)." );
172 continue;
174 $sha1 = substr( $sha1Key, 0, strpos( $sha1Key, '.' ) );
176 if ( $oldLayout === 'sha1' ) {
177 $spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
178 } else {
179 $spath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
180 '/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
183 if ( $newLayout === 'sha1' ) {
184 $dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
185 } else {
186 $dpath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
187 '/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
190 $status = $be->prepare( array( 'dir' => dirname( $dpath ) ) );
191 if ( !$status->isOK() ) {
192 $this->error( print_r( $status->getErrorsArray(), true ) );
195 $batch[] = array( 'op' => 'copy', 'src' => $spath, 'dst' => $dpath,
196 'overwriteSame' => true, 'img' => "(ID {$row->fa_id}) {$row->fa_name}" );
198 if ( count( $batch ) >= $this->mBatchSize ) {
199 $this->runBatch( $batch, $be );
200 $batch = array();
203 } while ( $res->numRows() );
205 if ( count( $batch ) ) {
206 $this->runBatch( $batch, $be );
209 $this->output( "Done (started $startTime)\n" );
212 protected function getRepo() {
213 return RepoGroup::singleton()->getLocalRepo();
216 protected function runBatch( array $ops, FileBackend $be ) {
217 $this->output( "Migrating file batch:\n" );
218 foreach ( $ops as $op ) {
219 $this->output( "\"{$op['img']}\" (dest: {$op['dst']})\n" );
222 $status = $be->doOperations( $ops );
223 if ( !$status->isOK() ) {
224 $this->output( print_r( $status->getErrorsArray(), true ) );
227 $this->output( "Batch done\n\n" );
231 $maintClass = 'MigrateFileRepoLayout';
232 require_once RUN_MAINTENANCE_IF_MAIN;