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