MediaWiki  master
cdb.php
Go to the documentation of this file.
1 <?php
25 use Cdb\Exception as CdbException;
26 use Cdb\Reader as CdbReader;
27 
28 require_once __DIR__ . '/Maintenance.php';
29 
30 // phpcs:disable MediaWiki.Files.ClassMatchesFilename.NotMatch
31 class CdbInspector extends Maintenance {
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Inspects a CDB file' );
35  $this->addArg( 'filename', 'File to open (optional)', false );
36  }
37 
38  private function showInternalHelp( ?string $command ) : void {
39  $commandList = [
40  'load' => 'Load a CDB file for reading',
41  'get' => 'Get a value for a key',
42  'list' => 'List all keys in the file. Optional parameter specifies the maximum number '
43  . 'of keys returned.',
44  'find' => 'Find keys matching a regular expression',
45  'exit' => 'Exit',
46  'quit' => 'Exit',
47  'help' => 'Help about a command',
48  ];
49  if ( !$command ) {
50  $command = 'fullhelp';
51  }
52  if ( $command === 'fullhelp' ) {
53  $max_cmd_len = max( array_map( 'strlen', array_keys( $commandList ) ) );
54  foreach ( $commandList as $cmd => $desc ) {
55  printf( "%-{$max_cmd_len}s: %s\n", $cmd, $desc );
56  }
57  } elseif ( isset( $commandList[$command] ) ) {
58  print "$command: $commandList[$command]\n";
59  } else {
60  print "$command: command does not exist or no help for it\n";
61  }
62  }
63 
67  public function execute() {
68  if ( $this->isQuiet() ) {
69  $this->fatalError( "This is an interactive script, can't use it with --quiet" );
70  }
71 
72  $fileName = $this->getArg( 0, null );
74  $fileHandle = $fileName ? $this->loadFile( $fileName ) : null;
75  do {
76  $bad = false;
77  $quit = false;
79  if ( $line === false ) {
80  exit;
81  }
82 
83  $args = explode( ' ', $line, 2 );
84  $command = array_shift( $args );
85 
86  // process command
87  switch ( $command ) {
88  case 'help':
89  // show an help message
90  $this->showInternalHelp( array_shift( $args ) );
91  break;
92  case 'load':
93  $fileHandle = $this->loadFile( array_shift( $args ) );
94  break;
95  case 'get':
96  if ( !$fileHandle ) {
97  print "Need to load a CDB file first\n";
98  break;
99  }
100  if ( !isset( $args[0] ) ) {
101  print "Need to specify a key, Luke\n";
102  break;
103  }
104  try {
105  $res = $fileHandle->get( $args[0] );
106  } catch ( CdbException $e ) {
107  print "Unable to read key from file\n";
108  break;
109  }
110  if ( $res === false ) {
111  print "No such key/value pair\n";
112  } elseif ( is_string( $res ) ) {
113  print "$res\n";
114  } else {
115  var_dump( $res );
116  }
117  break;
118  case 'list':
119  $this->listKeys( $fileHandle, (int)array_shift( $args ) );
120  break;
121  case 'find':
122  $this->findKeys( $fileHandle, array_shift( $args ) );
123  break;
124  case 'quit':
125  case 'exit':
126  $quit = true;
127  break;
128 
129  default:
130  $bad = true;
131  } // switch() end
132 
133  if ( $bad ) {
134  if ( $command ) {
135  print "Bad command\n";
136  }
137  } else {
138  if ( function_exists( 'readline_add_history' ) ) {
139  readline_add_history( $line );
140  }
141  }
142  } while ( !$quit );
143  }
144 
145  private function loadFile( ?string $fileName ) : ?CdbReader {
146  if ( !$fileName ) {
147  print "Need a filename there buddy\n";
148  return null;
149  }
150  print "Loading CDB file $fileName...";
151  $fileHandle = null;
152  try {
153  $fileHandle = CdbReader::open( $fileName );
154  } catch ( CdbException $e ) {
155  }
156 
157  if ( !$fileHandle ) {
158  print "Not a CDB file or unable to read it\n";
159  } else {
160  print "ok\n";
161  }
162  return $fileHandle;
163  }
164 
169  private function listKeys( CdbReader $cdb, int $maxKeys ) : void {
170  $count = 0;
171  $key = $cdb->firstkey();
172  while ( $key !== false && ( !$maxKeys || $count < $maxKeys ) ) {
173  print "'$key'\n";
174  $count++;
175  $key = $cdb->nextkey();
176  }
177  print "$count keys found.\n";
178  }
179 
184  private function findKeys( CdbReader $cdb, ?string $regexp ) : void {
185  if ( $regexp === null ) {
186  print "Regexp required\n";
187  return;
188  }
189  if ( !preg_match( '#^/.+/[imsxUXJ]*$#', $regexp ) ) {
190  $regexp = "/$regexp/";
191  }
192  // Test for invalid regexp
193  if ( preg_match( $regexp, $regexp ) === false ) {
194  print "Invalid regular expression\n";
195  return;
196  }
197 
198  $count = 0;
199  $key = $cdb->firstkey();
200  while ( $key !== false ) {
201  if ( preg_match( $regexp, $key ) ) {
202  print "'$key'\n";
203  $count++;
204  }
205  $key = $cdb->nextkey();
206  }
207  print "$count keys found.\n";
208  }
209 }
210 
211 $maintClass = CdbInspector::class;
212 
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:41
CdbInspector
Definition: cdb.php:31
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:510
CdbInspector\__construct
__construct()
Default constructor.
Definition: cdb.php:32
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:354
Maintenance\readconsole
static readconsole( $prompt='> ')
Prompt the console for input.
Definition: Maintenance.php:1622
CdbInspector\findKeys
findKeys(CdbReader $cdb, ?string $regexp)
Definition: cdb.php:184
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:88
CdbInspector\showInternalHelp
showInternalHelp(?string $command)
Definition: cdb.php:38
$res
$res
Definition: testCompression.php:54
CdbInspector\listKeys
listKeys(CdbReader $cdb, int $maxKeys)
Definition: cdb.php:169
$args
if( $line===false) $args
Definition: mcc.php:124
CdbInspector\loadFile
loadFile(?string $fileName)
Definition: cdb.php:145
Maintenance\isQuiet
isQuiet()
Definition: Maintenance.php:449
$line
$line
Definition: mcc.php:119
$maintClass
$maintClass
Definition: cdb.php:211
Maintenance\$fileHandle
resource $fileHandle
Used when creating separate schema files.
Definition: Maintenance.php:169
$command
$command
Definition: mcc.php:125
$quit
$quit
Definition: mcc.php:117
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:325
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:377
CdbInspector\execute
execute()
Do the actual work.All child classes will need to implement thisbool|null|void True for success,...
Definition: cdb.php:67