MediaWiki master
MissingExtensionException.php
Go to the documentation of this file.
1<?php
2
4
5use Exception;
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
51 private function renderHtml() {
52 if ( !headers_sent() ) {
53 HttpStatus::header( 500 );
54 header( 'Content-Type: text/html; charset=UTF-8' );
55 }
56
57 $templateParser = new TemplateParser( null, new EmptyBagOStuff() );
58
59 try {
60 echo $templateParser->processTemplate(
61 'ExtensionConfigError',
62 [
63 'version' => MW_VERSION,
64 'path' => $this->path,
65 'type' => $this->isSkin ? 'skin' : 'extension',
66 'error' => $this->error,
67 'extName' => $this->extName,
68 'trace' => $this->getTraceAsString(),
69 'mwLogo' => $this->getMWLogo(),
70 ]
71 );
72 } catch ( Exception $e ) {
73 echo 'Error: ' . htmlspecialchars( $e->getMessage() );
74 }
75 }
76
80 private function renderText() {
81 $type = $this->isSkin ? 'skin' : 'extension';
82 echo "Error: The $this->extName $type cannot be loaded. "
83 . "Check that all of its files are installed properly.\n\n";
84 echo $this->getTraceAsString();
85 echo "\n";
86 }
87
93 public function render(): never {
94 if ( wfIsCli() ) {
95 $this->renderText();
96 } else {
97 $this->renderHtml();
98 }
99 // Make sure that the error gets into logs.
100 error_log( $this->getMessage() );
101 exit( 1 );
102 }
103
109 private function getMWLogo() {
110 global $wgResourceBasePath;
111 $suffix = "/resources/assets/mediawiki.png";
112 if ( $wgResourceBasePath !== null ) {
113 // We are early in setup, so we can't rely on this.
114 return $wgResourceBasePath . $suffix;
115 }
116 $path = '/';
117 foreach ( array_filter( explode( '/', $_SERVER['PHP_SELF'] ) ) as $part ) {
118 if ( !preg_match( '/\.php$/', $part ) ) {
119 $path .= "$part/";
120 } else {
121 break;
122 }
123 }
124
125 return $path . $suffix;
126 }
127}
128
130class_alias( MissingExtensionException::class, 'MissingExtensionException' );
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:23
$templateParser
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
Handles compiling Mustache templates into PHP rendering functions.
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.