Debian Codegraph @

maintenance/eval.php

raw symbols SCIP
1 <?php
2 /**
3 * This script lets a command-line user start up the wiki engine and then poke
4 * about by issuing PHP commands directly.
5 *
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.
10 *
11 * To get decent line editing behavior, you should compile PHP with support
12 * for GNU readline (pass --with-readline to configure).
13 *
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.
18 *
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.
23 *
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
28 *
29 * @file
30 * @ingroup Maintenance
31 */
32
33 use MediaWiki\HookContainer\HookRunner;
34 use MediaWiki\Logger\ConsoleSpi;
35 use MediaWiki\Logger\LoggerFactory;
36 use MediaWiki\MediaWikiServices;
37
38 // @codeCoverageIgnoreStart
39 require_once __DIR__ . '/Maintenance.php';
40 // @codeCoverageIgnoreEnd
41
42 /**
43 * Maintenance script providing an interactive console for evaluating php commands
44 * in the context of an initialized MediaWiki instance.
45 *
46 * @ingroup Maintenance
47 */
48 // phpcs:disable MediaWiki.Files.ClassMatchesFilename.NotMatch
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 // Some services hold Logger instances in object properties
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 // pull all globals into local scope
89 foreach ( $GLOBALS as $name => $unused ) {
90 // phpcs:disable MediaWiki.NamingConventions.ValidGlobalName.allowedPrefix
91 // phpcs:disable MediaWiki.VariableAnalysis.UnusedGlobalVariables.UnusedGlobal$name
92 global $$name;
93 }
94
95 $__ignoreErrors = $this->hasOption( 'ignore-errors' );
96
97 $__useReadline = function_exists( 'readline_add_history' )
98 && Maintenance::posix_isatty( 0 /*STDIN*/ );
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; // PHP exception
112 // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
113 while ( ( $__line = Maintenance::readconsole() ) !== false ) {
114 if ( !$__ignoreErrors && $__e && !preg_match( '/^(exit|die);?$/', $__line ) ) {
115 // Internal state may be corrupted or fatals may occur later due
116 // to some object not being set. Don't drop out of eval in case
117 // lines were being pasted in (which would then get dumped to the shell).
118 // Instead, just absorb the remaining commands. Let "exit" through per DWIM.
119 echo "Exception was thrown before; please restart eval.php\n";
120 continue;
121 }
122 if ( $__useReadline ) {
123 readline_add_history( $__line );
124 // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal
125 readline_write_history( $__historyFile );
126 }
127 try {
128 // @phan-suppress-next-next-line SecurityCheck-RCE
129 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.eval
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 // @codeCoverageIgnoreStart
158 $maintClass = MWEval::class;
159 require_once RUN_MAINTENANCE_IF_MAIN;
160 // @codeCoverageIgnoreEnd