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