Remove unused global $IP
[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 foreach ( $commandList as $cmd => $desc ) {
35 print "$cmd: $desc\n";
36 }
37 } elseif ( isset( $commandList[$command] ) ) {
38 print "$command: $commandList[$command]\n";
39 } else {
40 print "$command: command does not exist or no help for it\n";
41 }
42 }
43
44 do {
45 $bad = false;
46 $showhelp = false;
47 $quit = false;
48
49 $line = readconsole( '> ' );
50 if ( $line === false ) exit;
51
52 $args = explode( ' ', $line );
53 $command = array_shift( $args );
54
55 // process command
56 switch ( $command ) {
57 case 'help':
58 // show an help message
59 mccShowHelp( array_shift( $args ) );
60 break;
61
62 case 'get':
63 $sub = '';
64 if ( array_key_exists( 1, $args ) ) {
65 $sub = $args[1];
66 }
67 print "Getting {$args[0]}[$sub]\n";
68 $res = $mcc->get( $args[0] );
69 if ( array_key_exists( 1, $args ) ) {
70 $res = $res[$args[1]];
71 }
72 if ( $res === false ) {
73 # print 'Error: ' . $mcc->error_string() . "\n";
74 print "MemCached error\n";
75 } elseif ( is_string( $res ) ) {
76 print "$res\n";
77 } else {
78 var_dump( $res );
79 }
80 break;
81
82 case 'getsock':
83 $res = $mcc->get( $args[0] );
84 $sock = $mcc->get_sock( $args[0] );
85 var_dump( $sock );
86 break;
87
88 case 'server':
89 if ( $mcc->_single_sock !== null ) {
90 print $mcc->_single_sock . "\n";
91 break;
92 }
93 $res = $mcc->get( $args[0] );
94 $hv = $mcc->_hashfunc( $args[0] );
95 for ( $i = 0; $i < 3; $i++ ) {
96 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
97 $hv += $mcc->_hashfunc( $i . $args[0] );
98 }
99 break;
100
101 case 'set':
102 $key = array_shift( $args );
103 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
104 $value = str_repeat( '*', $args[1] );
105 } else {
106 $value = implode( ' ', $args );
107 }
108 if ( !$mcc->set( $key, $value, 0 ) ) {
109 # print 'Error: ' . $mcc->error_string() . "\n";
110 print "MemCached error\n";
111 }
112 break;
113
114 case 'delete':
115 $key = implode( ' ', $args );
116 if ( !$mcc->delete( $key ) ) {
117 # print 'Error: ' . $mcc->error_string() . "\n";
118 print "MemCached error\n";
119 }
120 break;
121
122 case 'history':
123 if ( function_exists( 'readline_list_history' ) ) {
124 foreach ( readline_list_history() as $num => $line ) {
125 print "$num: $line\n";
126 }
127 } else {
128 print "readline_list_history() not available\n";
129 }
130 break;
131
132 case 'dumpmcc':
133 var_dump( $mcc );
134 break;
135
136 case 'quit':
137 case 'exit':
138 $quit = true;
139 break;
140
141 default:
142 $bad = true;
143 } // switch() end
144
145 if ( $bad ) {
146 if ( $command ) {
147 print "Bad command\n";
148 }
149 } else {
150 if ( function_exists( 'readline_add_history' ) ) {
151 readline_add_history( $line );
152 }
153 }
154 } while ( !$quit );