Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaWikiShell
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 5
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 canExecuteWithoutLocalSettings
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 setupLogging
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
42
 setupLegacy
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
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
39use MediaWiki\HookContainer\HookRunner;
40use MediaWiki\Logger\ConsoleSpi;
41use MediaWiki\Logger\LoggerFactory;
42use MediaWiki\MediaWikiServices;
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
53/**
54 * Interactive shell with completion and global scope.
55 *
56 */
57class MediaWikiShell extends Maintenance {
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 canExecuteWithoutLocalSettings(): bool {
77        return true;
78    }
79
80    public function execute() {
81        if ( !class_exists( \Psy\Shell::class ) ) {
82            $this->fatalError( 'PsySH not found. Please run composer with the --dev option.' );
83        }
84
85        $traverser = new \PhpParser\NodeTraverser();
86        $codeCleaner = new \Psy\CodeCleaner( null, null, $traverser );
87
88        // add this after initializing the code cleaner so all the default passes get added first
89        $traverser->addVisitor( new CodeCleanerGlobalsPass() );
90
91        $config = new \Psy\Configuration();
92        $config->setCodeCleaner( $codeCleaner );
93        $config->setUpdateCheck( \Psy\VersionUpdater\Checker::NEVER );
94        // prevent https://github.com/bobthecow/psysh/issues/443 when using sudo -E
95        $config->setRuntimeDir( wfTempDir() );
96
97        $shell = new \Psy\Shell( $config );
98
99        $this->setupLogging();
100
101        ( new HookRunner( $this->getServiceContainer()->getHookContainer() ) )->onMaintenanceShellStart();
102
103        $shell->run();
104    }
105
106    protected function setupLogging() {
107        if ( $this->hasOption( 'd' ) ) {
108            wfDeprecated( 'shell.php -d', '1.40' );
109            $this->setupLegacy();
110            return;
111        }
112
113        if ( $this->hasOption( 'log-all' ) ) {
114            LoggerFactory::registerProvider( new ConsoleSpi( [
115                'forwardTo' => LoggerFactory::getProvider(),
116            ] ) );
117            // Some services hold Logger instances in object properties
118            MediaWikiServices::resetGlobalInstance();
119            ObjectCache::clear();
120        } elseif ( $this->hasOption( 'log-channels' ) ) {
121            $channelsArg = $this->getOption( 'log-channels' );
122            $channels = [];
123            foreach ( explode( ',', $channelsArg ) as $channelArg ) {
124                $parts = explode( ':', $channelArg );
125                $channel = $parts[0];
126                $level = $parts[1] ?? LogLevel::DEBUG;
127                $channels[$channel] = $level;
128            }
129            LoggerFactory::registerProvider( new ConsoleSpi( [
130                'channels' => $channels,
131                'forwardTo' => LoggerFactory::getProvider(),
132            ] ) );
133            MediaWikiServices::resetGlobalInstance();
134            ObjectCache::clear();
135        }
136        if ( $this->hasOption( 'dbo-debug' ) ) {
137            $this->getPrimaryDB()->setFlag( DBO_DEBUG );
138            $this->getReplicaDB()->setFlag( DBO_DEBUG );
139        }
140    }
141
142    /**
143     * For back compatibility with eval.php
144     */
145    protected function setupLegacy() {
146        $d = intval( $this->getOption( 'd' ) );
147        if ( $d > 0 ) {
148            LoggerFactory::registerProvider( new ConsoleSpi );
149            // Some services hold Logger instances in object properties
150            MediaWikiServices::resetGlobalInstance();
151            ObjectCache::clear();
152        }
153        if ( $d > 1 ) {
154            # Set DBO_DEBUG (equivalent of $wgDebugDumpSql)
155            $this->getPrimaryDB()->setFlag( DBO_DEBUG );
156            $this->getReplicaDB()->setFlag( DBO_DEBUG );
157        }
158    }
159
160}
161
162$maintClass = MediaWikiShell::class;
163require_once RUN_MAINTENANCE_IF_MAIN;