21 require_once __DIR__ .
'/Maintenance.php';
32 parent::__construct();
34 'MemCached Command (mcc) is an interactive CLI that lets you interact ' .
35 'with the MediaWiki memcached backends.'
37 $this->
addOption(
'cache',
'Cache type to use (a key in $wgObjectCaches)',
false,
true );
38 $this->
addOption(
'debug',
'Set debug mode on the memcached connection' );
43 $this->
output(
"Interactive commands:\n " );
44 $this->
output( str_replace(
"\n",
"\n ", $this->mccGetHelp(
false ) ) );
55 $objectCaches = $config->get( MainConfigNames::ObjectCaches );
56 $mainCacheType = $config->get( MainConfigNames::MainCacheType );
59 if ( !isset( $objectCaches[$cache] ) ) {
60 $this->
fatalError(
"MediaWiki isn't configured with a cache named '$cache'" );
62 $servers = $objectCaches[$cache][
'servers'];
64 $mcc->set_servers( $config->get( MainConfigNames::MemCachedServers ) );
65 } elseif ( isset( $objectCaches[$mainCacheType][
'servers'] ) ) {
66 $mcc->set_servers( $objectCaches[$mainCacheType][
'servers'] );
68 $this->
fatalError(
"MediaWiki isn't configured for Memcached usage" );
76 if ( $line ===
false ) {
80 $args = explode(
' ', $line );
81 $command = array_shift( $args );
87 print $this->mccGetHelp( array_shift( $args ) );
92 if ( array_key_exists( 1, $args ) ) {
95 print
"Getting {$args[0]}[$sub]\n";
96 $res = $mcc->get( $args[0] );
97 if ( array_key_exists( 1, $args ) ) {
98 $res = $res[$args[1]];
100 if ( $res ===
false ) {
101 # print 'Error: ' . $mcc->error_string() . "\n";
102 print
"MemCached error\n";
103 } elseif ( is_string( $res ) ) {
111 $res = $mcc->get( $args[0] );
112 $sock = $mcc->get_sock( $args[0] );
117 if ( $mcc->_single_sock !==
null ) {
118 print $mcc->_single_sock .
"\n";
121 $res = $mcc->get( $args[0] );
122 $hv = $mcc->_hashfunc( $args[0] );
123 for ( $i = 0; $i < 3; $i++ ) {
124 print $mcc->_buckets[$hv % $mcc->_bucketcount] .
"\n";
125 $hv += $mcc->_hashfunc( $i . $args[0] );
130 $key = array_shift( $args );
131 if ( $args[0] ==
"#" && is_numeric( $args[1] ) ) {
132 $value = str_repeat(
'*', (
int)$args[1] );
134 $value = implode(
' ', $args );
136 if ( !$mcc->set( $key, $value, 0 ) ) {
137 # print 'Error: ' . $mcc->error_string() . "\n";
138 print
"MemCached error\n";
143 $key = implode(
' ', $args );
144 if ( !$mcc->delete( $key ) ) {
145 # print 'Error: ' . $mcc->error_string() . "\n";
146 print
"MemCached error\n";
151 if ( function_exists(
'readline_list_history' ) ) {
152 foreach ( readline_list_history() as $num => $line ) {
153 print
"$num: $line\n";
156 print
"readline_list_history() not available\n";
175 print
"Bad command\n";
178 if ( function_exists(
'readline_add_history' ) ) {
179 readline_add_history( $line );
185 private function mccGetHelp( $command ) {
188 'get' =>
'grabs something',
189 'getsock' =>
'lists sockets',
190 'set' =>
'changes something',
191 'delete' =>
'deletes something',
192 'history' =>
'show command line history',
193 'server' =>
'show current memcached server',
194 'dumpmcc' =>
'shows the whole thing',
195 'exit' =>
'exit mcc',
196 'quit' =>
'exit mcc',
197 'help' =>
'help about a command',
200 $command =
'fullhelp';
202 if ( $command ===
'fullhelp' ) {
203 $max_cmd_len = max( array_map(
'strlen', array_keys( $commandList ) ) );
204 foreach ( $commandList as $cmd => $desc ) {
205 $output .= sprintf(
"%-{$max_cmd_len}s: %s\n", $cmd, $desc );
207 } elseif ( isset( $commandList[$command] ) ) {
208 $output .=
"$command: $commandList[$command]\n";
210 $output .=
"$command: command does not exist or no help for it\n";
218 require_once RUN_MAINTENANCE_IF_MAIN;
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.
execute()
Do the actual work.
__construct()
Default constructor.
showHelp()
Definitely show the help.
A class containing constants representing the names of configuration variables.
memcached client class implemented using (p)fsockopen()