MediaWiki REL1_33
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
87 protected function doGet( $key, $flags = 0, &$casToken = null ) {
88 $casToken = null;
89
90 $req = [
91 'method' => 'GET',
92 'url' => $this->url . rawurlencode( $key ),
93 ];
94
95 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
96 if ( $rcode === 200 ) {
97 if ( is_string( $rbody ) ) {
98 $value = unserialize( $rbody );
100 $casToken = ( $value !== false ) ? $rbody : null;
101
102 return $value;
103 }
104 return false;
105 }
106 if ( $rcode === 0 || ( $rcode >= 400 && $rcode != 404 ) ) {
107 return $this->handleError( "Failed to fetch $key", $rcode, $rerr );
108 }
109 return false;
110 }
111
112 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
113 // @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
114 // @TODO: respect $exptime
115 $req = [
116 'method' => 'PUT',
117 'url' => $this->url . rawurlencode( $key ),
118 'body' => serialize( $value )
119 ];
120 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
121 if ( $rcode === 200 || $rcode === 201 || $rcode === 204 ) {
122 return true;
123 }
124 return $this->handleError( "Failed to store $key", $rcode, $rerr );
125 }
126
127 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
128 // @TODO: make this atomic
129 if ( $this->get( $key ) === false ) {
130 return $this->set( $key, $value, $exptime, $flags );
131 }
132
133 return false; // key already set
134 }
135
136 public function delete( $key, $flags = 0 ) {
137 // @TODO: respect WRITE_SYNC (e.g. EACH_QUORUM)
138 $req = [
139 'method' => 'DELETE',
140 'url' => $this->url . rawurlencode( $key ),
141 ];
142 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( $req );
143 if ( in_array( $rcode, [ 200, 204, 205, 404, 410 ] ) ) {
144 return true;
145 }
146 return $this->handleError( "Failed to delete $key", $rcode, $rerr );
147 }
148
149 public function incr( $key, $value = 1 ) {
150 // @TODO: make this atomic
151 $n = $this->get( $key, self::READ_LATEST );
152 if ( $this->isInteger( $n ) ) { // key exists?
153 $n = max( $n + intval( $value ), 0 );
154 // @TODO: respect $exptime
155 return $this->set( $key, $n ) ? $n : false;
156 }
157
158 return false;
159 }
160
168 protected function handleError( $msg, $rcode, $rerr ) {
169 $this->logger->error( "$msg : ({code}) {error}", [
170 'code' => $rcode,
171 'error' => $rerr
172 ] );
173 $this->setLastError( $rcode === 0 ? self::ERR_UNREACHABLE : self::ERR_UNEXPECTED );
174 return false;
175 }
176}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
serialize()
unserialize( $serialized)
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:58
isInteger( $value)
Check if a value is an integer.
const READ_LATEST
Bitfield constants for get()/getMulti()
Definition BagOStuff.php:91
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.
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
const DEFAULT_REQ_TIMEOUT
Default request timeout.
doGet( $key, $flags=0, &$casToken=null)
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 document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
this hook is for auditing only $req
Definition hooks.txt:979
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$params