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_MASTER )->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 $__e = null; // PHP exception
67 while ( ( $__line = Maintenance::readconsole() ) !== false ) {
68  if ( !$__ignoreErrors && $__e && !preg_match( '/^(exit|die);?$/', $__line ) ) {
69  // Internal state may be corrupted or fatals may occur later due
70  // to some object not being set. Don't drop out of eval in case
71  // lines were being pasted in (which would then get dumped to the shell).
72  // Instead, just absorb the remaning commands. Let "exit" through per DWIM.
73  echo "Exception was thrown before; please restart eval.php\n";
74  continue;
75  }
76  if ( $__useReadline ) {
77  readline_add_history( $__line );
78  readline_write_history( $__historyFile );
79  }
80  try {
81  // @phan-suppress-next-line SecurityCheck-RCE eval is wanted in this script
82  $__val = eval( $__line . ";" );
83  } catch ( Exception $__e ) {
84  fwrite( STDERR, "Caught exception " . get_class( $__e ) .
85  ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
86  continue;
87  } catch ( Throwable $__e ) {
88  if ( $__ignoreErrors ) {
89  fwrite( STDERR, "Caught " . get_class( $__e ) .
90  ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
91  continue;
92  } else {
93  throw $__e;
94  }
95  }
96  if ( $__val === null ) {
97  echo "\n";
98  } elseif ( is_string( $__val ) || is_numeric( $__val ) ) {
99  echo "$__val\n";
100  } else {
101  var_dump( $__val );
102  }
103 }
104 
105 print "\n";
$optionsWithoutArgs
$optionsWithoutArgs
Definition: eval.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
Maintenance\readconsole
static readconsole( $prompt='> ')
Prompt the console for input.
Definition: Maintenance.php:1608
$__e
if( $__useReadline) $__e
Definition: eval.php:66
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:2466
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
MediaWiki\Logger\ConsoleSpi
Definition: ConsoleSpi.php:5
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:1595
$optionsWithArgs
$optionsWithArgs
Definition: eval.php:37
$__useReadline
$__useReadline
Definition: eval.php:57
$__ignoreErrors
if(isset( $options['d'])) $__ignoreErrors
Definition: eval.php:55