00001 <?php
00011 require_once( dirname(__FILE__) . '/commandLine.inc' );
00012
00013 $mcc = new MWMemcached( array('persistant' => true) );
00014 $mcc->set_servers( $wgMemCachedServers );
00015 #$mcc->set_debug( true );
00016
00017 function mccShowHelp($command) {
00018 $commandList = array(
00019 'get' => 'grabs something',
00020 'getsock' => 'lists sockets',
00021 'set' => 'changes something',
00022 'delete' => 'deletes something',
00023 'history' => 'show command line history',
00024 'server' => 'show current memcached server',
00025 'dumpmcc' => 'shows the whole thing',
00026 'exit' => 'exit mcc',
00027 'quit' => 'exit mcc',
00028 'help' => 'help about a command',
00029 );
00030 if( !$command ) {
00031 $command = 'fullhelp';
00032 }
00033 if( $command === 'fullhelp' ) {
00034 foreach( $commandList as $cmd => $desc ) {
00035 print "$cmd: $desc\n";
00036 }
00037 } elseif( isset( $commandList[$command] ) ) {
00038 print "$command: $commandList[$command]\n";
00039 } else {
00040 print "$command: command does not exist or no help for it\n";
00041 }
00042 }
00043
00044 do {
00045 $bad = false;
00046 $showhelp = false;
00047 $quit = false;
00048
00049 $line = readconsole( '> ' );
00050 if ($line === false) exit;
00051
00052 $args = explode( ' ', $line );
00053 $command = array_shift( $args );
00054
00055
00056 switch ( $command ) {
00057 case 'help':
00058
00059 mccShowHelp(array_shift($args));
00060 break;
00061
00062 case 'get':
00063 $sub = '';
00064 if ( array_key_exists( 1, $args ) ) {
00065 $sub = $args[1];
00066 }
00067 print "Getting {$args[0]}[$sub]\n";
00068 $res = $mcc->get( $args[0] );
00069 if ( array_key_exists( 1, $args ) ) {
00070 $res = $res[$args[1]];
00071 }
00072 if ( $res === false ) {
00073 #print 'Error: ' . $mcc->error_string() . "\n";
00074 print "MemCached error\n";
00075 } elseif ( is_string( $res ) ) {
00076 print "$res\n";
00077 } else {
00078 var_dump( $res );
00079 }
00080 break;
00081
00082 case 'getsock':
00083 $res = $mcc->get( $args[0] );
00084 $sock = $mcc->get_sock( $args[0] );
00085 var_dump( $sock );
00086 break;
00087
00088 case 'server':
00089 if ( $mcc->_single_sock !== null ) {
00090 print $mcc->_single_sock . "\n";
00091 break;
00092 }
00093 $res = $mcc->get( $args[0] );
00094 $hv = $mcc->_hashfunc( $args[0] );
00095 for ( $i = 0; $i < 3; $i++ ) {
00096 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
00097 $hv += $mcc->_hashfunc( $i . $args[0] );
00098 }
00099 break;
00100
00101 case 'set':
00102 $key = array_shift( $args );
00103 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
00104 $value = str_repeat( '*', $args[1] );
00105 } else {
00106 $value = implode( ' ', $args );
00107 }
00108 if ( !$mcc->set( $key, $value, 0 ) ) {
00109 #print 'Error: ' . $mcc->error_string() . "\n";
00110 print "MemCached error\n";
00111 }
00112 break;
00113
00114 case 'delete':
00115 $key = implode( ' ', $args );
00116 if ( !$mcc->delete( $key ) ) {
00117 #print 'Error: ' . $mcc->error_string() . "\n";
00118 print "MemCached error\n";
00119 }
00120 break;
00121
00122 case 'history':
00123 if ( function_exists( 'readline_list_history' ) ) {
00124 foreach( readline_list_history() as $num => $line) {
00125 print "$num: $line\n";
00126 }
00127 } else {
00128 print "readline_list_history() not available\n";
00129 }
00130 break;
00131
00132 case 'dumpmcc':
00133 var_dump( $mcc );
00134 break;
00135
00136 case 'quit':
00137 case 'exit':
00138 $quit = true;
00139 break;
00140
00141 default:
00142 $bad = true;
00143 }
00144
00145 if ( $bad ) {
00146 if ( $command ) {
00147 print "Bad command\n";
00148 }
00149 } else {
00150 if ( function_exists( 'readline_add_history' ) ) {
00151 readline_add_history( $line );
00152 }
00153 }
00154 } while ( !$quit );