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 remaning 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  readline_write_history( $__historyFile );
81  }
82  try {
83  // @phan-suppress-next-line SecurityCheck-RCE
84  $__val = eval( $__line . ";" );
85  } catch ( Exception $__e ) {
86  fwrite( STDERR, "Caught exception " . get_class( $__e ) .
87  ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
88  continue;
89  } catch ( Throwable $__e ) {
90  if ( $__ignoreErrors ) {
91  fwrite( STDERR, "Caught " . get_class( $__e ) .
92  ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
93  continue;
94  } else {
95  throw $__e;
96  }
97  }
98  if ( $__val === null ) {
99  echo "\n";
100  } elseif ( is_string( $__val ) || is_numeric( $__val ) ) {
101  echo "$__val\n";
102  } else {
103  var_dump( $__val );
104  }
105 }
106 
107 print "\n";
$optionsWithoutArgs
$optionsWithoutArgs
Definition: eval.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:199
Maintenance\readconsole
static readconsole( $prompt='> ')
Prompt the console for input.
Definition: Maintenance.php:1502
DBO_DEBUG
const DBO_DEBUG
Definition: defines.php:9
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2200
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
$__e
$__e
Definition: eval.php:68
MediaWiki\Logger\ConsoleSpi
Definition: ConsoleSpi.php:5
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
Maintenance\posix_isatty
static posix_isatty( $fd)
Wrapper for posix_isatty() We default as considering stdin a tty (for nice readline methods) but trea...
Definition: Maintenance.php:1489
$optionsWithArgs
$optionsWithArgs
Definition: eval.php:37
$__useReadline
$__useReadline
Definition: eval.php:57
$__ignoreErrors
if(isset( $options['d'])) $__ignoreErrors
Definition: eval.php:55