MediaWiki master
MissingExtensionException.php
Go to the documentation of this file.
1<?php
4
17class MissingExtensionException extends Exception {
18 private bool $isSkin;
19 private string $extName = 'unknown';
20 private string $path;
21 private string $error;
22
27 public function __construct( string $path, string $error ) {
28 $this->isSkin = str_ends_with( $path, "/skin.json" );
29 $m = [];
30 preg_match( "!/([^/]*)/[^/]*.json$!", $path, $m );
31 if ( $m ) {
32 $this->extName = $m[1];
33 }
34 $this->path = $path;
35 $this->error = $error;
36
37 parent::__construct( "Error Loading extension. Unable to open file $path: $error" );
38 }
39
47 private function renderHtml() {
48 if ( !headers_sent() ) {
49 HttpStatus::header( 500 );
50 header( 'Content-Type: text/html; charset=UTF-8' );
51 }
52
54
55 try {
56 echo $templateParser->processTemplate(
57 'ExtensionConfigError',
58 [
59 'version' => MW_VERSION,
60 'path' => $this->path,
61 'type' => $this->isSkin ? 'skin' : 'extension',
62 'error' => $this->error,
63 'extName' => $this->extName,
64 'trace' => $this->getTraceAsString(),
65 'mwLogo' => $this->getMWLogo(),
66 ]
67 );
68 } catch ( Exception $e ) {
69 echo 'Error: ' . htmlspecialchars( $e->getMessage() );
70 }
71 }
72
76 private function renderText() {
77 $type = $this->isSkin ? 'skin' : 'extension';
78 echo "Error: The $this->extName $type cannot be loaded. "
79 . "Check that all of its files are installed properly.\n\n";
80 echo $this->getTraceAsString();
81 echo "\n";
82 }
83
89 public function render() {
90 if ( wfIsCli() ) {
91 $this->renderText();
92 } else {
93 $this->renderHtml();
94 }
95 // Make sure that the error gets into logs.
96 // This will also stop execution.
97 trigger_error( $this->getMessage(), E_USER_ERROR );
98 }
99
105 private function getMWLogo() {
106 global $wgResourceBasePath;
107 $suffix = "/resources/assets/mediawiki.png";
108 if ( $wgResourceBasePath !== null ) {
109 // We are early in setup, so we can't rely on this.
110 return $wgResourceBasePath . $suffix;
111 }
112 $path = '/';
113 foreach ( array_filter( explode( '/', $_SERVER['PHP_SELF'] ) ) as $part ) {
114 if ( !preg_match( '/\.php$/', $part ) ) {
115 $path .= "$part/";
116 } else {
117 break;
118 }
119 }
120
121 return $path . $suffix;
122 }
123}
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.
__construct(string $path, string $error)
render()
Output an error response and exit.
A BagOStuff object with no objects in it.
$wgResourceBasePath
Config variable stub for the ResourceBasePath setting, for use by phpdoc and IDEs.