Followup r76220: locateExecutableInDefaultPaths() was made static, so use static...
[mediawiki.git] / maintenance / mcc.php
bloba8c79a7259978023bb662fe5aa82819cca2a79e3
1 <?php
2 /**
3 * memcached diagnostic tool
5 * @file
6 * @todo document
7 * @ingroup Maintenance
8 */
10 /** */
11 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
13 $mcc = new MWMemcached( array( 'persistant' => true/*, 'debug' => true*/ ) );
14 $mcc->set_servers( $wgMemCachedServers );
15 # $mcc->set_debug( true );
17 function mccShowHelp( $command ) {
18 $commandList = array(
19 'get' => 'grabs something',
20 'getsock' => 'lists sockets',
21 'set' => 'changes something',
22 'delete' => 'deletes something',
23 'history' => 'show command line history',
24 'server' => 'show current memcached server',
25 'dumpmcc' => 'shows the whole thing',
26 'exit' => 'exit mcc',
27 'quit' => 'exit mcc',
28 'help' => 'help about a command',
30 if ( !$command ) {
31 $command = 'fullhelp';
33 if ( $command === 'fullhelp' ) {
34 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
35 foreach ( $commandList as $cmd => $desc ) {
36 printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
38 } elseif ( isset( $commandList[$command] ) ) {
39 print "$command: $commandList[$command]\n";
40 } else {
41 print "$command: command does not exist or no help for it\n";
45 do {
46 $bad = false;
47 $showhelp = false;
48 $quit = false;
50 $line = Maintenance::readconsole();
51 if ( $line === false ) exit;
53 $args = explode( ' ', $line );
54 $command = array_shift( $args );
56 // process command
57 switch ( $command ) {
58 case 'help':
59 // show an help message
60 mccShowHelp( array_shift( $args ) );
61 break;
63 case 'get':
64 $sub = '';
65 if ( array_key_exists( 1, $args ) ) {
66 $sub = $args[1];
68 print "Getting {$args[0]}[$sub]\n";
69 $res = $mcc->get( $args[0] );
70 if ( array_key_exists( 1, $args ) ) {
71 $res = $res[$args[1]];
73 if ( $res === false ) {
74 # print 'Error: ' . $mcc->error_string() . "\n";
75 print "MemCached error\n";
76 } elseif ( is_string( $res ) ) {
77 print "$res\n";
78 } else {
79 var_dump( $res );
81 break;
83 case 'getsock':
84 $res = $mcc->get( $args[0] );
85 $sock = $mcc->get_sock( $args[0] );
86 var_dump( $sock );
87 break;
89 case 'server':
90 if ( $mcc->_single_sock !== null ) {
91 print $mcc->_single_sock . "\n";
92 break;
94 $res = $mcc->get( $args[0] );
95 $hv = $mcc->_hashfunc( $args[0] );
96 for ( $i = 0; $i < 3; $i++ ) {
97 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
98 $hv += $mcc->_hashfunc( $i . $args[0] );
100 break;
102 case 'set':
103 $key = array_shift( $args );
104 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
105 $value = str_repeat( '*', $args[1] );
106 } else {
107 $value = implode( ' ', $args );
109 if ( !$mcc->set( $key, $value, 0 ) ) {
110 # print 'Error: ' . $mcc->error_string() . "\n";
111 print "MemCached error\n";
113 break;
115 case 'delete':
116 $key = implode( ' ', $args );
117 if ( !$mcc->delete( $key ) ) {
118 # print 'Error: ' . $mcc->error_string() . "\n";
119 print "MemCached error\n";
121 break;
123 case 'history':
124 if ( function_exists( 'readline_list_history' ) ) {
125 foreach ( readline_list_history() as $num => $line ) {
126 print "$num: $line\n";
128 } else {
129 print "readline_list_history() not available\n";
131 break;
133 case 'dumpmcc':
134 var_dump( $mcc );
135 break;
137 case 'quit':
138 case 'exit':
139 $quit = true;
140 break;
142 default:
143 $bad = true;
144 } // switch() end
146 if ( $bad ) {
147 if ( $command ) {
148 print "Bad command\n";
150 } else {
151 if ( function_exists( 'readline_add_history' ) ) {
152 readline_add_history( $line );
155 } while ( !$quit );