3 * This script lets a command-line user start up the wiki engine and then poke
4 * about by issuing PHP commands directly.
6 * Unlike eg Python, you need to use a 'return' statement explicitly for the
7 * interactive shell to print out the value of the expression. Multiple lines
8 * are evaluated separately, so blocks need to be input without a line break.
9 * Fatal errors such as use of undeclared functions can kill the shell.
11 * To get decent line editing behavior, you should compile PHP with support
12 * for GNU readline (pass --with-readline to configure).
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * http://www.gnu.org/copyleft/gpl.html
30 * @ingroup Maintenance
33 use MediaWiki\HookContainer\HookRunner
;
34 use MediaWiki\Logger\ConsoleSpi
;
35 use MediaWiki\Logger\LoggerFactory
;
36 use MediaWiki\MediaWikiServices
;
38 require_once __DIR__
. '/Maintenance.php';
41 * Maintenance script providing an interactive console for evaluating php commands
42 * in the context of an initialized MediaWiki instance.
44 * @ingroup Maintenance
46 // phpcs:disable MediaWiki.Files.ClassMatchesFilename.NotMatch
47 class MWEval
extends Maintenance
{
48 public function __construct() {
49 parent
::__construct();
50 $this->addDescription(
51 'Maintenance script providing an interactive console for evaluating php' .
52 'commands in the context of an initialized MediaWiki instance.'
57 'Enable (some) debug output',
64 'Ignore (some) errors'
68 public function canExecuteWithoutLocalSettings(): bool {
72 public function execute() {
73 if ( $this->hasOption( 'd' ) ) {
74 $d = $this->getOption( 'd' );
76 LoggerFactory
::registerProvider( new ConsoleSpi
);
77 // Some services hold Logger instances in object properties
78 MediaWikiServices
::resetGlobalInstance();
81 wfGetDB( DB_PRIMARY
)->setFlag( DBO_DEBUG
);
82 wfGetDB( DB_REPLICA
)->setFlag( DBO_DEBUG
);
86 // pull all globals into local scope
87 foreach ( $GLOBALS as $name => $unused ) {
88 // phpcs:disable MediaWiki.NamingConventions.ValidGlobalName.allowedPrefix
89 // phpcs:disable MediaWiki.VariableAnalysis.UnusedGlobalVariables.UnusedGlobal$name
93 $__ignoreErrors = $this->hasOption( 'ignore-errors' );
95 $__useReadline = function_exists( 'readline_add_history' )
96 && Maintenance
::posix_isatty( 0 /*STDIN*/ );
98 if ( $__useReadline ) {
99 $__historyFile = isset( $_ENV['HOME'] ) ?
100 "{$_ENV['HOME']}/.mweval_history" : ( MW_INSTALL_PATH
. "/maintenance/.mweval_history" );
101 readline_read_history( $__historyFile );
103 $__historyFile = null;
106 ( new HookRunner( $this->getServiceContainer()->getHookContainer() ) )->onMaintenanceShellStart();
108 $__e = null; // PHP exception
109 while ( ( $__line = Maintenance
::readconsole() ) !== false ) {
110 if ( !$__ignoreErrors && $__e && !preg_match( '/^(exit|die);?$/', $__line ) ) {
111 // Internal state may be corrupted or fatals may occur later due
112 // to some object not being set. Don't drop out of eval in case
113 // lines were being pasted in (which would then get dumped to the shell).
114 // Instead, just absorb the remaining commands. Let "exit" through per DWIM.
115 echo "Exception was thrown before; please restart eval.php\n";
118 if ( $__useReadline ) {
119 readline_add_history( $__line );
120 // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal
121 readline_write_history( $__historyFile );
124 // @phan-suppress-next-next-line SecurityCheck-RCE
125 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.eval
126 $__val = eval( $__line . ";" );
127 } catch ( Exception
$__e ) {
128 fwrite( STDERR
, "Caught exception " . get_class( $__e ) .
129 ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
131 } catch ( Throwable
$__e ) {
132 if ( $__ignoreErrors ) {
133 fwrite( STDERR
, "Caught " . get_class( $__e ) .
134 ": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
140 if ( $__val === null ) {
142 } elseif ( is_string( $__val ) ||
is_numeric( $__val ) ) {
153 $maintClass = MWEval
::class;
154 require_once RUN_MAINTENANCE_IF_MAIN
;