| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
> |
| 9 |
m> |
| 10 |
> |
| 11 |
. |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
> |
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 46 |
|
| 47 |
if ( in_array( '--no-session', $_SERVER['argv'], true ) ) { |
| 48 |
define( 'MW_NO_SESSION', 1 ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
require_once __DIR__ . '/Maintenance.php'; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 91 |
$traverser->addVisitor( new CodeCleanerGlobalsPass() ); |
| 92 |
|
| 93 |
$config = new \Psy\Configuration(); |
| 94 |
$config->setCodeCleaner( $codeCleaner ); |
| 95 |
$config->setUpdateCheck( \Psy\VersionUpdater\Checker::NEVER ); |
| 96 |
|
| 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 |
|
| 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 |
|
| 146 |
|
| 147 |
protected function setupLegacy() { |
| 148 |
$d = intval( $this->getOption( 'd' ) ); |
| 149 |
if ( $d > 0 ) { |
| 150 |
LoggerFactory::registerProvider( new ConsoleSpi ); |
| 151 |
|
| 152 |
MediaWikiServices::resetGlobalInstance(); |
| 153 |
MediaWikiServices::getInstance()->getObjectCacheFactory()->clear(); |
| 154 |
} |
| 155 |
if ( $d > 1 ) { |
| 156 |
|
| 157 |
$this->getPrimaryDB()->setFlag( DBO_DEBUG ); |
| 158 |
$this->getReplicaDB()->setFlag( DBO_DEBUG ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
$maintClass = MediaWikiShell::class; |
| 166 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 167 |
|