Add a way for packagers to override some installation details
[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 $parts = explode( '://', $url, 2 );
57
58 if ( count( $parts ) != 2 ) {
59 return false;
60 }
61
62 list( $proto, $path ) = $parts;
63
64 if ( $path == '' ) { // Bad URL
65 return false;
66 }
67
68 $store = self::getStoreObject( $proto, $params );
69 if ( $store === false )
70 return false;
71 return $store->fetchFromURL( $url );
72 }
73
74 /**
75 * Get an external store object of the given type, with the given parameters
76 *
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
80 */
81 static function getStoreObject( $proto, $params = array() ) {
82 global $wgExternalStores;
83 if( !$wgExternalStores )
84 return false;
85 /* Protocol not enabled */
86 if( !in_array( $proto, $wgExternalStores ) )
87 return false;
88
89 $class = 'ExternalStore' . ucfirst( $proto );
90 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
91 if( !MWInit::classExists( $class ) ) {
92 return false;
93 }
94
95 return new $class($params);
96 }
97
98 /**
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.
102 * @param $url
103 * @param $data
104 * @param $params array
105 * @return string|bool The URL of the stored data item, or false on error
106 */
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 ) {
111 return false;
112 } else {
113 return $store->store( $params, $data );
114 }
115 }
116
117 /**
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.
121 *
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
125 */
126 public static function insertToDefault( $data, $storageParams = array() ) {
127 global $wgDefaultExternalStore;
128 $tryStores = (array)$wgDefaultExternalStore;
129 $error = false;
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" );
138 }
139 try {
140 $url = $store->store( $params, $data ); // Try to save the object
141 } catch ( DBConnectionError $error ) {
142 $url = false;
143 } catch( DBQueryError $error ) {
144 $url = false;
145 }
146 if ( $url ) {
147 return $url; // Done!
148 } else {
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" );
152 }
153 }
154 // All stores failed
155 if ( $error ) {
156 // Rethrow the last connection error
157 throw $error;
158 } else {
159 throw new MWException( "Unable to store text to external storage" );
160 }
161 }
162
163 /**
164 * @param $data
165 * @param $wiki
166 *
167 * @return string
168 */
169 public static function insertToForeignDefault( $data, $wiki ) {
170 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );
171 }
172 }