3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use Psr\Log\LoggerAwareInterface
;
22 use Psr\Log\LoggerInterface
;
23 use Psr\Log\NullLogger
;
26 * Base class for external storage.
28 * There can be multiple "locations" for a storage medium type (e.g. DB clusters, filesystems).
29 * Blobs are stored under URLs of the form `<protocol>://<location>/<path>`. Each type of storage
30 * medium has an associated protocol.
32 * @see ExternalStoreAccess
33 * @ingroup ExternalStorage
36 abstract class ExternalStoreMedium
implements LoggerAwareInterface
{
37 /** @var array Usage context options for this instance */
38 protected $params = [];
39 /** @var string Default database domain to store content under */
41 /** @var bool Whether this was factoried with an explicit DB domain */
42 protected $isDbDomainExplicit;
44 /** @var LoggerInterface */
48 * @param array $params Usage context options for this instance:
49 * - domain: the DB domain ID of the wiki the content is for [required]
50 * - logger: LoggerInterface instance [optional]
51 * - isDomainImplicit: whether this was factoried without an explicit DB domain [optional]
53 public function __construct( array $params ) {
54 $this->params
= $params;
55 if ( isset( $params['domain'] ) ) {
56 $this->dbDomain
= $params['domain'];
57 $this->isDbDomainExplicit
= empty( $params['isDomainImplicit'] );
59 throw new InvalidArgumentException( 'Missing DB "domain" parameter.' );
62 $this->logger
= $params['logger'] ??
new NullLogger();
65 public function setLogger( LoggerInterface
$logger ) {
66 $this->logger
= $logger;
70 * Fetch data from given external store URL
72 * @param string $url An external store URL
73 * @return string|bool The text stored or false on error
74 * @throws ExternalStoreException
76 abstract public function fetchFromURL( $url );
79 * Fetch data from given external store URLs.
81 * @param array $urls A list of external store URLs
82 * @return string[] Map of (url => text) for the URLs where data was actually found
84 public function batchFetchFromURLs( array $urls ) {
86 foreach ( $urls as $url ) {
87 $data = $this->fetchFromURL( $url );
88 // Dont return when false to allow for simpler implementations
89 if ( $data !== false ) {
90 $retval[$url] = $data;
98 * Insert a data item into a given location
100 * @param string $location The location name
101 * @param string $data The data item
102 * @return string|bool The URL of the stored data item, or false on error
103 * @throws ExternalStoreException
105 abstract public function store( $location, $data );
108 * Check if a given location is read-only
110 * @param string $location The location name
111 * @return bool Whether this location is read-only
114 public function isReadOnly( $location ) {