Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / maintenance / migrateFileRepoLayout.php
1 <?php
2 /**
3 * Copy all files in FileRepo to an originals container using SHA1 paths.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Copy all files in FileRepo to an originals container using SHA1 paths.
28 *
29 * This script should be run while the repo is still set to the old layout.
30 *
31 * @ingroup Maintenance
32 */
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 );
41 }
42
43 public function execute() {
44 $oldLayout = $this->getOption( 'oldlayout' );
45 if ( !in_array( $oldLayout, array( 'name', 'sha1' ) ) ) {
46 $this->error( "Invalid old layout.", 1 );
47 }
48 $newLayout = $this->getOption( 'newlayout' );
49 if ( !in_array( $newLayout, array( 'name', 'sha1' ) ) ) {
50 $this->error( "Invalid new layout.", 1 );
51 }
52 $since = $this->getOption( 'since' );
53
54 $repo = $this->getRepo();
55
56 $be = $repo->getBackend();
57 if ( $be instanceof FileBackendDBRepoWrapper ) {
58 $be = $be->getInternalBackend(); // avoid path translations for this script
59 }
60
61 $dbw = $repo->getMasterDB();
62
63 $origBase = $be->getContainerStoragePath( "{$repo->getName()}-original" );
64 $startTime = wfTimestampNow();
65
66 // Do current and archived versions...
67 $conds = array();
68 if ( $since ) {
69 $conds[] = 'img_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $since ) );
70 }
71
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' )
79 );
80
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 );
88
89 if ( $oldLayout === 'sha1' ) {
90 $spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
91 } else {
92 $spath = $file->getPath();
93 }
94
95 if ( $newLayout === 'sha1' ) {
96 $dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
97 } else {
98 $dpath = $file->getPath();
99 }
100
101 $status = $be->prepare( array( 'dir' => dirname( $dpath ) ) );
102 if ( !$status->isOK() ) {
103 $this->error( print_r( $status->getErrorsArray(), true ) );
104 }
105
106 $batch[] = array( 'op' => 'copy', 'overwrite' => true,
107 'src' => $spath, 'dst' => $dpath, 'img' => $row->img_name );
108 }
109
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;
115 }
116
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();
125 }
126
127 if ( $newLayout === 'sha1' ) {
128 $dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
129 } else {
130 $dpath = $ofile->getPath();
131 }
132
133 $status = $be->prepare( array( 'dir' => dirname( $dpath ) ) );
134 if ( !$status->isOK() ) {
135 $this->error( print_r( $status->getErrorsArray(), true ) );
136 }
137 $batch[] = array( 'op' => 'copy', 'overwrite' => true,
138 'src' => $spath, 'dst' => $dpath, 'img' => $ofile->getArchiveName() );
139 }
140
141 if ( count( $batch ) >= $this->mBatchSize ) {
142 $this->runBatch( $batch, $be );
143 $batch = array();
144 }
145 }
146 } while ( $res->numRows() );
147
148 if ( count( $batch ) ) {
149 $this->runBatch( $batch, $be );
150 }
151
152 // Do deleted versions...
153 $conds = array();
154 if ( $since ) {
155 $conds[] = 'fa_deleted_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $since ) );
156 }
157
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' )
165 );
166
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;
173 }
174 $sha1 = substr( $sha1Key, 0, strpos( $sha1Key, '.' ) );
175
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;
181 }
182
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;
188 }
189
190 $status = $be->prepare( array( 'dir' => dirname( $dpath ) ) );
191 if ( !$status->isOK() ) {
192 $this->error( print_r( $status->getErrorsArray(), true ) );
193 }
194
195 $batch[] = array( 'op' => 'copy', 'src' => $spath, 'dst' => $dpath,
196 'overwriteSame' => true, 'img' => "(ID {$row->fa_id}) {$row->fa_name}" );
197
198 if ( count( $batch ) >= $this->mBatchSize ) {
199 $this->runBatch( $batch, $be );
200 $batch = array();
201 }
202 }
203 } while ( $res->numRows() );
204
205 if ( count( $batch ) ) {
206 $this->runBatch( $batch, $be );
207 }
208
209 $this->output( "Done (started $startTime)\n" );
210 }
211
212 protected function getRepo() {
213 return RepoGroup::singleton()->getLocalRepo();
214 }
215
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" );
220 }
221
222 $status = $be->doOperations( $ops );
223 if ( !$status->isOK() ) {
224 $this->output( print_r( $status->getErrorsArray(), true ) );
225 }
226
227 $this->output( "Batch done\n\n" );
228 }
229 }
230
231 $maintClass = 'MigrateFileRepoLayout';
232 require_once RUN_MAINTENANCE_IF_MAIN;