MediaWiki
REL1_21
|
00001 <?php 00046 class ExternalStore { 00054 public static function getStoreObject( $proto, array $params = array() ) { 00055 global $wgExternalStores; 00056 00057 if ( !$wgExternalStores || !in_array( $proto, $wgExternalStores ) ) { 00058 return false; // protocol not enabled 00059 } 00060 00061 $class = 'ExternalStore' . ucfirst( $proto ); 00062 // Any custom modules should be added to $wgAutoLoadClasses for on-demand loading 00063 return MWInit::classExists( $class ) ? new $class( $params ) : false; 00064 } 00065 00074 public static function fetchFromURL( $url, array $params = array() ) { 00075 $parts = explode( '://', $url, 2 ); 00076 if ( count( $parts ) != 2 ) { 00077 return false; // invalid URL 00078 } 00079 00080 list( $proto, $path ) = $parts; 00081 if ( $path == '' ) { // bad URL 00082 return false; 00083 } 00084 00085 $store = self::getStoreObject( $proto, $params ); 00086 if ( $store === false ) { 00087 return false; 00088 } 00089 00090 return $store->fetchFromURL( $url ); 00091 } 00092 00104 public static function insert( $url, $data, array $params = array() ) { 00105 $parts = explode( '://', $url, 2 ); 00106 if ( count( $parts ) != 2 ) { 00107 return false; // invalid URL 00108 } 00109 00110 list( $proto, $path ) = $parts; 00111 if ( $path == '' ) { // bad URL 00112 return false; 00113 } 00114 00115 $store = self::getStoreObject( $proto, $params ); 00116 if ( $store === false ) { 00117 return false; 00118 } else { 00119 return $store->store( $path, $data ); 00120 } 00121 } 00122 00133 public static function insertToDefault( $data, array $params = array() ) { 00134 global $wgDefaultExternalStore; 00135 00136 $error = false; 00137 $tryStores = (array)$wgDefaultExternalStore; 00138 while ( count( $tryStores ) > 0 ) { 00139 $index = mt_rand( 0, count( $tryStores ) - 1 ); 00140 $storeUrl = $tryStores[$index]; 00141 wfDebug( __METHOD__ . ": trying $storeUrl\n" ); 00142 list( $proto, $path ) = explode( '://', $storeUrl, 2 ); 00143 $store = self::getStoreObject( $proto, $params ); 00144 if ( $store === false ) { 00145 throw new MWException( "Invalid external storage protocol - $storeUrl" ); 00146 } 00147 try { 00148 $url = $store->store( $path, $data ); // Try to save the object 00149 } catch ( MWException $error ) { 00150 $url = false; 00151 } 00152 if ( strlen( $url ) ) { 00153 return $url; // Done! 00154 } else { 00155 unset( $tryStores[$index] ); // Don't try this one again! 00156 $tryStores = array_values( $tryStores ); // Must have consecutive keys 00157 wfDebugLog( 'ExternalStorage', 00158 "Unable to store text to external storage $storeUrl" ); 00159 } 00160 } 00161 // All stores failed 00162 if ( $error ) { 00163 throw $error; // rethrow the last error 00164 } else { 00165 throw new MWException( "Unable to store text to external storage" ); 00166 } 00167 } 00168 00175 public static function insertToForeignDefault( $data, $wiki ) { 00176 return self::insertToDefault( $data, array( 'wiki' => $wiki ) ); 00177 } 00178 }