Merge "Remove Parser_LinkHooks and CoreLinkFunctions classes"
[lhc/web/wiklou.git] / maintenance / copyFileBackend.php
1 <?php
2 /**
3 * Copy all files in some containers of one backend to another.
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 one container of one backend to another.
28 *
29 * This can also be used to re-shard the files for one backend using the
30 * config of second backend. The second backend should have the same config
31 * as the first, except for it having a different name and different sharding
32 * configuration. The backend should be made read-only while this runs.
33 * After this script finishes, the old files in the containers can be deleted.
34 *
35 * @ingroup Maintenance
36 */
37 class CopyFileBackend extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->mDescription = "Copy files in one backend to another.";
41 $this->addOption( 'src', 'Backend containing the source files', true, true );
42 $this->addOption( 'dst', 'Backend where files should be copied to', true, true );
43 $this->addOption( 'containers', 'Pipe separated list of containers', true, true );
44 $this->addOption( 'subdir', 'Only do items in this child directory', false, true );
45 $this->addOption( 'ratefile', 'File to check periodically for batch size', false, true );
46 $this->addOption( 'skiphash', 'Skip SHA-1 sync checks for files' );
47 $this->addOption( 'missingonly', 'Only copy files missing from destination listing' );
48 $this->addOption( 'utf8only', 'Skip source files that do not have valid UTF-8 names' );
49 $this->setBatchSize( 50 );
50 }
51
52 public function execute() {
53 $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) );
54 $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) );
55 $containers = explode( '|', $this->getOption( 'containers' ) );
56 $subDir = $this->getOption( rtrim( 'subdir', '/' ), '' );
57
58 $rateFile = $this->getOption( 'ratefile' );
59
60 if ( $this->hasOption( 'utf8only' ) && !extension_loaded( 'mbstring' ) ) {
61 $this->error( "Cannot check for UTF-8, mbstring extension missing.", 1 ); // die
62 }
63
64 $count = 0;
65 foreach ( $containers as $container ) {
66 if ( $subDir != '' ) {
67 $backendRel = "$container/$subDir";
68 $this->output( "Doing container '$container', directory '$subDir'...\n" );
69 } else {
70 $backendRel = $container;
71 $this->output( "Doing container '$container'...\n" );
72 }
73
74 $srcPathsRel = $src->getFileList( array(
75 'dir' => $src->getRootStoragePath() . "/$backendRel" ) );
76 if ( $srcPathsRel === null ) {
77 $this->error( "Could not list files in $container.", 1 ); // die
78 }
79
80 // Do a listing comparison if specified
81 if ( $this->hasOption( 'missingonly' ) ) {
82 $dstPathsRel = $dst->getFileList( array(
83 'dir' => $dst->getRootStoragePath() . "/$backendRel" ) );
84 if ( $dstPathsRel === null ) {
85 $this->error( "Could not list files in $container.", 1 ); // die
86 }
87 // Get the list of destination files
88 $relFilesDstSha1 = array();
89 foreach ( $dstPathsRel as $dstPathRel ) {
90 $relFilesDstSha1[sha1( $dstPathRel )] = 1;
91 }
92 unset( $dstPathsRel ); // free
93 // Get the list of missing files
94 $missingPathsRel = array();
95 foreach ( $srcPathsRel as $srcPathRel ) {
96 if ( !isset( $relFilesDstSha1[sha1( $srcPathRel )] ) ) {
97 $missingPathsRel[] = $srcPathRel;
98 }
99 }
100 unset( $srcPathsRel ); // free
101 // Only copy the missing files over in the next loop
102 $srcPathsRel = $missingPathsRel;
103 $this->output( count( $srcPathsRel ) . " file(s) need to be copied.\n" );
104 }
105
106 $batchPaths = array();
107 foreach ( $srcPathsRel as $srcPathRel ) {
108 // Check up on the rate file periodically to adjust the concurrency
109 if ( $rateFile && ( !$count || ( $count % 500 ) == 0 ) ) {
110 $this->mBatchSize = max( 1, (int)file_get_contents( $rateFile ) );
111 $this->output( "Batch size is now {$this->mBatchSize}.\n" );
112 }
113 $batchPaths[$srcPathRel] = 1; // remove duplicates
114 if ( count( $batchPaths ) >= $this->mBatchSize ) {
115 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
116 $batchPaths = array(); // done
117 }
118 ++$count;
119 }
120 if ( count( $batchPaths ) ) { // left-overs
121 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
122 $batchPaths = array(); // done
123 }
124
125 if ( $subDir != '' ) {
126 $this->output( "Finished container '$container', directory '$subDir'.\n" );
127 } else {
128 $this->output( "Finished container '$container'.\n" );
129 }
130 }
131
132 $this->output( "Done [$count file(s)].\n" );
133 }
134
135 protected function copyFileBatch(
136 array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst
137 ) {
138 $ops = array();
139 $fsFiles = array();
140 $copiedRel = array(); // for output message
141
142 // Download the batch of source files into backend cache...
143 if ( $this->hasOption( 'missingonly' ) ) {
144 $srcPaths = array();
145 foreach ( $srcPathsRel as $srcPathRel ) {
146 $srcPaths[] = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
147 }
148 $t_start = microtime( true );
149 $fsFiles = $src->getLocalReferenceMulti( array( 'srcs' => $srcPaths, 'latest' => 1 ) );
150 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
151 $this->output( "\nDownloaded these file(s) [{$ellapsed_ms}ms]:\n" .
152 implode( "\n", $srcPaths ) . "\n\n" );
153 }
154
155 // Determine what files need to be copied over...
156 foreach ( $srcPathsRel as $srcPathRel ) {
157 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
158 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
159 if ( $this->hasOption( 'utf8only' ) && !mb_check_encoding( $srcPath, 'UTF-8' ) ) {
160 $this->error( "Detected illegal (non-UTF8) path for $srcPath." );
161 continue;
162 } elseif ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
163 $this->output( "Already have $srcPathRel.\n" );
164 continue; // assume already copied...
165 }
166 $fsFile = array_key_exists( $srcPath, $fsFiles )
167 ? $fsFiles[$srcPath]
168 : $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
169 if ( !$fsFile ) {
170 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
171 } elseif ( !$fsFile->exists() ) {
172 // FSFileBackends just return the path for getLocalReference() and paths with
173 // illegal slashes may get normalized to a different path. This can cause the
174 // local reference to not exist...skip these broken files.
175 $this->error( "Detected possible illegal path for $srcPath." );
176 continue;
177 }
178 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
179 // Note: prepare() is usually fast for key/value backends
180 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ), 'bypassReadOnly' => 1 ) );
181 if ( !$status->isOK() ) {
182 $this->error( print_r( $status->getErrorsArray(), true ) );
183 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
184 }
185 $ops[] = array( 'op' => 'store',
186 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
187 $copiedRel[] = $srcPathRel;
188 }
189
190 // Copy in the batch of source files...
191 $t_start = microtime( true );
192 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
193 if ( !$status->isOK() ) {
194 sleep( 10 ); // wait and retry copy again
195 $status = $dst->doQuickOperations( $ops, array( 'bypassReadOnly' => 1 ) );
196 }
197 $ellapsed_ms = floor( ( microtime( true ) - $t_start ) * 1000 );
198 if ( !$status->isOK() ) {
199 $this->error( print_r( $status->getErrorsArray(), true ) );
200 $this->error( "Could not copy file batch.", 1 ); // die
201 } elseif ( count( $copiedRel ) ) {
202 $this->output( "\nCopied these file(s) [{$ellapsed_ms}ms]:\n" .
203 implode( "\n", $copiedRel ) . "\n\n" );
204 }
205 }
206
207 protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) {
208 $skipHash = $this->hasOption( 'skiphash' );
209 return (
210 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
211 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
212 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
213 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
214 ) && ( $skipHash || ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
215 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
216 ) )
217 );
218 }
219 }
220
221 $maintClass = 'CopyFileBackend';
222 require_once( RUN_MAINTENANCE_IF_MAIN );