3 * memcached diagnostic tool
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.
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.
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
22 * @ingroup Maintenance
25 $optionsWithArgs = [ 'cache' ];
26 $optionsWithoutArgs = [
29 require_once __DIR__
. '/commandLine.inc';
31 $debug = isset( $options['debug'] );
32 $help = isset( $options['help'] );
33 $cache = isset( $options['cache'] ) ?
$options['cache'] : null;
39 $mcc = new MemcachedClient( [
45 if ( !isset( $wgObjectCaches[$cache] ) ) {
46 print "MediaWiki isn't configured with a cache named '$cache'";
49 $servers = $wgObjectCaches[$cache]['servers'];
50 } elseif ( $wgMainCacheType === CACHE_MEMCACHED
) {
51 $mcc->set_servers( $wgMemCachedServers );
52 } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
53 $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
55 print "MediaWiki isn't configured for Memcached usage\n";
60 * Show this command line tool usage.
62 function mccShowUsage() {
68 MemCached Command (mcc) is an interactive command tool that let you interact
69 with the MediaWiki memcached cache.
72 --debug Set debug mode on the memcached connection.
73 --help This help screen.
79 print str_replace( "\n", "\n\t", mccGetHelp( false ) );
83 function mccGetHelp( $command ) {
86 'get' => 'grabs something',
87 'getsock' => 'lists sockets',
88 'set' => 'changes something',
89 'delete' => 'deletes something',
90 'history' => 'show command line history',
91 'server' => 'show current memcached server',
92 'dumpmcc' => 'shows the whole thing',
95 'help' => 'help about a command',
98 $command = 'fullhelp';
100 if ( $command === 'fullhelp' ) {
101 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
102 foreach ( $commandList as $cmd => $desc ) {
103 $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
105 } elseif ( isset( $commandList[$command] ) ) {
106 $output .= "$command: $commandList[$command]\n";
108 $output .= "$command: command does not exist or no help for it\n";
119 $line = Maintenance
::readconsole();
120 if ( $line === false ) {
124 $args = explode( ' ', $line );
125 $command = array_shift( $args );
128 switch ( $command ) {
130 // show an help message
131 print mccGetHelp( array_shift( $args ) );
136 if ( array_key_exists( 1, $args ) ) {
139 print "Getting {$args[0]}[$sub]\n";
140 $res = $mcc->get( $args[0] );
141 if ( array_key_exists( 1, $args ) ) {
142 $res = $res[$args[1]];
144 if ( $res === false ) {
145 # print 'Error: ' . $mcc->error_string() . "\n";
146 print "MemCached error\n";
147 } elseif ( is_string( $res ) ) {
155 $res = $mcc->get( $args[0] );
156 $sock = $mcc->get_sock( $args[0] );
161 if ( $mcc->_single_sock
!== null ) {
162 print $mcc->_single_sock
. "\n";
165 $res = $mcc->get( $args[0] );
166 $hv = $mcc->_hashfunc( $args[0] );
167 for ( $i = 0; $i < 3; $i++
) {
168 print $mcc->_buckets
[$hv %
$mcc->_bucketcount
] . "\n";
169 $hv +
= $mcc->_hashfunc( $i . $args[0] );
174 $key = array_shift( $args );
175 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
176 $value = str_repeat( '*', $args[1] );
178 $value = implode( ' ', $args );
180 if ( !$mcc->set( $key, $value, 0 ) ) {
181 # print 'Error: ' . $mcc->error_string() . "\n";
182 print "MemCached error\n";
187 $key = implode( ' ', $args );
188 if ( !$mcc->delete( $key ) ) {
189 # print 'Error: ' . $mcc->error_string() . "\n";
190 print "MemCached error\n";
195 if ( function_exists( 'readline_list_history' ) ) {
196 foreach ( readline_list_history() as $num => $line ) {
197 print "$num: $line\n";
200 print "readline_list_history() not available\n";
219 print "Bad command\n";
222 if ( function_exists( 'readline_add_history' ) ) {
223 readline_add_history( $line );