3 * @defgroup ExternalStorage ExternalStorage
7 * Interface for data storage in external repositories.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * Constructor class for data kept in external repositories.
30 * Objects in external stores are defined by a special URL. The URL is of
31 * the form "<store protocal>://<location>/<object name>". When an object
32 * is inserted into a store, the calling code uses a partial URL of the form
33 * "<store protocal>://<location>" and receives the full object URL on success.
34 * This is useful since object names can be sequential IDs, UUIDs, or hashes.
35 * Callers are not responsible for unique name generation.
37 * External repositories might be populated by maintenance/async
38 * scripts, thus partial moving of data may be possible, as well
39 * as possibility to have any storage format (i.e. for archives)
41 * @ingroup ExternalStorage
45 * Get an external store object of the given type, with the given parameters
47 * @param $proto String: type of external storage, should be a value in $wgExternalStores
48 * @param $params Array: associative array of parameters for the ExternalStore object.
49 * @return ExternalStore|bool ExternalStore class or false on error
51 public static function getStoreObject( $proto, array $params = array() ) {
52 global $wgExternalStores;
54 if ( !$wgExternalStores ||
!in_array( $proto, $wgExternalStores ) ) {
55 return false; // protocol not enabled
58 $class = 'ExternalStore' . ucfirst( $proto );
59 // Any custom modules should be added to $wgAutoLoadClasses for on-demand loading
60 return MWInit
::classExists( $class ) ?
new $class( $params ) : false;
64 * Fetch data from given URL
66 * @param $url String: The URL of the text to get
67 * @param $params Array: associative array of parameters for the ExternalStore object.
68 * @return string|bool The text stored or false on error
71 public static function fetchFromURL( $url, array $params = array() ) {
72 $parts = explode( '://', $url, 2 );
73 if ( count( $parts ) != 2 ) {
74 return false; // invalid URL
77 list( $proto, $path ) = $parts;
78 if ( $path == '' ) { // bad URL
82 $store = self
::getStoreObject( $proto, $params );
83 if ( $store === false ) {
87 return $store->fetchFromURL( $url );
91 * Store a data item to an external store, identified by a partial URL
92 * The protocol part is used to identify the class, the rest is passed to the
93 * class itself as a parameter.
96 * @param $params array
97 * @return string|bool The URL of the stored data item, or false on error
100 public static function insert( $url, $data, array $params = array() ) {
101 $parts = explode( '://', $url, 2 );
102 if ( count( $parts ) != 2 ) {
103 return false; // invalid URL
106 list( $proto, $path ) = $parts;
107 if ( $path == '' ) { // bad URL
111 $store = self
::getStoreObject( $proto, $params );
112 if ( $store === false ) {
115 return $store->store( $path, $data );
120 * Like insert() above, but does more of the work for us.
121 * This function does not need a url param, it builds it by
122 * itself. It also fails-over to the next possible clusters.
124 * @param $data String
125 * @param $params Array: associative array of parameters for the ExternalStore object.
126 * @return string|bool The URL of the stored data item, or false on error
127 * @throws MWException
129 public static function insertToDefault( $data, array $params = array() ) {
130 global $wgDefaultExternalStore;
133 $tryStores = (array)$wgDefaultExternalStore;
134 while ( count( $tryStores ) > 0 ) {
135 $index = mt_rand( 0, count( $tryStores ) - 1 );
136 $storeUrl = $tryStores[$index];
137 wfDebug( __METHOD__
.": trying $storeUrl\n" );
138 list( $proto, $path ) = explode( '://', $storeUrl, 2 );
139 $store = self
::getStoreObject( $proto, $params );
140 if ( $store === false ) {
141 throw new MWException( "Invalid external storage protocol - $storeUrl" );
144 $url = $store->store( $path, $data ); // Try to save the object
145 } catch ( MWException
$error ) {
148 if ( strlen( $url ) ) {
149 return $url; // Done!
151 unset( $tryStores[$index] ); // Don't try this one again!
152 $tryStores = array_values( $tryStores ); // Must have consecutive keys
153 wfDebugLog( 'ExternalStorage',
154 "Unable to store text to external storage $storeUrl" );
159 throw $error; // rethrow the last error
161 throw new MWException( "Unable to store text to external storage" );
168 * @return string|bool The URL of the stored data item, or false on error
169 * @throws MWException
171 public static function insertToForeignDefault( $data, $wiki ) {
172 return self
::insertToDefault( $data, array( 'wiki' => $wiki ) );