Title: Title::getSubpage should not lose the interwiki prefix
[lhc/web/wiklou.git] / includes / libs / 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 abstract class MemcachedBagOStuff extends BagOStuff {
30 function __construct( array $params ) {
31 parent::__construct( $params );
32
33 $this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_BE; // unreliable
34 $this->segmentationSize = $params['maxPreferedKeySize'] ?? 917504; // < 1MiB
35 }
36
37 /**
38 * Fill in some defaults for missing keys in $params.
39 *
40 * @param array $params
41 * @return array
42 */
43 protected function applyDefaultParams( $params ) {
44 return $params + [
45 'compress_threshold' => 1500,
46 'connect_timeout' => 0.5,
47 'debug' => false
48 ];
49 }
50
51 /**
52 * Construct a cache key.
53 *
54 * @since 1.27
55 * @param string $keyspace
56 * @param array $args
57 * @return string
58 */
59 public function makeKeyInternal( $keyspace, $args ) {
60 // Memcached keys have a maximum length of 255 characters. From that,
61 // subtract the number of characters we need for the keyspace and for
62 // the separator character needed for each argument. To handle some
63 // custom prefixes used by thing like WANObjectCache, limit to 205.
64 $charsLeft = 205 - strlen( $keyspace ) - count( $args );
65
66 $args = array_map(
67 function ( $arg ) use ( &$charsLeft ) {
68 $arg = strtr( $arg, ' ', '_' );
69
70 // Make sure %, #, and non-ASCII chars are escaped
71 $arg = preg_replace_callback(
72 '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
73 function ( $m ) {
74 return rawurlencode( $m[0] );
75 },
76 $arg
77 );
78
79 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
80 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
81 $arg = '#' . md5( $arg );
82 }
83
84 $charsLeft -= strlen( $arg );
85 return $arg;
86 },
87 $args
88 );
89
90 if ( $charsLeft < 0 ) {
91 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
92 }
93
94 return $keyspace . ':' . implode( ':', $args );
95 }
96
97 /**
98 * Ensure that a key is safe to use (contains no control characters and no
99 * characters above the ASCII range.)
100 *
101 * @param string $key
102 * @return string
103 * @throws Exception
104 */
105 public function validateKeyEncoding( $key ) {
106 if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) {
107 throw new Exception( "Key contains invalid characters: $key" );
108 }
109 return $key;
110 }
111
112 /**
113 * TTLs higher than 30 days will be detected as absolute TTLs
114 * (UNIX timestamps), and will result in the cache entry being
115 * discarded immediately because the expiry is in the past.
116 * Clamp expires >30d at 30d, unless they're >=1e9 in which
117 * case they are likely to really be absolute (1e9 = 2011-09-09)
118 * @param int $expiry
119 * @return int
120 */
121 function fixExpiry( $expiry ) {
122 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
123 $expiry = 2592000;
124 }
125 return (int)$expiry;
126 }
127 }