r101387 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101386‎ | r101387 | r101388 >
Date:19:25, 31 October 2011
Author:aaron
Status:ok (Comments)
Tags:swift 
Comment:
Added code that contains a function to handle the LocalFilePurgeThumbnails hook by purging swift-stored thumbnails.
Modified paths:
  • /trunk/extensions/SwiftMedia/wmf/swift.php (added) (history)

Diff [purge]

Index: trunk/extensions/SwiftMedia/wmf/swift.php
@@ -0,0 +1,80 @@
 2+<?php
 3+/**
 4+ * Helper functions for Swift related image thumbnail purging.
 5+ * The functions here should only be called after MediaWiki setup.
 6+ *
 7+ * $wmfSwiftConfig must reside in PrivateSettings.php. It should also
 8+ * be extracted in CommonSettings.php to set any swift backend settings.
 9+ *
 10+ * This file belongs under wmf-config/ and should be included by CommonSettings.php.
 11+ */
 12+
 13+# Swift API client module and helper functions
 14+require_once( '/usr/local/apache/common-local/lib/cloudfiles/php-cloudfiles/cloudfiles.php' );
 15+
 16+/**
 17+ * Handler for the LocalFilePurgeThumbnails hook.
 18+ * To avoid excess inclusion of cloudfiles.php, a hook handler can be added
 19+ * to CommonSettings.php which includes this file and calls this function.
 20+ *
 21+ * @param $file File
 22+ * @param $type string "current" or "old"
 23+ * @return true
 24+ */
 25+function wmfPurgeBackendThumbCache( File $file, $type ) {
 26+ global $site, $lang; // CommonSettings.php
 27+
 28+ if ( $type === 'current' ) {
 29+ $thumbRel = $file->getRel();
 30+ } elseif ( $type === 'archive' ) {
 31+ $thumbRel = $file->getArchiveThumbRel();
 32+ } else {
 33+ return true; // sanity
 34+ }
 35+
 36+ $container = wmfGetSwiftThumbContainer( $site, $lang );
 37+ if ( $container ) { // sanity
 38+ $files = $container->list_objects( 0, NULL, "$thumbRel/" );
 39+ foreach ( $files as $file ) {
 40+ $name = "{$thumbRel}/{$file}"; // swift object name
 41+ try {
 42+ $container->delete_object( $name );
 43+ } catch ( NoSuchObjectException $e ) { // probably a race condition
 44+ wfDebugLog( 'swiftThumb', "Could not delete `{$name}`; object does not exist." );
 45+ }
 46+ }
 47+ }
 48+
 49+ return true;
 50+}
 51+
 52+/**
 53+ * Get the Swift thumbnail container for this wiki.
 54+ *
 55+ * @param $site string
 56+ * @param $lang string
 57+ * @return CF_Container|null
 58+ */
 59+function wmfGetSwiftThumbContainer( $site, $lang ) {
 60+ global $wmfSwiftConfig; // PrivateSettings.php
 61+
 62+ $auth = new CF_Authentication(
 63+ $wmfSwiftConfig['user'],
 64+ $wmfSwiftConfig['key'],
 65+ NULL,
 66+ $wmfSwiftConfig['authUrl']
 67+ );
 68+ $auth->authenticate();
 69+
 70+ $conn = new CF_Connection( $auth );
 71+
 72+ $name = "{$site}-{$lang}-images-thumb"; // swift container name
 73+ try {
 74+ $container = $conn->get_container( $name );
 75+ } catch ( NoSuchContainerException $e ) { // container not created yet
 76+ $container = null;
 77+ wfDebugLog( 'swiftThumb', "Could not access `{$name}`; container does not exist." );
 78+ }
 79+
 80+ return $container;
 81+}
Property changes on: trunk/extensions/SwiftMedia/wmf/swift.php
___________________________________________________________________
Added: svn:eol-style
182 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r101398FU r101387: tweaked cloudfiles diraaron20:06, 31 October 2011

Comments

#Comment by Johnduhart (talk | contribs)   20:52, 24 January 2012
+# Swift API client module and helper functions
+require_once( '/usr/local/apache/common-local/lib/cloudfiles/php-cloudfiles/cloudfiles.php' );
+

Removed in r109950

Status & tagging log