Debian Codegraph @

maintenance/shell.php

raw symbols SCIP
1 <?php
2 /**
3 * Modern interactive shell within the MediaWiki engine.
4 *
5 * Merely wraps around http://psysh.org/ and drop an interactive PHP shell in
6 * the global scope.
7 *
8 * Copyright © 2017 Antoine Musso <[email protected]>
9 * Copyright © 2017 Gergő Tisza <[email protected]m>
10 * Copyright © 2017 Justin Hileman <[email protected]>
11 * Copyright © 2017 Wikimedia Foundation Inc.
12 * https://www.mediawiki.org/
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 * @author Antoine Musso <[email protected]>
33 * @author Justin Hileman <[email protected]>
34 * @author Gergő Tisza <[email protected]>
35 */
36
37 // NO_AUTOLOAD -- file-scope code
38
39 use MediaWiki\HookContainer\HookRunner;
40 use MediaWiki\Logger\ConsoleSpi;
41 use MediaWiki\Logger\LoggerFactory;
42 use MediaWiki\MediaWikiServices;
43 use Psr\Log\LogLevel;
44
45 // Horrible hack to support the --no-session parameter, which needs to be handled
46 // way before parameters are parsed.
47 if ( in_array( '--no-session', $_SERVER['argv'], true ) ) {
48 define( 'MW_NO_SESSION', 1 );
49 }
50
51 // @codeCoverageIgnoreStart
52 require_once __DIR__ . '/Maintenance.php';
53 // @codeCoverageIgnoreEnd
54
55 /**
56 * Interactive shell with completion and global scope.
57 *
58 */
59 class MediaWikiShell extends Maintenance {
60
61 public function __construct() {
62 parent::__construct();
63 $this->addOption( 'd',
64 'Deprecated, for back compatibility with eval.php. ' .
65 '1 send debug to stderr. ' .
66 'With 2 additionally initialize database with debugging ',
67 false, true
68 );
69 $this->addOption( 'log-channels', 'Send the given log channels to STDERR. '
70 . 'Format: channel[:level],...', false, true );
71 $this->addOption( 'log-all', 'Send all log channels to STDERR.' );
72 $this->addOption( 'dbo-debug', 'Set DBO_DEBUG flags (equivalent of $wgDebugDumpSql).' );
73 $this->addOption( 'no-session',
74 'Disable session support (like MW_NO_SESSION)'
75 );
76 }
77
78 public function canExecuteWithoutLocalSettings(): bool {
79 return true;
80 }
81
82 public function execute() {
83 if ( !class_exists( \Psy\Shell::class ) ) {
84 $this->fatalError( 'PsySH not found. Please run composer with the --dev option.' );
85 }
86
87 $traverser = new \PhpParser\NodeTraverser();
88 $codeCleaner = new \Psy\CodeCleaner( null, null, $traverser );
89
90 // add this after initializing the code cleaner so all the default passes get added first
91 $traverser->addVisitor( new CodeCleanerGlobalsPass() );
92
93 $config = new \Psy\Configuration();
94 $config->setCodeCleaner( $codeCleaner );
95 $config->setUpdateCheck( \Psy\VersionUpdater\Checker::NEVER );
96 // prevent https://github.com/bobthecow/psysh/issues/443 when using sudo -E
97 $config->setRuntimeDir( wfTempDir() );
98
99 $shell = new \Psy\Shell( $config );
100
101 $this->setupLogging();
102
103 ( new HookRunner( $this->getServiceContainer()->getHookContainer() ) )->onMaintenanceShellStart();
104
105 $shell->run();
106 }
107
108 protected function setupLogging() {
109 if ( $this->hasOption( 'd' ) ) {
110 wfDeprecated( 'shell.php -d', '1.40' );
111 $this->setupLegacy();
112 return;
113 }
114
115 if ( $this->hasOption( 'log-all' ) ) {
116 LoggerFactory::registerProvider( new ConsoleSpi( [
117 'forwardTo' => LoggerFactory::getProvider(),
118 ] ) );
119 // Some services hold Logger instances in object properties
120 MediaWikiServices::resetGlobalInstance();
121 MediaWikiServices::getInstance()->getObjectCacheFactory()->clear();
122 } elseif ( $this->hasOption( 'log-channels' ) ) {
123 $channelsArg = $this->getOption( 'log-channels' );
124 $channels = [];
125 foreach ( explode( ',', $channelsArg ) as $channelArg ) {
126 $parts = explode( ':', $channelArg );
127 $channel = $parts[0];
128 $level = $parts[1] ?? LogLevel::DEBUG;
129 $channels[$channel] = $level;
130 }
131 LoggerFactory::registerProvider( new ConsoleSpi( [
132 'channels' => $channels,
133 'forwardTo' => LoggerFactory::getProvider(),
134 ] ) );
135 MediaWikiServices::resetGlobalInstance();
136 MediaWikiServices::getInstance()->getObjectCacheFactory()->clear();
137 }
138 if ( $this->hasOption( 'dbo-debug' ) ) {
139 $this->getPrimaryDB()->setFlag( DBO_DEBUG );
140 $this->getReplicaDB()->setFlag( DBO_DEBUG );
141 }
142 }
143
144 /**
145 * For back compatibility with eval.php
146 */
147 protected function setupLegacy() {
148 $d = intval( $this->getOption( 'd' ) );
149 if ( $d > 0 ) {
150 LoggerFactory::registerProvider( new ConsoleSpi );
151 // Some services hold Logger instances in object properties
152 MediaWikiServices::resetGlobalInstance();
153 MediaWikiServices::getInstance()->getObjectCacheFactory()->clear();
154 }
155 if ( $d > 1 ) {
156 # Set DBO_DEBUG (equivalent of $wgDebugDumpSql)
157 $this->getPrimaryDB()->setFlag( DBO_DEBUG );
158 $this->getReplicaDB()->setFlag( DBO_DEBUG );
159 }
160 }
161
162 }
163
164 // @codeCoverageIgnoreStart
165 $maintClass = MediaWikiShell::class;
166 require_once RUN_MAINTENANCE_IF_MAIN;
167 // @codeCoverageIgnoreEnd