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