Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
LuaError
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getLuaMessage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getScriptTraceHtml
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2
3namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
4
5use MediaWiki\Extension\Scribunto\ScribuntoException;
6use MediaWiki\Html\Html;
7use MediaWiki\Title\Title;
8
9class LuaError extends ScribuntoException {
10    /** @var string */
11    public $luaMessage;
12
13    /**
14     * @param string $message
15     * @param array $options
16     */
17    public function __construct( $message, array $options = [] ) {
18        $this->luaMessage = $message;
19        $options += [ 'args' => [ $message ] ];
20        if ( isset( $options['module'] ) && isset( $options['line'] ) ) {
21            $msg = 'scribunto-lua-error-location';
22        } else {
23            $msg = 'scribunto-lua-error';
24        }
25
26        parent::__construct( $msg, $options );
27    }
28
29    /**
30     * @return string
31     */
32    public function getLuaMessage() {
33        return $this->luaMessage;
34    }
35
36    /**
37     * @param array $options Options for message processing. Currently supports:
38     * $options['msgOptions']['content'] to use content language.
39     * @return bool|string
40     */
41    public function getScriptTraceHtml( $options = [] ) {
42        if ( !isset( $this->params['trace'] ) ) {
43            return false;
44        }
45        $msgOptions = $options['msgOptions'] ?? [];
46
47        $s = '<ol class="scribunto-trace">';
48        foreach ( $this->params['trace'] as $info ) {
49            $short_src = $info['short_src'];
50            $currentline = $info['currentline'];
51
52            $src = htmlspecialchars( $short_src );
53            if ( $currentline > 0 ) {
54                $src .= ':' . htmlspecialchars( $currentline );
55
56                $title = Title::newFromText( $short_src );
57                if ( $title && $title->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
58                    $title = $title->createFragmentTarget( 'mw-ce-l' . $currentline );
59                    $src = Html::rawElement( 'a',
60                        [ 'href' => $title->getFullURL( 'action=edit' ) ],
61                        $src );
62                }
63            }
64
65            if ( strval( $info['namewhat'] ) !== '' ) {
66                $functionMsg = wfMessage( 'scribunto-lua-in-function', wfEscapeWikiText( $info['name'] ) );
67                in_array( 'content', $msgOptions ) ?
68                    $function = $functionMsg->inContentLanguage()->plain() :
69                    $function = $functionMsg->plain();
70            } elseif ( $info['what'] == 'main' ) {
71                $functionMsg = wfMessage( 'scribunto-lua-in-main' );
72                in_array( 'content', $msgOptions ) ?
73                    $function = $functionMsg->inContentLanguage()->plain() :
74                    $function = $functionMsg->plain();
75            } else {
76                // C function, tail call, or a Lua function where Lua can't
77                // guess the name
78                $function = '?';
79            }
80
81            $backtraceLineMsg = wfMessage( 'scribunto-lua-backtrace-line' )
82                ->rawParams( "<strong>$src</strong>" )
83                ->params( $function );
84            $backtraceLine = in_array( 'content', $msgOptions ) ?
85                $backtraceLineMsg->inContentLanguage()->parse() :
86                $backtraceLineMsg->parse();
87
88            $s .= "<li>$backtraceLine</li>";
89        }
90        $s .= '</ol>';
91        return $s;
92    }
93}
94
95class_alias( LuaError::class, 'Scribunto_LuaError' );