Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / externalstore / ExternalStoreMedium.php
1 <?php
2 /**
3 * External storage in some particular medium.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup ExternalStorage
22 */
23
24 use Psr\Log\LoggerAwareInterface;
25 use Psr\Log\LoggerInterface;
26 use Psr\Log\NullLogger;
27
28 /**
29 * Key/value blob storage for a particular storage medium type (e.g. RDBMs, files)
30 *
31 * There can be multiple "locations" for a storage medium type (e.g. DB clusters, filesystems).
32 * Blobs are stored under URLs of the form "<protocol>://<location>/<path>". Each type of storage
33 * medium has an associated protocol.
34 *
35 * @ingroup ExternalStorage
36 * @since 1.21
37 */
38 abstract class ExternalStoreMedium implements LoggerAwareInterface {
39 /** @var array Usage context options for this instance */
40 protected $params = [];
41 /** @var string Default database domain to store content under */
42 protected $dbDomain;
43 /** @var bool Whether this was factoried with an explicit DB domain */
44 protected $isDbDomainExplicit;
45 /** @var string[] Writable locations */
46 protected $writableLocations = [];
47
48 /** @var LoggerInterface */
49 protected $logger;
50
51 /**
52 * @param array $params Usage context options for this instance:
53 * - domain: the DB domain ID of the wiki the content is for [required]
54 * - writableLocations: locations that are writable [required]
55 * - logger: LoggerInterface instance [optional]
56 * - isDomainImplicit: whether this was factoried without an explicit DB domain [optional]
57 */
58 public function __construct( array $params ) {
59 $this->params = $params;
60 if ( isset( $params['domain'] ) ) {
61 $this->dbDomain = $params['domain'];
62 $this->isDbDomainExplicit = empty( $params['isDomainImplicit'] );
63 } else {
64 throw new InvalidArgumentException( 'Missing DB "domain" parameter.' );
65 }
66
67 $this->logger = $params['logger'] ?? new NullLogger();
68 $this->writableLocations = $params['writableLocations'] ?? [];
69 }
70
71 public function setLogger( LoggerInterface $logger ) {
72 $this->logger = $logger;
73 }
74
75 /**
76 * Fetch data from given external store URL
77 *
78 * @param string $url An external store URL
79 * @return string|bool The text stored or false on error
80 * @throws MWException
81 */
82 abstract public function fetchFromURL( $url );
83
84 /**
85 * Fetch data from given external store URLs.
86 *
87 * @param array $urls A list of external store URLs
88 * @return string[] Map of (url => text) for the URLs where data was actually found
89 */
90 public function batchFetchFromURLs( array $urls ) {
91 $retval = [];
92 foreach ( $urls as $url ) {
93 $data = $this->fetchFromURL( $url );
94 // Dont return when false to allow for simpler implementations
95 if ( $data !== false ) {
96 $retval[$url] = $data;
97 }
98 }
99
100 return $retval;
101 }
102
103 /**
104 * Insert a data item into a given location
105 *
106 * @param string $location The location name
107 * @param string $data The data item
108 * @return string|bool The URL of the stored data item, or false on error
109 * @throws MWException
110 */
111 abstract public function store( $location, $data );
112
113 /**
114 * Check if a given location is read-only
115 *
116 * @param string $location The location name
117 * @return bool Whether this location is read-only
118 * @since 1.31
119 */
120 public function isReadOnly( $location ) {
121 return !in_array( $location, $this->writableLocations, true );
122 }
123 }