MediaWiki  REL1_19
cdb.php
Go to the documentation of this file.
00001 <?php
00026 require_once( dirname( __FILE__ ) . '/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 ) exit;
00059 
00060         $args = explode( ' ', $line );
00061         $command = array_shift( $args );
00062 
00063         // process command
00064         switch ( $command ) {
00065                 case 'help':
00066                         // show an help message
00067                         cdbShowHelp( array_shift( $args ) );
00068                         break;
00069                 case 'load':
00070                         if( !isset( $args[0] ) ) {
00071                                 print "Need a filename there buddy\n";
00072                                 break;
00073                         }
00074                         $file = $args[0];
00075                         print "Loading cdb file $file...";
00076                         $fileHandle = CdbReader::open( $file );
00077                         if( !$fileHandle ) {
00078                                 print "not a cdb file or unable to read it\n";
00079                         } else {
00080                                 print "ok\n";
00081                         }
00082                         break;
00083                 case 'get':
00084                         if( !$fileHandle ) {
00085                                 print "Need to load a cdb file first\n";
00086                                 break;
00087                         }
00088                         if( !isset( $args[0] ) ) {
00089                                 print "Need to specify a key, Luke\n";
00090                                 break;
00091                         }
00092                         $res = $fileHandle->get( $args[0] );
00093                         if ( $res === false ) {
00094                                 print "No such key/value pair\n";
00095                         } elseif ( is_string( $res ) ) {
00096                                 print "$res\n";
00097                         } else {
00098                                 var_dump( $res );
00099                         }
00100                         break;
00101                 case 'quit':
00102                 case 'exit':
00103                         $quit = true;
00104                         break;
00105 
00106                 default:
00107                         $bad = true;
00108         } // switch() end
00109 
00110         if ( $bad ) {
00111                 if ( $command ) {
00112                         print "Bad command\n";
00113                 }
00114         } else {
00115                 if ( function_exists( 'readline_add_history' ) ) {
00116                         readline_add_history( $line );
00117                 }
00118         }
00119 } while ( !$quit );