Some more editor files to ignore.
[lhc/web/wiklou.git] / maintenance / mcc.php
1 <?php
2 /**
3 * memcached diagnostic tool
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @todo document
22 * @ingroup Maintenance
23 */
24
25 /** */
26 require_once( __DIR__ . '/commandLine.inc' );
27
28 $options = getopt( '', array( 'debug', 'help', 'cache:' ) );
29
30 $debug = isset( $options['debug'] );
31 $help = isset( $options['help'] );
32 $cache = isset( $options['cache'] ) ? $options['cache'] : null;
33
34 if ( $help ) {
35 mccShowUsage();
36 exit( 0 );
37 }
38 $mcc = new MWMemcached( array(
39 'persistent' => true,
40 'debug' => $debug,
41 ) );
42
43 if ( $cache ) {
44 if ( !isset( $wgObjectCaches[$cache] ) ) {
45 print "MediaWiki isn't configured with a cache named '$cache'";
46 exit( 1 );
47 }
48 $servers = $wgObjectCaches[$cache]['servers'];
49 } elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
50 $mcc->set_servers( $wgMemCachedServers );
51 } elseif( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
52 $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
53 } else {
54 print "MediaWiki isn't configured for Memcached usage\n";
55 exit( 1 );
56 }
57
58 /**
59 * Show this command line tool usage.
60 */
61 function mccShowUsage() {
62 echo <<<EOF
63 Usage:
64 mcc.php [--debug]
65 mcc.php --help
66
67 MemCached Command (mcc) is an interactive command tool that let you interact
68 with the MediaWiki memcached cache.
69
70 Options:
71 --debug Set debug mode on the memcached connection.
72 --help This help screen.
73
74 Interactive commands:
75
76 EOF;
77 print "\t";
78 print str_replace( "\n", "\n\t", mccGetHelp( false ) );
79 print "\n";
80 }
81
82 function mccGetHelp( $command ) {
83 $output = '';
84 $commandList = array(
85 'get' => 'grabs something',
86 'getsock' => 'lists sockets',
87 'set' => 'changes something',
88 'delete' => 'deletes something',
89 'history' => 'show command line history',
90 'server' => 'show current memcached server',
91 'dumpmcc' => 'shows the whole thing',
92 'exit' => 'exit mcc',
93 'quit' => 'exit mcc',
94 'help' => 'help about a command',
95 );
96 if ( !$command ) {
97 $command = 'fullhelp';
98 }
99 if ( $command === 'fullhelp' ) {
100 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
101 foreach ( $commandList as $cmd => $desc ) {
102 $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
103 }
104 } elseif ( isset( $commandList[$command] ) ) {
105 $output .= "$command: $commandList[$command]\n";
106 } else {
107 $output .= "$command: command does not exist or no help for it\n";
108 }
109
110 return $output;
111 }
112
113 do {
114 $bad = false;
115 $showhelp = false;
116 $quit = false;
117
118 $line = Maintenance::readconsole();
119 if ( $line === false ) exit;
120
121 $args = explode( ' ', $line );
122 $command = array_shift( $args );
123
124 // process command
125 switch ( $command ) {
126 case 'help':
127 // show an help message
128 print mccGetHelp( array_shift( $args ) );
129 break;
130
131 case 'get':
132 $sub = '';
133 if ( array_key_exists( 1, $args ) ) {
134 $sub = $args[1];
135 }
136 print "Getting {$args[0]}[$sub]\n";
137 $res = $mcc->get( $args[0] );
138 if ( array_key_exists( 1, $args ) ) {
139 $res = $res[$args[1]];
140 }
141 if ( $res === false ) {
142 # print 'Error: ' . $mcc->error_string() . "\n";
143 print "MemCached error\n";
144 } elseif ( is_string( $res ) ) {
145 print "$res\n";
146 } else {
147 var_dump( $res );
148 }
149 break;
150
151 case 'getsock':
152 $res = $mcc->get( $args[0] );
153 $sock = $mcc->get_sock( $args[0] );
154 var_dump( $sock );
155 break;
156
157 case 'server':
158 if ( $mcc->_single_sock !== null ) {
159 print $mcc->_single_sock . "\n";
160 break;
161 }
162 $res = $mcc->get( $args[0] );
163 $hv = $mcc->_hashfunc( $args[0] );
164 for ( $i = 0; $i < 3; $i++ ) {
165 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
166 $hv += $mcc->_hashfunc( $i . $args[0] );
167 }
168 break;
169
170 case 'set':
171 $key = array_shift( $args );
172 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
173 $value = str_repeat( '*', $args[1] );
174 } else {
175 $value = implode( ' ', $args );
176 }
177 if ( !$mcc->set( $key, $value, 0 ) ) {
178 # print 'Error: ' . $mcc->error_string() . "\n";
179 print "MemCached error\n";
180 }
181 break;
182
183 case 'delete':
184 $key = implode( ' ', $args );
185 if ( !$mcc->delete( $key ) ) {
186 # print 'Error: ' . $mcc->error_string() . "\n";
187 print "MemCached error\n";
188 }
189 break;
190
191 case 'history':
192 if ( function_exists( 'readline_list_history' ) ) {
193 foreach ( readline_list_history() as $num => $line ) {
194 print "$num: $line\n";
195 }
196 } else {
197 print "readline_list_history() not available\n";
198 }
199 break;
200
201 case 'dumpmcc':
202 var_dump( $mcc );
203 break;
204
205 case 'quit':
206 case 'exit':
207 $quit = true;
208 break;
209
210 default:
211 $bad = true;
212 } // switch() end
213
214 if ( $bad ) {
215 if ( $command ) {
216 print "Bad command\n";
217 }
218 } else {
219 if ( function_exists( 'readline_add_history' ) ) {
220 readline_add_history( $line );
221 }
222 }
223 } while ( !$quit );