3 * Data storage in external repositories.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * @defgroup ExternalStorage ExternalStorage
28 * Constructor class for data kept in external repositories
30 * External repositories might be populated by maintenance/async
31 * scripts, thus partial moving of data may be possible, as well
32 * as possibility to have any storage format (i.e. for archives)
34 * @ingroup ExternalStorage
39 function __construct( $params = array() ) {
40 $this->mParams
= $params;
44 * Fetch data from given URL
46 * @param $url String: The URL of the text to get
47 * @param $params Array: associative array of parameters for the ExternalStore object.
48 * @return string|bool The text stored or false on error
50 static function fetchFromURL( $url, $params = array() ) {
51 global $wgExternalStores;
53 if( !$wgExternalStores )
56 $parts = explode( '://', $url, 2 );
58 if ( count( $parts ) != 2 ) {
62 list( $proto, $path ) = $parts;
64 if ( $path == '' ) { // Bad URL
68 $store = self
::getStoreObject( $proto, $params );
69 if ( $store === false )
71 return $store->fetchFromURL( $url );
75 * Get an external store object of the given type, with the given parameters
77 * @param $proto String: type of external storage, should be a value in $wgExternalStores
78 * @param $params Array: associative array of parameters for the ExternalStore object.
79 * @return ExternalStore subclass or false on error
81 static function getStoreObject( $proto, $params = array() ) {
82 global $wgExternalStores;
83 if( !$wgExternalStores )
85 /* Protocol not enabled */
86 if( !in_array( $proto, $wgExternalStores ) )
89 $class = 'ExternalStore' . ucfirst( $proto );
90 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
91 if( !MWInit
::classExists( $class ) ) {
95 return new $class($params);
99 * Store a data item to an external store, identified by a partial URL
100 * The protocol part is used to identify the class, the rest is passed to the
101 * class itself as a parameter.
104 * @param $params array
105 * @return string|bool The URL of the stored data item, or false on error
107 static function insert( $url, $data, $params = array() ) {
108 list( $proto, $params ) = explode( '://', $url, 2 );
109 $store = self
::getStoreObject( $proto, $params );
110 if ( $store === false ) {
113 return $store->store( $params, $data );
118 * Like insert() above, but does more of the work for us.
119 * This function does not need a url param, it builds it by
120 * itself. It also fails-over to the next possible clusters.
122 * @param $data String
123 * @param $storageParams Array: associative array of parameters for the ExternalStore object.
124 * @return string The URL of the stored data item, or false on error
126 public static function insertToDefault( $data, $storageParams = array() ) {
127 global $wgDefaultExternalStore;
128 $tryStores = (array)$wgDefaultExternalStore;
130 while ( count( $tryStores ) > 0 ) {
131 $index = mt_rand(0, count( $tryStores ) - 1);
132 $storeUrl = $tryStores[$index];
133 wfDebug( __METHOD__
.": trying $storeUrl\n" );
134 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
135 $store = self
::getStoreObject( $proto, $storageParams );
136 if ( $store === false ) {
137 throw new MWException( "Invalid external storage protocol - $storeUrl" );
140 $url = $store->store( $params, $data ); // Try to save the object
141 } catch ( DBConnectionError
$error ) {
143 } catch( DBQueryError
$error ) {
147 return $url; // Done!
149 unset( $tryStores[$index] ); // Don't try this one again!
150 $tryStores = array_values( $tryStores ); // Must have consecutive keys
151 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
156 // Rethrow the last connection error
159 throw new MWException( "Unable to store text to external storage" );
169 public static function insertToForeignDefault( $data, $wiki ) {
170 return self
::insertToDefault( $data, array( 'wiki' => $wiki ) );