]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/mcc.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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 = max( array_map( 'strlen', array_keys( $commandList ) ) );
35                 foreach ( $commandList as $cmd => $desc ) {
36                         printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
37                 }
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";
42         }
43 }
44
45 do {
46         $bad = false;
47         $showhelp = false;
48         $quit = false;
49
50         $line = Maintenance::readconsole();
51         if ( $line === false ) exit;
52
53         $args = explode( ' ', $line );
54         $command = array_shift( $args );
55
56         // process command
57         switch ( $command ) {
58                 case 'help':
59                         // show an help message
60                         mccShowHelp( array_shift( $args ) );
61                 break;
62
63                 case 'get':
64                         $sub = '';
65                         if ( array_key_exists( 1, $args ) ) {
66                                 $sub = $args[1];
67                         }
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]];
72                         }
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 );
80                         }
81                 break;
82
83                 case 'getsock':
84                         $res = $mcc->get( $args[0] );
85                         $sock = $mcc->get_sock( $args[0] );
86                         var_dump( $sock );
87                         break;
88
89                 case 'server':
90                         if ( $mcc->_single_sock !== null ) {
91                                 print $mcc->_single_sock . "\n";
92                                 break;
93                         }
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] );
99                         }
100                         break;
101
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 );
108                         }
109                         if ( !$mcc->set( $key, $value, 0 ) ) {
110                                 # print 'Error: ' . $mcc->error_string() . "\n";
111                                 print "MemCached error\n";
112                         }
113                         break;
114
115                 case 'delete':
116                         $key = implode( ' ', $args );
117                         if ( !$mcc->delete( $key ) ) {
118                                 # print 'Error: ' . $mcc->error_string() . "\n";
119                                 print "MemCached error\n";
120                         }
121                         break;
122
123                 case 'history':
124                         if ( function_exists( 'readline_list_history' ) ) {
125                                 foreach ( readline_list_history() as $num => $line ) {
126                                         print "$num: $line\n";
127                                 }
128                         } else {
129                                 print "readline_list_history() not available\n";
130                         }
131                         break;
132
133                 case 'dumpmcc':
134                         var_dump( $mcc );
135                         break;
136
137                 case 'quit':
138                 case 'exit':
139                         $quit = true;
140                         break;
141
142                 default:
143                         $bad = true;
144         } // switch() end
145
146         if ( $bad ) {
147                 if ( $command ) {
148                         print "Bad command\n";
149                 }
150         } else {
151                 if ( function_exists( 'readline_add_history' ) ) {
152                         readline_add_history( $line );
153                 }
154         }
155 } while ( !$quit );