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;
23 * Fetch data from given URL
25 * @param $url String: The URL of the text to get
26 * @param $params Array: associative array of parameters for the ExternalStore object.
27 * @return string|bool The text stored or false on error
29 static function fetchFromURL( $url, $params = array() ) {
30 global $wgExternalStores;
32 if( !$wgExternalStores )
35 $parts = explode( '://', $url, 2 );
37 if ( count( $parts ) != 2 ) {
41 list( $proto, $path ) = $parts;
43 if ( $path == '' ) { // Bad URL
47 $store = self
::getStoreObject( $proto, $params );
48 if ( $store === false )
50 return $store->fetchFromURL( $url );
54 * Get an external store object of the given type, with the given parameters
56 * @param $proto String: type of external storage, should be a value in $wgExternalStores
57 * @param $params Array: associative array of parameters for the ExternalStore object.
58 * @return ExternalStore subclass or false on error
60 static function getStoreObject( $proto, $params = array() ) {
61 global $wgExternalStores;
62 if( !$wgExternalStores )
64 /* Protocol not enabled */
65 if( !in_array( $proto, $wgExternalStores ) )
68 $class = 'ExternalStore' . ucfirst( $proto );
69 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
70 if( !MWInit
::classExists( $class ) ) {
74 return new $class($params);
78 * Store a data item to an external store, identified by a partial URL
79 * The protocol part is used to identify the class, the rest is passed to the
80 * class itself as a parameter.
83 * @param $params array
84 * @return string|bool The URL of the stored data item, or false on error
86 static function insert( $url, $data, $params = array() ) {
87 list( $proto, $params ) = explode( '://', $url, 2 );
88 $store = self
::getStoreObject( $proto, $params );
89 if ( $store === false ) {
92 return $store->store( $params, $data );
97 * Like insert() above, but does more of the work for us.
98 * This function does not need a url param, it builds it by
99 * itself. It also fails-over to the next possible clusters.
101 * @param $data String
102 * @param $storageParams Array: associative array of parameters for the ExternalStore object.
103 * @return string The URL of the stored data item, or false on error
105 public static function insertToDefault( $data, $storageParams = array() ) {
106 global $wgDefaultExternalStore;
107 $tryStores = (array)$wgDefaultExternalStore;
109 while ( count( $tryStores ) > 0 ) {
110 $index = mt_rand(0, count( $tryStores ) - 1);
111 $storeUrl = $tryStores[$index];
112 wfDebug( __METHOD__
.": trying $storeUrl\n" );
113 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
114 $store = self
::getStoreObject( $proto, $storageParams );
115 if ( $store === false ) {
116 throw new MWException( "Invalid external storage protocol - $storeUrl" );
119 $url = $store->store( $params, $data ); // Try to save the object
120 } catch ( DBConnectionError
$error ) {
122 } catch( DBQueryError
$error ) {
126 return $url; // Done!
128 unset( $tryStores[$index] ); // Don't try this one again!
129 $tryStores = array_values( $tryStores ); // Must have consecutive keys
130 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
135 // Rethrow the last connection error
138 throw new MWException( "Unable to store text to external storage" );
148 public static function insertToForeignDefault( $data, $wiki ) {
149 return self
::insertToDefault( $data, array( 'wiki' => $wiki ) );