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