Merge "Removed useless begin()/commit() calls as DBO_TRX is not on in cli mode."
[lhc/web/wiklou.git] / includes / ExternalStore.php
1 <?php
2 /**
3 * Data storage in external repositories.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * @defgroup ExternalStorage ExternalStorage
25 */
26
27 /**
28 * Constructor class for data kept in external repositories
29 *
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)
33 *
34 * @ingroup ExternalStorage
35 */
36 class ExternalStore {
37 var $mParams;
38
39 function __construct( $params = array() ) {
40 $this->mParams = $params;
41 }
42
43 /**
44 * Fetch data from given URL
45 *
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
49 */
50 static function fetchFromURL( $url, $params = array() ) {
51 global $wgExternalStores;
52
53 if( !$wgExternalStores ) {
54 return false;
55 }
56
57 $parts = explode( '://', $url, 2 );
58
59 if ( count( $parts ) != 2 ) {
60 return false;
61 }
62
63 list( $proto, $path ) = $parts;
64
65 if ( $path == '' ) { // Bad URL
66 return false;
67 }
68
69 $store = self::getStoreObject( $proto, $params );
70 if ( $store === false ) {
71 return false;
72 }
73
74 return $store->fetchFromURL( $url );
75 }
76
77 /**
78 * Get an external store object of the given type, with the given parameters
79 *
80 * @param $proto String: type of external storage, should be a value in $wgExternalStores
81 * @param $params Array: associative array of parameters for the ExternalStore object.
82 * @return ExternalStore subclass or false on error
83 */
84 static function getStoreObject( $proto, $params = array() ) {
85 global $wgExternalStores;
86 if( !$wgExternalStores ) {
87 return false;
88 }
89
90 /* Protocol not enabled */
91 if( !in_array( $proto, $wgExternalStores ) ) {
92 return false;
93 }
94
95 $class = 'ExternalStore' . ucfirst( $proto );
96 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
97 if( !MWInit::classExists( $class ) ) {
98 return false;
99 }
100
101 return new $class($params);
102 }
103
104 /**
105 * Store a data item to an external store, identified by a partial URL
106 * The protocol part is used to identify the class, the rest is passed to the
107 * class itself as a parameter.
108 * @param $url
109 * @param $data
110 * @param $params array
111 * @return string|bool The URL of the stored data item, or false on error
112 */
113 static function insert( $url, $data, $params = array() ) {
114 list( $proto, $params ) = explode( '://', $url, 2 );
115 $store = self::getStoreObject( $proto, $params );
116 if ( $store === false ) {
117 return false;
118 } else {
119 return $store->store( $params, $data );
120 }
121 }
122
123 /**
124 * Like insert() above, but does more of the work for us.
125 * This function does not need a url param, it builds it by
126 * itself. It also fails-over to the next possible clusters.
127 *
128 * @param $data String
129 * @param $storageParams Array: associative array of parameters for the ExternalStore object.
130 * @return string The URL of the stored data item, or false on error
131 */
132 public static function insertToDefault( $data, $storageParams = array() ) {
133 global $wgDefaultExternalStore;
134 $tryStores = (array)$wgDefaultExternalStore;
135 $error = false;
136 while ( count( $tryStores ) > 0 ) {
137 $index = mt_rand(0, count( $tryStores ) - 1);
138 $storeUrl = $tryStores[$index];
139 wfDebug( __METHOD__.": trying $storeUrl\n" );
140 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
141 $store = self::getStoreObject( $proto, $storageParams );
142 if ( $store === false ) {
143 throw new MWException( "Invalid external storage protocol - $storeUrl" );
144 }
145 try {
146 $url = $store->store( $params, $data ); // Try to save the object
147 } catch ( DBConnectionError $error ) {
148 $url = false;
149 } catch( DBQueryError $error ) {
150 $url = false;
151 }
152 if ( $url ) {
153 return $url; // Done!
154 } else {
155 unset( $tryStores[$index] ); // Don't try this one again!
156 $tryStores = array_values( $tryStores ); // Must have consecutive keys
157 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
158 }
159 }
160 // All stores failed
161 if ( $error ) {
162 // Rethrow the last connection error
163 throw $error;
164 } else {
165 throw new MWException( "Unable to store text to external storage" );
166 }
167 }
168
169 /**
170 * @param $data
171 * @param $wiki
172 *
173 * @return string
174 */
175 public static function insertToForeignDefault( $data, $wiki ) {
176 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );
177 }
178 }