MediaWiki master
ExternalStoreAccess.php
Go to the documentation of this file.
1<?php
8use Psr\Log\LoggerAwareInterface;
9use Psr\Log\LoggerInterface;
10use Psr\Log\NullLogger;
11
29class ExternalStoreAccess implements LoggerAwareInterface {
31 private $storeFactory;
33 private $logger;
34
39 public function __construct( ExternalStoreFactory $factory, ?LoggerInterface $logger = null ) {
40 $this->storeFactory = $factory;
41 $this->logger = $logger ?: new NullLogger();
42 }
43
44 public function setLogger( LoggerInterface $logger ): void {
45 $this->logger = $logger;
46 }
47
58 public function fetchFromURL( $url, array $params = [] ) {
59 return $this->storeFactory->getStoreForUrl( $url, $params )->fetchFromURL( $url );
60 }
61
72 public function fetchFromURLs( array $urls, array $params = [] ) {
73 $batches = $this->storeFactory->getUrlsByProtocol( $urls );
74 $retval = [];
75 foreach ( $batches as $proto => $batchedUrls ) {
76 $store = $this->storeFactory->getStore( $proto, $params );
77 $retval += $store->batchFetchFromURLs( $batchedUrls );
78 }
79 // invalid, not found, db dead, etc.
80 $missing = array_diff( $urls, array_keys( $retval ) );
81 foreach ( $missing as $url ) {
82 $retval[$url] = false;
83 }
84
85 return $retval;
86 }
87
103 public function insert( $data, array $params = [], ?array $tryStores = null ) {
104 $tryStores ??= $this->storeFactory->getWriteBaseUrls();
105 if ( !$tryStores ) {
106 throw new ExternalStoreException( "List of external stores provided is empty." );
107 }
108
109 $error = false; // track the last exception thrown
110 $readOnlyCount = 0; // track if a store was read-only
111 while ( count( $tryStores ) > 0 ) {
112 $index = mt_rand( 0, count( $tryStores ) - 1 );
113 $storeUrl = $tryStores[$index];
114
115 $this->logger->debug( __METHOD__ . ": trying $storeUrl" );
116
117 $store = $this->storeFactory->getStoreForUrl( $storeUrl, $params );
118 if ( $store === false ) {
119 throw new ExternalStoreException( "Invalid external storage protocol - $storeUrl" );
120 }
121
122 $location = $this->storeFactory->getStoreLocationFromUrl( $storeUrl );
123 try {
124 if ( $store->isReadOnly( $location ) ) {
125 $readOnlyCount++;
126 $msg = 'read only';
127 } else {
128 $url = $store->store( $location, $data );
129 if ( $url !== false && $url !== '' ) {
130 // A store accepted the write; done!
131 return $url;
132 }
133 throw new ExternalStoreException(
134 "No URL returned by storage medium ($storeUrl)"
135 );
136 }
137 } catch ( ExternalStoreException $ex ) {
138 $error = $ex;
139 $msg = 'caught ' . get_class( $error ) . ' exception: ' . $error->getMessage();
140 }
141
142 unset( $tryStores[$index] ); // Don't try this one again!
143 $tryStores = array_values( $tryStores ); // Must have consecutive keys
144 $this->logger->error(
145 "Unable to store text to external storage {store_path} ({failure})",
146 [ 'store_path' => $storeUrl, 'failure' => $msg ]
147 );
148 }
149
150 // We only get here when all stores failed.
151 if ( $error ) {
152 // At least one store threw an exception. Re-throw the most recent one.
153 throw $error;
154 } elseif ( $readOnlyCount ) {
155 // If no exceptions where thrown and we get here,
156 // this should mean that all stores were in read-only mode.
157 throw new ReadOnlyError();
158 } else {
159 // We shouldn't get here. If there were no failures, this method should have returned
160 // from inside the body of the loop.
161 throw new LogicException( "Unexpected failure to store text to external store" );
162 }
163 }
164
170 public function isReadOnly( $storeUrls = null ) {
171 if ( $storeUrls === null ) {
172 $storeUrls = $this->storeFactory->getWriteBaseUrls();
173 } else {
174 $storeUrls = is_array( $storeUrls ) ? $storeUrls : [ $storeUrls ];
175 }
176
177 if ( !$storeUrls ) {
178 return false; // no stores exists which can be "read only"
179 }
180
181 foreach ( $storeUrls as $storeUrl ) {
182 $store = $this->storeFactory->getStoreForUrl( $storeUrl );
183 $location = $this->storeFactory->getStoreLocationFromUrl( $storeUrl );
184 if ( $store !== false && !$store->isReadOnly( $location ) ) {
185 return false; // at least one store is not read-only
186 }
187 }
188
189 return true; // all stores are read-only
190 }
191}
This is the main interface for fetching or inserting objects with ExternalStore.
insert( $data, array $params=[], ?array $tryStores=null)
Insert data into storage and return the assigned URL.
__construct(ExternalStoreFactory $factory, ?LoggerInterface $logger=null)
setLogger(LoggerInterface $logger)
fetchFromURL( $url, array $params=[])
Fetch data from given URL.
fetchFromURLs(array $urls, array $params=[])
Fetch data from multiple URLs with a minimum of round trips.
isReadOnly( $storeUrls=null)
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...