| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Scribunto\ScribuntoException; |
| 6 |
use MediaWiki\Html\Html; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
|
| 9 |
class LuaError extends ScribuntoException { |
| 10 |
|
| 11 |
public $luaMessage; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 31 |
|
| 32 |
public function getLuaMessage() { |
| 33 |
return $this->luaMessage; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 77 |
|
| 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 |
|
| 95 |
class_alias( LuaError::class, 'Scribunto_LuaError' ); |