Merge "Add passing ''italic'''s case to 'Unclosed and unmatched quotes' test"
[lhc/web/wiklou.git] / maintenance / copyFileBackend.php
1 <?php
2 /**
3 * Copy all files in one container 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( dirname( __FILE__ ) . '/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 all the 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( 'fast', 'Skip SHA-1 checks on pre-existing files' );
45 }
46
47 public function execute() {
48 $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) );
49 $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) );
50
51 $containers = explode( '|', $this->getOption( 'containers' ) );
52 foreach ( $containers as $container ) {
53 $this->output( "Doing container $container...\n" );
54
55 $srcPathsRel = $src->getFileList(
56 array( 'dir' => $src->getRootStoragePath() . "/$container" ) );
57 if ( $srcPathsRel === null ) {
58 $this->error( "Could not list files in $container.", 1 ); // die
59 }
60 foreach ( $srcPathsRel as $srcPathRel ) {
61 $srcPath = $src->getRootStoragePath() . "/$container/$srcPathRel";
62 $dstPath = $dst->getRootStoragePath() . "/$container/$srcPathRel";
63
64 if ( $dst->fileExists( array( 'src' => $dstPath, 'latest' => 1 ) ) ) {
65 if ( $this->hasOption( 'fast' ) ) {
66 $this->output( "Already have $dstPath.\n" );
67 continue; // assume already copied...
68 }
69 $srcSha1 = $src->getFileSha1Base36( array( 'src' => $srcPath ) );
70 $dstSha1 = $dst->getFileSha1Base36( array( 'src' => $dstPath ) );
71 if ( $srcSha1 && $srcSha1 === $dstSha1 ) {
72 $this->output( "Already have $dstPath.\n" );
73 continue; // already copied...
74 }
75 }
76
77 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
78 if ( !$fsFile ) {
79 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
80 }
81
82 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ) ) );
83 $status->merge( $dst->store(
84 array( 'src' => $fsFile->getPath(), 'dst' => $dstPath ),
85 array( 'nonLocking' => 1, 'nonJournaled' => 1 )
86 ) );
87 if ( !$status->isOK() ) {
88 print_r( $status->getErrorsArray() );
89 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
90 }
91
92 $this->output( "Copied $srcPath to $dstPath.\n" );
93 }
94 }
95 }
96 }
97
98 $maintClass = 'CopyFileBackend';
99 require_once( RUN_MAINTENANCE_IF_MAIN );