MediaWiki
REL1_20
|
00001 <?php 00036 class ExternalStore { 00037 var $mParams; 00038 00039 function __construct( $params = array() ) { 00040 $this->mParams = $params; 00041 } 00042 00050 static function fetchFromURL( $url, $params = array() ) { 00051 global $wgExternalStores; 00052 00053 if( !$wgExternalStores ) 00054 return false; 00055 00056 $parts = explode( '://', $url, 2 ); 00057 00058 if ( count( $parts ) != 2 ) { 00059 return false; 00060 } 00061 00062 list( $proto, $path ) = $parts; 00063 00064 if ( $path == '' ) { // Bad URL 00065 return false; 00066 } 00067 00068 $store = self::getStoreObject( $proto, $params ); 00069 if ( $store === false ) 00070 return false; 00071 return $store->fetchFromURL( $url ); 00072 } 00073 00081 static function getStoreObject( $proto, $params = array() ) { 00082 global $wgExternalStores; 00083 if( !$wgExternalStores ) 00084 return false; 00085 /* Protocol not enabled */ 00086 if( !in_array( $proto, $wgExternalStores ) ) 00087 return false; 00088 00089 $class = 'ExternalStore' . ucfirst( $proto ); 00090 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */ 00091 if( !MWInit::classExists( $class ) ) { 00092 return false; 00093 } 00094 00095 return new $class($params); 00096 } 00097 00107 static function insert( $url, $data, $params = array() ) { 00108 list( $proto, $params ) = explode( '://', $url, 2 ); 00109 $store = self::getStoreObject( $proto, $params ); 00110 if ( $store === false ) { 00111 return false; 00112 } else { 00113 return $store->store( $params, $data ); 00114 } 00115 } 00116 00126 public static function insertToDefault( $data, $storageParams = array() ) { 00127 global $wgDefaultExternalStore; 00128 $tryStores = (array)$wgDefaultExternalStore; 00129 $error = false; 00130 while ( count( $tryStores ) > 0 ) { 00131 $index = mt_rand(0, count( $tryStores ) - 1); 00132 $storeUrl = $tryStores[$index]; 00133 wfDebug( __METHOD__.": trying $storeUrl\n" ); 00134 list( $proto, $params ) = explode( '://', $storeUrl, 2 ); 00135 $store = self::getStoreObject( $proto, $storageParams ); 00136 if ( $store === false ) { 00137 throw new MWException( "Invalid external storage protocol - $storeUrl" ); 00138 } 00139 try { 00140 $url = $store->store( $params, $data ); // Try to save the object 00141 } catch ( DBConnectionError $error ) { 00142 $url = false; 00143 } catch( DBQueryError $error ) { 00144 $url = false; 00145 } 00146 if ( $url ) { 00147 return $url; // Done! 00148 } else { 00149 unset( $tryStores[$index] ); // Don't try this one again! 00150 $tryStores = array_values( $tryStores ); // Must have consecutive keys 00151 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" ); 00152 } 00153 } 00154 // All stores failed 00155 if ( $error ) { 00156 // Rethrow the last connection error 00157 throw $error; 00158 } else { 00159 throw new MWException( "Unable to store text to external storage" ); 00160 } 00161 } 00162 00169 public static function insertToForeignDefault( $data, $wiki ) { 00170 return self::insertToDefault( $data, array( 'wiki' => $wiki ) ); 00171 } 00172 }