MediaWiki  REL1_19
ExternalStore.php
Go to the documentation of this file.
00001 <?php
00015 class ExternalStore {
00016         var $mParams;
00017         
00018         function __construct( $params = array() ) {
00019                 $this->mParams = $params;
00020         }
00021         
00029         static function fetchFromURL( $url, $params = array() ) {
00030                 global $wgExternalStores;
00031 
00032                 if( !$wgExternalStores )
00033                         return false;
00034 
00035                 $parts = explode( '://', $url, 2 );
00036 
00037                 if ( count( $parts ) != 2 ) {
00038                         return false;
00039                 }
00040 
00041                 list( $proto, $path ) = $parts;
00042 
00043                 if ( $path == '' ) { // Bad URL
00044                         return false;
00045                 }
00046 
00047                 $store = self::getStoreObject( $proto, $params );
00048                 if ( $store === false )
00049                         return false;
00050                 return $store->fetchFromURL( $url );
00051         }
00052 
00060         static function getStoreObject( $proto, $params = array() ) {
00061                 global $wgExternalStores;
00062                 if( !$wgExternalStores )
00063                         return false;
00064                 /* Protocol not enabled */
00065                 if( !in_array( $proto, $wgExternalStores ) )
00066                         return false;
00067 
00068                 $class = 'ExternalStore' . ucfirst( $proto );
00069                 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
00070                 if( !MWInit::classExists( $class ) ) {
00071                         return false;
00072                 }
00073 
00074                 return new $class($params);
00075         }
00076 
00086         static function insert( $url, $data, $params = array() ) {
00087                 list( $proto, $params ) = explode( '://', $url, 2 );
00088                 $store = self::getStoreObject( $proto, $params );
00089                 if ( $store === false ) {
00090                         return false;
00091                 } else {
00092                         return $store->store( $params, $data );
00093                 }
00094         }
00095         
00105         public static function insertToDefault( $data, $storageParams = array() ) {
00106                 global $wgDefaultExternalStore;
00107                 $tryStores = (array)$wgDefaultExternalStore;
00108                 $error = false;
00109                 while ( count( $tryStores ) > 0 ) {
00110                         $index = mt_rand(0, count( $tryStores ) - 1);
00111                         $storeUrl = $tryStores[$index];
00112                         wfDebug( __METHOD__.": trying $storeUrl\n" );
00113                         list( $proto, $params ) = explode( '://', $storeUrl, 2 );
00114                         $store = self::getStoreObject( $proto, $storageParams );
00115                         if ( $store === false ) {
00116                                 throw new MWException( "Invalid external storage protocol - $storeUrl" );
00117                         }
00118                         try {
00119                                 $url = $store->store( $params, $data ); // Try to save the object
00120                         } catch ( DBConnectionError $error ) {
00121                                 $url = false;
00122                         } catch( DBQueryError $error ) {
00123                                 $url = false;
00124                         }
00125                         if ( $url ) {
00126                                 return $url; // Done!
00127                         } else {
00128                                 unset( $tryStores[$index] ); // Don't try this one again!
00129                                 $tryStores = array_values( $tryStores ); // Must have consecutive keys
00130                                 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
00131                         }
00132                 }
00133                 // All stores failed
00134                 if ( $error ) {
00135                         // Rethrow the last connection error
00136                         throw $error;
00137                 } else {
00138                         throw new MWException( "Unable to store text to external storage" );
00139                 }
00140         }
00141         
00148         public static function insertToForeignDefault( $data, $wiki ) {
00149                 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );
00150         }
00151 }