implement 'history' command
[lhc/web/wiklou.git] / maintenance / mcc.php
1 <?php
2 /**
3 * memcached diagnostic tool
4 *
5 * @todo document
6 * @package MediaWiki
7 * @subpackage Maintenance
8 */
9
10 /** */
11 require_once( 'commandLine.inc' );
12 require_once( 'memcached-client.php' );
13
14 $mcc = new memcached( array('persistant' => true, 'debug' => true) );
15 $mcc->set_servers( $wgMemCachedServers );
16 $mcc->set_debug( true );
17
18 function mccShowHelp($command) {
19
20 if(! $command ) { $command = 'fullhelp'; }
21 $onlyone = true;
22
23 switch ( $command ) {
24
25 case 'fullhelp':
26 // will show help for all commands
27 $onlyone = false;
28
29 case 'get':
30 print "get: grabs something\n";
31 if($onlyone) { break; }
32
33 case 'getsock':
34 print "getsock: lists sockets\n";
35 if($onlyone) { break; }
36
37 case 'set':
38 print "set: changes something\n";
39 if($onlyone) { break; }
40
41 case 'delete':
42 print "delete: deletes something\n";
43 if($onlyone) { break; }
44
45 case 'history':
46 print "history: show command line history\n";
47 if($onlyone) { break; }
48
49 case 'dumpmcc':
50 print "dumpmcc: shows the whole thing\n";
51 if($onlyone) { break; }
52
53 case 'exit':
54 case 'quit':
55 print "exit or quit: exit mcc\n";
56 if($onlyone) { break; }
57
58 case 'help':
59 print "help: help about a command\n";
60 if($onlyone) { break; }
61
62 default:
63 if($onlyone) {
64 print "$command: command does not exist or no help for it\n";
65 }
66 }
67 }
68
69 do {
70 $bad = false;
71 $showhelp = false;
72 $quit = false;
73
74 $line = readconsole( '> ' );
75 if ($line === false) exit;
76
77 $args = explode( ' ', $line );
78 $command = array_shift( $args );
79
80 // process command
81 switch ( $command ) {
82 case 'help':
83 // show an help message
84 mccShowHelp(array_shift($args));
85 break;
86
87 case 'get':
88 print "Getting {$args[0]}[{$args[1]}]\n";
89 $res = $mcc->get( $args[0] );
90 if ( array_key_exists( 1, $args ) ) {
91 $res = $res[$args[1]];
92 }
93 if ( $res === false ) {
94 #print 'Error: ' . $mcc->error_string() . "\n";
95 print "MemCached error\n";
96 } elseif ( is_string( $res ) ) {
97 print "$res\n";
98 } else {
99 var_dump( $res );
100 }
101 break;
102
103 case 'getsock':
104 $res = $mcc->get( $args[0] );
105 $sock = $mcc->get_sock( $args[0] );
106 var_dump( $sock );
107 break;
108
109 case 'set':
110 $key = array_shift( $args );
111 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
112 $value = str_repeat( '*', $args[1] );
113 } else {
114 $value = implode( ' ', $args );
115 }
116 if ( !$mcc->set( $key, $value, 0 ) ) {
117 #print 'Error: ' . $mcc->error_string() . "\n";
118 print "MemCached error\n";
119 }
120 break;
121
122 case 'delete':
123 $key = implode( ' ', $args );
124 if ( !$mcc->delete( $key ) ) {
125 #print 'Error: ' . $mcc->error_string() . "\n";
126 print "MemCached error\n";
127 }
128 break;
129
130 case 'history':
131 if ( function_exists( 'readline_list_history' ) ) {
132 print_r(readline_list_history());
133 } else {
134 print "readline_list_history() not available\n";
135 }
136 break;
137
138 case 'dumpmcc':
139 var_dump( $mcc );
140 break;
141
142 case 'quit':
143 case 'exit':
144 $quit = true;
145 break;
146
147 default:
148 $bad = true;
149 } // switch() end
150
151 if ( $bad ) {
152 if ( $command ) {
153 print "Bad command\n";
154 }
155 } else {
156 if ( function_exists( 'readline_add_history' ) ) {
157 readline_add_history( $line );
158 }
159 }
160 } while ( !$quit );
161
162 ?>