MediaWiki  REL1_24
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 
00083             if ( !$fileHandle ) {
00084                 print "not a cdb file or unable to read it\n";
00085             } else {
00086                 print "ok\n";
00087             }
00088             break;
00089         case 'get':
00090             if ( !$fileHandle ) {
00091                 print "Need to load a cdb file first\n";
00092                 break;
00093             }
00094             if ( !isset( $args[0] ) ) {
00095                 print "Need to specify a key, Luke\n";
00096                 break;
00097             }
00098             try {
00099                 $res = $fileHandle->get( $args[0] );
00100             } catch ( CdbException $e ) {
00101                 print "Unable to read key from file\n";
00102                 break;
00103             }
00104             if ( $res === false ) {
00105                 print "No such key/value pair\n";
00106             } elseif ( is_string( $res ) ) {
00107                 print "$res\n";
00108             } else {
00109                 var_dump( $res );
00110             }
00111             break;
00112         case 'quit':
00113         case 'exit':
00114             $quit = true;
00115             break;
00116 
00117         default:
00118             $bad = true;
00119     } // switch() end
00120 
00121     if ( $bad ) {
00122         if ( $command ) {
00123             print "Bad command\n";
00124         }
00125     } else {
00126         if ( function_exists( 'readline_add_history' ) ) {
00127             readline_add_history( $line );
00128         }
00129     }
00130 } while ( !$quit );