| 1 |
<?php |
| 2 |
// phpcs:disable Generic.Files.LineLength.TooLong |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Tools; |
| 5 |
|
| 6 |
global $argv; |
| 7 |
|
| 8 |
// Hacky preprocessing of command-line arguments: look for |
| 9 |
// --integrated and/or --standalone flags. |
| 10 |
use MediaWiki\Settings\SettingsBuilder; |
| 11 |
|
| 12 |
$parsoidMode = null; |
| 13 |
for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) { |
| 14 |
if ( $arg === '--' ) { |
| 15 |
# end of options |
| 16 |
break; |
| 17 |
} elseif ( $arg === '--integrated' || $arg === '--standalone' ) { |
| 18 |
$parsoidMode = substr( $arg, 2 ); |
| 19 |
break; |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
# On parsoidtest1001 and production machines, you should use: |
| 24 |
# sudo -u www-data php /srv/mediawiki/multiversion/MWScript.php \ |
| 25 |
# /srv/parsoid-testing/bin/<cmd>.php --wiki=hiwiki --integrated <args> |
| 26 |
# |
| 27 |
# eg: |
| 28 |
# |
| 29 |
# USER@parsoidtest1001:/srv/mediawiki/multiversion$ echo '==Foo==' | \ |
| 30 |
# sudo -u www-data php MWScript.php \ |
| 31 |
# /srv/parsoid-testing/bin/parse.php --wiki=hiwiki --integrated |
| 32 |
# |
| 33 |
|
| 34 |
// phpcs:disable Generic.Classes.DuplicateClassName.Found |
| 35 |
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 36 |
if ( $parsoidMode === 'integrated' ) { |
| 37 |
/* Is MW installed w/ Parsoid? Then use core's copy of Maintenance.php. */ |
| 38 |
if ( strval( getenv( 'MW_INSTALL_PATH' ) ) !== '' ) { |
| 39 |
require_once getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'; |
| 40 |
require_once getenv( 'MW_INSTALL_PATH' ) . '/includes/AutoLoader.php'; |
| 41 |
} else { |
| 42 |
error_log( 'MW_INSTALL_PATH environment variable must be defined.' ); |
| 43 |
} |
| 44 |
|
| 45 |
// EVIL(ish) hack: |
| 46 |
// Override autoloader to ensure all of Parsoid is running from the |
| 47 |
// same place as this file (since there will also be another copy of |
| 48 |
// Parsoid included from the vendor/wikimedia/parsoid directory) |
| 49 |
// @phan-suppress-next-line PhanUndeclaredClassStaticProperty |
| 50 |
\AutoLoader::registerNamespaces( [ |
| 51 |
// Keep this in sync with the "autoload" clause in /composer.json! |
| 52 |
'Wikimedia\\Parsoid\\' => __DIR__ . "/../src", |
| 53 |
// And this is from autoload-dev |
| 54 |
'Wikimedia\\Parsoid\\Tools\\' => __DIR__ . "/../tools/", |
| 55 |
] ); |
| 56 |
|
| 57 |
abstract class Maintenance extends \Maintenance { |
| 58 |
private $requiresParsoid; |
| 59 |
|
| 60 |
/** |
| 61 |
* @param bool $requiresParsoid Whether parsoid-specific processing |
| 62 |
* should be done (default: true) |
| 63 |
*/ |
| 64 |
public function __construct( bool $requiresParsoid = true ) { |
| 65 |
parent::__construct(); |
| 66 |
$this->requiresParsoid = $requiresParsoid; |
| 67 |
if ( $this->requiresParsoid ) { |
| 68 |
$this->requireExtension( 'Parsoid' ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @inheritDoc |
| 74 |
*/ |
| 75 |
public function finalSetup( ?SettingsBuilder $settingsBuilder = null ) { |
| 76 |
parent::finalSetup( $settingsBuilder ?? SettingsBuilder::getInstance() ); |
| 77 |
} |
| 78 |
|
| 79 |
public function addDefaultParams(): void { |
| 80 |
if ( $this->requiresParsoid ) { |
| 81 |
$this->addOption( |
| 82 |
'integrated', |
| 83 |
'Run parsoid integrated with a host MediaWiki installation ' . |
| 84 |
'at MW_INSTALL_PATH' |
| 85 |
); |
| 86 |
$this->addOption( |
| 87 |
'standalone', |
| 88 |
'Run parsoid standalone, communicating with a host MediaWiki ' . |
| 89 |
'using network API (see --domain option)' |
| 90 |
); |
| 91 |
$this->addOption( |
| 92 |
'mock', |
| 93 |
'Use mock environment instead of api or standalone' |
| 94 |
); |
| 95 |
} |
| 96 |
parent::addDefaultParams(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Make the protected method from the superclass into a public method |
| 101 |
* so we can use this from TestUtils.php. |
| 102 |
* |
| 103 |
* @inheritDoc |
| 104 |
*/ |
| 105 |
public function addOption( |
| 106 |
$name, $description, $required = false, |
| 107 |
$withArg = false, $shortName = false, |
| 108 |
$multiOccurrence = false |
| 109 |
) { |
| 110 |
parent::addOption( |
| 111 |
$name, $description, $required, $withArg, $shortName, |
| 112 |
$multiOccurrence |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Make the options array available to ExtendedOptsProcessor |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
protected function getOptions(): array { |
| 122 |
return $this->mOptions; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
define( 'PARSOID_RUN_MAINTENANCE_IF_MAIN', RUN_MAINTENANCE_IF_MAIN ); |
| 127 |
|
| 128 |
// Ensure parsoid extension is loaded on production machines. |
| 129 |
$_SERVER['SERVERGROUP'] = 'parsoid'; |
| 130 |
} else { |
| 131 |
/* Use Parsoid's stand-alone clone of the Maintenance framework */ |
| 132 |
require_once __DIR__ . '/../vendor/autoload.php'; |
| 133 |
|
| 134 |
abstract class Maintenance extends OptsProcessor { |
| 135 |
/** @var bool Whether to perform Parsoid-specific processing */ |
| 136 |
private $requiresParsoid; |
| 137 |
|
| 138 |
/** |
| 139 |
* @param bool $requiresParsoid Whether parsoid-specific processing |
| 140 |
* should be done (default: true) |
| 141 |
*/ |
| 142 |
public function __construct( bool $requiresParsoid = true ) { |
| 143 |
parent::__construct(); |
| 144 |
$this->requiresParsoid = $requiresParsoid; |
| 145 |
} |
| 146 |
|
| 147 |
public function addDefaultParams(): void { |
| 148 |
if ( $this->requiresParsoid ) { |
| 149 |
$this->addOption( |
| 150 |
'integrated', |
| 151 |
'Run parsoid integrated with a host MediaWiki installation ' . |
| 152 |
'at MW_INSTALL_PATH' |
| 153 |
); |
| 154 |
$this->addOption( |
| 155 |
'standalone', |
| 156 |
'Run parsoid standalone, communicating with a host MediaWiki ' . |
| 157 |
'using network API (see --domain option)' |
| 158 |
); |
| 159 |
$this->addOption( |
| 160 |
'mock', |
| 161 |
'Use mock environment instead of api or standalone' |
| 162 |
); |
| 163 |
} |
| 164 |
parent::addDefaultParams(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Make the options array available to ExtendedOptsProcessor |
| 169 |
* |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
protected function getOptions(): array { |
| 173 |
return $this->options; |
| 174 |
} |
| 175 |
|
| 176 |
public function setup() { |
| 177 |
# Abort if called from a web server |
| 178 |
# wfIsCLI() is not available yet |
| 179 |
if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) { |
| 180 |
$this->fatalError( 'This script must be run from the command line' ); |
| 181 |
} |
| 182 |
# Make sure we can handle script parameters |
| 183 |
if ( !ini_get( 'register_argc_argv' ) ) { |
| 184 |
$this->fatalError( 'Cannot get command line arguments, register_argc_argv is set to false' ); |
| 185 |
} |
| 186 |
|
| 187 |
// Send PHP warnings and errors to stderr instead of stdout. |
| 188 |
// This aids in diagnosing problems, while keeping messages |
| 189 |
// out of redirected output. |
| 190 |
if ( ini_get( 'display_errors' ) ) { |
| 191 |
ini_set( 'display_errors', 'stderr' ); |
| 192 |
} |
| 193 |
|
| 194 |
$this->loadParamsAndArgs(); |
| 195 |
|
| 196 |
# Set max execution time to 0 (no limit). PHP.net says that |
| 197 |
# "When running PHP from the command line the default setting is 0." |
| 198 |
# But sometimes this doesn't seem to be the case. |
| 199 |
ini_set( 'max_execution_time', 0 ); |
| 200 |
|
| 201 |
# Turn off output buffering if it's on |
| 202 |
while ( ob_get_level() > 0 ) { |
| 203 |
ob_end_flush(); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** For compatibility with core. */ |
| 208 |
public static function requireTestsAutoloader() { |
| 209 |
/* do nothing, for compatibility only */ |
| 210 |
} |
| 211 |
|
| 212 |
/** For compatibility with core */ |
| 213 |
public function finalSetup() { |
| 214 |
if ( ob_get_level() ) { |
| 215 |
ob_end_flush(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Implementation copied from core |
| 221 |
* |
| 222 |
* Wrapper for posix_isatty() |
| 223 |
* We default as considering stdin a tty (for nice readline methods) |
| 224 |
* but treating stdout as not a tty to avoid color codes |
| 225 |
* |
| 226 |
* @param mixed $fd File descriptor |
| 227 |
* @return bool |
| 228 |
*/ |
| 229 |
public static function posix_isatty( $fd ): bool { // phpcs:ignore MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName |
| 230 |
if ( !function_exists( 'posix_isatty' ) ) { |
| 231 |
return !$fd; |
| 232 |
} else { |
| 233 |
return posix_isatty( $fd ); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
define( 'PARSOID_RUN_MAINTENANCE_IF_MAIN', __DIR__ . '/doMaintenance.php' ); |
| 239 |
} |