Merge " (bug 42004) Enable subpages per default on Project and Help namespaces."
[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 $debug = in_array( '--debug', $argv );
29 $help = in_array( '--help', $argv );
30
31 if( $help ) {
32 mccShowUsage();
33 exit( 0 );
34 }
35 $mcc = new MWMemcached( array(
36 'persistent' => true,
37 'debug' => $debug,
38 ) );
39
40 if ( $wgMainCacheType === CACHE_MEMCACHED ) {
41 $mcc->set_servers( $wgMemCachedServers );
42 } elseif( isset( $wgObjectCaches[$wgMainCacheType] ) ) {
43 $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
44 } else {
45 print "MediaWiki isn't configured for Memcached usage\n";
46 exit( 1 );
47 }
48
49 /**
50 * Show this command line tool usage.
51 */
52 function mccShowUsage() {
53 echo <<<EOF
54 Usage:
55 mcc.php [--debug]
56 mcc.php --help
57
58 MemCached Command (mcc) is an interactive command tool that let you interact
59 with the MediaWiki memcached cache.
60
61 Options:
62 --debug Set debug mode on the memcached connection.
63 --help This help screen.
64
65 Interactive commands:
66
67 EOF;
68 print "\t";
69 print str_replace( "\n", "\n\t", mccGetHelp( false ) );
70 print "\n";
71 }
72
73 function mccGetHelp( $command ) {
74 $output = '';
75 $commandList = array(
76 'get' => 'grabs something',
77 'getsock' => 'lists sockets',
78 'set' => 'changes something',
79 'delete' => 'deletes something',
80 'history' => 'show command line history',
81 'server' => 'show current memcached server',
82 'dumpmcc' => 'shows the whole thing',
83 'exit' => 'exit mcc',
84 'quit' => 'exit mcc',
85 'help' => 'help about a command',
86 );
87 if ( !$command ) {
88 $command = 'fullhelp';
89 }
90 if ( $command === 'fullhelp' ) {
91 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
92 foreach ( $commandList as $cmd => $desc ) {
93 $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
94 }
95 } elseif ( isset( $commandList[$command] ) ) {
96 $output .= "$command: $commandList[$command]\n";
97 } else {
98 $output .= "$command: command does not exist or no help for it\n";
99 }
100
101 return $output;
102 }
103
104 do {
105 $bad = false;
106 $showhelp = false;
107 $quit = false;
108
109 $line = Maintenance::readconsole();
110 if ( $line === false ) exit;
111
112 $args = explode( ' ', $line );
113 $command = array_shift( $args );
114
115 // process command
116 switch ( $command ) {
117 case 'help':
118 // show an help message
119 print mccGetHelp( array_shift( $args ) );
120 break;
121
122 case 'get':
123 $sub = '';
124 if ( array_key_exists( 1, $args ) ) {
125 $sub = $args[1];
126 }
127 print "Getting {$args[0]}[$sub]\n";
128 $res = $mcc->get( $args[0] );
129 if ( array_key_exists( 1, $args ) ) {
130 $res = $res[$args[1]];
131 }
132 if ( $res === false ) {
133 # print 'Error: ' . $mcc->error_string() . "\n";
134 print "MemCached error\n";
135 } elseif ( is_string( $res ) ) {
136 print "$res\n";
137 } else {
138 var_dump( $res );
139 }
140 break;
141
142 case 'getsock':
143 $res = $mcc->get( $args[0] );
144 $sock = $mcc->get_sock( $args[0] );
145 var_dump( $sock );
146 break;
147
148 case 'server':
149 if ( $mcc->_single_sock !== null ) {
150 print $mcc->_single_sock . "\n";
151 break;
152 }
153 $res = $mcc->get( $args[0] );
154 $hv = $mcc->_hashfunc( $args[0] );
155 for ( $i = 0; $i < 3; $i++ ) {
156 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
157 $hv += $mcc->_hashfunc( $i . $args[0] );
158 }
159 break;
160
161 case 'set':
162 $key = array_shift( $args );
163 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
164 $value = str_repeat( '*', $args[1] );
165 } else {
166 $value = implode( ' ', $args );
167 }
168 if ( !$mcc->set( $key, $value, 0 ) ) {
169 # print 'Error: ' . $mcc->error_string() . "\n";
170 print "MemCached error\n";
171 }
172 break;
173
174 case 'delete':
175 $key = implode( ' ', $args );
176 if ( !$mcc->delete( $key ) ) {
177 # print 'Error: ' . $mcc->error_string() . "\n";
178 print "MemCached error\n";
179 }
180 break;
181
182 case 'history':
183 if ( function_exists( 'readline_list_history' ) ) {
184 foreach ( readline_list_history() as $num => $line ) {
185 print "$num: $line\n";
186 }
187 } else {
188 print "readline_list_history() not available\n";
189 }
190 break;
191
192 case 'dumpmcc':
193 var_dump( $mcc );
194 break;
195
196 case 'quit':
197 case 'exit':
198 $quit = true;
199 break;
200
201 default:
202 $bad = true;
203 } // switch() end
204
205 if ( $bad ) {
206 if ( $command ) {
207 print "Bad command\n";
208 }
209 } else {
210 if ( function_exists( 'readline_add_history' ) ) {
211 readline_add_history( $line );
212 }
213 }
214 } while ( !$quit );