Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
18.37% covered (danger)
18.37%
9 / 49
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
LuaStandaloneEngine
18.37% covered (danger)
18.37%
9 / 49
28.57% covered (danger)
28.57%
2 / 7
174.21
0.00% covered (danger)
0.00%
0 / 1
 load
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getPerformanceCharacteristics
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 reportLimitData
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 formatLimitData
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getClockTick
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 newInterpreter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSoftwareInfo
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\Scribunto\Engines\LuaStandalone;
4
5use Exception;
6use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaEngine;
7use MediaWiki\Logger\LoggerFactory;
8use MediaWiki\Parser\ParserOutput;
9
10class LuaStandaloneEngine extends LuaEngine {
11    /** @var int|null */
12    protected static $clockTick;
13    /** @var array|false */
14    public $initialStatus;
15
16    /**
17     * @var LuaStandaloneInterpreter
18     */
19    protected $interpreter;
20
21    public function load() {
22        parent::load();
23        if ( php_uname( 's' ) === 'Linux' ) {
24            $this->initialStatus = $this->interpreter->getStatus();
25        } else {
26            $this->initialStatus = false;
27        }
28    }
29
30    /** @inheritDoc */
31    public function getPerformanceCharacteristics() {
32        return [
33            'phpCallsRequireSerialization' => true,
34        ];
35    }
36
37    /** @inheritDoc */
38    public function reportLimitData( ParserOutput $parserOutput ) {
39        try {
40            $this->load();
41        } catch ( Exception $e ) {
42            return;
43        }
44        if ( $this->initialStatus ) {
45            $status = $this->interpreter->getStatus();
46            $parserOutput->setLimitReportData( 'scribunto-limitreport-timeusage',
47                [
48                    sprintf( "%.3f", $status['time'] / $this->getClockTick() ),
49                    // Strip trailing .0s
50                    rtrim( rtrim( sprintf( "%.3f", $this->options['cpuLimit'] ), '0' ), '.' )
51                ]
52            );
53            $parserOutput->setLimitReportData( 'scribunto-limitreport-virtmemusage',
54                [
55                    $status['vsize'],
56                    $this->options['memoryLimit']
57                ]
58            );
59            $parserOutput->setLimitReportData( 'scribunto-limitreport-estmemusage',
60                $status['vsize'] - $this->initialStatus['vsize']
61            );
62        }
63        $logs = $this->getLogBuffer();
64        if ( $logs !== '' ) {
65            $parserOutput->addModules( [ 'ext.scribunto.logs' ] );
66            $parserOutput->setLimitReportData( 'scribunto-limitreport-logs', $logs );
67        }
68    }
69
70    /** @inheritDoc */
71    public function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) {
72        switch ( $key ) {
73            case 'scribunto-limitreport-logs':
74                if ( $isHTML ) {
75                    $report .= $this->formatHtmlLogs( $value, $localize );
76                }
77                return false;
78        }
79        return true;
80    }
81
82    /**
83     * @return int
84     */
85    protected function getClockTick() {
86        if ( self::$clockTick === null ) {
87            // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
88            self::$clockTick = intval( @shell_exec( 'getconf CLK_TCK' ) );
89            if ( !self::$clockTick ) {
90                self::$clockTick = 100;
91            }
92        }
93        return self::$clockTick;
94    }
95
96    /**
97     * @return LuaStandaloneInterpreter
98     */
99    protected function newInterpreter() {
100        return new LuaStandaloneInterpreter( $this, $this->options + [
101            'logger' => LoggerFactory::getInstance( 'Scribunto' )
102        ] );
103    }
104
105    /** @inheritDoc */
106    public function getSoftwareInfo( array &$software ) {
107        $ver = LuaStandaloneInterpreter::getLuaVersion( $this->options );
108        if ( $ver !== null ) {
109            if ( substr( $ver, 0, 6 ) === 'LuaJIT' ) {
110                $software['[http://luajit.org/ LuaJIT]'] = str_replace( 'LuaJIT ', '', $ver );
111            } else {
112                $software['[http://www.lua.org/ Lua]'] = str_replace( 'Lua ', '', $ver );
113            }
114        }
115    }
116}