Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[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 use \Cdb\Exception as CdbException;
25 use \Cdb\Reader as CdbReader;
26
27 require_once __DIR__ . '/commandLine.inc';
28
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',
36 ];
37 if ( !$command ) {
38 $command = 'fullhelp';
39 }
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 );
44 }
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";
49 }
50 }
51
52 do {
53 $bad = false;
54 $showhelp = false;
55 $quit = false;
56 static $fileHandle = false;
57
58 $line = Maintenance::readconsole();
59 if ( $line === false ) {
60 exit;
61 }
62
63 $args = explode( ' ', $line, 2 );
64 $command = array_shift( $args );
65
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;
76 }
77 $file = $args[0];
78 print "Loading cdb file $file...";
79 try {
80 $fileHandle = CdbReader::open( $file );
81 } catch ( CdbException $e ) {
82 }
83
84 if ( !$fileHandle ) {
85 print "not a cdb file or unable to read it\n";
86 } else {
87 print "ok\n";
88 }
89 break;
90 case 'get':
91 if ( !$fileHandle ) {
92 print "Need to load a cdb file first\n";
93 break;
94 }
95 if ( !isset( $args[0] ) ) {
96 print "Need to specify a key, Luke\n";
97 break;
98 }
99 try {
100 $res = $fileHandle->get( $args[0] );
101 } catch ( CdbException $e ) {
102 print "Unable to read key from file\n";
103 break;
104 }
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 );
111 }
112 break;
113 case 'quit':
114 case 'exit':
115 $quit = true;
116 break;
117
118 default:
119 $bad = true;
120 } // switch() end
121
122 if ( $bad ) {
123 if ( $command ) {
124 print "Bad command\n";
125 }
126 } else {
127 if ( function_exists( 'readline_add_history' ) ) {
128 readline_add_history( $line );
129 }
130 }
131 } while ( !$quit );