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