Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 126 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
Mcc | |
0.00% |
0 / 123 |
|
0.00% |
0 / 4 |
1560 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
showHelp | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 89 |
|
0.00% |
0 / 1 |
1056 | |||
mccGetHelp | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | /** |
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. |
7 | * |
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. |
12 | * |
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 |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | require_once __DIR__ . '/Maintenance.php'; |
22 | |
23 | use MediaWiki\MainConfigNames; |
24 | |
25 | /** |
26 | * Diagnostic tool for interacting with memcached. |
27 | * |
28 | * @ingroup Maintenance |
29 | */ |
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 a 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; |