MediaWiki REL1_32
RESTBagOStuff.php
Go to the documentation of this file.
1<?php
2
3use Psr\Log\LoggerInterface;
4
31class RESTBagOStuff extends BagOStuff {
38
43
47 private $client;
48
53 private $url;
54
55 public function __construct( $params ) {
56 if ( empty( $params['url'] ) ) {
57 throw new InvalidArgumentException( 'URL parameter is required' );
58 }
59 if ( empty( $params['client'] ) ) {
60 // Pass through some params to the HTTP client.
61 $clientParams = [
62 'connTimeout' => $params['connTimeout'] ?? self::DEFAULT_CONN_TIMEOUT,
63 'reqTimeout' => $params['reqTimeout'] ?? self::DEFAULT_REQ_TIMEOUT,
64 ];
65 foreach ( [ 'caBundlePath', 'proxy' ] as $key ) {
66 if ( isset( $params[$key] ) ) {
67 $clientParams[$key] = $params[$key];
68 }
69 }
70 $this->client = new MultiHttpClient( $clientParams );
71 } else {
72 $this->client = $params['client'];
73 }
74 // The parent constructor calls setLogger() which sets the logger in $this->client
75 parent::__construct( $params );
76 // Make sure URL ends with /
77 $this->url = rtrim( $params['url'], '/' ) . '/';
78 // Default config, R+W > N; no locks on reads though; writes go straight to state-machine
80 }
81
82 public function setLogger( LoggerInterface $logger ) {
83 parent::setLogger( $logger );
84 $this->client->setLogger( $logger );
85 }
86
92 protected function doGet( $key, $flags = 0 ) {
93 $req = [
94 'method' => 'GET',
95 'url' => $this->url . rawurlencode( $key ),
96 ];
97
98 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
99 if ( $rcode === 200 ) {
100 if ( is_string( $rbody ) ) {
101 return unserialize( $rbody );
102 }
103 return false;
104 }
105 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
106 return $this->handleError( "Failed to fetch $key", $rcode, $rerr );
107 }
108 return false;
109 }
110
118 protected function handleError( $msg, $rcode, $rerr ) {
119 $this->logger->error( "$msg : ({code}) {error}", [
120 'code' => $rcode,
121 'error' => $rerr
122 ] );
123 $this->setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
124 return false;
125 }
126
136 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
137 $req = [
138 'method' => 'PUT',
139 'url' => $this->url . rawurlencode( $key ),
140 'body' => serialize( $value )
141 ];
142 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
143 if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
144 return true;
145 }
146 return $this->handleError( "Failed to store $key", $rcode, $rerr );
147 }
148
155 public function delete( $key ) {
156 $req = [
157 'method' => 'DELETE',
158 'url' => $this->url . rawurlencode( $key ),
159 ];
160 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
161 if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
162 return true;
163 }
164 return $this->handleError( "Failed to delete $key", $rcode, $rerr );
165 }
166}
serialize()
unserialize( $serialized)
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:58
const ERR_UNEXPECTED
Definition BagOStuff.php:94
LoggerInterface $logger
Definition BagOStuff.php:66
setLastError( $err)
Set the "last error" registry.
Class to handle multiple HTTP requests.
Interface to key-value storage behind an HTTP server.
__construct( $params)
setLogger(LoggerInterface $logger)
const DEFAULT_CONN_TIMEOUT
Default connection timeout in seconds.
doGet( $key, $flags=0)
const DEFAULT_REQ_TIMEOUT
Default request timeout.
handleError( $msg, $rcode, $rerr)
Handle storage error.
string $url
REST URL to use for storage.
MultiHttpClient $client
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
this hook is for auditing only $req
Definition hooks.txt:1018
$params