Merge "(bug 25830) fix JS preview causing the page to "jump""
[lhc/web/wiklou.git] / maintenance / mcc.php
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 $mcc = new MWMemcached( array( 'persistent' => true/*, 'debug' => true*/ ) );
29 $mcc->set_servers( $wgMemCachedServers );
30 # $mcc->set_debug( true );
31
32 function mccShowHelp( $command ) {
33 $commandList = array(
34 'get' => 'grabs something',
35 'getsock' => 'lists sockets',
36 'set' => 'changes something',
37 'delete' => 'deletes something',
38 'history' => 'show command line history',
39 'server' => 'show current memcached server',
40 'dumpmcc' => 'shows the whole thing',
41 'exit' => 'exit mcc',
42 'quit' => 'exit mcc',
43 'help' => 'help about a command',
44 );
45 if ( !$command ) {
46 $command = 'fullhelp';
47 }
48 if ( $command === 'fullhelp' ) {
49 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
50 foreach ( $commandList as $cmd => $desc ) {
51 printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
52 }
53 } elseif ( isset( $commandList[$command] ) ) {
54 print "$command: $commandList[$command]\n";
55 } else {
56 print "$command: command does not exist or no help for it\n";
57 }
58 }
59
60 do {
61 $bad = false;
62 $showhelp = false;
63 $quit = false;
64
65 $line = Maintenance::readconsole();
66 if ( $line === false ) exit;
67
68 $args = explode( ' ', $line );
69 $command = array_shift( $args );
70
71 // process command
72 switch ( $command ) {
73 case 'help':
74 // show an help message
75 mccShowHelp( array_shift( $args ) );
76 break;
77
78 case 'get':
79 $sub = '';
80 if ( array_key_exists( 1, $args ) ) {
81 $sub = $args[1];
82 }
83 print "Getting {$args[0]}[$sub]\n";
84 $res = $mcc->get( $args[0] );
85 if ( array_key_exists( 1, $args ) ) {
86 $res = $res[$args[1]];
87 }
88 if ( $res === false ) {
89 # print 'Error: ' . $mcc->error_string() . "\n";
90 print "MemCached error\n";
91 } elseif ( is_string( $res ) ) {
92 print "$res\n";
93 } else {
94 var_dump( $res );
95 }
96 break;
97
98 case 'getsock':
99 $res = $mcc->get( $args[0] );
100 $sock = $mcc->get_sock( $args[0] );
101 var_dump( $sock );
102 break;
103
104 case 'server':
105 if ( $mcc->_single_sock !== null ) {
106 print $mcc->_single_sock . "\n";
107 break;
108 }
109 $res = $mcc->get( $args[0] );
110 $hv = $mcc->_hashfunc( $args[0] );
111 for ( $i = 0; $i < 3; $i++ ) {
112 print $mcc->_buckets[$hv % $mcc->_bucketcount] . "\n";
113 $hv += $mcc->_hashfunc( $i . $args[0] );
114 }
115 break;
116
117 case 'set':
118 $key = array_shift( $args );
119 if ( $args[0] == "#" && is_numeric( $args[1] ) ) {
120 $value = str_repeat( '*', $args[1] );
121 } else {
122 $value = implode( ' ', $args );
123 }
124 if ( !$mcc->set( $key, $value, 0 ) ) {
125 # print 'Error: ' . $mcc->error_string() . "\n";
126 print "MemCached error\n";
127 }
128 break;
129
130 case 'delete':
131 $key = implode( ' ', $args );
132 if ( !$mcc->delete( $key ) ) {
133 # print 'Error: ' . $mcc->error_string() . "\n";
134 print "MemCached error\n";
135 }
136 break;
137
138 case 'history':
139 if ( function_exists( 'readline_list_history' ) ) {
140 foreach ( readline_list_history() as $num => $line ) {
141 print "$num: $line\n";
142 }
143 } else {
144 print "readline_list_history() not available\n";
145 }
146 break;
147
148 case 'dumpmcc':
149 var_dump( $mcc );
150 break;
151
152 case 'quit':
153 case 'exit':
154 $quit = true;
155 break;
156
157 default:
158 $bad = true;
159 } // switch() end
160
161 if ( $bad ) {
162 if ( $command ) {
163 print "Bad command\n";
164 }
165 } else {
166 if ( function_exists( 'readline_add_history' ) ) {
167 readline_add_history( $line );
168 }
169 }
170 } while ( !$quit );