| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\SyntaxHighlight; |
| 22 |
|
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use Shellbox\Command\BoxedCommand; |
| 25 |
use Shellbox\ShellboxError; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class Pygmentize { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static function useBundled(): bool { |
| 36 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 37 |
return $config->get( 'PygmentizePath' ) === false; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private static function getPath(): string { |
| 44 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 45 |
|
| 46 |
return $config->get( 'PygmentizePath' ) ?: __DIR__ . '/../pygments/pygmentize'; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static function getVersion(): string { |
| 53 |
static $version; |
| 54 |
if ( $version !== null ) { |
| 55 |
return $version; |
| 56 |
} |
| 57 |
if ( self::useBundled() ) { |
| 58 |
$version = self::getBundledVersion(); |
| 59 |
return $version; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
$srvCache = MediaWikiServices::getInstance()->getLocalServerObjectCache(); |
| 72 |
return $srvCache->getWithSetCallback( |
| 73 |
$srvCache->makeGlobalKey( 'pygmentize-version' ), |
| 74 |
|
| 75 |
mt_rand( 55 * $srvCache::TTL_MINUTE, 60 * $srvCache::TTL_MINUTE ), |
| 76 |
static function () { |
| 77 |
$wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 78 |
return $wanCache->getWithSetCallback( |
| 79 |
$wanCache->makeGlobalKey( 'pygmentize-version' ), |
| 80 |
|
| 81 |
30 * $wanCache::TTL_MINUTE, |
| 82 |
[ __CLASS__, 'fetchVersion' ] |
| 83 |
); |
| 84 |
} |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private static function getBundledVersion(): string { |
| 92 |
return trim( file_get_contents( __DIR__ . '/../pygments/VERSION' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static function fetchVersion(): string { |
| 101 |
$result = self::boxedCommand() |
| 102 |
->params( self::getPath(), '-V' ) |
| 103 |
->includeStderr() |
| 104 |
->execute(); |
| 105 |
self::recordShellout( 'version' ); |
| 106 |
|
| 107 |
$output = $result->getStdout(); |
| 108 |
if ( $result->getExitCode() != 0 || |
| 109 |
!preg_match( '/^Pygments version (\S+),/', $output, $matches ) |
| 110 |
) { |
| 111 |
throw new PygmentsException( $output ); |
| 112 |
} |
| 113 |
|
| 114 |
return $matches[1]; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static function getGeneratedCSS(): string { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 129 |
return $cache->getWithSetCallback( |
| 130 |
$cache->makeGlobalKey( 'pygmentize-css', self::getVersion() ), |
| 131 |
$cache::TTL_WEEK, |
| 132 |
[ __CLASS__, 'fetchGeneratedCSS' ] |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public static function fetchGeneratedCSS(): string { |
| 142 |
$lightModeRun = self::boxedCommand() |
| 143 |
->params( |
| 144 |
self::getPath(), '-f', 'html', |
| 145 |
'-S', 'default', '-a', '.mw-highlight' ) |
| 146 |
->includeStderr() |
| 147 |
->execute(); |
| 148 |
$darkModeRun = self::boxedCommand() |
| 149 |
->params( |
| 150 |
self::getPath(), '-f', 'html', |
| 151 |
'-S', 'monokai', '-a', ' .skin-theme-clientpref-night .mw-highlight' ) |
| 152 |
->includeStderr() |
| 153 |
->execute(); |
| 154 |
self::recordShellout( 'generated_css' ); |
| 155 |
|
| 156 |
$lightModeOutput = trim( $lightModeRun->getStdout() ); |
| 157 |
if ( $lightModeRun->getExitCode() != 0 ) { |
| 158 |
throw new PygmentsException( $lightModeOutput ); |
| 159 |
} |
| 160 |
$darkModeOutput = trim( $darkModeRun->getStdout() ); |
| 161 |
if ( $darkModeRun->getExitCode() != 0 ) { |
| 162 |
throw new PygmentsException( $darkModeOutput ); |
| 163 |
} |
| 164 |
|
| 165 |
$lightModeRules = explode( "\n", $lightModeOutput ); |
| 166 |
$darkModeRules = explode( "\n", $darkModeOutput ); |
| 167 |
$commonRules = array_intersect( $lightModeRules, $darkModeRules ); |
| 168 |
|
| 169 |
$nightThemeCss = implode( "\n", array_diff( $darkModeRules, $commonRules ) ); |
| 170 |
$osThemeCss = str_replace( '.skin-theme-clientpref-night', |
| 171 |
'.skin-theme-clientpref-os', $nightThemeCss ); |
| 172 |
|
| 173 |
return <<<EOD |
| 174 |
$lightModeOutput |
| 175 |
@media screen { |
| 176 |
$nightThemeCss |
| 177 |
} |
| 178 |
@media screen and ( prefers-color-scheme: dark ) { |
| 179 |
$osThemeCss |
| 180 |
} |
| 181 |
|
| 182 |
EOD; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public static function getLexers(): array { |
| 191 |
if ( self::useBundled() ) { |
| 192 |
return require __DIR__ . '/../SyntaxHighlight.lexers.php'; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
$cache = MediaWikiServices::getInstance()->getLocalServerObjectCache(); |
| 201 |
return $cache->getWithSetCallback( |
| 202 |
$cache->makeGlobalKey( 'pygmentize-lexers', self::getVersion() ), |
| 203 |
$cache::TTL_WEEK, |
| 204 |
[ __CLASS__, 'fetchLexers' ] |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
private static function pygmentsSupportsJsonOutput(): bool { |
| 214 |
$version = self::getVersion(); |
| 215 |
return ( version_compare( $version, '2.11.0' ) !== -1 ); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public static function fetchLexers(): array { |
| 225 |
$cliParams = [ self::getPath(), '-L', 'lexer' ]; |
| 226 |
if ( self::pygmentsSupportsJsonOutput() ) { |
| 227 |
$cliParams[] = '--json'; |
| 228 |
} |
| 229 |
|
| 230 |
$result = self::boxedCommand() |
| 231 |
->params( $cliParams ) |
| 232 |
->includeStderr() |
| 233 |
->execute(); |
| 234 |
self::recordShellout( 'fetch_lexers' ); |
| 235 |
$output = $result->getStdout(); |
| 236 |
if ( $result->getExitCode() != 0 ) { |
| 237 |
throw new PygmentsException( $output ); |
| 238 |
} |
| 239 |
|
| 240 |
if ( self::pygmentsSupportsJsonOutput() ) { |
| 241 |
$lexers = self::parseLexersFromJson( $output ); |
| 242 |
} else { |
| 243 |
$lexers = self::parseLexersFromText( $output ); |
| 244 |
} |
| 245 |
|
| 246 |
sort( $lexers ); |
| 247 |
return array_fill_keys( $lexers, true ); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private static function parseLexersFromJson( $output ): array { |
| 257 |
$data = json_decode( $output, true ); |
| 258 |
if ( $data === null ) { |
| 259 |
throw new PygmentsException( |
| 260 |
'Got invalid JSON from Pygments: ' . $output ); |
| 261 |
} |
| 262 |
$lexers = []; |
| 263 |
foreach ( array_values( $data['lexers'] ) as $lexer ) { |
| 264 |
$lexers = array_merge( $lexers, $lexer['aliases'] ); |
| 265 |
} |
| 266 |
return $lexers; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
private static function parseLexersFromText( $output ): array { |
| 278 |
$lexers = []; |
| 279 |
foreach ( explode( "\n", $output ) as $line ) { |
| 280 |
if ( str_starts_with( $line, '*' ) ) { |
| 281 |
$newLexers = explode( ', ', trim( $line, "* :\r\n" ) ); |
| 282 |
|
| 283 |
|
| 284 |
if ( $newLexers[0] !== '' ) { |
| 285 |
$lexers = array_merge( $lexers, $newLexers ); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
return $lexers; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public static function highlight( $lexer, $code, array $options ): string { |
| 301 |
$optionPairs = []; |
| 302 |
foreach ( $options as $k => $v ) { |
| 303 |
$optionPairs[] = "{$k}={$v}"; |
| 304 |
} |
| 305 |
self::recordShellout( 'highlight' ); |
| 306 |
|
| 307 |
try { |
| 308 |
$result = self::boxedCommand() |
| 309 |
->params( |
| 310 |
self::getPath(), |
| 311 |
'-l', $lexer, |
| 312 |
'-f', 'html', |
| 313 |
'-O', implode( ',', $optionPairs ), |
| 314 |
'file' |
| 315 |
) |
| 316 |
->inputFileFromString( 'file', $code ) |
| 317 |
->execute(); |
| 318 |
} catch ( ShellboxError $exception ) { |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
throw new PygmentsException( 'ShellboxError', 0, $exception ); |
| 325 |
} |
| 326 |
|
| 327 |
$output = $result->getStdout(); |
| 328 |
if ( $result->getExitCode() != 0 ) { |
| 329 |
if ( $output === "" || $output === null ) { |
| 330 |
|
| 331 |
$output = $result->getStderr(); |
| 332 |
} |
| 333 |
throw new PygmentsException( (string)$output ); |
| 334 |
} |
| 335 |
|
| 336 |
return $output; |
| 337 |
} |
| 338 |
|
| 339 |
private static function boxedCommand(): BoxedCommand { |
| 340 |
$command = MediaWikiServices::getInstance()->getShellCommandFactory() |
| 341 |
->createBoxed( 'syntaxhighlight' ) |
| 342 |
->disableNetwork() |
| 343 |
->firejailDefaultSeccomp() |
| 344 |
->routeName( 'syntaxhighlight-pygments' ); |
| 345 |
|
| 346 |
if ( wfIsWindows() ) { |
| 347 |
|
| 348 |
$command->environment( [ |
| 349 |
'SystemRoot' => getenv( 'SystemRoot' ), |
| 350 |
] ); |
| 351 |
} |
| 352 |
|
| 353 |
return $command; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
private static function recordShellout( $type ) { |
| 362 |
MediaWikiServices::getInstance()->getStatsFactory() |
| 363 |
->getCounter( 'syntaxhighlight_shell_total' ) |
| 364 |
->setLabel( 'type', $type ) |
| 365 |
->copyToStatsdAt( "syntaxhighlight_shell.$type" ) |
| 366 |
->increment(); |
| 367 |
} |
| 368 |
} |