| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
use MediaWiki\HookContainer\HookRunner; |
| 34 |
use MediaWiki\Logger\ConsoleSpi; |
| 35 |
use MediaWiki\Logger\LoggerFactory; |
| 36 |
use MediaWiki\MediaWikiServices; |
| 37 |
|
| 38 |
|
| 39 |
require_once __DIR__ . '/Maintenance.php'; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class MWEval extends Maintenance { |
| 50 |
public function __construct() { |
| 51 |
parent::__construct(); |
| 52 |
$this->addDescription( |
| 53 |
'Maintenance script providing an interactive console for evaluating php' . |
| 54 |
'commands in the context of an initialized MediaWiki instance.' |
| 55 |
); |
| 56 |
|
| 57 |
$this->addOption( |
| 58 |
'd', |
| 59 |
'Enable (some) debug output', |
| 60 |
false, |
| 61 |
true |
| 62 |
); |
| 63 |
|
| 64 |
$this->addOption( |
| 65 |
'ignore-errors', |
| 66 |
'Ignore (some) errors' |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
public function canExecuteWithoutLocalSettings(): bool { |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
public function execute() { |
| 75 |
if ( $this->hasOption( 'd' ) ) { |
| 76 |
$d = $this->getOption( 'd' ); |
| 77 |
if ( $d > 0 ) { |
| 78 |
LoggerFactory::registerProvider( new ConsoleSpi ); |
| 79 |
|
| 80 |
MediaWikiServices::resetGlobalInstance(); |
| 81 |
} |
| 82 |
if ( $d > 1 ) { |
| 83 |
$this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase()->setFlag( DBO_DEBUG ); |
| 84 |
$this->getServiceContainer()->getConnectionProvider()->getReplicaDatabase()->setFlag( DBO_DEBUG ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
foreach ( $GLOBALS as $name => $unused ) { |
| 90 |
|
| 91 |
|
| 92 |
global $$name; |
| 93 |
} |
| 94 |
|
| 95 |
$__ignoreErrors = $this->hasOption( 'ignore-errors' ); |
| 96 |
|
| 97 |
$__useReadline = function_exists( 'readline_add_history' ) |
| 98 |
&& Maintenance::posix_isatty( 0 ); |
| 99 |
|
| 100 |
if ( $__useReadline ) { |
| 101 |
$home = getenv( 'HOME' ); |
| 102 |
$__historyFile = $home ? |
| 103 |
"$home/.mweval_history" : ( MW_INSTALL_PATH . "/maintenance/.mweval_history" ); |
| 104 |
readline_read_history( $__historyFile ); |
| 105 |
} else { |
| 106 |
$__historyFile = null; |
| 107 |
} |
| 108 |
|
| 109 |
( new HookRunner( $this->getServiceContainer()->getHookContainer() ) )->onMaintenanceShellStart(); |
| 110 |
|
| 111 |
$__e = null; |
| 112 |
|
| 113 |
while ( ( $__line = Maintenance::readconsole() ) !== false ) { |
| 114 |
if ( !$__ignoreErrors && $__e && !preg_match( '/^(exit|die);?$/', $__line ) ) { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
echo "Exception was thrown before; please restart eval.php\n"; |
| 120 |
continue; |
| 121 |
} |
| 122 |
if ( $__useReadline ) { |
| 123 |
readline_add_history( $__line ); |
| 124 |
|
| 125 |
readline_write_history( $__historyFile ); |
| 126 |
} |
| 127 |
try { |
| 128 |
|
| 129 |
|
| 130 |
$__val = eval( $__line . ";" ); |
| 131 |
} catch ( Exception $__e ) { |
| 132 |
fwrite( STDERR, "Caught exception " . get_class( $__e ) . |
| 133 |
": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" ); |
| 134 |
continue; |
| 135 |
} catch ( Throwable $__e ) { |
| 136 |
if ( $__ignoreErrors ) { |
| 137 |
fwrite( STDERR, "Caught " . get_class( $__e ) . |
| 138 |
": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" ); |
| 139 |
continue; |
| 140 |
} else { |
| 141 |
throw $__e; |
| 142 |
} |
| 143 |
} |
| 144 |
if ( $__val === null ) { |
| 145 |
echo "\n"; |
| 146 |
} elseif ( is_string( $__val ) || is_numeric( $__val ) ) { |
| 147 |
echo "$__val\n"; |
| 148 |
} else { |
| 149 |
var_dump( $__val ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
echo "\n"; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
$maintClass = MWEval::class; |
| 159 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 160 |
|