Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / externalstore / ExternalStore.php
1 <?php
2 /**
3 * @defgroup ExternalStorage ExternalStorage
4 */
5
6 use MediaWiki\MediaWikiServices;
7
8 /**
9 * Interface for data storage in external repositories.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 /**
30 * Constructor class for key/value blob data kept in external repositories.
31 *
32 * Objects in external stores are defined by a special URL. The URL is of
33 * the form "<store protocol>://<location>/<object name>". The protocol is used
34 * to determine what ExternalStoreMedium class is used. The location identifies
35 * particular storage instances or database clusters for store class to use.
36 *
37 * When an object is inserted into a store, the calling code uses a partial URL of
38 * the form "<store protocol>://<location>" and receives the full object URL on success.
39 * This is useful since object names can be sequential IDs, UUIDs, or hashes.
40 * Callers are not responsible for unique name generation.
41 *
42 * External repositories might be populated by maintenance/async
43 * scripts, thus partial moving of data may be possible, as well
44 * as the possibility to have any storage format (i.e. for archives).
45 *
46 * @ingroup ExternalStorage
47 * @deprecated 1.34 Use ExternalStoreFactory directly instead
48 */
49 class ExternalStore {
50 /**
51 * Get an external store object of the given type, with the given parameters
52 *
53 * @param string $proto Type of external storage, should be a value in $wgExternalStores
54 * @param array $params Associative array of ExternalStoreMedium parameters
55 * @return ExternalStoreMedium|bool The store class or false on error
56 * @deprecated 1.34
57 */
58 public static function getStoreObject( $proto, array $params = [] ) {
59 try {
60 return MediaWikiServices::getInstance()
61 ->getExternalStoreFactory()
62 ->getStore( $proto, $params );
63 } catch ( ExternalStoreException $e ) {
64 return false;
65 }
66 }
67
68 /**
69 * Fetch data from given URL
70 *
71 * @param string $url The URL of the text to get
72 * @param array $params Associative array of ExternalStoreMedium parameters
73 * @return string|bool The text stored or false on error
74 * @throws MWException
75 * @deprecated 1.34
76 */
77 public static function fetchFromURL( $url, array $params = [] ) {
78 try {
79 return MediaWikiServices::getInstance()
80 ->getExternalStoreAccess()
81 ->fetchFromURL( $url, $params );
82 } catch ( ExternalStoreException $e ) {
83 return false;
84 }
85 }
86
87 /**
88 * Store a data item to an external store, identified by a partial URL
89 * The protocol part is used to identify the class, the rest is passed to the
90 * class itself as a parameter.
91 *
92 * @param string $url A partial external store URL ("<store type>://<location>")
93 * @param string $data
94 * @param array $params Associative array of ExternalStoreMedium parameters
95 * @return string|bool The URL of the stored data item, or false on error
96 * @throws MWException
97 * @deprecated 1.34
98 */
99 public static function insert( $url, $data, array $params = [] ) {
100 try {
101 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
102 $location = $esFactory->getStoreLocationFromUrl( $url );
103
104 return $esFactory->getStoreForUrl( $url, $params )->store( $location, $data );
105 } catch ( ExternalStoreException $e ) {
106 return false;
107 }
108 }
109
110 /**
111 * Fetch data from multiple URLs with a minimum of round trips
112 *
113 * @param array $urls The URLs of the text to get
114 * @return array Map from url to its data. Data is either string when found
115 * or false on failure.
116 * @throws MWException
117 * @deprecated 1.34
118 */
119 public static function batchFetchFromURLs( array $urls ) {
120 return MediaWikiServices::getInstance()->getExternalStoreAccess()->fetchFromURLs( $urls );
121 }
122
123 /**
124 * Like insert() above, but does more of the work for us.
125 * This function does not need a url param, it builds it by
126 * itself. It also fails-over to the next possible clusters
127 * provided by $wgDefaultExternalStore.
128 *
129 * @param string $data
130 * @param array $params Map of ExternalStoreMedium::__construct context parameters
131 * @return string The URL of the stored data item
132 * @throws MWException
133 * @deprecated 1.34
134 */
135 public static function insertToDefault( $data, array $params = [] ) {
136 return MediaWikiServices::getInstance()->getExternalStoreAccess()->insert( $data, $params );
137 }
138
139 /**
140 * Like insert() above, but does more of the work for us.
141 * This function does not need a url param, it builds it by
142 * itself. It also fails-over to the next possible clusters
143 * as provided in the first parameter.
144 *
145 * @param array $tryStores Refer to $wgDefaultExternalStore
146 * @param string $data
147 * @param array $params Map of ExternalStoreMedium::__construct context parameters
148 * @return string The URL of the stored data item
149 * @throws MWException
150 * @deprecated 1.34
151 */
152 public static function insertWithFallback( array $tryStores, $data, array $params = [] ) {
153 return MediaWikiServices::getInstance()
154 ->getExternalStoreAccess()
155 ->insert( $data, $params, $tryStores );
156 }
157
158 /**
159 * @param string $data
160 * @param string $wiki
161 * @return string The URL of the stored data item
162 * @throws MWException
163 * @deprecated 1.34 Use insertToDefault() with 'wiki' set
164 */
165 public static function insertToForeignDefault( $data, $wiki ) {
166 return MediaWikiServices::getInstance()
167 ->getExternalStoreAccess()
168 ->insert( $data, [ 'domain' => $wiki ] );
169 }
170 }