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