3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 // @codeCoverageIgnoreStart
22 require_once __DIR__
. '/Maintenance.php';
23 // @codeCoverageIgnoreEnd
25 use MediaWiki\MainConfigNames
;
26 use MediaWiki\Maintenance\Maintenance
;
29 * Diagnostic tool for interacting with memcached.
31 * @ingroup Maintenance
33 class Mcc
extends Maintenance
{
34 public function __construct() {
35 parent
::__construct();
36 $this->addDescription(
37 'MemCached Command (mcc) is an interactive CLI that lets you interact ' .
38 'with the MediaWiki memcached backends.'
40 $this->addOption( 'cache', 'Cache type to use (a key in $wgObjectCaches)', false, true );
41 $this->addOption( 'debug', 'Set debug mode on the memcached connection' );
44 protected function showHelp() {
46 $this->output( "Interactive commands:\n " );
47 $this->output( str_replace( "\n", "\n ", $this->mccGetHelp( false ) ) );
48 $this->output( "\n" );
51 public function execute() {
52 $mcc = new MemcachedClient( [
54 'debug' => $this->hasOption( 'debug' ),
57 $config = $this->getConfig();
58 $objectCaches = $config->get( MainConfigNames
::ObjectCaches
);
59 $mainCacheType = $config->get( MainConfigNames
::MainCacheType
);
60 if ( $this->hasOption( 'cache' ) ) {
61 $cache = $this->getOption( 'cache' );
62 if ( !isset( $objectCaches[$cache] ) ) {
63 $this->fatalError( "MediaWiki isn't configured with a cache named '$cache'" );
65 $servers = $objectCaches[$cache]['servers'];
66 } elseif ( $mainCacheType === CACHE_MEMCACHED
) {
67 $mcc->set_servers( $config->get( MainConfigNames
::MemCachedServers
) );
68 } elseif ( isset( $objectCaches[$mainCacheType]['servers'] ) ) {
69 $mcc->set_servers( $objectCaches[$mainCacheType]['servers'] );
71 $this->fatalError( "MediaWiki isn't configured for Memcached usage" );
78 $line = self
::readconsole();
79 if ( $line === false ) {
83 $args = explode( ' ', $line );
84 $command = array_shift( $args );
89 // show a help message
90 print $this->mccGetHelp( array_shift( $args ) );
95 if ( array_key_exists( 1, $args ) ) {
98 print "Getting {$args[0]}[$sub]\n";
99 $res = $mcc->get( $args[0] );
100 if ( array_key_exists( 1, $args ) ) {
101 $res = $res[$args[1]];
103 if ( $res === false ) {
104 # print 'Error: ' . $mcc->error_string() . "\n";
105 print "MemCached error\n";
106 } elseif ( is_string( $res ) ) {
114 $res = $mcc->get( $args[0] );
115 $sock = $mcc->get_sock( $args[0] );
120 if ( $mcc->_single_sock
!== null ) {
121 print $mcc->_single_sock
. "\n";
124 $res = $mcc->get( $args[0] );
125 $hv = $mcc->_hashfunc( $args[0] );
126 for ( $i = 0; $i < 3; $i++
) {
127 print $mcc->_buckets
[$hv %
$mcc->_bucketcount
] . "\n";
128 $hv +
= $mcc->_hashfunc( $i . $args[0] );
133 $key = array_shift( $args );
134 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
135 $value = str_repeat( '*', (int)$args[1] );
137 $value = implode( ' ', $args );
139 if ( !$mcc->set( $key, $value, 0 ) ) {
140 # print 'Error: ' . $mcc->error_string() . "\n";
141 print "MemCached error\n";
146 $key = implode( ' ', $args );
147 if ( !$mcc->delete( $key ) ) {
148 # print 'Error: ' . $mcc->error_string() . "\n";
149 print "MemCached error\n";
154 if ( function_exists( 'readline_list_history' ) ) {
155 foreach ( readline_list_history() as $num => $line ) {
156 print "$num: $line\n";
159 print "readline_list_history() not available\n";
178 print "Bad command\n";
181 if ( function_exists( 'readline_add_history' ) ) {
182 readline_add_history( $line );
188 private function mccGetHelp( $command ) {
191 'get' => 'grabs something',
192 'getsock' => 'lists sockets',
193 'set' => 'changes something',
194 'delete' => 'deletes something',
195 'history' => 'show command line history',
196 'server' => 'show current memcached server',
197 'dumpmcc' => 'shows the whole thing',
198 'exit' => 'exit mcc',
199 'quit' => 'exit mcc',
200 'help' => 'help about a command',
203 $command = 'fullhelp';
205 if ( $command === 'fullhelp' ) {
206 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
207 foreach ( $commandList as $cmd => $desc ) {
208 $output .= sprintf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
210 } elseif ( isset( $commandList[$command] ) ) {
211 $output .= "$command: $commandList[$command]\n";
213 $output .= "$command: command does not exist or no help for it\n";
220 // @codeCoverageIgnoreStart
221 $maintClass = Mcc
::class;
222 require_once RUN_MAINTENANCE_IF_MAIN
;
223 // @codeCoverageIgnoreEnd