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 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( !class_exists( $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.
81 * @return The URL of the stored data item, or false on error
83 static function insert( $url, $data, $params = array() ) {
84 list( $proto, $params ) = explode( '://', $url, 2 );
85 $store = self::getStoreObject( $proto, $params );
86 if ( $store === false ) {
89 return $store->store( $params, $data );
94 * Like insert() above, but does more of the work for us.
95 * This function does not need a url param, it builds it by
96 * itself. It also fails-over to the next possible clusters.
99 * @param $storageParams Array: associative array of parameters for the ExternalStore object.
100 * @return The URL of the stored data item, or false on error
102 public static function insertToDefault( $data, $storageParams = array() ) {
103 global $wgDefaultExternalStore;
104 $tryStores = (array)$wgDefaultExternalStore;
106 while ( count( $tryStores ) > 0 ) {
107 $index = mt_rand(0, count( $tryStores ) - 1);
108 $storeUrl = $tryStores[$index];
109 wfDebug( __METHOD__.": trying $storeUrl\n" );
110 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
111 $store = self::getStoreObject( $proto, $storageParams );
112 if ( $store === false ) {
113 throw new MWException( "Invalid external storage protocol - $storeUrl" );
116 $url = $store->store( $params, $data ); // Try to save the object
117 } catch ( DBConnectionError $error ) {
119 } catch( DBQueryError $error ) {
123 return $url; // Done!
125 unset( $tryStores[$index] ); // Don't try this one again!
126 $tryStores = array_values( $tryStores ); // Must have consecutive keys
127 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
132 // Rethrow the last connection error
135 throw new MWException( "Unable to store text to external storage" );
139 /** Like insertToDefault, but inserts on another wiki */
140 public static function insertToForeignDefault( $data, $wiki ) {
141 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );