r109950 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109949‎ | r109950 | r109951 >
Date:20:50, 24 January 2012
Author:aaron
Status:ok
Tags:
Comment:
Upated thumbnail purge script to account for latest file backend code changes. Also made it handle sharding of enwiki and commons containers. Finally, fixed delete_object calls, as the list_object paths are already absolute.
Modified paths:
  • /trunk/extensions/SwiftMedia/wmf/swift.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SwiftMedia/wmf/swift.php
@@ -3,15 +3,13 @@
44 * Helper functions for Swift related image thumbnail purging.
55 * The functions here should only be called after MediaWiki setup.
66 *
 7+ * The SwiftCloudFiles extensions must be installed.
78 * $wmfSwiftConfig must reside in PrivateSettings.php. It should also
89 * be extracted in CommonSettings.php to set any swift backend settings.
910 *
1011 * This file belongs under wmf-config/ and should be included by CommonSettings.php.
1112 */
1213
13 -# Swift API client module and helper functions
14 -require_once( '/usr/local/apache/common-local/lib/php-cloudfiles/cloudfiles.php' );
15 -
1614 /**
1715 * Handler for the LocalFilePurgeThumbnails hook.
1816 * To avoid excess inclusion of cloudfiles.php, a hook handler can be added
@@ -24,21 +22,22 @@
2523 function wmfPurgeBackendThumbCache( File $file, $archiveName ) {
2624 global $site, $lang; // CommonSettings.php
2725
 26+ // Get thumbnail dir relative to thumb zone
2827 if ( $archiveName !== false ) {
2928 $thumbRel = $file->getArchiveThumbRel( $archiveName ); // old version
3029 } else {
3130 $thumbRel = $file->getRel(); // current version
3231 }
3332
34 - $container = wmfGetSwiftThumbContainer( $site, $lang );
 33+ // Get the container for the thumb zone and delete the objects
 34+ $container = wmfGetSwiftThumbContainer( $site, $lang, "$thumbRel/" );
3535 if ( $container ) { // sanity
3636 $files = $container->list_objects( 0, NULL, "$thumbRel/" );
3737 foreach ( $files as $file ) {
38 - $name = "{$thumbRel}/{$file}"; // swift object name
3938 try {
40 - $container->delete_object( $name );
 39+ $container->delete_object( $file );
4140 } 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." );
4342 }
4443 }
4544 }
@@ -51,10 +50,11 @@
5251 *
5352 * @param $site string
5453 * @param $lang string
 54+ * @param $relPath string Path relative to container
5555 * @return CF_Container|null
5656 */
57 -function wmfGetSwiftThumbContainer( $site, $lang ) {
58 - global $wmfSwiftConfig; // PrivateSettings.php
 57+function wmfGetSwiftThumbContainer( $site, $lang, $relPath ) {
 58+ global $wmfSwiftConfig; // from PrivateSettings.php
5959
6060 $auth = new CF_Authentication(
6161 $wmfSwiftConfig['user'],
@@ -66,7 +66,19 @@
6767
6868 $conn = new CF_Connection( $auth );
6969
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+
7183 try {
7284 $container = $conn->get_container( $name );
7385 } catch ( NoSuchContainerException $e ) { // container not created yet

Status & tagging log