2004-06-02 02:16:40 +00:00
|
|
|
<?php
|
2004-09-03 20:33:01 +00:00
|
|
|
/**
|
2004-11-15 00:15:58 +00:00
|
|
|
* This script lets a command-line user start up the wiki engine and then poke
|
|
|
|
|
* about by issuing PHP commands directly.
|
|
|
|
|
*
|
|
|
|
|
* Unlike eg Python, you need to use a 'return' statement explicitly for the
|
|
|
|
|
* interactive shell to print out the value of the expression. Multiple lines
|
|
|
|
|
* are evaluated separately, so blocks need to be input without a line break.
|
|
|
|
|
* Fatal errors such as use of undeclared functions can kill the shell.
|
|
|
|
|
*
|
2006-01-07 13:09:30 +00:00
|
|
|
* To get decent line editing behavior, you should compile PHP with support
|
2004-11-15 00:15:58 +00:00
|
|
|
* for GNU readline (pass --with-readline to configure).
|
|
|
|
|
*
|
2010-12-16 19:15:12 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2009-08-12 03:44:06 +00:00
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2004-09-03 20:33:01 +00:00
|
|
|
*/
|
|
|
|
|
|
2023-05-06 20:01:10 +00:00
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2017-04-27 23:56:38 +00:00
|
|
|
use MediaWiki\Logger\ConsoleSpi;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2017-04-28 01:50:10 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-04-27 23:56:38 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2023-01-13 18:54:08 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2005-04-12 04:03:21 +00:00
|
|
|
|
2023-01-13 18:54:08 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script providing an interactive console for evaluating php commands
|
|
|
|
|
* in the context of an initialized MediaWiki instance.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
// phpcs:disable MediaWiki.Files.ClassMatchesFilename.NotMatch
|
|
|
|
|
class MWEval extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addDescription(
|
|
|
|
|
'Maintenance script providing an interactive console for evaluating php' .
|
|
|
|
|
'commands in the context of an initialized MediaWiki instance.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->addOption(
|
|
|
|
|
'd',
|
|
|
|
|
'Enable (some) debug output',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
2012-01-12 23:10:22 +00:00
|
|
|
|
2023-01-13 18:54:08 +00:00
|
|
|
$this->addOption(
|
|
|
|
|
'ignore-errors',
|
|
|
|
|
'Ignore (some) errors'
|
|
|
|
|
);
|
2012-01-14 22:21:30 +00:00
|
|
|
}
|
2023-01-13 18:54:08 +00:00
|
|
|
|
|
|
|
|
public function canExecuteWithoutLocalSettings(): bool {
|
|
|
|
|
return true;
|
2012-01-14 22:21:30 +00:00
|
|
|
}
|
2009-08-12 03:44:06 +00:00
|
|
|
|
2023-01-13 18:54:08 +00:00
|
|
|
public function execute() {
|
|
|
|
|
if ( $this->hasOption( 'd' ) ) {
|
|
|
|
|
$d = $this->getOption( 'd' );
|
|
|
|
|
if ( $d > 0 ) {
|
|
|
|
|
LoggerFactory::registerProvider( new ConsoleSpi );
|
|
|
|
|
// Some services hold Logger instances in object properties
|
|
|
|
|
MediaWikiServices::resetGlobalInstance();
|
|
|
|
|
}
|
|
|
|
|
if ( $d > 1 ) {
|
2024-02-13 17:20:17 +00:00
|
|
|
$this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase()->setFlag( DBO_DEBUG );
|
|
|
|
|
$this->getServiceContainer()->getConnectionProvider()->getReplicaDatabase()->setFlag( DBO_DEBUG );
|
2023-01-13 18:54:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-10 03:02:53 +00:00
|
|
|
|
2023-01-13 18:54:08 +00:00
|
|
|
// pull all globals into local scope
|
|
|
|
|
foreach ( $GLOBALS as $name => $unused ) {
|
|
|
|
|
// phpcs:disable MediaWiki.NamingConventions.ValidGlobalName.allowedPrefix
|
|
|
|
|
// phpcs:disable MediaWiki.VariableAnalysis.UnusedGlobalVariables.UnusedGlobal$name
|
|
|
|
|
global $$name;
|
|
|
|
|
}
|
2009-08-12 03:44:06 +00:00
|
|
|
|
2023-01-13 18:54:08 +00:00
|
|
|
$__ignoreErrors = $this->hasOption( 'ignore-errors' );
|
2012-01-12 23:10:22 +00:00
|
|
|
|
2023-01-13 18:54:08 +00:00
|
|
|
$__useReadline = function_exists( 'readline_add_history' )
|
|
|
|
|
&& Maintenance::posix_isatty( 0 /*STDIN*/ );
|
2020-08-26 07:40:40 +00:00
|
|
|
|
2023-01-13 18:54:08 +00:00
|
|
|
if ( $__useReadline ) {
|
2024-08-13 09:46:45 +00:00
|
|
|
$home = getenv( 'HOME' );
|
|
|
|
|
$__historyFile = $home ?
|
|
|
|
|
"$home/.mweval_history" : ( MW_INSTALL_PATH . "/maintenance/.mweval_history" );
|
2023-01-13 18:54:08 +00:00
|
|
|
readline_read_history( $__historyFile );
|
2020-12-10 03:02:53 +00:00
|
|
|
} else {
|
2023-01-13 18:54:08 +00:00
|
|
|
$__historyFile = null;
|
2020-12-10 03:02:53 +00:00
|
|
|
}
|
2023-01-13 18:54:08 +00:00
|
|
|
|
2023-08-31 09:21:12 +00:00
|
|
|
( new HookRunner( $this->getServiceContainer()->getHookContainer() ) )->onMaintenanceShellStart();
|
2023-01-13 18:54:08 +00:00
|
|
|
|
|
|
|
|
$__e = null; // PHP exception
|
2024-08-31 21:34:30 +00:00
|
|
|
// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
|
2023-01-13 18:54:08 +00:00
|
|
|
while ( ( $__line = Maintenance::readconsole() ) !== false ) {
|
|
|
|
|
if ( !$__ignoreErrors && $__e && !preg_match( '/^(exit|die);?$/', $__line ) ) {
|
|
|
|
|
// Internal state may be corrupted or fatals may occur later due
|
|
|
|
|
// to some object not being set. Don't drop out of eval in case
|
|
|
|
|
// lines were being pasted in (which would then get dumped to the shell).
|
|
|
|
|
// Instead, just absorb the remaining commands. Let "exit" through per DWIM.
|
|
|
|
|
echo "Exception was thrown before; please restart eval.php\n";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ( $__useReadline ) {
|
|
|
|
|
readline_add_history( $__line );
|
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal
|
|
|
|
|
readline_write_history( $__historyFile );
|
|
|
|
|
}
|
|
|
|
|
try {
|
2023-04-13 10:57:51 +00:00
|
|
|
// @phan-suppress-next-next-line SecurityCheck-RCE
|
|
|
|
|
// phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.eval
|
2023-01-13 18:54:08 +00:00
|
|
|
$__val = eval( $__line . ";" );
|
|
|
|
|
} catch ( Exception $__e ) {
|
|
|
|
|
fwrite( STDERR, "Caught exception " . get_class( $__e ) .
|
|
|
|
|
": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
|
|
|
|
|
continue;
|
|
|
|
|
} catch ( Throwable $__e ) {
|
|
|
|
|
if ( $__ignoreErrors ) {
|
|
|
|
|
fwrite( STDERR, "Caught " . get_class( $__e ) .
|
|
|
|
|
": {$__e->getMessage()}\n" . $__e->getTraceAsString() . "\n" );
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
throw $__e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $__val === null ) {
|
|
|
|
|
echo "\n";
|
|
|
|
|
} elseif ( is_string( $__val ) || is_numeric( $__val ) ) {
|
|
|
|
|
echo "$__val\n";
|
|
|
|
|
} else {
|
|
|
|
|
var_dump( $__val );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-14 22:21:30 +00:00
|
|
|
echo "\n";
|
2009-06-24 02:49:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2023-01-13 18:54:08 +00:00
|
|
|
$maintClass = MWEval::class;
|
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|