Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / maintenance / mcc.php
blob7b7b761445264b834f7ed7192a954b8ed4051171
1 <?php
2 /**
3 * memcached diagnostic tool
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.
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.
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
20 * @file
21 * @todo document
22 * @ingroup Maintenance
25 /** */
26 require_once __DIR__ . '/commandLine.inc';
28 $options = getopt( '', array( 'debug', 'help', 'cache:' ) );
30 $debug = isset( $options['debug'] );
31 $help = isset( $options['help'] );
32 $cache = isset( $options['cache'] ) ? $options['cache'] : null;
34 if ( $help ) {
35 mccShowUsage();
36 exit( 0 );
38 $mcc = new MWMemcached( array(
39 'persistent' => true,
40 'debug' => $debug,
41 ) );
43 if ( $cache ) {
44 if ( !isset( $wgObjectCaches[$cache] ) ) {
45 print "MediaWiki isn't configured with a cache named '$cache'";
46 exit( 1 );
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 );
58 /**
59 * Show this command line tool usage.
61 function mccShowUsage() {
62 echo <<<EOF
63 Usage:
64 mcc.php [--debug]
65 mcc.php --help
67 MemCached Command (mcc) is an interactive command tool that let you interact
68 with the MediaWiki memcached cache.
70 Options:
71 --debug Set debug mode on the memcached connection.
72 --help This help screen.
74 Interactive commands:
76 EOF;
77 print "\t";
78 print str_replace( "\n", "\n\t", mccGetHelp( false ) );
79 print "\n";
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',
96 if ( !$command ) {
97 $command = 'fullhelp';
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 );
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";
110 return $output;
113 do {
114 $bad = false;
115 $showhelp = false;
116 $quit = false;
118 $line = Maintenance::readconsole();
119 if ( $line === false ) {
120 exit;
123 $args = explode( ' ', $line );
124 $command = array_shift( $args );
126 // process command
127 switch ( $command ) {
128 case 'help':
129 // show an help message
130 print mccGetHelp( array_shift( $args ) );
131 break;
133 case 'get':
134 $sub = '';
135 if ( array_key_exists( 1, $args ) ) {
136 $sub = $args[1];
138 print "Getting {$args[0]}[$sub]\n";
139 $res = $mcc->get( $args[0] );
140 if ( array_key_exists( 1, $args ) ) {
141 $res = $res[$args[1]];
143 if ( $res === false ) {
144 # print 'Error: ' . $mcc->error_string() . "\n";
145 print "MemCached error\n";
146 } elseif ( is_string( $res ) ) {
147 print "$res\n";
148 } else {
149 var_dump( $res );
151 break;
153 case 'getsock':
154 $res = $mcc->get( $args[0] );
155 $sock = $mcc->get_sock( $args[0] );
156 var_dump( $sock );
157 break;
159 case 'server':
160 if ( $mcc->_single_sock !== null ) {
161 print $mcc->_single_sock . "\n";
162 break;
164 $res = $mcc->get( $args[0] );
165 $hv = $mcc->_hashfunc( $args[0] );
166 for ( $i = 0; $i < 3; $i++ ) {
167 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
168 $hv += $mcc->_hashfunc( $i . $args[0] );
170 break;
172 case 'set':
173 $key = array_shift( $args );
174 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
175 $value = str_repeat( '*', $args[1] );
176 } else {
177 $value = implode( ' ', $args );
179 if ( !$mcc->set( $key, $value, 0 ) ) {
180 # print 'Error: ' . $mcc->error_string() . "\n";
181 print "MemCached error\n";
183 break;
185 case 'delete':
186 $key = implode( ' ', $args );
187 if ( !$mcc->delete( $key ) ) {
188 # print 'Error: ' . $mcc->error_string() . "\n";
189 print "MemCached error\n";
191 break;
193 case 'history':
194 if ( function_exists( 'readline_list_history' ) ) {
195 foreach ( readline_list_history() as $num => $line ) {
196 print "$num: $line\n";
198 } else {
199 print "readline_list_history() not available\n";
201 break;
203 case 'dumpmcc':
204 var_dump( $mcc );
205 break;
207 case 'quit':
208 case 'exit':
209 $quit = true;
210 break;
212 default:
213 $bad = true;
214 } // switch() end
216 if ( $bad ) {
217 if ( $command ) {
218 print "Bad command\n";
220 } else {
221 if ( function_exists( 'readline_add_history' ) ) {
222 readline_add_history( $line );
225 } while ( !$quit );