MediaWiki master
MissingExtensionException.php
Go to the documentation of this file.
1<?php
2
4
5use Exception;
6use HttpStatus;
9
22class MissingExtensionException extends Exception {
23 private bool $isSkin;
24 private string $extName = 'unknown';
25 private string $path;
26 private string $error;
27
32 public function __construct( string $path, string $error ) {
33 $this->isSkin = str_ends_with( $path, "/skin.json" );
34 $m = [];
35 preg_match( "!/([^/]*)/[^/]*.json$!", $path, $m );
36 if ( $m ) {
37 $this->extName = $m[1];
38 }
39 $this->path = $path;
40 $this->error = $error;
41
42 parent::__construct( "Error Loading extension. Unable to open file $path: $error" );
43 }
44
52 private function renderHtml() {
53 if ( !headers_sent() ) {
54 HttpStatus::header( 500 );
55 header( 'Content-Type: text/html; charset=UTF-8' );
56 }
57
58 $templateParser = new TemplateParser( null, new EmptyBagOStuff() );
59
60 try {
61 echo $templateParser->processTemplate(
62 'ExtensionConfigError',
63 [
64 'version' => MW_VERSION,
65 'path' => $this->path,
66 'type' => $this->isSkin ? 'skin' : 'extension',
67 'error' => $this->error,
68 'extName' => $this->extName,
69 'trace' => $this->getTraceAsString(),
70 'mwLogo' => $this->getMWLogo(),
71 ]
72 );
73 } catch ( Exception $e ) {
74 echo 'Error: ' . htmlspecialchars( $e->getMessage() );
75 }
76 }
77
81 private function renderText() {
82 $type = $this->isSkin ? 'skin' : 'extension';
83 echo "Error: The $this->extName $type cannot be loaded. "
84 . "Check that all of its files are installed properly.\n\n";
85 echo $this->getTraceAsString();
86 echo "\n";
87 }
88
94 public function render() {
95 if ( wfIsCli() ) {
96 $this->renderText();
97 } else {
98 $this->renderHtml();
99 }
100 // Make sure that the error gets into logs.
101 // This will also stop execution.
102 trigger_error( $this->getMessage(), E_USER_ERROR );
103 }
104
110 private function getMWLogo() {
111 global $wgResourceBasePath;
112 $suffix = "/resources/assets/mediawiki.png";
113 if ( $wgResourceBasePath !== null ) {
114 // We are early in setup, so we can't rely on this.
115 return $wgResourceBasePath . $suffix;
116 }
117 $path = '/';
118 foreach ( array_filter( explode( '/', $_SERVER['PHP_SELF'] ) ) as $part ) {
119 if ( !preg_match( '/\.php$/', $part ) ) {
120 $path .= "$part/";
121 } else {
122 break;
123 }
124 }
125
126 return $path . $suffix;
127 }
128}
129
131class_alias( MissingExtensionException::class, 'MissingExtensionException' );
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:37
$templateParser
Thrown when ExtensionRegistry cannot open the extension.json or skin.json file.
No-op implementation that stores nothing.
$wgResourceBasePath
Config variable stub for the ResourceBasePath setting, for use by phpdoc and IDEs.