Merge "Add passing ''italic'''s case to 'Unclosed and unmatched quotes' test"
[lhc/web/wiklou.git] / maintenance / cdb.php
1 <?php
2 /**
3 * cdb inspector 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( dirname( __FILE__ ) . '/commandLine.inc' );
27
28 function cdbShowHelp( $command ) {
29 $commandList = array(
30 'load' => 'load a cdb file for reading',
31 'get' => 'get a value for a key',
32 'exit' => 'exit cdb',
33 'quit' => 'exit cdb',
34 'help' => 'help about a command',
35 );
36 if ( !$command ) {
37 $command = 'fullhelp';
38 }
39 if ( $command === 'fullhelp' ) {
40 $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
41 foreach ( $commandList as $cmd => $desc ) {
42 printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
43 }
44 } elseif ( isset( $commandList[$command] ) ) {
45 print "$command: $commandList[$command]\n";
46 } else {
47 print "$command: command does not exist or no help for it\n";
48 }
49 }
50
51 do {
52 $bad = false;
53 $showhelp = false;
54 $quit = false;
55 static $fileHandle;
56
57 $line = Maintenance::readconsole();
58 if ( $line === false ) exit;
59
60 $args = explode( ' ', $line );
61 $command = array_shift( $args );
62
63 // process command
64 switch ( $command ) {
65 case 'help':
66 // show an help message
67 cdbShowHelp( array_shift( $args ) );
68 break;
69 case 'load':
70 if( !isset( $args[0] ) ) {
71 print "Need a filename there buddy\n";
72 break;
73 }
74 $file = $args[0];
75 print "Loading cdb file $file...";
76 $fileHandle = CdbReader::open( $file );
77 if( !$fileHandle ) {
78 print "not a cdb file or unable to read it\n";
79 } else {
80 print "ok\n";
81 }
82 break;
83 case 'get':
84 if( !$fileHandle ) {
85 print "Need to load a cdb file first\n";
86 break;
87 }
88 if( !isset( $args[0] ) ) {
89 print "Need to specify a key, Luke\n";
90 break;
91 }
92 $res = $fileHandle->get( $args[0] );
93 if ( $res === false ) {
94 print "No such key/value pair\n";
95 } elseif ( is_string( $res ) ) {
96 print "$res\n";
97 } else {
98 var_dump( $res );
99 }
100 break;
101 case 'quit':
102 case 'exit':
103 $quit = true;
104 break;
105
106 default:
107 $bad = true;
108 } // switch() end
109
110 if ( $bad ) {
111 if ( $command ) {
112 print "Bad command\n";
113 }
114 } else {
115 if ( function_exists( 'readline_add_history' ) ) {
116 readline_add_history( $line );
117 }
118 }
119 } while ( !$quit );