(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / ExternalStore.php
blobf2eed4b3c5dd9ca1eccb1081fd15f3aca88ea032
1 <?php
2 /**
3 * @defgroup ExternalStorage ExternalStorage
4 */
6 /**
7 * Constructor class for data kept in external repositories
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)
13 * @ingroup ExternalStorage
15 class ExternalStore {
16 var $mParams;
18 function __construct( $params = array() ) {
19 $this->mParams = $params;
22 /**
23 * Fetch data from given URL
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
29 static function fetchFromURL( $url, $params = array() ) {
30 global $wgExternalStores;
32 if( !$wgExternalStores )
33 return false;
35 $parts = explode( '://', $url, 2 );
37 if ( count( $parts ) != 2 ) {
38 return false;
41 list( $proto, $path ) = $parts;
43 if ( $path == '' ) { // Bad URL
44 return false;
47 $store = self::getStoreObject( $proto, $params );
48 if ( $store === false )
49 return false;
50 return $store->fetchFromURL( $url );
53 /**
54 * Get an external store object of the given type, with the given parameters
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
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;
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;
74 return new $class($params);
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
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 );
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.
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
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" );
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;
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" );
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" );
143 * @param $data
144 * @param $wiki
146 * @return string
148 public static function insertToForeignDefault( $data, $wiki ) {
149 return self::insertToDefault( $data, array( 'wiki' => $wiki ) );