00001 <?php
00015 class ExternalStore {
00016
00017 static function fetchFromURL( $url ) {
00018 global $wgExternalStores;
00019
00020 if( !$wgExternalStores )
00021 return false;
00022
00023 @list( $proto, $path ) = explode( '://', $url, 2 );
00024
00025 if( $path == '' )
00026 return false;
00027
00028 $store = self::getStoreObject( $proto );
00029 if ( $store === false )
00030 return false;
00031 return $store->fetchFromURL( $url );
00032 }
00033
00037 static function getStoreObject( $proto ) {
00038 global $wgExternalStores;
00039 if( !$wgExternalStores )
00040 return false;
00041
00042 if( !in_array( $proto, $wgExternalStores ) )
00043 return false;
00044
00045 $class = 'ExternalStore' . ucfirst( $proto );
00046
00047 if( !class_exists( $class ) ) {
00048 return false;
00049 }
00050
00051 return new $class();
00052 }
00053
00060 static function insert( $url, $data ) {
00061 list( $proto, $params ) = explode( '://', $url, 2 );
00062 $store = self::getStoreObject( $proto );
00063 if ( $store === false ) {
00064 return false;
00065 } else {
00066 return $store->store( $params, $data );
00067 }
00068 }
00069
00078 public static function insertToDefault( $data ) {
00079 global $wgDefaultExternalStore;
00080 $tryStores = (array)$wgDefaultExternalStore;
00081 $error = false;
00082 while ( count( $tryStores ) > 0 ) {
00083 $index = mt_rand(0, count( $tryStores ) - 1);
00084 $storeUrl = $tryStores[$index];
00085 wfDebug( __METHOD__.": trying $storeUrl\n" );
00086 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
00087 $store = self::getStoreObject( $proto );
00088 if ( $store === false ) {
00089 throw new MWException( "Invalid external storage protocol - $storeUrl" );
00090 }
00091 try {
00092 $url = $store->store( $params, $data );
00093 } catch ( DBConnectionError $error ) {
00094 $url = false;
00095 } catch( DBQueryError $error ) {
00096 $url = false;
00097 }
00098 if ( $url ) {
00099 return $url;
00100 } else {
00101 unset( $tryStores[$index] );
00102 $tryStores = array_values( $tryStores );
00103 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
00104 }
00105 }
00106
00107 if ( $error ) {
00108
00109 throw $error;
00110 } else {
00111 throw new MWException( "Unable to store text to external storage" );
00112 }
00113 }
00114 }