justify help memcached console help
[lhc/web/wiklou.git] / maintenance / mcc.php
1 <?php
2 /**
3 * memcached diagnostic tool
4 *
5 * @file
6 * @todo document
7 * @ingroup Maintenance
8 */
9
10 /** */
11 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
12
13 $mcc = new MWMemcached( array( 'persistant' => true/*, 'debug' => true*/ ) );
14 $mcc->set_servers( $wgMemCachedServers );
15 # $mcc->set_debug( true );
16
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',
29 );
30 if ( !$command ) {
31 $command = 'fullhelp';
32 }
33 if ( $command === 'fullhelp' ) {
34 $max_cmd_len = 0;
35 foreach( array_keys( $commandList ) as $cmd ) {
36 $max_cmd_len = max( $max_cmd_len, strlen($cmd) );
37 }
38 foreach ( $commandList as $cmd => $desc ) {
39 printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
40 }
41 } elseif ( isset( $commandList[$command] ) ) {
42 print "$command: $commandList[$command]\n";
43 } else {
44 print "$command: command does not exist or no help for it\n";
45 }
46 }
47
48 do {
49 $bad = false;
50 $showhelp = false;
51 $quit = false;
52
53 $line = Maintenance::readconsole();
54 if ( $line === false ) exit;
55
56 $args = explode( ' ', $line );
57 $command = array_shift( $args );
58
59 // process command
60 switch ( $command ) {
61 case 'help':
62 // show an help message
63 mccShowHelp( array_shift( $args ) );
64 break;
65
66 case 'get':
67 $sub = '';
68 if ( array_key_exists( 1, $args ) ) {
69 $sub = $args[1];
70 }
71 print "Getting {$args[0]}[$sub]\n";
72 $res = $mcc->get( $args[0] );
73 if ( array_key_exists( 1, $args ) ) {
74 $res = $res[$args[1]];
75 }
76 if ( $res === false ) {
77 # print 'Error: ' . $mcc->error_string() . "\n";
78 print "MemCached error\n";
79 } elseif ( is_string( $res ) ) {
80 print "$res\n";
81 } else {
82 var_dump( $res );
83 }
84 break;
85
86 case 'getsock':
87 $res = $mcc->get( $args[0] );
88 $sock = $mcc->get_sock( $args[0] );
89 var_dump( $sock );
90 break;
91
92 case 'server':
93 if ( $mcc->_single_sock !== null ) {
94 print $mcc->_single_sock . "\n";
95 break;
96 }
97 $res = $mcc->get( $args[0] );
98 $hv = $mcc->_hashfunc( $args[0] );
99 for ( $i = 0; $i < 3; $i++ ) {
100 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
101 $hv += $mcc->_hashfunc( $i . $args[0] );
102 }
103 break;
104
105 case 'set':
106 $key = array_shift( $args );
107 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
108 $value = str_repeat( '*', $args[1] );
109 } else {
110 $value = implode( ' ', $args );
111 }
112 if ( !$mcc->set( $key, $value, 0 ) ) {
113 # print 'Error: ' . $mcc->error_string() . "\n";
114 print "MemCached error\n";
115 }
116 break;
117
118 case 'delete':
119 $key = implode( ' ', $args );
120 if ( !$mcc->delete( $key ) ) {
121 # print 'Error: ' . $mcc->error_string() . "\n";
122 print "MemCached error\n";
123 }
124 break;
125
126 case 'history':
127 if ( function_exists( 'readline_list_history' ) ) {
128 foreach ( readline_list_history() as $num => $line ) {
129 print "$num: $line\n";
130 }
131 } else {
132 print "readline_list_history() not available\n";
133 }
134 break;
135
136 case 'dumpmcc':
137 var_dump( $mcc );
138 break;
139
140 case 'quit':
141 case 'exit':
142 $quit = true;
143 break;
144
145 default:
146 $bad = true;
147 } // switch() end
148
149 if ( $bad ) {
150 if ( $command ) {
151 print "Bad command\n";
152 }
153 } else {
154 if ( function_exists( 'readline_add_history' ) ) {
155 readline_add_history( $line );
156 }
157 }
158 } while ( !$quit );