Merge "CologneBlue rewrite: kill mWatchLinkNum, watchThisPage() is only called once...
[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 /**
40 * @param $params array
41 */
42 function __construct( $params = array() ) {
43 $this->mParams = $params;
44 }
45
46 /**
47 * Fetch data from given URL
48 *
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
52 */
53 static function fetchFromURL( $url, $params = array() ) {
54 global $wgExternalStores;
55
56 if( !$wgExternalStores ) {
57 return false;
58 }
59
60 $parts = explode( '://', $url, 2 );
61
62 if ( count( $parts ) != 2 ) {
63 return false;
64 }
65
66 list( $proto, $path ) = $parts;
67
68 if ( $path == '' ) { // Bad URL
69 return false;
70 }
71
72 $store = self::getStoreObject( $proto, $params );
73 if ( $store === false ) {
74 return false;
75 }
76
77 return $store->fetchFromURL( $url );
78 }
79
80 /**
81 * Get an external store object of the given type, with the given parameters
82 *
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
86 */
87 static function getStoreObject( $proto, $params = array() ) {
88 global $wgExternalStores;
89 if( !$wgExternalStores ) {
90 return false;
91 }
92
93 /* Protocol not enabled */
94 if( !in_array( $proto, $wgExternalStores ) ) {
95 return false;
96 }
97
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;
102 }
103
104 return new $class($params);
105 }
106
107 /**
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
115 */
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 );
123 }
124 }
125
126 /**
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.
130 *
131 * @param $data String
132 * @param $storageParams Array: associative array of parameters for the ExternalStore object.
133 * @throws DBConnectionError|DBQueryError|MWException
134 * @return string The URL of the stored data item, or false on error
135 */
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" );
148 }
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;
155 }
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" );
162 }
163 }
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" );
170 }
171 }
172
173 /**
174 * @param $data
175 * @param $wiki
176 *
177 * @return string
178 */
179 public static function insertToForeignDefault( $data, $wiki ) {
180 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );
181 }
182 }