[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> mcc.php (source)

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


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1