MediaWiki  REL1_23
cdb.php
Go to the documentation of this file.
00001 <?php
00026 require_once __DIR__ . '/commandLine.inc';
00027 
00028 function cdbShowHelp( $command ) {
00029     $commandList = array(
00030         'load' => 'load a cdb file for reading',
00031         'get' => 'get a value for a key',
00032         'exit' => 'exit cdb',
00033         'quit' => 'exit cdb',
00034         'help' => 'help about a command',
00035     );
00036     if ( !$command ) {
00037         $command = 'fullhelp';
00038     }
00039     if ( $command === 'fullhelp' ) {
00040         $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
00041         foreach ( $commandList as $cmd => $desc ) {
00042             printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
00043         }
00044     } elseif ( isset( $commandList[$command] ) ) {
00045         print "$command: $commandList[$command]\n";
00046     } else {
00047         print "$command: command does not exist or no help for it\n";
00048     }
00049 }
00050 
00051 do {
00052     $bad = false;
00053     $showhelp = false;
00054     $quit = false;
00055     static $fileHandle = false;
00056 
00057     $line = Maintenance::readconsole();
00058     if ( $line === false ) {
00059         exit;
00060     }
00061 
00062     $args = explode( ' ', $line );
00063     $command = array_shift( $args );
00064 
00065     // process command
00066     switch ( $command ) {
00067         case 'help':
00068             // show an help message
00069             cdbShowHelp( array_shift( $args ) );
00070             break;
00071         case 'load':
00072             if ( !isset( $args[0] ) ) {
00073                 print "Need a filename there buddy\n";
00074                 break;
00075             }
00076             $file = $args[0];
00077             print "Loading cdb file $file...";
00078             try {
00079                 $fileHandle = CdbReader::open( $file );
00080             } catch ( CdbException $e ) {}
00081 
00082             if ( !$fileHandle ) {
00083                 print "not a cdb file or unable to read it\n";
00084             } else {
00085                 print "ok\n";
00086             }
00087             break;
00088         case 'get':
00089             if ( !$fileHandle ) {
00090                 print "Need to load a cdb file first\n";
00091                 break;
00092             }
00093             if ( !isset( $args[0] ) ) {
00094                 print "Need to specify a key, Luke\n";
00095                 break;
00096             }
00097             try {
00098                 $res = $fileHandle->get( $args[0] );
00099             } catch ( CdbException $e ) {
00100                 print "Unable to read key from file\n";
00101                 break;
00102             }
00103             if ( $res === false ) {
00104                 print "No such key/value pair\n";
00105             } elseif ( is_string( $res ) ) {
00106                 print "$res\n";
00107             } else {
00108                 var_dump( $res );
00109             }
00110             break;
00111         case 'quit':
00112         case 'exit':
00113             $quit = true;
00114             break;
00115 
00116         default:
00117             $bad = true;
00118     } // switch() end
00119 
00120     if ( $bad ) {
00121         if ( $command ) {
00122             print "Bad command\n";
00123         }
00124     } else {
00125         if ( function_exists( 'readline_add_history' ) ) {
00126             readline_add_history( $line );
00127         }
00128     }
00129 } while ( !$quit );