-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmazonS3Repo.php
executable file
·552 lines (500 loc) · 18.6 KB
/
AmazonS3Repo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
class AmazonS3Repo extends CloudStorageRepo {
var $fileFactory = array( 'AmazonS3File', 'newFromTitle' );
var $oldFileFactory = array( 'CloudStorageFileArchive', 'newFromTitle' );
var $fileFromRowFactory = array( 'AmazonS3File', 'newFromRow' );
var $oldFileFromRowFactory = array( 'CloudStorageFileArchive', 'newFromRow' );
function __construct( $info ) {
parent::__construct( $info );
$this->hashLevels = isset( $info['hashLevels'] ) ? $info['hashLevels'] : 2;
$this->deletedHashLevels = isset( $info['deletedHashLevels'] ) ?
$info['deletedHashLevels'] : $this->hashLevels;
$this->deletedDir = isset( $info['deletedDir'] ) ? $info['deletedDir'] : false;
$this->urlbase = $info['urlbase'];
if (!$this->directory)
$this->directory = isset( $info['directory'] ) ? $info['directory'] : "http://s3.amazonaws.com/$wgUploadS3Bucket/$wgCloudStorageDirectory";
$this->CSS_ACCESS_KEY = $info['AWS_ACCESS_KEY'];
$this->CSS_SECRET_KEY = $info['AWS_SECRET_KEY'];
$this->bucketName = $info['AWS_S3_BUCKET'];
global $wgCss;
$wgCss->setAuth($this->CSS_ACCESS_KEY, $this->CSS_SECRET_KEY);
// Optional settings
$this->CSS_PUBLIC = isset( $info['AWS_S3_PUBLIC'] ) ? $info['AWS_S3_PUBLIC'] : false;
$wgCss->useSSL = $this->CSS_SSL = isset( $info['AWS_S3_SSL'] ) ? $info['AWS_S3_SSL'] : true;
$this->url = isset( $info['url'] ) ? $info['url'] :
($this->CSS_SSL ? "https://" : "http://") . "s3.amazonaws.com/" .
$this->bucketName . "/" . $this->directory;
}
function newFileFromRow( $row ) {
if ( isset( $row->img_name ) ) {
return call_user_func( $this->fileFromRowFactory, $row, $this );
} elseif ( isset( $row->oi_name ) ) {
return call_user_func( $this->oldFileFromRowFactory, $row, $this );
} else {
throw new MWException( __METHOD__.': invalid row' );
}
}
function newFromArchiveName( $title, $archiveName ) {
return CloudStorageFileArchive::newFromArchiveName( $title, $this, $archiveName );
}
/**
* Delete files in the deleted directory if they are not referenced in the
* filearchive table. This needs to be done in the repo because it needs to
* interleave database locks with file operations, which is potentially a
* remote operation.
* @return FileRepoStatus
*/
function cleanupDeletedBatch( $storageKeys ) {
$root = $this->getZonePath( 'deleted' );
$dbw = $this->getMasterDB();
$status = $this->newGood();
$storageKeys = array_unique($storageKeys);
foreach ( $storageKeys as $key ) {
$hashPath = $this->getDeletedHashPath( $key );
$path = "$root/$hashPath$key";
$dbw->begin();
$inuse = $dbw->selectField( 'filearchive', '1',
array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
__METHOD__, array( 'FOR UPDATE' ) );
if( !$inuse ) {
$sha1 = substr( $key, 0, strcspn( $key, '.' ) );
$ext = substr( $key, strcspn($key,'.') + 1 );
$ext = File::normalizeExtension($ext);
$inuse = $dbw->selectField( 'oldimage', '1',
array( 'oi_sha1' => $sha1,
'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE ),
__METHOD__, array( 'FOR UPDATE' ) );
}
if ( !$inuse ) {
wfDebug( __METHOD__ . ": deleting $key\n" );
if ( !@unlink( $path ) ) {
$status->error( 'undelete-cleanup-error', $path );
$status->failCount++;
}
} else {
wfDebug( __METHOD__ . ": $key still in use\n" );
$status->successCount++;
}
$dbw->commit();
}
return $status;
}
/**
* Checks if there is a redirect named as $title
*
* @param $title Title of file
*/
function checkRedirect( $title ) {
global $wgMemc;
if( is_string( $title ) ) {
$title = Title::newFromTitle( $title );
}
if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
$title = Title::makeTitle( NS_FILE, $title->getText() );
}
$memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
if ( $memcKey === false ) {
$memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
$expiry = 300; // no invalidation, 5 minutes
} else {
$expiry = 86400; // has invalidation, 1 day
}
$cachedValue = $wgMemc->get( $memcKey );
if ( $cachedValue === ' ' || $cachedValue === '' ) {
// Does not exist
return false;
} elseif ( strval( $cachedValue ) !== '' ) {
return Title::newFromText( $cachedValue, NS_FILE );
} // else $cachedValue is false or null: cache miss
$id = $this->getArticleID( $title );
if( !$id ) {
$wgMemc->set( $memcKey, " ", $expiry );
return false;
}
$dbr = $this->getSlaveDB();
$row = $dbr->selectRow(
'redirect',
array( 'rd_title', 'rd_namespace' ),
array( 'rd_from' => $id ),
__METHOD__
);
if( $row && $row->rd_namespace == NS_FILE ) {
$targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
$wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
return $targetTitle;
} else {
$wgMemc->set( $memcKey, '', $expiry );
return false;
}
}
/**
* Function link Title::getArticleID().
* We can't say Title object, what database it should use, so we duplicate that function here.
*/
protected function getArticleID( $title ) {
if( !$title instanceof Title ) {
return 0;
}
$dbr = $this->getSlaveDB();
$id = $dbr->selectField(
'page', // Table
'page_id', //Field
array( //Conditions
'page_namespace' => $title->getNamespace(),
'page_title' => $title->getDBkey(),
),
__METHOD__ //Function name
);
return $id;
}
/**
* Get an array or iterator of file objects for files that have a given
* SHA-1 content hash.
*/
function findBySha1( $hash ) {
$dbr = $this->getSlaveDB();
$res = $dbr->select(
'image',
CloudStorageFile::selectFields(),
array( 'img_sha1' => $hash )
);
$result = array();
while ( $row = $res->fetchObject() )
$result[] = $this->newFileFromRow( $row );
$res->free();
return $result;
}
/**
* Get a connection to the slave DB
*/
function getSlaveDB() {
return wfGetDB( DB_SLAVE );
}
/**
* Get a connection to the master DB
*/
function getMasterDB() {
return wfGetDB( DB_MASTER );
}
/**
* Get a key on the primary cache for this repository.
* Returns false if the repository's cache is not accessible at this site.
* The parameters are the parts of the key, as for wfMemcKey().
*/
function getSharedCacheKey( /*...*/ ) {
$args = func_get_args();
return call_user_func_array( 'wfMemcKey', $args );
}
/**
* Invalidates image redirect cache related to that image
*
* @param $title Title of page
*/
function invalidateImageRedirect( $title ) {
global $wgMemc;
$memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
if ( $memcKey ) {
$wgMemc->delete( $memcKey );
}
}
/**
* Store a batch of files from local (i.e. Windows or Linux) filesystem to S3
*
* @param $triplets Array: (src,zone,dest) triplets as per store()
* @param $flags Integer: bitwise combination of the following flags:
* self::DELETE_SOURCE Delete the source file after upload
* self::OVERWRITE Overwrite an existing destination file instead of failing
* self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
* same contents as the source (not implemented in S3)
*/
function storeBatch( $triplets, $flags = 0 ) {
wfDebug(__METHOD__." triplets: ".print_r($triplets,true)."flags: ".print_r($flags)."\n");
global $wgCss;
$status = $this->newGood();
foreach ( $triplets as $i => $triplet ) {
list( $srcPath, $dstZone, $dstRel ) = $triplet;
$root = $this->getZonePath( $dstZone );
if ( !$root ) {
throw new MWException( "Invalid zone: $dstZone" );
}
if ( !$this->validateFilename( $dstRel ) ) {
throw new MWException( 'Validation error in $dstRel' );
}
$dstPath = "$root/$dstRel";
if ( self::isVirtualUrl( $srcPath ) ) {
$srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
}
$s3path = $srcPath;
$info = $wgCss->getObjectInfo($this->bucketName, $s3path);
if ( ! $info && !is_file( $srcPath ) ) { // check both local system and S3
// Make a list of files that don't exist for return to the caller
$status->fatal( 'filenotfound', $srcPath );
continue;
}
$s3path = $dstPath;
$info = $wgCss->getObjectInfo($this->bucketName, $s3path);
wfDebug(__METHOD__."(validation) s3path-dest: $s3path\ninfo:".print_r($info,true)."\n");
if ( !( $flags & self::OVERWRITE ) && $info ) {
$status->fatal( 'fileexistserror', $dstPath );
}
}
$deleteDest = wfIsWindows() && ( $flags & self::OVERWRITE );
// Abort now on failure
if ( !$status->ok ) {
return $status;
}
foreach ( $triplets as $triplet ) {
list( $srcPath, $dstZone, $dstRel ) = $triplet;
$root = $this->getZonePath( $dstZone );
$dstPath = "$root/$dstRel";
$good = true;
if ( $flags & self::DELETE_SOURCE ) {
wfDebug(__METHOD__."(delete): dstPath: $dstPath, ".print_r($triplet,true));
if ( $deleteDest ) {
if(! $wgCss->deleteObject($this->bucketName, $dstPath)) {
wfDebug(__METHOD__.": FAILED - delete: $dstPath");
}
}
$info = $wgCss->getObjectInfo($this->bucketName, $srcPath);
if ( ! $info ) { // local file
if ( ! $wgCss->putObjectFile($srcPath, $this->bucketName, $dstPath,
($this->CSS_PUBLIC ? AmazonS3Service::ACL_PUBLIC_READ : AmazonS3Service::ACL_PRIVATE))) {
$status->error( 'filecopyerror', $srcPath, $dstPath );
$good = false;
}
unlink( $srcPath );
} else { // s3 file
if ( ! $wgCss->copyObject($this->bucketName, $srcPath,
$this->bucketName, $dstPath,
($this->CSS_PUBLIC ? AmazonS3Service::ACL_PUBLIC_READ : AmazonS3Service::ACL_PRIVATE)) &&
! $wgCss->deleteObject($this->bucketName, $srcPath)) {
$status->error( 'filecopyerror', $srcPath, $dstPath );
$good = false;
}
}
} else {
wfDebug(__METHOD__."(transfer): dstPath: $dstPath, ".print_r($triplet,true));
$info = $wgCss->getObjectInfo($this->bucketName, $srcPath);
if ( ! $info ) { // local file
if ( ! $wgCss->putObjectFile($srcPath, $this->bucketName, $dstPath,
($this->CSS_PUBLIC ? AmazonS3Service::ACL_PUBLIC_READ : AmazonS3Service::ACL_PRIVATE))) {
$status->error( 'filecopyerror', $srcPath, $dstPath );
$good = false;
}
unlink( $srcPath );
} else { // s3 file
if ( ! $wgCss->copyObject($this->bucketName, $srcPath,
$this->bucketName, $dstPath,
($this->CSS_PUBLIC ? AmazonS3Service::ACL_PUBLIC_READ : AmazonS3Service::ACL_PRIVATE)) &&
! $wgCss->deleteObject($this->bucketName, $srcPath)) {
$status->error( 'filecopyerror', $srcPath, $dstPath );
$good = false;
}
}
}
if ( $good ) {
$status->successCount++;
} else {
$status->failCount++;
}
}
return $status;
}
/**
* Append a file from from local (i.e. Windows or Linux) filesystem to existing file
*
* @param $srcPath - file on filesystem
* @param $toAppendPath - file from local filesystem
* @param $flags Integer: bitwise combination of the following flags:
* self::DELETE_SOURCE Delete the toAppend file after append
*/
function append( $srcPath, $toAppendPath, $flags = 0 ) {
global $wgCss;
$status = $this->newGood();
// Resolve the virtual URL
if ( self::isVirtualUrl( $srcPath ) ) {
$srcPath = $this->resolveVirtualUrl( $srcPath );
}
// Make sure the files are there
if ( !is_file( $toAppendPath ) )
$status->fatal( 'filenotfound', $toAppendPath );
$info = $wgCss->getObjectInfo($this->bucketName, $srcPath);
if ( ! $info )
$status->fatal( 'filenotfound', $srcPath );
if ( !$status->isOk() ) return $status;
// Do the append
$tmpLoc = tempnam(wfTempDir(), "Append");
if(! $wgCss->getObject($this->bucketName, $srcPath, $tmpLoc)) {
$status->fatal( 'fileappenderrorread', $srcPath );
}
$chunk = file_get_contents( $toAppendPath );
if( $chunk === false ) {
$status->fatal( 'fileappenderrorread', $toAppendPath );
}
if( $status->isOk() ) {
if ( file_put_contents( $tmpLoc, $chunk, FILE_APPEND ) ) {
$status->value = $srcPath;
if ( ! $wgCss->putObjectFile($tmpLoc, $this->bucketName, $srcPath,
($this->CSS_PUBLIC ? AmazonS3Service::ACL_PUBLIC_READ : AmazonS3Service::ACL_PRIVATE))) {
$status->fatal( 'fileappenderror', $toAppendPath, $srcPath);
}
} else {
$status->fatal( 'fileappenderror', $toAppendPath, $srcPath);
}
}
if ( $flags & self::DELETE_SOURCE ) {
unlink( $toAppendPath );
}
return $status;
}
/**
* Publish a batch of files
* @param $triplets Array: (source,dest,archive) triplets as per publish()
* source can be on local machine or on S3, dest must be on S3
* @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
* that the source files should be deleted if possible
*/
function publishBatch( $triplets, $flags = 0 ) {
// Perform initial checks
wfDebug(__METHOD__.": ".print_r($triplets,true));
global $wgCss;
$status = $this->newGood( array() );
foreach ( $triplets as $i => $triplet ) {
list( $srcPath, $dstRel, $archiveRel ) = $triplet;
if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
$triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
}
if ( !$this->validateFilename( $dstRel ) ) {
throw new MWException( 'Validation error in $dstRel' );
}
if ( !$this->validateFilename( $archiveRel ) ) {
throw new MWException( 'Validation error in $archiveRel' );
}
$dstPath = "{$this->directory}/$dstRel";
$archivePath = "{$this->directory}/$archiveRel";
$dstDir = dirname( $dstPath );
$archiveDir = dirname( $archivePath );
$infoS3 = $wgCss->getObjectInfo($this->bucketName, $srcPath); // see if on S3
$infoLoc = is_file( $srcPath ); // see if local file
wfDebug(__METHOD__."(validation) srcPath: $srcPath, infoLoc: $infoLoc, infoS3:".print_r($infoS3,true)."\n");
if ( ! $infoS3 && ! $infoLoc ) {
// Make a list of files that don't exist for return to the caller
$status->fatal( 'filenotfound', $srcPath );
}
}
if ( !$status->ok ) {
return $status;
}
foreach ( $triplets as $i => $triplet ) {
list( $srcPath, $dstRel, $archiveRel ) = $triplet;
$dstPath = "{$this->directory}/$dstRel";
$archivePath = "{$this->directory}/$archiveRel";
// Archive destination file if it exists
$info = $wgCss->getObjectInfo($this->bucketName, $dstPath);
wfDebug(__METHOD__."(transfer) dstPath: $dstPath, info:".print_r($info,true)."\n");
if( $info ) {
// Check if the archive file exists
// This is a sanity check to avoid data loss. In UNIX, the rename primitive
// unlinks the destination file if it exists. DB-based synchronisation in
// publishBatch's caller should prevent races. In Windows there's no
// problem because the rename primitive fails if the destination exists.
if ( is_file( $archivePath ) ) {
$success = false;
} else // Check if the archive file exists
// This is a sanity check to avoid data loss. In UNIX, the rename primitive
// unlinks the destination file if it exists. DB-based synchronisation in
// publishBatch's caller should prevent races. In Windows there's no
// problem because the rename primitive fails if the destination exists.
$path = /*$this->directory/*AWS_S3_FOLDER .*/ $archivePath;
$info = $wgCss->getObjectInfo($this->bucketName, $path);
wfDebug(__METHOD__."(file exists): $path, info:".print_r($info,true)."\n");
if ( $info /*is_file( $archivePath )*/ ) {
$success = false;
} else {
wfDebug(__METHOD__.": moving file $dstPath to $archivePath\n");
if(! (
$wgCss->copyObject($this->bucketName, $dstPath,
$this->bucketName, $archivePath,
($this->CSS_PUBLIC ? S3::ACL_PUBLIC_READ : S3::ACL_PRIVATE)) &&
$wgCss->deleteObject($this->bucketName, $dstPath))
) {
wfDebug(__METHOD__.": FAILED moving file $dstPath to $archivePath\n");
$success = false;
} else {
$success = true;
}
}
if( !$success ) {
$status->error( 'filerenameerror',$dstPath, $archivePath );
$status->failCount++;
continue;
} else {
wfDebug(__METHOD__.": moved file $dstPath to $archivePath\n");
}
$status->value[$i] = 'archived';
} else {
$status->value[$i] = 'new';
}
$good = true;
wfSuppressWarnings();
if(! is_file( $srcPath )) {
// S3
if(! $wgCss->copyObject($this->bucketName, $srcPath, $this->bucketName,
$this->AWS_S3_FOLDER . $dstPath, ($this->CSS_PUBLIC ? S3::ACL_PUBLIC_READ : S3::ACL_PRIVATE))) {
wfDebug(__METHOD__.": FAILED - copy: $srcPath to $dstPath");
}
//$css->putObjectFile($srcPath, $this->AWS_S3_BUCKET, $this->directory/*AWS_S3_FOLDER*/ . $dstPath, ($this->AWS_S3_PUBLIC ? S3::ACL_PUBLIC_READ : S3::ACL_PRIVATE));
if ( $flags & self::DELETE_SOURCE ) {
if(! $wgCss->deleteObject($this->bucketName, /*$this->directory/*AWS_S3_FOLDER .*/ $srcPath)) {
wfDebug(__METHOD__.": FAILED - delete: $srcPath");
}
}
} else {
// Local file
if(! $wgCss->putObjectFile($srcPath, $this->bucketName, $dstPath,
($this->CSS_PUBLIC ? S3::ACL_PUBLIC_READ : S3::ACL_PRIVATE))) {
$status->error( 'filecopyerror', $srcPath, $dstPath );
$good = false;
}
if ( $flags & self::DELETE_SOURCE ) {
unlink($srcPath);
}
}
wfRestoreWarnings();
if ( $good ) {
$status->successCount++;
wfDebug(__METHOD__.": wrote tempfile $srcPath to $dstPath\n");
} else {
$status->failCount++;
}
}
return $status;
}
/**
* Checks existence of specified array of files.
*
* @param $files Array: URLs of files to check
* @param $flags Integer: bitwise combination of the following flags:
* self::FILES_ONLY Mark file as existing only if it is a file (not directory)
* Will mark all items found on S3 as true, no directory concept exists on the S3
* @return Either array of files and existence flags, or false
*/
function fileExistsBatch( $files, $flags = 0 ) {
global $wgCss;
$result = array();
foreach ( $files as $key => $file ) {
if ( self::isVirtualUrl( $file ) ) {
$file = $this->resolveVirtualUrl( $file );
}
$info = $wgCss->getObjectInfo($this->bucketName, $file);
$result[$key] = ($info ? true : false) ;
// if( $flags & self::FILES_ONLY ) {
// $result[$key] = is_file( $file );
// } else {
// $result[$key] = file_exists( $file );
// }
}
return $result;
}
}