Merge "Exclude redirects from Special:Fewestrevisions"
[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
46 /** @var LoggerInterface */
47 protected $logger;
48
49 /**
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]
54 */
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'] );
60 } else {
61 throw new InvalidArgumentException( 'Missing DB "domain" parameter.' );
62 }
63
64 $this->logger = $params['logger'] ?? new NullLogger();
65 }
66
67 public function setLogger( LoggerInterface $logger ) {
68 $this->logger = $logger;
69 }
70
71 /**
72 * Fetch data from given external store URL
73 *
74 * @param string $url An external store URL
75 * @return string|bool The text stored or false on error
76 * @throws MWException
77 */
78 abstract public function fetchFromURL( $url );
79
80 /**
81 * Fetch data from given external store URLs.
82 *
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
85 */
86 public function batchFetchFromURLs( array $urls ) {
87 $retval = [];
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;
93 }
94 }
95
96 return $retval;
97 }
98
99 /**
100 * Insert a data item into a given location
101 *
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
106 */
107 abstract public function store( $location, $data );
108
109 /**
110 * Check if a given location is read-only
111 *
112 * @param string $location The location name
113 * @return bool Whether this location is read-only
114 * @since 1.31
115 */
116 public function isReadOnly( $location ) {
117 return false;
118 }
119 }