(bug 22617), FileRepo::append() definition does not match child, change it to be...
[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 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 @list( $proto, $path ) = explode( '://', $url, 2 );
36 /* Bad URL */
37 if( $path == '' )
38 return false;
39
40 $store = self::getStoreObject( $proto, $params );
41 if ( $store === false )
42 return false;
43 return $store->fetchFromURL( $url );
44 }
45
46 /**
47 * Get an external store object of the given type, with the given parameters
48 *
49 * @param $proto String: type of external storage, should be a value in $wgExternalStores
50 * @param $params Array: associative array of parameters for the ExternalStore object.
51 * @return ExternalStore subclass or false on error
52 */
53 static function getStoreObject( $proto, $params = array() ) {
54 global $wgExternalStores;
55 if( !$wgExternalStores )
56 return false;
57 /* Protocol not enabled */
58 if( !in_array( $proto, $wgExternalStores ) )
59 return false;
60
61 $class = 'ExternalStore' . ucfirst( $proto );
62 /* Any custom modules should be added to $wgAutoLoadClasses for on-demand loading */
63 if( !class_exists( $class ) ) {
64 return false;
65 }
66
67 return new $class($params);
68 }
69
70 /**
71 * Store a data item to an external store, identified by a partial URL
72 * The protocol part is used to identify the class, the rest is passed to the
73 * class itself as a parameter.
74 * @return The URL of the stored data item, or false on error
75 */
76 static function insert( $url, $data, $params = array() ) {
77 list( $proto, $params ) = explode( '://', $url, 2 );
78 $store = self::getStoreObject( $proto, $params );
79 if ( $store === false ) {
80 return false;
81 } else {
82 return $store->store( $params, $data );
83 }
84 }
85
86 /**
87 * Like insert() above, but does more of the work for us.
88 * This function does not need a url param, it builds it by
89 * itself. It also fails-over to the next possible clusters.
90 *
91 * @param $data String
92 * @param $storageParams Array: associative array of parameters for the ExternalStore object.
93 * @return The URL of the stored data item, or false on error
94 */
95 public static function insertToDefault( $data, $storageParams = array() ) {
96 global $wgDefaultExternalStore;
97 $tryStores = (array)$wgDefaultExternalStore;
98 $error = false;
99 while ( count( $tryStores ) > 0 ) {
100 $index = mt_rand(0, count( $tryStores ) - 1);
101 $storeUrl = $tryStores[$index];
102 wfDebug( __METHOD__.": trying $storeUrl\n" );
103 list( $proto, $params ) = explode( '://', $storeUrl, 2 );
104 $store = self::getStoreObject( $proto, $storageParams );
105 if ( $store === false ) {
106 throw new MWException( "Invalid external storage protocol - $storeUrl" );
107 }
108 try {
109 $url = $store->store( $params, $data ); // Try to save the object
110 } catch ( DBConnectionError $error ) {
111 $url = false;
112 } catch( DBQueryError $error ) {
113 $url = false;
114 }
115 if ( $url ) {
116 return $url; // Done!
117 } else {
118 unset( $tryStores[$index] ); // Don't try this one again!
119 $tryStores = array_values( $tryStores ); // Must have consecutive keys
120 wfDebugLog( 'ExternalStorage', "Unable to store text to external storage $storeUrl" );
121 }
122 }
123 // All stores failed
124 if ( $error ) {
125 // Rethrow the last connection error
126 throw $error;
127 } else {
128 throw new MWException( "Unable to store text to external storage" );
129 }
130 }
131
132 /** Like insertToDefault, but inserts on another wiki */
133 public static function insertToForeignDefault( $data, $wiki ) {
134 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );
135 }
136 }