MediaWiki  master
mcc.php
Go to the documentation of this file.
1 <?php
21 require_once __DIR__ . '/Maintenance.php';
22 
24 
30 class Mcc extends Maintenance {
31  public function __construct() {
32  parent::__construct();
33  $this->addDescription(
34  'MemCached Command (mcc) is an interactive CLI that lets you interact ' .
35  'with the MediaWiki memcached backends.'
36  );
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' );
39  }
40 
41  protected function showHelp() {
42  parent::showHelp();
43  $this->output( "Interactive commands:\n " );
44  $this->output( str_replace( "\n", "\n ", $this->mccGetHelp( false ) ) );
45  $this->output( "\n" );
46  }
47 
48  public function execute() {
49  $mcc = new MemcachedClient( [
50  'persistent' => true,
51  'debug' => $this->hasOption( 'debug' ),
52  ] );
53 
54  $config = $this->getConfig();
55  $objectCaches = $config->get( MainConfigNames::ObjectCaches );
56  $mainCacheType = $config->get( MainConfigNames::MainCacheType );
57  if ( $this->hasOption( 'cache' ) ) {
58  $cache = $this->getOption( 'cache' );
59  if ( !isset( $objectCaches[$cache] ) ) {
60  $this->fatalError( "MediaWiki isn't configured with a cache named '$cache'" );
61  }
62  $servers = $objectCaches[$cache]['servers'];
63  } elseif ( $mainCacheType === CACHE_MEMCACHED ) {
64  $mcc->set_servers( $config->get( MainConfigNames::MemCachedServers ) );
65  } elseif ( isset( $objectCaches[$mainCacheType]['servers'] ) ) {
66  $mcc->set_servers( $objectCaches[$mainCacheType]['servers'] );
67  } else {
68  $this->fatalError( "MediaWiki isn't configured for Memcached usage" );
69  }
70 
71  do {
72  $bad = false;
73  $quit = false;
74 
75  $line = self::readconsole();
76  if ( $line === false ) {
77  break;
78  }
79 
80  $args = explode( ' ', $line );
81  $command = array_shift( $args );
82 
83  // process command
84  switch ( $command ) {
85  case 'help':
86  // show an help message
87  print $this->mccGetHelp( array_shift( $args ) );
88  break;
89 
90  case 'get':
91  $sub = '';
92  if ( array_key_exists( 1, $args ) ) {
93  $sub = $args[1];
94  }
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]];
99  }
100  if ( $res === false ) {
101  # print 'Error: ' . $mcc->error_string() . "\n";
102  print "MemCached error\n";
103  } elseif ( is_string( $res ) ) {
104  print "$res\n";
105  } else {
106  var_dump( $res );
107  }
108  break;
109 
110  case 'getsock':
111  $res = $mcc->get( $args[0] );
112  $sock = $mcc->get_sock( $args[0] );
113  var_dump( $sock );
114  break;
115 
116  case 'server':
117  if ( $mcc->_single_sock !== null ) {
118  print $mcc->_single_sock . "\n";
119  break;
120  }
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] );
126  }
127  break;
128 
129  case 'set':
130  $key = array_shift( $args );
131  if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
132  $value = str_repeat( '*', (int)$args[1] );
133  } else {
134  $value = implode( ' ', $args );
135  }
136  if ( !$mcc->set( $key, $value, 0 ) ) {
137  # print 'Error: ' . $mcc->error_string() . "\n";
138  print "MemCached error\n";
139  }
140  break;
141 
142  case 'delete':
143  $key = implode( ' ', $args );
144  if ( !$mcc->delete( $key ) ) {
145  # print 'Error: ' . $mcc->error_string() . "\n";
146  print "MemCached error\n";
147  }
148  break;
149 
150  case 'history':
151  if ( function_exists( 'readline_list_history' ) ) {
152  foreach ( readline_list_history() as $num => $line ) {
153  print "$num: $line\n";
154  }
155  } else {
156  print "readline_list_history() not available\n";
157  }
158  break;
159 
160  case 'dumpmcc':
161  var_dump( $mcc );
162  break;
163 
164  case 'quit':
165  case 'exit':
166  $quit = true;
167  break;
168 
169  default:
170  $bad = true;
171  }
172 
173  if ( $bad ) {
174  if ( $command ) {
175  print "Bad command\n";
176  }
177  } else {
178  if ( function_exists( 'readline_add_history' ) ) {
179  readline_add_history( $line );
180  }
181  }
182  } while ( !$quit );
183  }
184 
185  private function mccGetHelp( $command ) {
186  $output = '';
187  $commandList = [
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',
198  ];
199  if ( !$command ) {
200  $command = 'fullhelp';
201  }
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 );
206  }
207  } elseif ( isset( $commandList[$command] ) ) {
208  $output .= "$command: $commandList[$command]\n";
209  } else {
210  $output .= "$command: command does not exist or no help for it\n";
211  }
212 
213  return $output;
214  }
215 }
216 
217 $maintClass = Mcc::class;
218 require_once RUN_MAINTENANCE_IF_MAIN;
const CACHE_MEMCACHED
Definition: Defines.php:88
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
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:30
execute()
Do the actual work.
Definition: mcc.php:48
__construct()
Default constructor.
Definition: mcc.php:31
showHelp()
Definitely show the help.
Definition: mcc.php:41
A class containing constants representing the names of configuration variables.
memcached client class implemented using (p)fsockopen()
$maintClass
Definition: mcc.php:217