Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / maintenance / shell.php
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 use MediaWiki\Logger\LoggerFactory;
38 use MediaWiki\Logger\ConsoleSpi;
39 use MediaWiki\MediaWikiServices;
40
41 require_once __DIR__ . '/Maintenance.php';
42
43 /**
44 * Interactive shell with completion and global scope.
45 *
46 */
47 class MediaWikiShell extends Maintenance {
48
49 public function __construct() {
50 parent::__construct();
51 $this->addOption( 'd',
52 'For back compatibility with eval.php. ' .
53 '1 send debug to stderr. ' .
54 'With 2 additionally initialize database with debugging ',
55 false, true
56 );
57 }
58
59 public function execute() {
60 if ( !class_exists( \Psy\Shell::class ) ) {
61 $this->fatalError( 'PsySH not found. Please run composer with the --dev option.' );
62 }
63
64 $traverser = new \PhpParser\NodeTraverser();
65 $codeCleaner = new \Psy\CodeCleaner( null, null, $traverser );
66
67 // add this after initializing the code cleaner so all the default passes get added first
68 $traverser->addVisitor( new CodeCleanerGlobalsPass() );
69
70 $config = new \Psy\Configuration( [ 'codeCleaner' => $codeCleaner ] );
71 $config->setUpdateCheck( \Psy\VersionUpdater\Checker::NEVER );
72 $shell = new \Psy\Shell( $config );
73 if ( $this->hasOption( 'd' ) ) {
74 $this->setupLegacy();
75 }
76
77 $shell->run();
78 }
79
80 /**
81 * For back compatibility with eval.php
82 */
83 protected function setupLegacy() {
84 $d = intval( $this->getOption( 'd' ) );
85 if ( $d > 0 ) {
86 LoggerFactory::registerProvider( new ConsoleSpi );
87 // Some services hold Logger instances in object properties
88 MediaWikiServices::resetGlobalInstance();
89 }
90 if ( $d > 1 ) {
91 # Set DBO_DEBUG (equivalent of $wgDebugDumpSql)
92 wfGetDB( DB_MASTER )->setFlag( DBO_DEBUG );
93 wfGetDB( DB_REPLICA )->setFlag( DBO_DEBUG );
94 }
95 }
96
97 }
98
99 $maintClass = MediaWikiShell::class;
100 require_once RUN_MAINTENANCE_IF_MAIN;