Merge "jquery.byteLimit: Partial rewrite to fix logic errors"
[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( 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 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->setBatchSize( 50 );
46 }
47
48 public function execute() {
49 $src = FileBackendGroup::singleton()->get( $this->getOption( 'src' ) );
50 $dst = FileBackendGroup::singleton()->get( $this->getOption( 'dst' ) );
51 $containers = explode( '|', $this->getOption( 'containers' ) );
52 $subDir = $this->getOption( rtrim( 'subdir', '/' ), '' );
53
54 $count = 0;
55 foreach ( $containers as $container ) {
56 if ( $subDir != '' ) {
57 $backendRel = "$container/$subDir";
58 $this->output( "Doing container '$container', directory '$subDir'...\n" );
59 } else {
60 $backendRel = $container;
61 $this->output( "Doing container '$container'...\n" );
62 }
63
64 $dir = $src->getRootStoragePath() . "/$backendRel";
65 $srcPathsRel = $src->getFileList( array( 'dir' => $dir ) );
66 if ( $srcPathsRel === null ) {
67 $this->error( "Could not list files in $container.", 1 ); // die
68 }
69
70 $batchPaths = array();
71 foreach ( $srcPathsRel as $srcPathRel ) {
72 $batchPaths[$srcPathRel] = 1; // remove duplicates
73 if ( count( $batchPaths ) >= $this->mBatchSize ) {
74 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
75 $batchPaths = array(); // done
76 }
77 ++$count;
78 }
79 if ( count( $batchPaths ) ) { // left-overs
80 $this->copyFileBatch( array_keys( $batchPaths ), $backendRel, $src, $dst );
81 $batchPaths = array(); // done
82 }
83
84 if ( $subDir != '' ) {
85 $this->output( "Finished container '$container', directory '$subDir'.\n" );
86 } else {
87 $this->output( "Finished container '$container'.\n" );
88 }
89 }
90
91 $this->output( "Done [$count file(s)].\n" );
92 }
93
94 protected function copyFileBatch(
95 array $srcPathsRel, $backendRel, FileBackend $src, FileBackend $dst
96 ) {
97 $ops = array();
98 $fsFiles = array();
99 foreach ( $srcPathsRel as $srcPathRel ) {
100 $srcPath = $src->getRootStoragePath() . "/$backendRel/$srcPathRel";
101 $dstPath = $dst->getRootStoragePath() . "/$backendRel/$srcPathRel";
102 if ( $this->filesAreSame( $src, $dst, $srcPath, $dstPath ) ) {
103 $this->output( "Already have $srcPathRel.\n" );
104 continue; // assume already copied...
105 }
106 // Note: getLocalReference() is fast for FS backends
107 $fsFile = $src->getLocalReference( array( 'src' => $srcPath, 'latest' => 1 ) );
108 if ( !$fsFile ) {
109 $this->error( "Could not get local copy of $srcPath.", 1 ); // die
110 }
111 $fsFiles[] = $fsFile; // keep TempFSFile objects alive as needed
112 // Note: prepare() is usually fast for key/value backends
113 $status = $dst->prepare( array( 'dir' => dirname( $dstPath ) ) );
114 if ( !$status->isOK() ) {
115 $this->error( print_r( $status->getErrorsArray(), true ) );
116 $this->error( "Could not copy $srcPath to $dstPath.", 1 ); // die
117 }
118 $ops[] = array( 'op' => 'store',
119 'src' => $fsFile->getPath(), 'dst' => $dstPath, 'overwrite' => 1 );
120 }
121
122 $status = $dst->doOperations( $ops, array( 'nonJournaled' => 1 ) );
123 if ( !$status->isOK() ) {
124 $this->error( print_r( $status->getErrorsArray(), true ) );
125 $this->error( "Could not copy file batch.", 1 ); // die
126 } else {
127 $this->output( "Copied these file(s):\n" . implode( "\n", $srcPathsRel ) . "\n\n" );
128 }
129 }
130
131 protected function filesAreSame( FileBackend $src, FileBackend $dst, $sPath, $dPath ) {
132 return (
133 ( $src->fileExists( array( 'src' => $sPath, 'latest' => 1 ) )
134 === $dst->fileExists( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
135 ) && ( $src->getFileSize( array( 'src' => $sPath, 'latest' => 1 ) )
136 === $dst->getFileSize( array( 'src' => $dPath, 'latest' => 1 ) ) // short-circuit
137 ) && ( $src->getFileSha1Base36( array( 'src' => $sPath, 'latest' => 1 ) )
138 === $dst->getFileSha1Base36( array( 'src' => $dPath, 'latest' => 1 ) )
139 )
140 );
141 }
142 }
143
144 $maintClass = 'CopyFileBackend';
145 require_once( RUN_MAINTENANCE_IF_MAIN );