| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use Psr\Log\LoggerAwareInterface; |
| 22 |
use Psr\Log\LoggerInterface; |
| 23 |
use Psr\Log\NullLogger; |
| 24 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class ExternalStoreAccess implements LoggerAwareInterface { |
| 44 |
|
| 45 |
private $storeFactory; |
| 46 |
|
| 47 |
private $logger; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( ExternalStoreFactory $factory, ?LoggerInterface $logger = null ) { |
| 54 |
$this->storeFactory = $factory; |
| 55 |
$this->logger = $logger ?: new NullLogger(); |
| 56 |
} |
| 57 |
|
| 58 |
public function setLogger( LoggerInterface $logger ) { |
| 59 |
$this->logger = $logger; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function fetchFromURL( $url, array $params = [] ) { |
| 73 |
return $this->storeFactory->getStoreForUrl( $url, $params )->fetchFromURL( $url ); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function fetchFromURLs( array $urls, array $params = [] ) { |
| 87 |
$batches = $this->storeFactory->getUrlsByProtocol( $urls ); |
| 88 |
$retval = []; |
| 89 |
foreach ( $batches as $proto => $batchedUrls ) { |
| 90 |
$store = $this->storeFactory->getStore( $proto, $params ); |
| 91 |
$retval += $store->batchFetchFromURLs( $batchedUrls ); |
| 92 |
} |
| 93 |
|
| 94 |
$missing = array_diff( $urls, array_keys( $retval ) ); |
| 95 |
foreach ( $missing as $url ) { |
| 96 |
$retval[$url] = false; |
| 97 |
} |
| 98 |
|
| 99 |
return $retval; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function insert( $data, array $params = [], ?array $tryStores = null ) { |
| 118 |
$tryStores ??= $this->storeFactory->getWriteBaseUrls(); |
| 119 |
if ( !$tryStores ) { |
| 120 |
throw new ExternalStoreException( "List of external stores provided is empty." ); |
| 121 |
} |
| 122 |
|
| 123 |
$error = false; |
| 124 |
$readOnlyCount = 0; |
| 125 |
while ( count( $tryStores ) > 0 ) { |
| 126 |
$index = mt_rand( 0, count( $tryStores ) - 1 ); |
| 127 |
$storeUrl = $tryStores[$index]; |
| 128 |
|
| 129 |
$this->logger->debug( __METHOD__ . ": trying $storeUrl" ); |
| 130 |
|
| 131 |
$store = $this->storeFactory->getStoreForUrl( $storeUrl, $params ); |
| 132 |
if ( $store === false ) { |
| 133 |
throw new ExternalStoreException( "Invalid external storage protocol - $storeUrl" ); |
| 134 |
} |
| 135 |
|
| 136 |
$location = $this->storeFactory->getStoreLocationFromUrl( $storeUrl ); |
| 137 |
try { |
| 138 |
if ( $store->isReadOnly( $location ) ) { |
| 139 |
$readOnlyCount++; |
| 140 |
$msg = 'read only'; |
| 141 |
} else { |
| 142 |
$url = $store->store( $location, $data ); |
| 143 |
if ( strlen( $url ) ) { |
| 144 |
|
| 145 |
return $url; |
| 146 |
} |
| 147 |
throw new ExternalStoreException( |
| 148 |
"No URL returned by storage medium ($storeUrl)" |
| 149 |
); |
| 150 |
} |
| 151 |
} catch ( TimeoutException $e ) { |
| 152 |
throw $e; |
| 153 |
} catch ( Exception $ex ) { |
| 154 |
$error = $ex; |
| 155 |
$msg = 'caught ' . get_class( $error ) . ' exception: ' . $error->getMessage(); |
| 156 |
} |
| 157 |
|
| 158 |
unset( $tryStores[$index] ); |
| 159 |
$tryStores = array_values( $tryStores ); |
| 160 |
$this->logger->error( |
| 161 |
"Unable to store text to external storage {store_path} ({failure})", |
| 162 |
[ 'store_path' => $storeUrl, 'failure' => $msg ] |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
if ( $error ) { |
| 168 |
|
| 169 |
throw $error; |
| 170 |
} elseif ( $readOnlyCount ) { |
| 171 |
|
| 172 |
|
| 173 |
throw new ReadOnlyError(); |
| 174 |
} else { |
| 175 |
|
| 176 |
|
| 177 |
throw new LogicException( "Unexpected failure to store text to external store" ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function isReadOnly( $storeUrls = null ) { |
| 187 |
if ( $storeUrls === null ) { |
| 188 |
$storeUrls = $this->storeFactory->getWriteBaseUrls(); |
| 189 |
} else { |
| 190 |
$storeUrls = is_array( $storeUrls ) ? $storeUrls : [ $storeUrls ]; |
| 191 |
} |
| 192 |
|
| 193 |
if ( !$storeUrls ) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
foreach ( $storeUrls as $storeUrl ) { |
| 198 |
$store = $this->storeFactory->getStoreForUrl( $storeUrl ); |
| 199 |
$location = $this->storeFactory->getStoreLocationFromUrl( $storeUrl ); |
| 200 |
if ( $store !== false && !$store->isReadOnly( $location ) ) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return true; |
| 206 |
} |
| 207 |
} |