MediaWiki REL1_35
mcc.php
Go to the documentation of this file.
1<?php
25$optionsWithArgs = [ 'cache' ];
27 'debug', 'help'
28];
29require_once __DIR__ . '/commandLine.inc';
30
31$debug = isset( $options['debug'] );
32$help = isset( $options['help'] );
33$cache = $options['cache'] ?? null;
34
35if ( $help ) {
37 exit( 0 );
38}
40 'persistent' => true,
41 'debug' => $debug,
42] );
43
44if ( $cache ) {
45 if ( !isset( $wgObjectCaches[$cache] ) ) {
46 print "MediaWiki isn't configured with a cache named '$cache'";
47 exit( 1 );
48 }
49 $servers = $wgObjectCaches[$cache]['servers'];
50} elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
51 $mcc->set_servers( $wgMemCachedServers );
52} elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
53 $mcc->set_servers( $wgObjectCaches[$wgMainCacheType]['servers'] );
54} else {
55 print "MediaWiki isn't configured for Memcached usage\n";
56 exit( 1 );
57}
58
62function mccShowUsage() {
63 echo <<<EOF
64Usage:
65 mcc.php [--debug]
66 mcc.php --help
67
68MemCached Command (mcc) is an interactive command tool that let you interact
69with the MediaWiki memcached cache.
70
71Options:
72 --debug Set debug mode on the memcached connection.
73 --help This help screen.
74
75Interactive commands:
76
77EOF;
78 print "\t";
79 print str_replace( "\n", "\n\t", mccGetHelp( false ) );
80 print "\n";
81}
82
83function mccGetHelp( $command ) {
84 $output = '';
85 $commandList = [
86 'get' => 'grabs something',
87 'getsock' => 'lists sockets',
88 'set' => 'changes something',
89 'delete' => 'deletes something',
90 'history' => 'show command line history',
91 'server' => 'show current memcached server',
92 'dumpmcc' => 'shows the whole thing',
93 'exit' => 'exit mcc',
94 'quit' => 'exit mcc',
95 'help' => 'help about a command',
96 ];
97 if ( !$command ) {
98 $command = 'fullhelp';
99 }
100 if ( $command === 'fullhelp' ) {
101 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
102 foreach ( $commandList as $cmd => $desc ) {
103 $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
104 }
105 } elseif ( isset( $commandList[$command] ) ) {
106 $output .= "$command: $commandList[$command]\n";
107 } else {
108 $output .= "$command: command does not exist or no help for it\n";
109 }
110
111 return $output;
112}
113
114do {
115 $bad = false;
116 $showhelp = false;
117 $quit = false;
118
120 if ( $line === false ) {
121 exit;
122 }
123
124 $args = explode( ' ', $line );
125 $command = array_shift( $args );
126
127 // process command
128 switch ( $command ) {
129 case 'help':
130 // show an help message
131 print mccGetHelp( array_shift( $args ) );
132 break;
133
134 case 'get':
135 $sub = '';
136 if ( array_key_exists( 1, $args ) ) {
137 $sub = $args[1];
138 }
139 print "Getting {$args[0]}[$sub]\n";
140 $res = $mcc->get( $args[0] );
141 if ( array_key_exists( 1, $args ) ) {
142 $res = $res[$args[1]];
143 }
144 if ( $res === false ) {
145 # print 'Error: ' . $mcc->error_string() . "\n";
146 print "MemCached error\n";
147 } elseif ( is_string( $res ) ) {
148 print "$res\n";
149 } else {
150 var_dump( $res );
151 }
152 break;
153
154 case 'getsock':
155 $res = $mcc->get( $args[0] );
156 $sock = $mcc->get_sock( $args[0] );
157 var_dump( $sock );
158 break;
159
160 case 'server':
161 if ( $mcc->_single_sock !== null ) {
162 print $mcc->_single_sock . "\n";
163 break;
164 }
165 $res = $mcc->get( $args[0] );
166 $hv = $mcc->_hashfunc( $args[0] );
167 for ( $i = 0; $i < 3; $i++ ) {
168 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
169 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
170 $hv += $mcc->_hashfunc( $i . $args[0] );
171 }
172 break;
173
174 case 'set':
175 $key = array_shift( $args );
176 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
177 $value = str_repeat( '*', $args[1] );
178 } else {
179 $value = implode( ' ', $args );
180 }
181 if ( !$mcc->set( $key, $value, 0 ) ) {
182 # print 'Error: ' . $mcc->error_string() . "\n";
183 print "MemCached error\n";
184 }
185 break;
186
187 case 'delete':
188 $key = implode( ' ', $args );
189 if ( !$mcc->delete( $key ) ) {
190 # print 'Error: ' . $mcc->error_string() . "\n";
191 print "MemCached error\n";
192 }
193 break;
194
195 case 'history':
196 if ( function_exists( 'readline_list_history' ) ) {
197 foreach ( readline_list_history() as $num => $line ) {
198 print "$num: $line\n";
199 }
200 } else {
201 print "readline_list_history() not available\n";
202 }
203 break;
204
205 case 'dumpmcc':
206 var_dump( $mcc );
207 break;
208
209 case 'quit':
210 case 'exit':
211 $quit = true;
212 break;
213
214 default:
215 $bad = true;
216 }
217
218 if ( $bad ) {
219 if ( $command ) {
220 print "Bad command\n";
221 }
222 } else {
223 if ( function_exists( 'readline_add_history' ) ) {
224 readline_add_history( $line );
225 }
226 }
227} while ( !$quit );
$wgObjectCaches
Advanced object cache configuration.
$wgMainCacheType
Main cache type.
$wgMemCachedServers
The list of MemCached servers and port numbers.
static readconsole( $prompt='> ')
Prompt the console for input.
memcached client class implemented using (p)fsockopen()
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
const CACHE_MEMCACHED
Definition Defines.php:94
$optionsWithoutArgs
Definition mcc.php:26
$optionsWithArgs
Definition mcc.php:25
mccShowUsage()
Show this command line tool usage.
Definition mcc.php:62
$line
Definition mcc.php:119
$debug
Definition mcc.php:31
$command
Definition mcc.php:125
mccGetHelp( $command)
Definition mcc.php:83
if( $help) $mcc
Definition mcc.php:39
$cache
Definition mcc.php:33
$showhelp
Definition mcc.php:116
$help
Definition mcc.php:32
$quit
Definition mcc.php:117
if( $line===false) $args
Definition mcc.php:124
A helper class for throttling authentication attempts.