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
26 require_once( __DIR__
. '/commandLine.inc' );
28 $options = getopt( '', array( 'debug', 'help', 'cache:' ) );
30 $debug = isset( $options['debug'] );
31 $help = isset( $options['help'] );
32 $cache = isset( $options['cache'] ) ?
$options['cache'] : null;
38 $mcc = new MWMemcached( array(
44 if ( !isset( $wgObjectCaches[$cache] ) ) {
45 print "MediaWiki isn't configured with a cache named '$cache'";
48 $servers = $wgObjectCaches[$cache]['servers'];
49 } elseif ( $wgMainCacheType === CACHE_MEMCACHED
) {
50 $mcc->set_servers( $wgMemCachedServers );
51 } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
52 $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
54 print "MediaWiki isn't configured for Memcached usage\n";
59 * Show this command line tool usage.
61 function mccShowUsage() {
67 MemCached Command (mcc) is an interactive command tool that let you interact
68 with the MediaWiki memcached cache.
71 --debug Set debug mode on the memcached connection.
72 --help This help screen.
78 print str_replace( "\n", "\n\t", mccGetHelp( false ) );
82 function mccGetHelp( $command ) {
85 'get' => 'grabs something',
86 'getsock' => 'lists sockets',
87 'set' => 'changes something',
88 'delete' => 'deletes something',
89 'history' => 'show command line history',
90 'server' => 'show current memcached server',
91 'dumpmcc' => 'shows the whole thing',
94 'help' => 'help about a command',
97 $command = 'fullhelp';
99 if ( $command === 'fullhelp' ) {
100 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
101 foreach ( $commandList as $cmd => $desc ) {
102 $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
104 } elseif ( isset( $commandList[$command] ) ) {
105 $output .= "$command: $commandList[$command]\n";
107 $output .= "$command: command does not exist or no help for it\n";
118 $line = Maintenance
::readconsole();
119 if ( $line === false ) {
123 $args = explode( ' ', $line );
124 $command = array_shift( $args );
127 switch ( $command ) {
129 // show an help message
130 print mccGetHelp( array_shift( $args ) );
135 if ( array_key_exists( 1, $args ) ) {
138 print "Getting {$args[0]}[$sub]\n";
139 $res = $mcc->get( $args[0] );
140 if ( array_key_exists( 1, $args ) ) {
141 $res = $res[$args[1]];
143 if ( $res === false ) {
144 # print 'Error: ' . $mcc->error_string() . "\n";
145 print "MemCached error\n";
146 } elseif ( is_string( $res ) ) {
154 $res = $mcc->get( $args[0] );
155 $sock = $mcc->get_sock( $args[0] );
160 if ( $mcc->_single_sock
!== null ) {
161 print $mcc->_single_sock
. "\n";
164 $res = $mcc->get( $args[0] );
165 $hv = $mcc->_hashfunc( $args[0] );
166 for ( $i = 0; $i < 3; $i++
) {
167 print $mcc->_buckets
[$hv %
$mcc->_bucketcount
] . "\n";
168 $hv +
= $mcc->_hashfunc( $i . $args[0] );
173 $key = array_shift( $args );
174 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
175 $value = str_repeat( '*', $args[1] );
177 $value = implode( ' ', $args );
179 if ( !$mcc->set( $key, $value, 0 ) ) {
180 # print 'Error: ' . $mcc->error_string() . "\n";
181 print "MemCached error\n";
186 $key = implode( ' ', $args );
187 if ( !$mcc->delete( $key ) ) {
188 # print 'Error: ' . $mcc->error_string() . "\n";
189 print "MemCached error\n";
194 if ( function_exists( 'readline_list_history' ) ) {
195 foreach ( readline_list_history() as $num => $line ) {
196 print "$num: $line\n";
199 print "readline_list_history() not available\n";
218 print "Bad command\n";
221 if ( function_exists( 'readline_add_history' ) ) {
222 readline_add_history( $line );