Create IResultWrapper interface for type-hints
[mediawiki.git] / maintenance / cdb.php
blobbff2c13faf21fb494c752e2627baa7935287cd63
1 <?php
2 /**
3 * cdb inspector tool
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.
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.
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
20 * @file
21 * @todo document
22 * @ingroup Maintenance
24 use \Cdb\Exception as CdbException;
25 use \Cdb\Reader as CdbReader;
27 require_once __DIR__ . '/commandLine.inc';
29 function cdbShowHelp( $command ) {
30 $commandList = [
31 'load' => 'load a cdb file for reading',
32 'get' => 'get a value for a key',
33 'exit' => 'exit cdb',
34 'quit' => 'exit cdb',
35 'help' => 'help about a command',
37 if ( !$command ) {
38 $command = 'fullhelp';
40 if ( $command === 'fullhelp' ) {
41 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
42 foreach ( $commandList as $cmd => $desc ) {
43 printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
45 } elseif ( isset( $commandList[$command] ) ) {
46 print "$command: $commandList[$command]\n";
47 } else {
48 print "$command: command does not exist or no help for it\n";
52 do {
53 $bad = false;
54 $showhelp = false;
55 $quit = false;
56 static $fileHandle = false;
58 $line = Maintenance::readconsole();
59 if ( $line === false ) {
60 exit;
63 $args = explode( ' ', $line, 2 );
64 $command = array_shift( $args );
66 // process command
67 switch ( $command ) {
68 case 'help':
69 // show an help message
70 cdbShowHelp( array_shift( $args ) );
71 break;
72 case 'load':
73 if ( !isset( $args[0] ) ) {
74 print "Need a filename there buddy\n";
75 break;
77 $file = $args[0];
78 print "Loading cdb file $file...";
79 try {
80 $fileHandle = CdbReader::open( $file );
81 } catch ( CdbException $e ) {
84 if ( !$fileHandle ) {
85 print "not a cdb file or unable to read it\n";
86 } else {
87 print "ok\n";
89 break;
90 case 'get':
91 if ( !$fileHandle ) {
92 print "Need to load a cdb file first\n";
93 break;
95 if ( !isset( $args[0] ) ) {
96 print "Need to specify a key, Luke\n";
97 break;
99 try {
100 $res = $fileHandle->get( $args[0] );
101 } catch ( CdbException $e ) {
102 print "Unable to read key from file\n";
103 break;
105 if ( $res === false ) {
106 print "No such key/value pair\n";
107 } elseif ( is_string( $res ) ) {
108 print "$res\n";
109 } else {
110 var_dump( $res );
112 break;
113 case 'quit':
114 case 'exit':
115 $quit = true;
116 break;
118 default:
119 $bad = true;
120 } // switch() end
122 if ( $bad ) {
123 if ( $command ) {
124 print "Bad command\n";
126 } else {
127 if ( function_exists( 'readline_add_history' ) ) {
128 readline_add_history( $line );
131 } while ( !$quit );