MediaWiki  REL1_22
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 ) {
00120         exit;
00121     }
00122 
00123     $args = explode( ' ', $line );
00124     $command = array_shift( $args );
00125 
00126     // process command
00127     switch ( $command ) {
00128         case 'help':
00129             // show an help message
00130             print mccGetHelp( array_shift( $args ) );
00131             break;
00132 
00133         case 'get':
00134             $sub = '';
00135             if ( array_key_exists( 1, $args ) ) {
00136                 $sub = $args[1];
00137             }
00138             print "Getting {$args[0]}[$sub]\n";
00139             $res = $mcc->get( $args[0] );
00140             if ( array_key_exists( 1, $args ) ) {
00141                 $res = $res[$args[1]];
00142             }
00143             if ( $res === false ) {
00144                 # print 'Error: ' . $mcc->error_string() . "\n";
00145                 print "MemCached error\n";
00146             } elseif ( is_string( $res ) ) {
00147                 print "$res\n";
00148             } else {
00149                 var_dump( $res );
00150             }
00151             break;
00152 
00153         case 'getsock':
00154             $res = $mcc->get( $args[0] );
00155             $sock = $mcc->get_sock( $args[0] );
00156             var_dump( $sock );
00157             break;
00158 
00159         case 'server':
00160             if ( $mcc->_single_sock !== null ) {
00161                 print $mcc->_single_sock . "\n";
00162                 break;
00163             }
00164             $res = $mcc->get( $args[0] );
00165             $hv = $mcc->_hashfunc( $args[0] );
00166             for ( $i = 0; $i < 3; $i++ ) {
00167                 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
00168                 $hv += $mcc->_hashfunc( $i . $args[0] );
00169             }
00170             break;
00171 
00172         case 'set':
00173             $key = array_shift( $args );
00174             if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
00175                 $value = str_repeat( '*', $args[1] );
00176             } else {
00177                 $value = implode( ' ', $args );
00178             }
00179             if ( !$mcc->set( $key, $value, 0 ) ) {
00180                 # print 'Error: ' . $mcc->error_string() . "\n";
00181                 print "MemCached error\n";
00182             }
00183             break;
00184 
00185         case 'delete':
00186             $key = implode( ' ', $args );
00187             if ( !$mcc->delete( $key ) ) {
00188                 # print 'Error: ' . $mcc->error_string() . "\n";
00189                 print "MemCached error\n";
00190             }
00191             break;
00192 
00193         case 'history':
00194             if ( function_exists( 'readline_list_history' ) ) {
00195                 foreach ( readline_list_history() as $num => $line ) {
00196                     print "$num: $line\n";
00197                 }
00198             } else {
00199                 print "readline_list_history() not available\n";
00200             }
00201             break;
00202 
00203         case 'dumpmcc':
00204             var_dump( $mcc );
00205             break;
00206 
00207         case 'quit':
00208         case 'exit':
00209             $quit = true;
00210             break;
00211 
00212         default:
00213             $bad = true;
00214     } // switch() end
00215 
00216     if ( $bad ) {
00217         if ( $command ) {
00218             print "Bad command\n";
00219         }
00220     } else {
00221         if ( function_exists( 'readline_add_history' ) ) {
00222             readline_add_history( $line );
00223         }
00224     }
00225 } while ( !$quit );