Index: trunk/extensions/SwiftMedia/wmf/swift.php |
— | — | @@ -3,15 +3,13 @@ |
4 | 4 | * Helper functions for Swift related image thumbnail purging. |
5 | 5 | * The functions here should only be called after MediaWiki setup. |
6 | 6 | * |
| 7 | + * The SwiftCloudFiles extensions must be installed. |
7 | 8 | * $wmfSwiftConfig must reside in PrivateSettings.php. It should also |
8 | 9 | * be extracted in CommonSettings.php to set any swift backend settings. |
9 | 10 | * |
10 | 11 | * This file belongs under wmf-config/ and should be included by CommonSettings.php. |
11 | 12 | */ |
12 | 13 | |
13 | | -# Swift API client module and helper functions |
14 | | -require_once( '/usr/local/apache/common-local/lib/php-cloudfiles/cloudfiles.php' ); |
15 | | - |
16 | 14 | /** |
17 | 15 | * Handler for the LocalFilePurgeThumbnails hook. |
18 | 16 | * To avoid excess inclusion of cloudfiles.php, a hook handler can be added |
— | — | @@ -24,21 +22,22 @@ |
25 | 23 | function wmfPurgeBackendThumbCache( File $file, $archiveName ) { |
26 | 24 | global $site, $lang; // CommonSettings.php |
27 | 25 | |
| 26 | + // Get thumbnail dir relative to thumb zone |
28 | 27 | if ( $archiveName !== false ) { |
29 | 28 | $thumbRel = $file->getArchiveThumbRel( $archiveName ); // old version |
30 | 29 | } else { |
31 | 30 | $thumbRel = $file->getRel(); // current version |
32 | 31 | } |
33 | 32 | |
34 | | - $container = wmfGetSwiftThumbContainer( $site, $lang ); |
| 33 | + // Get the container for the thumb zone and delete the objects |
| 34 | + $container = wmfGetSwiftThumbContainer( $site, $lang, "$thumbRel/" ); |
35 | 35 | if ( $container ) { // sanity |
36 | 36 | $files = $container->list_objects( 0, NULL, "$thumbRel/" ); |
37 | 37 | foreach ( $files as $file ) { |
38 | | - $name = "{$thumbRel}/{$file}"; // swift object name |
39 | 38 | try { |
40 | | - $container->delete_object( $name ); |
| 39 | + $container->delete_object( $file ); |
41 | 40 | } catch ( NoSuchObjectException $e ) { // probably a race condition |
42 | | - wfDebugLog( 'swiftThumb', "Could not delete `{$name}`; object does not exist." ); |
| 41 | + wfDebugLog( 'swiftThumb', "Could not delete `{$file}`; object does not exist." ); |
43 | 42 | } |
44 | 43 | } |
45 | 44 | } |
— | — | @@ -51,10 +50,11 @@ |
52 | 51 | * |
53 | 52 | * @param $site string |
54 | 53 | * @param $lang string |
| 54 | + * @param $relPath string Path relative to container |
55 | 55 | * @return CF_Container|null |
56 | 56 | */ |
57 | | -function wmfGetSwiftThumbContainer( $site, $lang ) { |
58 | | - global $wmfSwiftConfig; // PrivateSettings.php |
| 57 | +function wmfGetSwiftThumbContainer( $site, $lang, $relPath ) { |
| 58 | + global $wmfSwiftConfig; // from PrivateSettings.php |
59 | 59 | |
60 | 60 | $auth = new CF_Authentication( |
61 | 61 | $wmfSwiftConfig['user'], |
— | — | @@ -66,7 +66,19 @@ |
67 | 67 | |
68 | 68 | $conn = new CF_Connection( $auth ); |
69 | 69 | |
70 | | - $name = "{$site}-{$lang}-media-thumb"; // swift container name |
| 70 | + $wikiId = "{$site}-{$lang}"; |
| 71 | + |
| 72 | + // Get the full swift container name, including any shard suffix |
| 73 | + $name = "{$wikiId}-local-thumb"; |
| 74 | + if ( in_array( $wikiId, array( 'wikipedia-commons', 'wikipedia-en' ) ) ) { |
| 75 | + // Code stolen from FileBackend::getContainerShard() |
| 76 | + if ( preg_match( "!^(?:[^/]{2,}/)*[0-9a-f]/(?P<shard>[0-9a-f]{2})(?:/|$)!", $relPath, $m ) ) { |
| 77 | + $name .= '.' . $m['shard']; |
| 78 | + } else { |
| 79 | + throw new MWException( "Can't determine shard of path '$relPath' for '$wikiId'." ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
71 | 83 | try { |
72 | 84 | $container = $conn->get_container( $name ); |
73 | 85 | } catch ( NoSuchContainerException $e ) { // container not created yet |