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 $debug = in_array( '--debug', $argv );
29 $help = in_array( '--help', $argv );
35 $mcc = new MWMemcached( array(
40 if ( $wgMainCacheType === CACHE_MEMCACHED
) {
41 $mcc->set_servers( $wgMemCachedServers );
42 } elseif( isset( $wgObjectCaches[$wgMainCacheType] ) ) {
43 $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
45 print "MediaWiki isn't configured for Memcached usage\n";
50 * Show this command line tool usage.
52 function mccShowUsage() {
58 MemCached Command (mcc) is an interactive command tool that let you interact
59 with the MediaWiki memcached cache.
62 --debug Set debug mode on the memcached connection.
63 --help This help screen.
69 print str_replace( "\n", "\n\t", mccGetHelp( false ) );
73 function mccGetHelp( $command ) {
76 'get' => 'grabs something',
77 'getsock' => 'lists sockets',
78 'set' => 'changes something',
79 'delete' => 'deletes something',
80 'history' => 'show command line history',
81 'server' => 'show current memcached server',
82 'dumpmcc' => 'shows the whole thing',
85 'help' => 'help about a command',
88 $command = 'fullhelp';
90 if ( $command === 'fullhelp' ) {
91 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
92 foreach ( $commandList as $cmd => $desc ) {
93 $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
95 } elseif ( isset( $commandList[$command] ) ) {
96 $output .= "$command: $commandList[$command]\n";
98 $output .= "$command: command does not exist or no help for it\n";
109 $line = Maintenance
::readconsole();
110 if ( $line === false ) exit;
112 $args = explode( ' ', $line );
113 $command = array_shift( $args );
116 switch ( $command ) {
118 // show an help message
119 print mccGetHelp( array_shift( $args ) );
124 if ( array_key_exists( 1, $args ) ) {
127 print "Getting {$args[0]}[$sub]\n";
128 $res = $mcc->get( $args[0] );
129 if ( array_key_exists( 1, $args ) ) {
130 $res = $res[$args[1]];
132 if ( $res === false ) {
133 # print 'Error: ' . $mcc->error_string() . "\n";
134 print "MemCached error\n";
135 } elseif ( is_string( $res ) ) {
143 $res = $mcc->get( $args[0] );
144 $sock = $mcc->get_sock( $args[0] );
149 if ( $mcc->_single_sock
!== null ) {
150 print $mcc->_single_sock
. "\n";
153 $res = $mcc->get( $args[0] );
154 $hv = $mcc->_hashfunc( $args[0] );
155 for ( $i = 0; $i < 3; $i++
) {
156 print $mcc->_buckets
[$hv %
$mcc->_bucketcount
] . "\n";
157 $hv +
= $mcc->_hashfunc( $i . $args[0] );
162 $key = array_shift( $args );
163 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
164 $value = str_repeat( '*', $args[1] );
166 $value = implode( ' ', $args );
168 if ( !$mcc->set( $key, $value, 0 ) ) {
169 # print 'Error: ' . $mcc->error_string() . "\n";
170 print "MemCached error\n";
175 $key = implode( ' ', $args );
176 if ( !$mcc->delete( $key ) ) {
177 # print 'Error: ' . $mcc->error_string() . "\n";
178 print "MemCached error\n";
183 if ( function_exists( 'readline_list_history' ) ) {
184 foreach ( readline_list_history() as $num => $line ) {
185 print "$num: $line\n";
188 print "readline_list_history() not available\n";
207 print "Bad command\n";
210 if ( function_exists( 'readline_add_history' ) ) {
211 readline_add_history( $line );