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
16 /* Fetch data from given URL */
17 static function fetchFromURL( $url ) {
18 global $wgExternalStores;
20 if( !$wgExternalStores )
23 @list
( $proto, $path ) = explode( '://', $url, 2 );
28 $store = self
::getStoreObject( $proto );
29 if ( $store === false )
31 return $store->fetchFromURL( $url );
35 * Get an external store object of the given type
37 static function getStoreObject( $proto ) {
38 global $wgExternalStores;
39 if( !$wgExternalStores )
41 /* Protocol not enabled */
42 if( !in_array( $proto, $wgExternalStores ) )
45 $class = 'ExternalStore' . ucfirst( $proto );
46 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
47 if( !class_exists( $class ) ) {
55 * Store a data item to an external store, identified by a partial URL
56 * The protocol part is used to identify the class, the rest is passed to the
57 * class itself as a parameter.
58 * Returns the URL of the stored data item, or false on error
60 static function insert( $url, $data ) {
61 list( $proto, $params ) = explode( '://', $url, 2 );
62 $store = self
::getStoreObject( $proto );
63 if ( $store === false ) {
66 return $store->store( $params, $data );
71 * Like insert() above, but does more of the work for us.
72 * This function does not need a url param, it builds it by
73 * itself. It also fails-over to the next possible clusters.
76 * Returns the URL of the stored data item, or false on error
78 public static function randomInsert( $data ) {
79 global $wgDefaultExternalStore;
80 $tryStorages = (array)$wgDefaultExternalStore;
81 // Do not wait and do second retry per master if we
82 // have other active cluster masters to try instead.
83 $retry = count($tryStorages) > 1 ?
false : true;
84 while ( count($tryStorages) > 0 ) {
85 $index = mt_rand(0, count( $tryStorages ) - 1);
86 $storeUrl = $tryStorages[$index];
87 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
88 $store = self
::getStoreObject( $proto );
89 if ( $store === false ) {
90 throw new MWException( "Invalid external storage protocol - $storeUrl" );
93 $url = $store->store( $params, $data, $retry ); // Try to save the object
97 unset( $tryStorages[$index] ); // Don't try this one again!
98 sort( $tryStorages ); // Must have consecutive keys
99 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
103 throw new MWException( "Unable to store text to external storage" );
104 return false; // All cluster masters dead :(