MediaWiki master
mcc.php
Go to the documentation of this file.
1<?php
21// @codeCoverageIgnoreStart
22require_once __DIR__ . '/Maintenance.php';
23// @codeCoverageIgnoreEnd
24
26
32class Mcc extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription(
36 'MemCached Command (mcc) is an interactive CLI that lets you interact ' .
37 'with the MediaWiki memcached backends.'
38 );
39 $this->addOption( 'cache', 'Cache type to use (a key in $wgObjectCaches)', false, true );
40 $this->addOption( 'debug', 'Set debug mode on the memcached connection' );
41 }
42
43 protected function showHelp() {
44 parent::showHelp();
45 $this->output( "Interactive commands:\n " );
46 $this->output( str_replace( "\n", "\n ", $this->mccGetHelp( false ) ) );
47 $this->output( "\n" );
48 }
49
50 public function execute() {
51 $mcc = new MemcachedClient( [
52 'persistent' => true,
53 'debug' => $this->hasOption( 'debug' ),
54 ] );
55
56 $config = $this->getConfig();
57 $objectCaches = $config->get( MainConfigNames::ObjectCaches );
58 $mainCacheType = $config->get( MainConfigNames::MainCacheType );
59 if ( $this->hasOption( 'cache' ) ) {
60 $cache = $this->getOption( 'cache' );
61 if ( !isset( $objectCaches[$cache] ) ) {
62 $this->fatalError( "MediaWiki isn't configured with a cache named '$cache'" );
63 }
64 $servers = $objectCaches[$cache]['servers'];
65 } elseif ( $mainCacheType === CACHE_MEMCACHED ) {
66 $mcc->set_servers( $config->get( MainConfigNames::MemCachedServers ) );
67 } elseif ( isset( $objectCaches[$mainCacheType]['servers'] ) ) {
68 $mcc->set_servers( $objectCaches[$mainCacheType]['servers'] );
69 } else {
70 $this->fatalError( "MediaWiki isn't configured for Memcached usage" );
71 }
72
73 do {
74 $bad = false;
75 $quit = false;
76
77 $line = self::readconsole();
78 if ( $line === false ) {
79 break;
80 }
81
82 $args = explode( ' ', $line );
83 $command = array_shift( $args );
84
85 // process command
86 switch ( $command ) {
87 case 'help':
88 // show a help message
89 print $this->mccGetHelp( array_shift( $args ) );
90 break;
91
92 case 'get':
93 $sub = '';
94 if ( array_key_exists( 1, $args ) ) {
95 $sub = $args[1];
96 }
97 print "Getting {$args[0]}[$sub]\n";
98 $res = $mcc->get( $args[0] );
99 if ( array_key_exists( 1, $args ) ) {
100 $res = $res[$args[1]];
101 }
102 if ( $res === false ) {
103 # print 'Error: ' . $mcc->error_string() . "\n";
104 print "MemCached error\n";
105 } elseif ( is_string( $res ) ) {
106 print "$res\n";
107 } else {
108 var_dump( $res );
109 }
110 break;
111
112 case 'getsock':
113 $res = $mcc->get( $args[0] );
114 $sock = $mcc->get_sock( $args[0] );
115 var_dump( $sock );
116 break;
117
118 case 'server':
119 if ( $mcc->_single_sock !== null ) {
120 print $mcc->_single_sock . "\n";
121 break;
122 }
123 $res = $mcc->get( $args[0] );
124 $hv = $mcc->_hashfunc( $args[0] );
125 for ( $i = 0; $i < 3; $i++ ) {
126 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
127 $hv += $mcc->_hashfunc( $i . $args[0] );
128 }
129 break;
130
131 case 'set':
132 $key = array_shift( $args );
133 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
134 $value = str_repeat( '*', (int)$args[1] );
135 } else {
136 $value = implode( ' ', $args );
137 }
138 if ( !$mcc->set( $key, $value, 0 ) ) {
139 # print 'Error: ' . $mcc->error_string() . "\n";
140 print "MemCached error\n";
141 }
142 break;
143
144 case 'delete':
145 $key = implode( ' ', $args );
146 if ( !$mcc->delete( $key ) ) {
147 # print 'Error: ' . $mcc->error_string() . "\n";
148 print "MemCached error\n";
149 }
150 break;
151
152 case 'history':
153 if ( function_exists( 'readline_list_history' ) ) {
154 foreach ( readline_list_history() as $num => $line ) {
155 print "$num: $line\n";
156 }
157 } else {
158 print "readline_list_history() not available\n";
159 }
160 break;
161
162 case 'dumpmcc':
163 var_dump( $mcc );
164 break;
165
166 case 'quit':
167 case 'exit':
168 $quit = true;
169 break;
170
171 default:
172 $bad = true;
173 }
174
175 if ( $bad ) {
176 if ( $command ) {
177 print "Bad command\n";
178 }
179 } else {
180 if ( function_exists( 'readline_add_history' ) ) {
181 readline_add_history( $line );
182 }
183 }
184 } while ( !$quit );
185 }
186
187 private function mccGetHelp( $command ) {
188 $output = '';
189 $commandList = [
190 'get' => 'grabs something',
191 'getsock' => 'lists sockets',
192 'set' => 'changes something',
193 'delete' => 'deletes something',
194 'history' => 'show command line history',
195 'server' => 'show current memcached server',
196 'dumpmcc' => 'shows the whole thing',
197 'exit' => 'exit mcc',
198 'quit' => 'exit mcc',
199 'help' => 'help about a command',
200 ];
201 if ( !$command ) {
202 $command = 'fullhelp';
203 }
204 if ( $command === 'fullhelp' ) {
205 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
206 foreach ( $commandList as $cmd => $desc ) {
207 $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
208 }
209 } elseif ( isset( $commandList[$command] ) ) {
210 $output .= "$command: $commandList[$command]\n";
211 } else {
212 $output .= "$command: command does not exist or no help for it\n";
213 }
214
215 return $output;
216 }
217}
218
219// @codeCoverageIgnoreStart
220$maintClass = Mcc::class;
221require_once RUN_MAINTENANCE_IF_MAIN;
222// @codeCoverageIgnoreEnd
const CACHE_MEMCACHED
Definition Defines.php:89
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
static readconsole( $prompt='> ')
Prompt the console for input.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Diagnostic tool for interacting with memcached.
Definition mcc.php:32
execute()
Do the actual work.
Definition mcc.php:50
__construct()
Default constructor.
Definition mcc.php:33
showHelp()
Definitely show the help.
Definition mcc.php:43
A class containing constants representing the names of configuration variables.
memcached client class implemented using (p)fsockopen()
$maintClass
Definition mcc.php:220