MediaWiki
master
|
00001 <?php 00036 class ExternalStore { 00037 var $mParams; 00038 00042 function __construct( $params = array() ) { 00043 $this->mParams = $params; 00044 } 00045 00053 static function fetchFromURL( $url, $params = array() ) { 00054 global $wgExternalStores; 00055 00056 if( !$wgExternalStores ) { 00057 return false; 00058 } 00059 00060 $parts = explode( '://', $url, 2 ); 00061 00062 if ( count( $parts ) != 2 ) { 00063 return false; 00064 } 00065 00066 list( $proto, $path ) = $parts; 00067 00068 if ( $path == '' ) { // Bad URL 00069 return false; 00070 } 00071 00072 $store = self::getStoreObject( $proto, $params ); 00073 if ( $store === false ) { 00074 return false; 00075 } 00076 00077 return $store->fetchFromURL( $url ); 00078 } 00079 00087 static function getStoreObject( $proto, $params = array() ) { 00088 global $wgExternalStores; 00089 if( !$wgExternalStores ) { 00090 return false; 00091 } 00092 00093 /* Protocol not enabled */ 00094 if( !in_array( $proto, $wgExternalStores ) ) { 00095 return false; 00096 } 00097 00098 $class = 'ExternalStore' . ucfirst( $proto ); 00099 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */ 00100 if( !MWInit::classExists( $class ) ) { 00101 return false; 00102 } 00103 00104 return new $class($params); 00105 } 00106 00116 static function insert( $url, $data, $params = array() ) { 00117 list( $proto, $params ) = explode( '://', $url, 2 ); 00118 $store = self::getStoreObject( $proto, $params ); 00119 if ( $store === false ) { 00120 return false; 00121 } else { 00122 return $store->store( $params, $data ); 00123 } 00124 } 00125 00136 public static function insertToDefault( $data, $storageParams = array() ) { 00137 global $wgDefaultExternalStore; 00138 $tryStores = (array)$wgDefaultExternalStore; 00139 $error = false; 00140 while ( count( $tryStores ) > 0 ) { 00141 $index = mt_rand(0, count( $tryStores ) - 1); 00142 $storeUrl = $tryStores[$index]; 00143 wfDebug( __METHOD__.": trying $storeUrl\n" ); 00144 list( $proto, $params ) = explode( '://', $storeUrl, 2 ); 00145 $store = self::getStoreObject( $proto, $storageParams ); 00146 if ( $store === false ) { 00147 throw new MWException( "Invalid external storage protocol - $storeUrl" ); 00148 } 00149 try { 00150 $url = $store->store( $params, $data ); // Try to save the object 00151 } catch ( DBConnectionError $error ) { 00152 $url = false; 00153 } catch( DBQueryError $error ) { 00154 $url = false; 00155 } 00156 if ( $url ) { 00157 return $url; // Done! 00158 } else { 00159 unset( $tryStores[$index] ); // Don't try this one again! 00160 $tryStores = array_values( $tryStores ); // Must have consecutive keys 00161 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" ); 00162 } 00163 } 00164 // All stores failed 00165 if ( $error ) { 00166 // Rethrow the last connection error 00167 throw $error; 00168 } else { 00169 throw new MWException( "Unable to store text to external storage" ); 00170 } 00171 } 00172 00179 public static function insertToForeignDefault( $data, $wiki ) { 00180 return self::insertToDefault( $data, array( 'wiki' => $wiki ) ); 00181 } 00182 }