(bug 31565) Option to use group members on Special:UserRights
[mediawiki.git] / includes / ExternalStore.php
blob1b7c29dbbbf277e1113311f5a55b3c05cc2efa0b
1 <?php
2 /**
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
20 * @file
23 /**
24 * @defgroup ExternalStorage ExternalStorage
27 /**
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
36 class ExternalStore {
37 var $mParams;
39 /**
40 * @param $params array
42 function __construct( $params = array() ) {
43 $this->mParams = $params;
46 /**
47 * Fetch data from given URL
49 * @param $url String: The URL of the text to get
50 * @param $params Array: associative array of parameters for the ExternalStore object.
51 * @return string|bool The text stored or false on error
53 static function fetchFromURL( $url, $params = array() ) {
54 global $wgExternalStores;
56 if( !$wgExternalStores ) {
57 return false;
60 $parts = explode( '://', $url, 2 );
62 if ( count( $parts ) != 2 ) {
63 return false;
66 list( $proto, $path ) = $parts;
68 if ( $path == '' ) { // Bad URL
69 return false;
72 $store = self::getStoreObject( $proto, $params );
73 if ( $store === false ) {
74 return false;
77 return $store->fetchFromURL( $url );
80 /**
81 * Get an external store object of the given type, with the given parameters
83 * @param $proto String: type of external storage, should be a value in $wgExternalStores
84 * @param $params Array: associative array of parameters for the ExternalStore object.
85 * @return ExternalStore|bool ExternalStore class or false on error
87 static function getStoreObject( $proto, $params = array() ) {
88 global $wgExternalStores;
89 if( !$wgExternalStores ) {
90 return false;
93 /* Protocol not enabled */
94 if( !in_array( $proto, $wgExternalStores ) ) {
95 return false;
98 $class = 'ExternalStore' . ucfirst( $proto );
99 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
100 if( !MWInit::classExists( $class ) ) {
101 return false;
104 return new $class($params);
108 * Store a data item to an external store, identified by a partial URL
109 * The protocol part is used to identify the class, the rest is passed to the
110 * class itself as a parameter.
111 * @param $url
112 * @param $data
113 * @param $params array
114 * @return string|bool The URL of the stored data item, or false on error
116 static function insert( $url, $data, $params = array() ) {
117 list( $proto, $params ) = explode( '://', $url, 2 );
118 $store = self::getStoreObject( $proto, $params );
119 if ( $store === false ) {
120 return false;
121 } else {
122 return $store->store( $params, $data );
127 * Like insert() above, but does more of the work for us.
128 * This function does not need a url param, it builds it by
129 * itself. It also fails-over to the next possible clusters.
131 * @param $data String
132 * @param $storageParams Array: associative array of parameters for the ExternalStore object.
133 * @throws MWException|DBConnectionError|DBQueryError
134 * @return string|bool The URL of the stored data item, or false on error
136 public static function insertToDefault( $data, $storageParams = array() ) {
137 global $wgDefaultExternalStore;
138 $tryStores = (array)$wgDefaultExternalStore;
139 $error = false;
140 while ( count( $tryStores ) > 0 ) {
141 $index = mt_rand(0, count( $tryStores ) - 1);
142 $storeUrl = $tryStores[$index];
143 wfDebug( __METHOD__.": trying $storeUrl\n" );
144 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
145 $store = self::getStoreObject( $proto, $storageParams );
146 if ( $store === false ) {
147 throw new MWException( "Invalid external storage protocol - $storeUrl" );
149 try {
150 $url = $store->store( $params, $data ); // Try to save the object
151 } catch ( DBConnectionError $error ) {
152 $url = false;
153 } catch( DBQueryError $error ) {
154 $url = false;
156 if ( $url ) {
157 return $url; // Done!
158 } else {
159 unset( $tryStores[$index] ); // Don't try this one again!
160 $tryStores = array_values( $tryStores ); // Must have consecutive keys
161 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
164 // All stores failed
165 if ( $error ) {
166 // Rethrow the last connection error
167 throw $error;
168 } else {
169 throw new MWException( "Unable to store text to external storage" );
174 * @param $data
175 * @param $wiki
177 * @return string
179 public static function insertToForeignDefault( $data, $wiki ) {
180 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );