MediaWiki REL1_41
shell.php
Go to the documentation of this file.
1<?php
37// NO_AUTOLOAD -- file-scope code
38
43use Psr\Log\LogLevel;
44
45// Horrible hack to support the --no-session parameter, which needs to be handled
46// way before parameters are parsed.
47if ( in_array( '--no-session', $_SERVER['argv'], true ) ) {
48 define( 'MW_NO_SESSION', 1 );
49}
50
51require_once __DIR__ . '/Maintenance.php';
52
58
59 public function __construct() {
60 parent::__construct();
61 $this->addOption( 'd',
62 'Deprecated, for back compatibility with eval.php. ' .
63 '1 send debug to stderr. ' .
64 'With 2 additionally initialize database with debugging ',
65 false, true
66 );
67 $this->addOption( 'log-channels', 'Send the given log channels to STDERR. '
68 . 'Format: channel[:level],...', false, true );
69 $this->addOption( 'log-all', 'Send all log channels to STDERR.' );
70 $this->addOption( 'dbo-debug', 'Set DBO_DEBUG flags (equivalent of $wgDebugDumpSql).' );
71 $this->addOption( 'no-session',
72 'Disable session support (like MW_NO_SESSION)'
73 );
74 }
75
76 public function execute() {
77 if ( !class_exists( \Psy\Shell::class ) ) {
78 $this->fatalError( 'PsySH not found. Please run composer with the --dev option.' );
79 }
80
81 $traverser = new \PhpParser\NodeTraverser();
82 $codeCleaner = new \Psy\CodeCleaner( null, null, $traverser );
83
84 // add this after initializing the code cleaner so all the default passes get added first
85 $traverser->addVisitor( new CodeCleanerGlobalsPass() );
86
87 $config = new \Psy\Configuration();
88 $config->setCodeCleaner( $codeCleaner );
89 $config->setUpdateCheck( \Psy\VersionUpdater\Checker::NEVER );
90 // prevent https://github.com/bobthecow/psysh/issues/443 when using sudo -E
91 $config->setRuntimeDir( wfTempDir() );
92
93 $shell = new \Psy\Shell( $config );
94
95 $this->setupLogging();
96
97 ( new HookRunner( $this->getServiceContainer()->getHookContainer() ) )->onMaintenanceShellStart();
98
99 $shell->run();
100 }
101
102 protected function setupLogging() {
103 if ( $this->hasOption( 'd' ) ) {
104 wfDeprecated( 'shell.php -d', '1.40' );
105 $this->setupLegacy();
106 return;
107 }
108
109 if ( $this->hasOption( 'log-all' ) ) {
110 LoggerFactory::registerProvider( new ConsoleSpi( [
111 'forwardTo' => LoggerFactory::getProvider(),
112 ] ) );
113 // Some services hold Logger instances in object properties
114 MediaWikiServices::resetGlobalInstance();
115 } elseif ( $this->hasOption( 'log-channels' ) ) {
116 $channelsArg = $this->getOption( 'log-channels' );
117 $channels = [];
118 foreach ( explode( ',', $channelsArg ) as $channelArg ) {
119 $parts = explode( ':', $channelArg );
120 $channel = $parts[0];
121 $level = $parts[1] ?? LogLevel::DEBUG;
122 $channels[$channel] = $level;
123 }
124 LoggerFactory::registerProvider( new ConsoleSpi( [
125 'channels' => $channels,
126 'forwardTo' => LoggerFactory::getProvider(),
127 ] ) );
128 MediaWikiServices::resetGlobalInstance();
129 }
130 if ( $this->hasOption( 'dbo-debug' ) ) {
131 $this->getDB( DB_PRIMARY )->setFlag( DBO_DEBUG );
132 $this->getDB( DB_REPLICA )->setFlag( DBO_DEBUG );
133 }
134 }
135
139 protected function setupLegacy() {
140 $d = intval( $this->getOption( 'd' ) );
141 if ( $d > 0 ) {
142 LoggerFactory::registerProvider( new ConsoleSpi );
143 // Some services hold Logger instances in object properties
144 MediaWikiServices::resetGlobalInstance();
145 }
146 if ( $d > 1 ) {
147 # Set DBO_DEBUG (equivalent of $wgDebugDumpSql)
148 $this->getDB( DB_PRIMARY )->setFlag( DBO_DEBUG );
149 $this->getDB( DB_REPLICA )->setFlag( DBO_DEBUG );
150 }
151 }
152
153}
154
155$maintClass = MediaWikiShell::class;
156require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
wfTempDir()
Tries to get the system directory for temporary files.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Prefix the real command with a 'global $VAR, $VAR2, ...;' command, where $VAR etc.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getHookContainer()
Get a HookContainer, for running extension hooks or for hook metadata.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Interactive shell with completion and global scope.
Definition shell.php:57
execute()
Do the actual work.
Definition shell.php:76
setupLegacy()
For back compatibility with eval.php.
Definition shell.php:139
__construct()
Default constructor.
Definition shell.php:59
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
ConsoleLogger service provider for MediaWiki\Logger\LoggerFactory.
Create PSR-3 logger objects.
Service locator for MediaWiki core services.
const DB_REPLICA
Definition defines.php:26
const DBO_DEBUG
Definition defines.php:9
const DB_PRIMARY
Definition defines.php:28
$maintClass
Definition shell.php:155