mediawiki-extensions-Scribunto/includes/engines/LuaStandalone/LuaStandaloneEngine.php
C. Scott Ananian 6633e446d9 Passing a string to ParserOutput::addModules()/addModuleStyles() is deprecated
In addition, several variables which contained instances of ParserOutput
were renamed to $parserOutput to help future humans and code-searchers
to distinguish these from instances of OutputPage.

Bug: T296123
Change-Id: Ic532bca4348b17882716fcb2ca8656a04766c095
2022-01-12 11:31:31 -05:00

115 lines
2.9 KiB
PHP

<?php
use MediaWiki\Logger\LoggerFactory;
class Scribunto_LuaStandaloneEngine extends Scribunto_LuaEngine {
/** @var int|null */
protected static $clockTick;
/** @var array|false */
public $initialStatus;
/**
* @var Scribunto_LuaStandaloneInterpreter
*/
protected $interpreter;
public function load() {
parent::load();
if ( php_uname( 's' ) === 'Linux' ) {
$this->initialStatus = $this->interpreter->getStatus();
} else {
$this->initialStatus = false;
}
}
/** @inheritDoc */
public function getPerformanceCharacteristics() {
return [
'phpCallsRequireSerialization' => true,
];
}
/** @inheritDoc */
public function reportLimitData( ParserOutput $parserOutput ) {
try {
$this->load();
} catch ( Exception $e ) {
return;
}
if ( $this->initialStatus ) {
$status = $this->interpreter->getStatus();
$parserOutput->setLimitReportData( 'scribunto-limitreport-timeusage',
[
sprintf( "%.3f", $status['time'] / $this->getClockTick() ),
// Strip trailing .0s
rtrim( rtrim( sprintf( "%.3f", $this->options['cpuLimit'] ), '0' ), '.' )
]
);
$parserOutput->setLimitReportData( 'scribunto-limitreport-virtmemusage',
[
$status['vsize'],
$this->options['memoryLimit']
]
);
$parserOutput->setLimitReportData( 'scribunto-limitreport-estmemusage',
$status['vsize'] - $this->initialStatus['vsize']
);
}
$logs = $this->getLogBuffer();
if ( $logs !== '' ) {
$parserOutput->addModules( [ 'ext.scribunto.logs' ] );
$parserOutput->setLimitReportData( 'scribunto-limitreport-logs', $logs );
}
}
/** @inheritDoc */
public function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) {
global $wgLang;
$lang = $localize ? $wgLang : Language::factory( 'en' );
switch ( $key ) {
case 'scribunto-limitreport-logs':
if ( $isHTML ) {
$report .= $this->formatHtmlLogs( $value, $localize );
}
return false;
}
return true;
}
/**
* @return int
*/
protected function getClockTick() {
if ( self::$clockTick === null ) {
Wikimedia\suppressWarnings();
self::$clockTick = intval( shell_exec( 'getconf CLK_TCK' ) );
Wikimedia\restoreWarnings();
if ( !self::$clockTick ) {
self::$clockTick = 100;
}
}
return self::$clockTick;
}
/**
* @return Scribunto_LuaStandaloneInterpreter
*/
protected function newInterpreter() {
return new Scribunto_LuaStandaloneInterpreter( $this, $this->options + [
'logger' => LoggerFactory::getInstance( 'Scribunto' )
] );
}
/** @inheritDoc */
public function getSoftwareInfo( array &$software ) {
$ver = Scribunto_LuaStandaloneInterpreter::getLuaVersion( $this->options );
if ( $ver !== null ) {
if ( substr( $ver, 0, 6 ) === 'LuaJIT' ) {
$software['[http://luajit.org/ LuaJIT]'] = str_replace( 'LuaJIT ', '', $ver );
} else {
$software['[http://www.lua.org/ Lua]'] = str_replace( 'Lua ', '', $ver );
}
}
}
}