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