MediaWiki  master
eval.php
Go to the documentation of this file.
1 <?php
36 
37 $optionsWithArgs = [ 'd' ];
38 $optionsWithoutArgs = [ 'ignore-errors' ];
39 
40 require_once __DIR__ . "/CommandLineInc.php";
41 
42 if ( isset( $options['d'] ) ) {
43  $d = $options['d'];
44  if ( $d > 0 ) {
45  LoggerFactory::registerProvider( new ConsoleSpi );
46  // Some services hold Logger instances in object properties
47  MediaWikiServices::resetGlobalInstance();
48  }
49  if ( $d > 1 ) {
50  wfGetDB( DB_PRIMARY )->setFlag( DBO_DEBUG );
51  wfGetDB( DB_REPLICA )->setFlag( DBO_DEBUG );
52  }
53 }
54 
55 $__ignoreErrors = isset( $options['ignore-errors'] );
56 
57 $__useReadline = function_exists( 'readline_add_history' )
58  && Maintenance::posix_isatty( 0 /*STDIN*/ );
59 
60 if ( $__useReadline ) {
61  $__historyFile = isset( $_ENV['HOME'] ) ?
62  "{$_ENV['HOME']}/.mweval_history" : "$IP/maintenance/.mweval_history";
63  readline_read_history( $__historyFile );
64 }
65 
66 Hooks::runner()->onMaintenanceShellStart();
67 
68 $__e = null; // PHP exception
69 while ( ( $__line = Maintenance::readconsole() ) !== false ) {
70  if ( !$__ignoreErrors && $__e && !preg_match( '/^(exit|die);?$/', $__line ) ) {
71  // Internal state may be corrupted or fatals may occur later due
72  // to some object not being set. Don't drop out of eval in case
73  // lines were being pasted in (which would then get dumped to the shell).
74  // Instead, just absorb the remaining commands. Let "exit" through per DWIM.
75  echo "Exception was thrown before; please restart eval.php\n";
76  continue;
77  }
78  if ( $__useReadline ) {
79  readline_add_history( $__line );
80  // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal
81  readline_write_history( $__historyFile );
82  }
83  try {
84  // @phan-suppress-next-line SecurityCheck-RCE
85  $__val = eval( $__line . ";" );
86  } catch ( Exception $__e ) {
87  fwrite( STDERR, "Caught exception " . get_class( $__e ) .
88  ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
89  continue;
90  } catch ( Throwable $__e ) {
91  if ( $__ignoreErrors ) {
92  fwrite( STDERR, "Caught " . get_class( $__e ) .
93  ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
94  continue;
95  } else {
96  throw $__e;
97  }
98  }
99  if ( $__val === null ) {
100  echo "\n";
101  } elseif ( is_string( $__val ) || is_numeric( $__val ) ) {
102  echo "$__val\n";
103  } else {
104  var_dump( $__val );
105  }
106 }
107 
108 print "\n";
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
static readconsole( $prompt='> ')
Prompt the console for input.
static posix_isatty( $fd)
Wrapper for posix_isatty() We default as considering stdin a tty (for nice readline methods) but trea...
PSR-3 logger instance factory.
Service locator for MediaWiki core services.
$optionsWithoutArgs
Definition: eval.php:38
$optionsWithArgs
Definition: eval.php:37
$__useReadline
Definition: eval.php:57
if(isset( $options['d'])) $__ignoreErrors
Definition: eval.php:55
$__e
Definition: eval.php:68
const DB_REPLICA
Definition: defines.php:26
const DBO_DEBUG
Definition: defines.php:9
const DB_PRIMARY
Definition: defines.php:28