MediaWiki  master
shell.php
Go to the documentation of this file.
1 <?php
40 
41 require_once __DIR__ . '/Maintenance.php';
42 
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();
71  $config->setCodeCleaner( $codeCleaner );
72  $config->setUpdateCheck( \Psy\VersionUpdater\Checker::NEVER );
73  // prevent https://github.com/bobthecow/psysh/issues/443 when using sudo -E
74  $config->setRuntimeDir( wfTempDir() );
75 
76  $shell = new \Psy\Shell( $config );
77  if ( $this->hasOption( 'd' ) ) {
78  $this->setupLegacy();
79  }
80 
81  Hooks::runner()->onMaintenanceShellStart();
82 
83  $shell->run();
84  }
85 
89  protected function setupLegacy() {
90  $d = intval( $this->getOption( 'd' ) );
91  if ( $d > 0 ) {
92  LoggerFactory::registerProvider( new ConsoleSpi );
93  // Some services hold Logger instances in object properties
94  MediaWikiServices::resetGlobalInstance();
95  }
96  if ( $d > 1 ) {
97  # Set DBO_DEBUG (equivalent of $wgDebugDumpSql)
98  $this->getDB( DB_PRIMARY )->setFlag( DBO_DEBUG );
99  $this->getDB( DB_REPLICA )->setFlag( DBO_DEBUG );
100  }
101  }
102 
103 }
104 
105 $maintClass = MediaWikiShell::class;
106 require_once RUN_MAINTENANCE_IF_MAIN;
MediaWikiShell
Interactive shell with completion and global scope.
Definition: shell.php:47
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
MediaWikiShell\execute
execute()
Do the actual work.
Definition: shell.php:59
DBO_DEBUG
const DBO_DEBUG
Definition: defines.php:9
MediaWikiShell\__construct
__construct()
Default constructor.
Definition: shell.php:49
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
MediaWikiShell\setupLegacy
setupLegacy()
For back compatibility with eval.php.
Definition: shell.php:89
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
Maintenance\$config
Config null $config
Accessible via getConfig()
Definition: Maintenance.php:153
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
$maintClass
$maintClass
Definition: shell.php:105
CodeCleanerGlobalsPass
Prefix the real command with a 'global $VAR, $VAR2, ...;' command, where $VAR etc.
Definition: CodeCleanerGlobalsPass.php:37
MediaWiki\Logger\ConsoleSpi
Definition: ConsoleSpi.php:5
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1375
wfTempDir
wfTempDir()
Tries to get the system directory for temporary files.
Definition: GlobalFunctions.php:1712
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271