MediaWiki  REL1_21
mcc.php
Go to the documentation of this file.
00001 <?php
00026 require_once( __DIR__ . '/commandLine.inc' );
00027 
00028 $options = getopt( '', array( 'debug', 'help', 'cache:' ) );
00029 
00030 $debug = isset( $options['debug'] );
00031 $help = isset( $options['help'] );
00032 $cache = isset( $options['cache'] ) ? $options['cache'] : null;
00033 
00034 if ( $help ) {
00035         mccShowUsage();
00036         exit( 0 );
00037 }
00038 $mcc = new MWMemcached( array(
00039         'persistent' => true,
00040         'debug' => $debug,
00041 ) );
00042 
00043 if ( $cache ) {
00044         if ( !isset( $wgObjectCaches[$cache] ) ) {
00045                 print "MediaWiki isn't configured with a cache named '$cache'";
00046                 exit( 1 );
00047         }
00048         $servers = $wgObjectCaches[$cache]['servers'];
00049 } elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
00050         $mcc->set_servers( $wgMemCachedServers );
00051 } elseif( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
00052         $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
00053 } else {
00054         print "MediaWiki isn't configured for Memcached usage\n";
00055         exit( 1 );
00056 }
00057 
00061 function mccShowUsage() {
00062         echo <<<EOF
00063 Usage:
00064         mcc.php [--debug]
00065         mcc.php --help
00066 
00067 MemCached Command (mcc) is an interactive command tool that let you interact
00068 with the MediaWiki memcached cache.
00069 
00070 Options:
00071         --debug Set debug mode on the memcached connection.
00072         --help  This help screen.
00073 
00074 Interactive commands:
00075 
00076 EOF;
00077         print "\t";
00078         print str_replace( "\n", "\n\t", mccGetHelp( false ) );
00079         print "\n";
00080 }
00081 
00082 function mccGetHelp( $command ) {
00083         $output = '';
00084         $commandList = array(
00085                 'get' => 'grabs something',
00086                 'getsock' => 'lists sockets',
00087                 'set' => 'changes something',
00088                 'delete' => 'deletes something',
00089                 'history' => 'show command line history',
00090                 'server' => 'show current memcached server',
00091                 'dumpmcc' => 'shows the whole thing',
00092                 'exit' => 'exit mcc',
00093                 'quit' => 'exit mcc',
00094                 'help' => 'help about a command',
00095         );
00096         if ( !$command ) {
00097                 $command = 'fullhelp';
00098         }
00099         if ( $command === 'fullhelp' ) {
00100                 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
00101                 foreach ( $commandList as $cmd => $desc ) {
00102                         $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
00103                 }
00104         } elseif ( isset( $commandList[$command] ) ) {
00105                 $output .= "$command: $commandList[$command]\n";
00106         } else {
00107                 $output .= "$command: command does not exist or no help for it\n";
00108         }
00109 
00110         return $output;
00111 }
00112 
00113 do {
00114         $bad = false;
00115         $showhelp = false;
00116         $quit = false;
00117 
00118         $line = Maintenance::readconsole();
00119         if ( $line === false ) exit;
00120 
00121         $args = explode( ' ', $line );
00122         $command = array_shift( $args );
00123 
00124         // process command
00125         switch ( $command ) {
00126                 case 'help':
00127                         // show an help message
00128                         print mccGetHelp( array_shift( $args ) );
00129                         break;
00130 
00131                 case 'get':
00132                         $sub = '';
00133                         if ( array_key_exists( 1, $args ) ) {
00134                                 $sub = $args[1];
00135                         }
00136                         print "Getting {$args[0]}[$sub]\n";
00137                         $res = $mcc->get( $args[0] );
00138                         if ( array_key_exists( 1, $args ) ) {
00139                                 $res = $res[$args[1]];
00140                         }
00141                         if ( $res === false ) {
00142                                 # print 'Error: ' . $mcc->error_string() . "\n";
00143                                 print "MemCached error\n";
00144                         } elseif ( is_string( $res ) ) {
00145                                 print "$res\n";
00146                         } else {
00147                                 var_dump( $res );
00148                         }
00149                         break;
00150 
00151                 case 'getsock':
00152                         $res = $mcc->get( $args[0] );
00153                         $sock = $mcc->get_sock( $args[0] );
00154                         var_dump( $sock );
00155                         break;
00156 
00157                 case 'server':
00158                         if ( $mcc->_single_sock !== null ) {
00159                                 print $mcc->_single_sock . "\n";
00160                                 break;
00161                         }
00162                         $res = $mcc->get( $args[0] );
00163                         $hv = $mcc->_hashfunc( $args[0] );
00164                         for ( $i = 0; $i < 3; $i++ ) {
00165                                 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
00166                                 $hv += $mcc->_hashfunc( $i . $args[0] );
00167                         }
00168                         break;
00169 
00170                 case 'set':
00171                         $key = array_shift( $args );
00172                         if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
00173                                 $value = str_repeat( '*', $args[1] );
00174                         } else {
00175                                 $value = implode( ' ', $args );
00176                         }
00177                         if ( !$mcc->set( $key, $value, 0 ) ) {
00178                                 # print 'Error: ' . $mcc->error_string() . "\n";
00179                                 print "MemCached error\n";
00180                         }
00181                         break;
00182 
00183                 case 'delete':
00184                         $key = implode( ' ', $args );
00185                         if ( !$mcc->delete( $key ) ) {
00186                                 # print 'Error: ' . $mcc->error_string() . "\n";
00187                                 print "MemCached error\n";
00188                         }
00189                         break;
00190 
00191                 case 'history':
00192                         if ( function_exists( 'readline_list_history' ) ) {
00193                                 foreach ( readline_list_history() as $num => $line ) {
00194                                         print "$num: $line\n";
00195                                 }
00196                         } else {
00197                                 print "readline_list_history() not available\n";
00198                         }
00199                         break;
00200 
00201                 case 'dumpmcc':
00202                         var_dump( $mcc );
00203                         break;
00204 
00205                 case 'quit':
00206                 case 'exit':
00207                         $quit = true;
00208                         break;
00209 
00210                 default:
00211                         $bad = true;
00212         } // switch() end
00213 
00214         if ( $bad ) {
00215                 if ( $command ) {
00216                         print "Bad command\n";
00217                 }
00218         } else {
00219                 if ( function_exists( 'readline_add_history' ) ) {
00220                         readline_add_history( $line );
00221                 }
00222         }
00223 } while ( !$quit );