cdb.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. function cdbShowHelp( $command ) {
  29. $commandList = [
  30. 'load' => 'load a cdb file for reading',
  31. 'get' => 'get a value for a key',
  32. 'exit' => 'exit cdb',
  33. 'quit' => 'exit cdb',
  34. 'help' => 'help about a command',
  35. ];
  36. if ( !$command ) {
  37. $command = 'fullhelp';
  38. }
  39. if ( $command === 'fullhelp' ) {
  40. $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
  41. foreach ( $commandList as $cmd => $desc ) {
  42. printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
  43. }
  44. } elseif ( isset( $commandList[$command] ) ) {
  45. print "$command: $commandList[$command]\n";
  46. } else {
  47. print "$command: command does not exist or no help for it\n";
  48. }
  49. }
  50. do {
  51. $bad = false;
  52. $showhelp = false;
  53. $quit = false;
  54. static $fileHandle = false;
  55. $line = Maintenance::readconsole();
  56. if ( $line === false ) {
  57. exit;
  58. }
  59. $args = explode( ' ', $line, 2 );
  60. $command = array_shift( $args );
  61. // process command
  62. switch ( $command ) {
  63. case 'help':
  64. // show an help message
  65. cdbShowHelp( array_shift( $args ) );
  66. break;
  67. case 'load':
  68. if ( !isset( $args[0] ) ) {
  69. print "Need a filename there buddy\n";
  70. break;
  71. }
  72. $file = $args[0];
  73. print "Loading cdb file $file...";
  74. try {
  75. $fileHandle = CdbReader::open( $file );
  76. } catch ( CdbException $e ) {
  77. }
  78. if ( !$fileHandle ) {
  79. print "not a cdb file or unable to read it\n";
  80. } else {
  81. print "ok\n";
  82. }
  83. break;
  84. case 'get':
  85. if ( !$fileHandle ) {
  86. print "Need to load a cdb file first\n";
  87. break;
  88. }
  89. if ( !isset( $args[0] ) ) {
  90. print "Need to specify a key, Luke\n";
  91. break;
  92. }
  93. try {
  94. $res = $fileHandle->get( $args[0] );
  95. } catch ( CdbException $e ) {
  96. print "Unable to read key from file\n";
  97. break;
  98. }
  99. if ( $res === false ) {
  100. print "No such key/value pair\n";
  101. } elseif ( is_string( $res ) ) {
  102. print "$res\n";
  103. } else {
  104. var_dump( $res );
  105. }
  106. break;
  107. case 'quit':
  108. case 'exit':
  109. $quit = true;
  110. break;
  111. default:
  112. $bad = true;
  113. } // switch() end
  114. if ( $bad ) {
  115. if ( $command ) {
  116. print "Bad command\n";
  117. }
  118. } else {
  119. if ( function_exists( 'readline_add_history' ) ) {
  120. readline_add_history( $line );
  121. }
  122. }
  123. } while ( !$quit );