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 @list( $proto, $path ) = explode( '://', $url, 2 );
00036
00037 if( $path == '' )
00038 return false;
00039
00040 $store = self::getStoreObject( $proto, $params );
00041 if ( $store === false )
00042 return false;
00043 return $store->fetchFromURL( $url );
00044 }
00045
00053 static function getStoreObject( $proto, $params = array() ) {
00054 global $wgExternalStores;
00055 if( !$wgExternalStores )
00056 return false;
00057
00058 if( !in_array( $proto, $wgExternalStores ) )
00059 return false;
00060
00061 $class = 'ExternalStore' . ucfirst( $proto );
00062
00063 if( !class_exists( $class ) ) {
00064 return false;
00065 }
00066
00067 return new $class($params);
00068 }
00069
00076 static function insert( $url, $data, $params = array() ) {
00077 list( $proto, $params ) = explode( '://', $url, 2 );
00078 $store = self::getStoreObject( $proto, $params );
00079 if ( $store === false ) {
00080 return false;
00081 } else {
00082 return $store->store( $params, $data );
00083 }
00084 }
00085
00095 public static function insertToDefault( $data, $storageParams = array() ) {
00096 global $wgDefaultExternalStore;
00097 $tryStores = (array)$wgDefaultExternalStore;
00098 $error = false;
00099 while ( count( $tryStores ) > 0 ) {
00100 $index = mt_rand(0, count( $tryStores ) - 1);
00101 $storeUrl = $tryStores[$index];
00102 wfDebug( __METHOD__.": trying $storeUrl\n" );
00103 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
00104 $store = self::getStoreObject( $proto, $storageParams );
00105 if ( $store === false ) {
00106 throw new MWException( "Invalid external storage protocol - $storeUrl" );
00107 }
00108 try {
00109 $url = $store->store( $params, $data );
00110 } catch ( DBConnectionError $error ) {
00111 $url = false;
00112 } catch( DBQueryError $error ) {
00113 $url = false;
00114 }
00115 if ( $url ) {
00116 return $url;
00117 } else {
00118 unset( $tryStores[$index] );
00119 $tryStores = array_values( $tryStores );
00120 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
00121 }
00122 }
00123
00124 if ( $error ) {
00125
00126 throw $error;
00127 } else {
00128 throw new MWException( "Unable to store text to external storage" );
00129 }
00130 }
00131
00133 public static function insertToForeignDefault( $data, $wiki ) {
00134 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );
00135 }
00136 }