3 * @defgroup ExternalStorage ExternalStorage
7 * Constructor class for data kept in external repositories
9 * External repositories might be populated by maintenance/async
10 * scripts, thus partial moving of data may be possible, as well
11 * as possibility to have any storage format (i.e. for archives)
13 * @ingroup ExternalStorage
18 function __construct( $params = array() ) {
19 $this->mParams
= $params;
22 /* Fetch data from given URL */
23 static function fetchFromURL( $url, $params = array() ) {
24 global $wgExternalStores;
26 if( !$wgExternalStores )
29 @list
( $proto, $path ) = explode( '://', $url, 2 );
34 $store = self
::getStoreObject( $proto, $params );
35 if ( $store === false )
37 return $store->fetchFromURL( $url );
41 * Get an external store object of the given type, with the given parameters
43 static function getStoreObject( $proto, $params = array() ) {
44 global $wgExternalStores;
45 if( !$wgExternalStores )
47 /* Protocol not enabled */
48 if( !in_array( $proto, $wgExternalStores ) )
51 $class = 'ExternalStore' . ucfirst( $proto );
52 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
53 if( !class_exists( $class ) ) {
57 return new $class($params);
61 * Store a data item to an external store, identified by a partial URL
62 * The protocol part is used to identify the class, the rest is passed to the
63 * class itself as a parameter.
64 * Returns the URL of the stored data item, or false on error
66 static function insert( $url, $data, $params = array() ) {
67 list( $proto, $params ) = explode( '://', $url, 2 );
68 $store = self
::getStoreObject( $proto, $params );
69 if ( $store === false ) {
72 return $store->store( $params, $data );
77 * Like insert() above, but does more of the work for us.
78 * This function does not need a url param, it builds it by
79 * itself. It also fails-over to the next possible clusters.
82 * @param array $params Associative array of parameters for the ExternalStore object.
83 * Returns the URL of the stored data item, or false on error
85 public static function insertToDefault( $data, $storageParams = array() ) {
86 global $wgDefaultExternalStore;
87 $tryStores = (array)$wgDefaultExternalStore;
89 while ( count( $tryStores ) > 0 ) {
90 $index = mt_rand(0, count( $tryStores ) - 1);
91 $storeUrl = $tryStores[$index];
92 wfDebug( __METHOD__
.": trying $storeUrl\n" );
93 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
94 $store = self
::getStoreObject( $proto, $storageParams );
95 if ( $store === false ) {
96 throw new MWException( "Invalid external storage protocol - $storeUrl" );
99 $url = $store->store( $params, $data ); // Try to save the object
100 } catch ( DBConnectionError
$error ) {
102 } catch( DBQueryError
$error ) {
106 return $url; // Done!
108 unset( $tryStores[$index] ); // Don't try this one again!
109 $tryStores = array_values( $tryStores ); // Must have consecutive keys
110 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
115 // Rethrow the last connection error
118 throw new MWException( "Unable to store text to external storage" );
122 /** Like insertToDefault, but inserts on another wiki */
123 public static function insertToForeignDefault( $data, $wiki ) {
124 return self
::insertToDefault( $data, array( 'wiki' => $wiki ) );