3 * External storage in some particular medium.
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.
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.
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
21 * @ingroup ExternalStorage
24 use Psr\Log\LoggerAwareInterface
;
25 use Psr\Log\LoggerInterface
;
26 use Psr\Log\NullLogger
;
29 * Key/value blob storage for a particular storage medium type (e.g. RDBMs, files)
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.
35 * @ingroup ExternalStorage
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 */
43 /** @var bool Whether this was factoried with an explicit DB domain */
44 protected $isDbDomainExplicit;
46 /** @var LoggerInterface */
50 * @param array $params Usage context options for this instance:
51 * - domain: the DB domain ID of the wiki the content is for [required]
52 * - logger: LoggerInterface instance [optional]
53 * - isDomainImplicit: whether this was factoried without an explicit DB domain [optional]
55 public function __construct( array $params ) {
56 $this->params
= $params;
57 if ( isset( $params['domain'] ) ) {
58 $this->dbDomain
= $params['domain'];
59 $this->isDbDomainExplicit
= empty( $params['isDomainImplicit'] );
61 throw new InvalidArgumentException( 'Missing DB "domain" parameter.' );
64 $this->logger
= $params['logger'] ??
new NullLogger();
67 public function setLogger( LoggerInterface
$logger ) {
68 $this->logger
= $logger;
72 * Fetch data from given external store URL
74 * @param string $url An external store URL
75 * @return string|bool The text stored or false on error
78 abstract public function fetchFromURL( $url );
81 * Fetch data from given external store URLs.
83 * @param array $urls A list of external store URLs
84 * @return string[] Map of (url => text) for the URLs where data was actually found
86 public function batchFetchFromURLs( array $urls ) {
88 foreach ( $urls as $url ) {
89 $data = $this->fetchFromURL( $url );
90 // Dont return when false to allow for simpler implementations
91 if ( $data !== false ) {
92 $retval[$url] = $data;
100 * Insert a data item into a given location
102 * @param string $location The location name
103 * @param string $data The data item
104 * @return string|bool The URL of the stored data item, or false on error
105 * @throws MWException
107 abstract public function store( $location, $data );
110 * Check if a given location is read-only
112 * @param string $location The location name
113 * @return bool Whether this location is read-only
116 public function isReadOnly( $location ) {