Merge "Make HTMLTagFilter disappear totally when $wgUseTagFilter = false"
[lhc/web/wiklou.git] / includes / objectcache / MemcachedBagOStuff.php
1 <?php
2 /**
3 * Base class for memcached clients.
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 Cache
22 */
23
24 /**
25 * Base class for memcached clients.
26 *
27 * @ingroup Cache
28 */
29 class MemcachedBagOStuff extends BagOStuff {
30 protected $client;
31
32 /**
33 * Fill in the defaults for any parameters missing from $params, using the
34 * backwards-compatible global variables
35 * @param array $params
36 * @return array
37 */
38 protected function applyDefaultParams( $params ) {
39 if ( !isset( $params['servers'] ) ) {
40 $params['servers'] = $GLOBALS['wgMemCachedServers'];
41 }
42 if ( !isset( $params['debug'] ) ) {
43 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
44 }
45 if ( !isset( $params['persistent'] ) ) {
46 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
47 }
48 if ( !isset( $params['compress_threshold'] ) ) {
49 $params['compress_threshold'] = 1500;
50 }
51 if ( !isset( $params['timeout'] ) ) {
52 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
53 }
54 if ( !isset( $params['connect_timeout'] ) ) {
55 $params['connect_timeout'] = 0.5;
56 }
57 return $params;
58 }
59
60 /**
61 * @param string $key
62 * @param mixed $casToken [optional]
63 * @return mixed
64 */
65 public function get( $key, &$casToken = null ) {
66 return $this->client->get( $this->encodeKey( $key ), $casToken );
67 }
68
69 /**
70 * @param string $key
71 * @param mixed $value
72 * @param int $exptime
73 * @return bool
74 */
75 public function set( $key, $value, $exptime = 0 ) {
76 return $this->client->set( $this->encodeKey( $key ), $value,
77 $this->fixExpiry( $exptime ) );
78 }
79
80 /**
81 * @param mixed $casToken
82 * @param string $key
83 * @param mixed $value
84 * @param int $exptime
85 * @return bool
86 */
87 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
88 return $this->client->cas( $casToken, $this->encodeKey( $key ),
89 $value, $this->fixExpiry( $exptime ) );
90 }
91
92 /**
93 * @param string $key
94 * @return bool
95 */
96 public function delete( $key ) {
97 return $this->client->delete( $this->encodeKey( $key ) );
98 }
99
100 /**
101 * @param string $key
102 * @param int $value
103 * @param int $exptime (default 0)
104 * @return mixed
105 */
106 public function add( $key, $value, $exptime = 0 ) {
107 return $this->client->add( $this->encodeKey( $key ), $value,
108 $this->fixExpiry( $exptime ) );
109 }
110
111 /**
112 * Get the underlying client object. This is provided for debugging
113 * purposes.
114 * @return BagOStuff
115 */
116 public function getClient() {
117 return $this->client;
118 }
119
120 /**
121 * Encode a key for use on the wire inside the memcached protocol.
122 *
123 * We encode spaces and line breaks to avoid protocol errors. We encode
124 * the other control characters for compatibility with libmemcached
125 * verify_key. We leave other punctuation alone, to maximise backwards
126 * compatibility.
127 * @param string $key
128 * @return string
129 */
130 public function encodeKey( $key ) {
131 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
132 array( $this, 'encodeKeyCallback' ), $key );
133 }
134
135 /**
136 * @param array $m
137 * @return string
138 */
139 protected function encodeKeyCallback( $m ) {
140 return rawurlencode( $m[0] );
141 }
142
143 /**
144 * TTLs higher than 30 days will be detected as absolute TTLs
145 * (UNIX timestamps), and will result in the cache entry being
146 * discarded immediately because the expiry is in the past.
147 * Clamp expires >30d at 30d, unless they're >=1e9 in which
148 * case they are likely to really be absolute (1e9 = 2011-09-09)
149 * @param int $expiry
150 * @return int
151 */
152 function fixExpiry( $expiry ) {
153 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
154 $expiry = 2592000;
155 }
156 return (int)$expiry;
157 }
158
159 /**
160 * Decode a key encoded with encodeKey(). This is provided as a convenience
161 * function for debugging.
162 *
163 * @param string $key
164 *
165 * @return string
166 */
167 public function decodeKey( $key ) {
168 return urldecode( $key );
169 }
170
171 /**
172 * Send a debug message to the log
173 * @param string $text
174 */
175 protected function debugLog( $text ) {
176 $this->logger->debug( $text );
177 }
178 }