Make HttpError set actual HTTP error code.
[lhc/web/wiklou.git] / includes / externalstore / ExternalStore.php
1 <?php
2 /**
3 * @defgroup ExternalStorage ExternalStorage
4 */
5
6 /**
7 * Interface for data storage in external repositories.
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * Constructor class for data kept in external repositories.
29 *
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.
36 *
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)
40 *
41 * @ingroup ExternalStorage
42 */
43 class ExternalStore {
44 /**
45 * Get an external store object of the given type, with the given parameters
46 *
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
50 */
51 public static function getStoreObject( $proto, array $params = array() ) {
52 global $wgExternalStores;
53
54 if ( !$wgExternalStores || !in_array( $proto, $wgExternalStores ) ) {
55 return false; // protocol not enabled
56 }
57
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;
61 }
62
63 /**
64 * Fetch data from given URL
65 *
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
69 * @throws MWException
70 */
71 public static function fetchFromURL( $url, array $params = array() ) {
72 $parts = explode( '://', $url, 2 );
73 if ( count( $parts ) != 2 ) {
74 return false; // invalid URL
75 }
76
77 list( $proto, $path ) = $parts;
78 if ( $path == '' ) { // bad URL
79 return false;
80 }
81
82 $store = self::getStoreObject( $proto, $params );
83 if ( $store === false ) {
84 return false;
85 }
86
87 return $store->fetchFromURL( $url );
88 }
89
90 /**
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.
94 * @param $url
95 * @param $data
96 * @param $params array
97 * @return string|bool The URL of the stored data item, or false on error
98 * @throws MWException
99 */
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
104 }
105
106 list( $proto, $path ) = $parts;
107 if ( $path == '' ) { // bad URL
108 return false;
109 }
110
111 $store = self::getStoreObject( $proto, $params );
112 if ( $store === false ) {
113 return false;
114 } else {
115 return $store->store( $path, $data );
116 }
117 }
118
119 /**
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.
123 *
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
128 */
129 public static function insertToDefault( $data, array $params = array() ) {
130 global $wgDefaultExternalStore;
131
132 $error = false;
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" );
142 }
143 try {
144 $url = $store->store( $path, $data ); // Try to save the object
145 } catch ( MWException $error ) {
146 $url = false;
147 }
148 if ( strlen( $url ) ) {
149 return $url; // Done!
150 } else {
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" );
155 }
156 }
157 // All stores failed
158 if ( $error ) {
159 throw $error; // rethrow the last error
160 } else {
161 throw new MWException( "Unable to store text to external storage" );
162 }
163 }
164
165 /**
166 * @param $data
167 * @param $wiki
168 * @return string|bool The URL of the stored data item, or false on error
169 * @throws MWException
170 */
171 public static function insertToForeignDefault( $data, $wiki ) {
172 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );
173 }
174 }