MediaWiki  1.33.0
MemcachedClient.php
Go to the documentation of this file.
1 <?php
2 // phpcs:ignoreFile -- It's an external lib and it isn't. Let's not bother.
69 use Psr\Log\LoggerInterface;
70 use Psr\Log\NullLogger;
71 
72 // {{{ class MemcachedClient
80  // {{{ properties
81  // {{{ public
82 
83  // {{{ constants
84  // {{{ flags
85 
89  const SERIALIZED = 1;
90 
94  const COMPRESSED = 2;
95 
99  const INTVAL = 4;
100 
101  // }}}
102 
106  const COMPRESSION_SAVINGS = 0.20;
107 
108  // }}}
109 
116  public $stats;
117 
118  // }}}
119  // {{{ private
120 
127  public $_cache_sock;
128 
135  public $_debug;
136 
143  public $_host_dead;
144 
151  public $_have_zlib;
152 
160 
168 
175  public $_persistent;
176 
184 
191  public $_servers;
192 
199  public $_buckets;
200 
208 
215  public $_active;
216 
224 
232 
237 
242 
246  private $_logger;
247 
248  // }}}
249  // }}}
250  // {{{ methods
251  // {{{ public functions
252  // {{{ memcached()
253 
259  public function __construct( $args ) {
260  $this->set_servers( $args['servers'] ?? array() );
261  $this->_debug = $args['debug'] ?? false;
262  $this->stats = array();
263  $this->_compress_threshold = $args['compress_threshold'] ?? 0;
264  $this->_persistent = $args['persistent'] ?? false;
265  $this->_compress_enable = true;
266  $this->_have_zlib = function_exists( 'gzcompress' );
267 
268  $this->_cache_sock = array();
269  $this->_host_dead = array();
270 
271  $this->_timeout_seconds = 0;
272  $this->_timeout_microseconds = $args['timeout'] ?? 500000;
273 
274  $this->_connect_timeout = $args['connect_timeout'] ?? 0.1;
275  $this->_connect_attempts = 2;
276 
277  $this->_logger = $args['logger'] ?? new NullLogger();
278  }
279 
280  // }}}
281  // {{{ add()
282 
297  public function add( $key, $val, $exp = 0 ) {
298  return $this->_set( 'add', $key, $val, $exp );
299  }
300 
301  // }}}
302  // {{{ decr()
303 
312  public function decr( $key, $amt = 1 ) {
313  return $this->_incrdecr( 'decr', $key, $amt );
314  }
315 
316  // }}}
317  // {{{ delete()
318 
327  public function delete( $key, $time = 0 ) {
328  if ( !$this->_active ) {
329  return false;
330  }
331 
332  $sock = $this->get_sock( $key );
333  if ( !is_resource( $sock ) ) {
334  return false;
335  }
336 
337  $key = is_array( $key ) ? $key[1] : $key;
338 
339  if ( isset( $this->stats['delete'] ) ) {
340  $this->stats['delete']++;
341  } else {
342  $this->stats['delete'] = 1;
343  }
344  $cmd = "delete $key $time\r\n";
345  if ( !$this->_fwrite( $sock, $cmd ) ) {
346  return false;
347  }
348  $res = $this->_fgets( $sock );
349 
350  if ( $this->_debug ) {
351  $this->_debugprint( sprintf( "MemCache: delete %s (%s)", $key, $res ) );
352  }
353 
354  if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
355  return true;
356  }
357 
358  return false;
359  }
360 
369  public function touch( $key, $time = 0 ) {
370  if ( !$this->_active ) {
371  return false;
372  }
373 
374  $sock = $this->get_sock( $key );
375  if ( !is_resource( $sock ) ) {
376  return false;
377  }
378 
379  $key = is_array( $key ) ? $key[1] : $key;
380 
381  if ( isset( $this->stats['touch'] ) ) {
382  $this->stats['touch']++;
383  } else {
384  $this->stats['touch'] = 1;
385  }
386  $cmd = "touch $key $time\r\n";
387  if ( !$this->_fwrite( $sock, $cmd ) ) {
388  return false;
389  }
390  $res = $this->_fgets( $sock );
391 
392  if ( $this->_debug ) {
393  $this->_debugprint( sprintf( "MemCache: touch %s (%s)", $key, $res ) );
394  }
395 
396  if ( $res == "TOUCHED" ) {
397  return true;
398  }
399 
400  return false;
401  }
402 
408  public function lock( $key, $timeout = 0 ) {
409  /* stub */
410  return true;
411  }
412 
417  public function unlock( $key ) {
418  /* stub */
419  return true;
420  }
421 
422  // }}}
423  // {{{ disconnect_all()
424 
428  public function disconnect_all() {
429  foreach ( $this->_cache_sock as $sock ) {
430  fclose( $sock );
431  }
432 
433  $this->_cache_sock = array();
434  }
435 
436  // }}}
437  // {{{ enable_compress()
438 
444  public function enable_compress( $enable ) {
445  $this->_compress_enable = $enable;
446  }
447 
448  // }}}
449  // {{{ forget_dead_hosts()
450 
454  public function forget_dead_hosts() {
455  $this->_host_dead = array();
456  }
457 
458  // }}}
459  // {{{ get()
460 
469  public function get( $key, &$casToken = null ) {
470  if ( $this->_debug ) {
471  $this->_debugprint( "get($key)" );
472  }
473 
474  if ( !is_array( $key ) && strval( $key ) === '' ) {
475  $this->_debugprint( "Skipping key which equals to an empty string" );
476  return false;
477  }
478 
479  if ( !$this->_active ) {
480  return false;
481  }
482 
483  $sock = $this->get_sock( $key );
484 
485  if ( !is_resource( $sock ) ) {
486  return false;
487  }
488 
489  $key = is_array( $key ) ? $key[1] : $key;
490  if ( isset( $this->stats['get'] ) ) {
491  $this->stats['get']++;
492  } else {
493  $this->stats['get'] = 1;
494  }
495 
496  $cmd = "gets $key\r\n";
497  if ( !$this->_fwrite( $sock, $cmd ) ) {
498  return false;
499  }
500 
501  $val = array();
502  $this->_load_items( $sock, $val, $casToken );
503 
504  if ( $this->_debug ) {
505  foreach ( $val as $k => $v ) {
506  $this->_debugprint( sprintf( "MemCache: sock %s got %s", serialize( $sock ), $k ) );
507  }
508  }
509 
510  $value = false;
511  if ( isset( $val[$key] ) ) {
512  $value = $val[$key];
513  }
514  return $value;
515  }
516 
517  // }}}
518  // {{{ get_multi()
519 
527  public function get_multi( $keys ) {
528  if ( !$this->_active ) {
529  return array();
530  }
531 
532  if ( isset( $this->stats['get_multi'] ) ) {
533  $this->stats['get_multi']++;
534  } else {
535  $this->stats['get_multi'] = 1;
536  }
537  $sock_keys = array();
538  $socks = array();
539  foreach ( $keys as $key ) {
540  $sock = $this->get_sock( $key );
541  if ( !is_resource( $sock ) ) {
542  continue;
543  }
544  $key = is_array( $key ) ? $key[1] : $key;
545  if ( !isset( $sock_keys[$sock] ) ) {
546  $sock_keys[intval( $sock )] = array();
547  $socks[] = $sock;
548  }
549  $sock_keys[intval( $sock )][] = $key;
550  }
551 
552  $gather = array();
553  // Send out the requests
554  foreach ( $socks as $sock ) {
555  $cmd = 'gets';
556  foreach ( $sock_keys[intval( $sock )] as $key ) {
557  $cmd .= ' ' . $key;
558  }
559  $cmd .= "\r\n";
560 
561  if ( $this->_fwrite( $sock, $cmd ) ) {
562  $gather[] = $sock;
563  }
564  }
565 
566  // Parse responses
567  $val = array();
568  foreach ( $gather as $sock ) {
569  $this->_load_items( $sock, $val, $casToken );
570  }
571 
572  if ( $this->_debug ) {
573  foreach ( $val as $k => $v ) {
574  $this->_debugprint( sprintf( "MemCache: got %s", $k ) );
575  }
576  }
577 
578  return $val;
579  }
580 
581  // }}}
582  // {{{ incr()
583 
594  public function incr( $key, $amt = 1 ) {
595  return $this->_incrdecr( 'incr', $key, $amt );
596  }
597 
598  // }}}
599  // {{{ replace()
600 
614  public function replace( $key, $value, $exp = 0 ) {
615  return $this->_set( 'replace', $key, $value, $exp );
616  }
617 
618  // }}}
619  // {{{ run_command()
620 
630  public function run_command( $sock, $cmd ) {
631  if ( !is_resource( $sock ) ) {
632  return array();
633  }
634 
635  if ( !$this->_fwrite( $sock, $cmd ) ) {
636  return array();
637  }
638 
639  $ret = array();
640  while ( true ) {
641  $res = $this->_fgets( $sock );
642  $ret[] = $res;
643  if ( preg_match( '/^END/', $res ) ) {
644  break;
645  }
646  if ( strlen( $res ) == 0 ) {
647  break;
648  }
649  }
650  return $ret;
651  }
652 
653  // }}}
654  // {{{ set()
655 
670  public function set( $key, $value, $exp = 0 ) {
671  return $this->_set( 'set', $key, $value, $exp );
672  }
673 
674  // }}}
675  // {{{ cas()
676 
692  public function cas( $casToken, $key, $value, $exp = 0 ) {
693  return $this->_set( 'cas', $key, $value, $exp, $casToken );
694  }
695 
696  // }}}
697  // {{{ set_compress_threshold()
698 
704  public function set_compress_threshold( $thresh ) {
705  $this->_compress_threshold = $thresh;
706  }
707 
708  // }}}
709  // {{{ set_debug()
710 
717  public function set_debug( $dbg ) {
718  $this->_debug = $dbg;
719  }
720 
721  // }}}
722  // {{{ set_servers()
723 
730  public function set_servers( $list ) {
731  $this->_servers = $list;
732  $this->_active = count( $list );
733  $this->_buckets = null;
734  $this->_bucketcount = 0;
735 
736  $this->_single_sock = null;
737  if ( $this->_active == 1 ) {
738  $this->_single_sock = $this->_servers[0];
739  }
740  }
741 
748  public function set_timeout( $seconds, $microseconds ) {
749  $this->_timeout_seconds = $seconds;
750  $this->_timeout_microseconds = $microseconds;
751  }
752 
753  // }}}
754  // }}}
755  // {{{ private methods
756  // {{{ _close_sock()
757 
765  function _close_sock( $sock ) {
766  $host = array_search( $sock, $this->_cache_sock );
767  fclose( $this->_cache_sock[$host] );
768  unset( $this->_cache_sock[$host] );
769  }
770 
771  // }}}
772  // {{{ _connect_sock()
773 
783  function _connect_sock( &$sock, $host ) {
784  list( $ip, $port ) = preg_split( '/:(?=\d)/', $host );
785  $sock = false;
786  $timeout = $this->_connect_timeout;
787  $errno = $errstr = null;
788  for ( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
789  Wikimedia\suppressWarnings();
790  if ( $this->_persistent == 1 ) {
791  $sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
792  } else {
793  $sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
794  }
795  Wikimedia\restoreWarnings();
796  }
797  if ( !$sock ) {
798  $this->_error_log( "Error connecting to $host: $errstr" );
799  $this->_dead_host( $host );
800  return false;
801  }
802 
803  // Initialise timeout
804  stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
805 
806  // If the connection was persistent, flush the read buffer in case there
807  // was a previous incomplete request on this connection
808  if ( $this->_persistent ) {
809  $this->_flush_read_buffer( $sock );
810  }
811  return true;
812  }
813 
814  // }}}
815  // {{{ _dead_sock()
816 
824  function _dead_sock( $sock ) {
825  $host = array_search( $sock, $this->_cache_sock );
826  $this->_dead_host( $host );
827  }
828 
832  function _dead_host( $host ) {
833  $ip = explode( ':', $host )[0];
834  $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
835  $this->_host_dead[$host] = $this->_host_dead[$ip];
836  unset( $this->_cache_sock[$host] );
837  }
838 
839  // }}}
840  // {{{ get_sock()
841 
850  function get_sock( $key ) {
851  if ( !$this->_active ) {
852  return false;
853  }
854 
855  if ( $this->_single_sock !== null ) {
856  return $this->sock_to_host( $this->_single_sock );
857  }
858 
859  $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
860  if ( $this->_buckets === null ) {
861  $bu = array();
862  foreach ( $this->_servers as $v ) {
863  if ( is_array( $v ) ) {
864  for ( $i = 0; $i < $v[1]; $i++ ) {
865  $bu[] = $v[0];
866  }
867  } else {
868  $bu[] = $v;
869  }
870  }
871  $this->_buckets = $bu;
872  $this->_bucketcount = count( $bu );
873  }
874 
875  $realkey = is_array( $key ) ? $key[1] : $key;
876  for ( $tries = 0; $tries < 20; $tries++ ) {
877  $host = $this->_buckets[$hv % $this->_bucketcount];
878  $sock = $this->sock_to_host( $host );
879  if ( is_resource( $sock ) ) {
880  return $sock;
881  }
882  $hv = $this->_hashfunc( $hv . $realkey );
883  }
884 
885  return false;
886  }
887 
888  // }}}
889  // {{{ _hashfunc()
890 
899  function _hashfunc( $key ) {
900  # Hash function must be in [0,0x7ffffff]
901  # We take the first 31 bits of the MD5 hash, which unlike the hash
902  # function used in a previous version of this client, works
903  return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
904  }
905 
906  // }}}
907  // {{{ _incrdecr()
908 
919  function _incrdecr( $cmd, $key, $amt = 1 ) {
920  if ( !$this->_active ) {
921  return null;
922  }
923 
924  $sock = $this->get_sock( $key );
925  if ( !is_resource( $sock ) ) {
926  return null;
927  }
928 
929  $key = is_array( $key ) ? $key[1] : $key;
930  if ( isset( $this->stats[$cmd] ) ) {
931  $this->stats[$cmd]++;
932  } else {
933  $this->stats[$cmd] = 1;
934  }
935  if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
936  return null;
937  }
938 
939  $line = $this->_fgets( $sock );
940  $match = array();
941  if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
942  return null;
943  }
944  return $match[1];
945  }
946 
947  // }}}
948  // {{{ _load_items()
949 
960  function _load_items( $sock, &$ret, &$casToken = null ) {
961  $results = array();
962 
963  while ( 1 ) {
964  $decl = $this->_fgets( $sock );
965 
966  if ( $decl === false ) {
967  /*
968  * If nothing can be read, something is wrong because we know exactly when
969  * to stop reading (right after "END") and we return right after that.
970  */
971  return false;
972  } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
973  /*
974  * Read all data returned. This can be either one or multiple values.
975  * Save all that data (in an array) to be processed later: we'll first
976  * want to continue reading until "END" before doing anything else,
977  * to make sure that we don't leave our client in a state where it's
978  * output is not yet fully read.
979  */
980  $results[] = array(
981  $match[1], // rkey
982  $match[2], // flags
983  $match[3], // len
984  $match[4], // casToken
985  $this->_fread( $sock, $match[3] + 2 ), // data
986  );
987  } elseif ( $decl == "END" ) {
988  if ( count( $results ) == 0 ) {
989  return false;
990  }
991 
996  foreach ( $results as $vars ) {
997  list( $rkey, $flags, $len, $casToken, $data ) = $vars;
998 
999  if ( $data === false || substr( $data, -2 ) !== "\r\n" ) {
1000  $this->_handle_error( $sock,
1001  'line ending missing from data block from $1' );
1002  return false;
1003  }
1004  $data = substr( $data, 0, -2 );
1005  $ret[$rkey] = $data;
1006 
1007  if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
1008  $ret[$rkey] = gzuncompress( $ret[$rkey] );
1009  }
1010 
1011  /*
1012  * This unserialize is the exact reason that we only want to
1013  * process data after having read until "END" (instead of doing
1014  * this right away): "unserialize" can trigger outside code:
1015  * in the event that $ret[$rkey] is a serialized object,
1016  * unserializing it will trigger __wakeup() if present. If that
1017  * function attempted to read from memcached (while we did not
1018  * yet read "END"), these 2 calls would collide.
1019  */
1020  if ( $flags & self::SERIALIZED ) {
1021  $ret[$rkey] = unserialize( $ret[$rkey] );
1022  } elseif ( $flags & self::INTVAL ) {
1023  $ret[$rkey] = intval( $ret[$rkey] );
1024  }
1025  }
1026 
1027  return true;
1028  } else {
1029  $this->_handle_error( $sock, 'Error parsing response from $1' );
1030  return false;
1031  }
1032  }
1033  }
1034 
1035  // }}}
1036  // {{{ _set()
1037 
1054  function _set( $cmd, $key, $val, $exp, $casToken = null ) {
1055  if ( !$this->_active ) {
1056  return false;
1057  }
1058 
1059  $sock = $this->get_sock( $key );
1060  if ( !is_resource( $sock ) ) {
1061  return false;
1062  }
1063 
1064  if ( isset( $this->stats[$cmd] ) ) {
1065  $this->stats[$cmd]++;
1066  } else {
1067  $this->stats[$cmd] = 1;
1068  }
1069 
1070  $flags = 0;
1071 
1072  if ( is_int( $val ) ) {
1073  $flags |= self::INTVAL;
1074  } elseif ( !is_scalar( $val ) ) {
1075  $val = serialize( $val );
1076  $flags |= self::SERIALIZED;
1077  if ( $this->_debug ) {
1078  $this->_debugprint( sprintf( "client: serializing data as it is not scalar" ) );
1079  }
1080  }
1081 
1082  $len = strlen( $val );
1083 
1084  if ( $this->_have_zlib && $this->_compress_enable
1085  && $this->_compress_threshold && $len >= $this->_compress_threshold
1086  ) {
1087  $c_val = gzcompress( $val, 9 );
1088  $c_len = strlen( $c_val );
1089 
1090  if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
1091  if ( $this->_debug ) {
1092  $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes", $len, $c_len ) );
1093  }
1094  $val = $c_val;
1095  $len = $c_len;
1096  $flags |= self::COMPRESSED;
1097  }
1098  }
1099 
1100  $command = "$cmd $key $flags $exp $len";
1101  if ( $casToken ) {
1102  $command .= " $casToken";
1103  }
1104 
1105  if ( !$this->_fwrite( $sock, "$command\r\n$val\r\n" ) ) {
1106  return false;
1107  }
1108 
1109  $line = $this->_fgets( $sock );
1110 
1111  if ( $this->_debug ) {
1112  $this->_debugprint( sprintf( "%s %s (%s)", $cmd, $key, $line ) );
1113  }
1114  if ( $line === "STORED" ) {
1115  return true;
1116  } elseif ( $line === "NOT_STORED" && $cmd === "set" ) {
1117  // "Not stored" is always used as the mcrouter response with AllAsyncRoute
1118  return true;
1119  }
1120 
1121  return false;
1122  }
1123 
1124  // }}}
1125  // {{{ sock_to_host()
1126 
1135  function sock_to_host( $host ) {
1136  if ( isset( $this->_cache_sock[$host] ) ) {
1137  return $this->_cache_sock[$host];
1138  }
1139 
1140  $sock = null;
1141  $now = time();
1142  list( $ip, /* $port */) = explode( ':', $host );
1143  if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
1144  isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
1145  ) {
1146  return null;
1147  }
1148 
1149  if ( !$this->_connect_sock( $sock, $host ) ) {
1150  return null;
1151  }
1152 
1153  // Do not buffer writes
1154  stream_set_write_buffer( $sock, 0 );
1155 
1156  $this->_cache_sock[$host] = $sock;
1157 
1158  return $this->_cache_sock[$host];
1159  }
1160 
1164  function _debugprint( $text ) {
1165  $this->_logger->debug( $text );
1166  }
1167 
1171  function _error_log( $text ) {
1172  $this->_logger->error( "Memcached error: $text" );
1173  }
1174 
1182  function _fwrite( $sock, $buf ) {
1183  $bytesWritten = 0;
1184  $bufSize = strlen( $buf );
1185  while ( $bytesWritten < $bufSize ) {
1186  $result = fwrite( $sock, $buf );
1187  $data = stream_get_meta_data( $sock );
1188  if ( $data['timed_out'] ) {
1189  $this->_handle_error( $sock, 'timeout writing to $1' );
1190  return false;
1191  }
1192  // Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
1193  if ( $result === false || $result === 0 ) {
1194  $this->_handle_error( $sock, 'error writing to $1' );
1195  return false;
1196  }
1197  $bytesWritten += $result;
1198  }
1199 
1200  return true;
1201  }
1202 
1209  function _handle_error( $sock, $msg ) {
1210  $peer = stream_socket_get_name( $sock, true );
1211  if ( strval( $peer ) === '' ) {
1212  $peer = array_search( $sock, $this->_cache_sock );
1213  if ( $peer === false ) {
1214  $peer = '[unknown host]';
1215  }
1216  }
1217  $msg = str_replace( '$1', $peer, $msg );
1218  $this->_error_log( "$msg" );
1219  $this->_dead_sock( $sock );
1220  }
1221 
1230  function _fread( $sock, $len ) {
1231  $buf = '';
1232  while ( $len > 0 ) {
1233  $result = fread( $sock, $len );
1234  $data = stream_get_meta_data( $sock );
1235  if ( $data['timed_out'] ) {
1236  $this->_handle_error( $sock, 'timeout reading from $1' );
1237  return false;
1238  }
1239  if ( $result === false ) {
1240  $this->_handle_error( $sock, 'error reading buffer from $1' );
1241  return false;
1242  }
1243  if ( $result === '' ) {
1244  // This will happen if the remote end of the socket is shut down
1245  $this->_handle_error( $sock, 'unexpected end of file reading from $1' );
1246  return false;
1247  }
1248  $len -= strlen( $result );
1249  $buf .= $result;
1250  }
1251  return $buf;
1252  }
1253 
1261  function _fgets( $sock ) {
1262  $result = fgets( $sock );
1263  // fgets() may return a partial line if there is a select timeout after
1264  // a successful recv(), so we have to check for a timeout even if we
1265  // got a string response.
1266  $data = stream_get_meta_data( $sock );
1267  if ( $data['timed_out'] ) {
1268  $this->_handle_error( $sock, 'timeout reading line from $1' );
1269  return false;
1270  }
1271  if ( $result === false ) {
1272  $this->_handle_error( $sock, 'error reading line from $1' );
1273  return false;
1274  }
1275  if ( substr( $result, -2 ) === "\r\n" ) {
1276  $result = substr( $result, 0, -2 );
1277  } elseif ( substr( $result, -1 ) === "\n" ) {
1278  $result = substr( $result, 0, -1 );
1279  } else {
1280  $this->_handle_error( $sock, 'line ending missing in response from $1' );
1281  return false;
1282  }
1283  return $result;
1284  }
1285 
1290  function _flush_read_buffer( $f ) {
1291  if ( !is_resource( $f ) ) {
1292  return;
1293  }
1294  $r = array( $f );
1295  $w = null;
1296  $e = null;
1297  $n = stream_select( $r, $w, $e, 0, 0 );
1298  while ( $n == 1 && !feof( $f ) ) {
1299  fread( $f, 1024 );
1300  $r = array( $f );
1301  $w = null;
1302  $e = null;
1303  $n = stream_select( $r, $w, $e, 0, 0 );
1304  }
1305  }
1306 
1307  // }}}
1308  // }}}
1309  // }}}
1310 }
1311 
1312 // }}}
MemcachedClient\get_multi
get_multi( $keys)
Get multiple keys from the server(s)
Definition: MemcachedClient.php:527
MemcachedClient\get_sock
get_sock( $key)
get_sock
Definition: MemcachedClient.php:850
MemcachedClient\$_compress_threshold
int $_compress_threshold
At how many bytes should we compress?
Definition: MemcachedClient.php:167
MemcachedClient\_close_sock
_close_sock( $sock)
Close the specified socket.
Definition: MemcachedClient.php:765
captcha-old.count
count
Definition: captcha-old.py:249
MemcachedClient\set_servers
set_servers( $list)
Set the server list to distribute key gets and puts between.
Definition: MemcachedClient.php:730
MemcachedClient\_debugprint
_debugprint( $text)
Definition: MemcachedClient.php:1164
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1983
MemcachedClient\$_bucketcount
int $_bucketcount
Total # of bit buckets we have.
Definition: MemcachedClient.php:207
MemcachedClient\$_active
int $_active
Definition: MemcachedClient.php:215
MemcachedClient\$_servers
array $_servers
Array containing ip:port or array(ip:port, weight)
Definition: MemcachedClient.php:191
MemcachedClient\set_debug
set_debug( $dbg)
Set the debug flag.
Definition: MemcachedClient.php:717
MemcachedClient\$_timeout_microseconds
int $_timeout_microseconds
Stream timeout in microseconds.
Definition: MemcachedClient.php:231
MemcachedClient\touch
touch( $key, $time=0)
Changes the TTL on a key from the server to $time.
Definition: MemcachedClient.php:369
MemcachedClient\$_connect_attempts
$_connect_attempts
Number of connection attempts for each server.
Definition: MemcachedClient.php:241
MemcachedClient\$_compress_enable
bool $_compress_enable
Do we want to use compression?
Definition: MemcachedClient.php:159
$res
$res
Definition: database.txt:21
MemcachedClient\$_timeout_seconds
int $_timeout_seconds
Stream timeout in seconds.
Definition: MemcachedClient.php:223
serialize
serialize()
Definition: ApiMessageTrait.php:134
MemcachedClient\_dead_host
_dead_host( $host)
Definition: MemcachedClient.php:832
MemcachedClient\COMPRESSED
const COMPRESSED
Flag: indicates data is compressed.
Definition: MemcachedClient.php:94
php
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:35
MemcachedClient\$_connect_timeout
$_connect_timeout
Connect timeout in seconds.
Definition: MemcachedClient.php:236
MemcachedClient
memcached client class implemented using (p)fsockopen()
Definition: MemcachedClient.php:79
MemcachedClient\_error_log
_error_log( $text)
Definition: MemcachedClient.php:1171
MemcachedClient\incr
incr( $key, $amt=1)
Increments $key (optionally) by $amt.
Definition: MemcachedClient.php:594
MemcachedClient\disconnect_all
disconnect_all()
Disconnects all connected sockets.
Definition: MemcachedClient.php:428
MemcachedClient\$_host_dead
array $_host_dead
Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'.
Definition: MemcachedClient.php:143
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
MemcachedClient\_handle_error
_handle_error( $sock, $msg)
Handle an I/O error.
Definition: MemcachedClient.php:1209
MemcachedClient\COMPRESSION_SAVINGS
const COMPRESSION_SAVINGS
Minimum savings to store data compressed.
Definition: MemcachedClient.php:106
MemcachedClient\_fgets
_fgets( $sock)
Read a line from a stream.
Definition: MemcachedClient.php:1261
MemcachedClient\unlock
unlock( $key)
Definition: MemcachedClient.php:417
MemcachedClient\_flush_read_buffer
_flush_read_buffer( $f)
Flush the read buffer of a stream.
Definition: MemcachedClient.php:1290
MemcachedClient\replace
replace( $key, $value, $exp=0)
Overwrites an existing value for key; only works if key is already set.
Definition: MemcachedClient.php:614
MemcachedClient\_fread
_fread( $sock, $len)
Read the specified number of bytes from a stream.
Definition: MemcachedClient.php:1230
MemcachedClient\_set
_set( $cmd, $key, $val, $exp, $casToken=null)
Performs the requested storage operation to the memcache server.
Definition: MemcachedClient.php:1054
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MemcachedClient\forget_dead_hosts
forget_dead_hosts()
Forget about all of the dead hosts.
Definition: MemcachedClient.php:454
$vars
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
Definition: hooks.txt:2220
MemcachedClient\$_debug
bool $_debug
Current debug status; 0 - none to 9 - profiling.
Definition: MemcachedClient.php:135
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
list
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
MemcachedClient\sock_to_host
sock_to_host( $host)
Returns the socket for the host.
Definition: MemcachedClient.php:1135
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
MemcachedClient\$stats
array $stats
Command statistics.
Definition: MemcachedClient.php:116
$command
$command
Definition: cdb.php:65
$line
$line
Definition: cdb.php:59
MemcachedClient\decr
decr( $key, $amt=1)
Decrease a value stored on the memcache server.
Definition: MemcachedClient.php:312
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
$value
$value
Definition: styleTest.css.php:49
MemcachedClient\set_compress_threshold
set_compress_threshold( $thresh)
Set the compression threshold.
Definition: MemcachedClient.php:704
MemcachedClient\add
add( $key, $val, $exp=0)
Adds a key/value to the memcache server if one isn't already set with that key.
Definition: MemcachedClient.php:297
MemcachedClient\INTVAL
const INTVAL
Flag: indicates data is an integer.
Definition: MemcachedClient.php:99
MemcachedClient\$_persistent
bool $_persistent
Are we using persistent links?
Definition: MemcachedClient.php:175
MemcachedClient\_fwrite
_fwrite( $sock, $buf)
Write to a stream.
Definition: MemcachedClient.php:1182
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1985
MemcachedClient\$_buckets
array $_buckets
Our bit buckets.
Definition: MemcachedClient.php:199
MemcachedClient\$_cache_sock
array $_cache_sock
Cached Sockets that are connected.
Definition: MemcachedClient.php:127
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
$args
if( $line===false) $args
Definition: cdb.php:64
MemcachedClient\_hashfunc
_hashfunc( $key)
Creates a hash integer based on the $key.
Definition: MemcachedClient.php:899
MemcachedClient\__construct
__construct( $args)
Memcache initializer.
Definition: MemcachedClient.php:259
MemcachedClient\$_have_zlib
bool $_have_zlib
Is compression available?
Definition: MemcachedClient.php:151
as
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
Definition: distributors.txt:9
MemcachedClient\cas
cas( $casToken, $key, $value, $exp=0)
Sets a key to a given value in the memcache if the current value still corresponds to a known,...
Definition: MemcachedClient.php:692
MemcachedClient\_connect_sock
_connect_sock(&$sock, $host)
Connects $sock to $host, timing out after $timeout.
Definition: MemcachedClient.php:783
$keys
$keys
Definition: testCompression.php:67
MemcachedClient\enable_compress
enable_compress( $enable)
Enable / Disable compression.
Definition: MemcachedClient.php:444
$f
$f
Definition: router.php:79
$time
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1802
MemcachedClient\_dead_sock
_dead_sock( $sock)
Marks a host as dead until 30-40 seconds in the future.
Definition: MemcachedClient.php:824
MemcachedClient\$_logger
LoggerInterface $_logger
Definition: MemcachedClient.php:246
MemcachedClient\lock
lock( $key, $timeout=0)
Definition: MemcachedClient.php:408
MemcachedClient\run_command
run_command( $sock, $cmd)
Passes through $cmd to the memcache server connected by $sock; returns output as an array (null array...
Definition: MemcachedClient.php:630
MemcachedClient\_incrdecr
_incrdecr( $cmd, $key, $amt=1)
Perform increment/decriment on $key.
Definition: MemcachedClient.php:919
MemcachedClient\SERIALIZED
const SERIALIZED
Flag: indicates data is serialized.
Definition: MemcachedClient.php:89
MemcachedClient\$_single_sock
string $_single_sock
If only using one server; contains ip:port to connect to.
Definition: MemcachedClient.php:183
MemcachedClient\set_timeout
set_timeout( $seconds, $microseconds)
Sets the timeout for new connections.
Definition: MemcachedClient.php:748
MemcachedClient\_load_items
_load_items( $sock, &$ret, &$casToken=null)
Load items into $ret from $sock.
Definition: MemcachedClient.php:960