summaryrefslogtreecommitdiff
path: root/includes/Exception.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/Exception.php')
-rw-r--r--includes/Exception.php87
1 files changed, 55 insertions, 32 deletions
diff --git a/includes/Exception.php b/includes/Exception.php
index 714f73e8..0bd7a2a7 100644
--- a/includes/Exception.php
+++ b/includes/Exception.php
@@ -64,8 +64,8 @@ class MWException extends Exception {
/**
* Run hook to allow extensions to modify the text of the exception
*
- * @param $name string: class name of the exception
- * @param $args array: arguments to pass to the callback functions
+ * @param string $name class name of the exception
+ * @param array $args arguments to pass to the callback functions
* @return string|null string to output or null if any hook has been called
*/
function runHooks( $name, $args = array() ) {
@@ -83,7 +83,7 @@ class MWException extends Exception {
$callargs = array_merge( array( $this ), $args );
foreach ( $hooks as $hook ) {
- if ( is_string( $hook ) || ( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { // 'function' or array( 'class', hook' )
+ if ( is_string( $hook ) || ( is_array( $hook ) && count( $hook ) >= 2 && is_string( $hook[0] ) ) ) { // 'function' or array( 'class', hook' )
$result = call_user_func_array( $hook, $callargs );
} else {
$result = null;
@@ -99,8 +99,8 @@ class MWException extends Exception {
/**
* Get a message from i18n
*
- * @param $key string: message name
- * @param $fallback string: default message if the message cache can't be
+ * @param string $key message name
+ * @param string $fallback default message if the message cache can't be
* called by the exception
* The function also has other parameters that are arguments for the message
* @return string message with arguments replaced
@@ -171,7 +171,7 @@ class MWException extends Exception {
/**
* Get a random ID for this error.
- * This allows to link the exception to its correspoding log entry when
+ * This allows to link the exception to its corresponding log entry when
* $wgShowExceptionDetails is set to false.
*
* @return string
@@ -262,7 +262,7 @@ class MWException extends Exception {
if ( defined( 'MW_API' ) ) {
// Unhandled API exception, we can't be sure that format printer is alive
header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $this ) );
- wfHttpError(500, 'Internal Server Error', $this->getText() );
+ wfHttpError( 500, 'Internal Server Error', $this->getText() );
} elseif ( self::isCommandLine() ) {
MWExceptionHandler::printError( $this->getText() );
} else {
@@ -320,16 +320,16 @@ class ErrorPageError extends MWException {
/**
* Note: these arguments are keys into wfMessage(), not text!
*
- * @param $title string|Message Message key (string) for page title, or a Message object
- * @param $msg string|Message Message key (string) for error text, or a Message object
- * @param $params array with parameters to wfMessage()
+ * @param string|Message $title Message key (string) for page title, or a Message object
+ * @param string|Message $msg Message key (string) for error text, or a Message object
+ * @param array $params with parameters to wfMessage()
*/
function __construct( $title, $msg, $params = null ) {
$this->title = $title;
$this->msg = $msg;
$this->params = $params;
- if( $msg instanceof Message ){
+ if( $msg instanceof Message ) {
parent::__construct( $msg );
} else {
parent::__construct( wfMessage( $msg )->text() );
@@ -354,8 +354,8 @@ class ErrorPageError extends MWException {
*/
class BadTitleError extends ErrorPageError {
/**
- * @param $msg string|Message A message key (default: 'badtitletext')
- * @param $params Array parameter to wfMessage()
+ * @param string|Message $msg A message key (default: 'badtitletext')
+ * @param array $params parameter to wfMessage()
*/
function __construct( $msg = 'badtitletext', $params = null ) {
parent::__construct( 'badtitle', $msg, $params );
@@ -423,7 +423,7 @@ class PermissionsError extends ErrorPageError {
* @ingroup Exception
*/
class ReadOnlyError extends ErrorPageError {
- public function __construct(){
+ public function __construct() {
parent::__construct(
'readonly',
'readonlytext',
@@ -439,14 +439,14 @@ class ReadOnlyError extends ErrorPageError {
* @ingroup Exception
*/
class ThrottledError extends ErrorPageError {
- public function __construct(){
+ public function __construct() {
parent::__construct(
'actionthrottled',
'actionthrottledtext'
);
}
- public function report(){
+ public function report() {
global $wgOut;
$wgOut->setStatusCode( 503 );
parent::report();
@@ -460,7 +460,7 @@ class ThrottledError extends ErrorPageError {
* @ingroup Exception
*/
class UserBlockedError extends ErrorPageError {
- public function __construct( Block $block ){
+ public function __construct( Block $block ) {
global $wgLang, $wgRequest;
$blocker = $block->getBlocker();
@@ -500,25 +500,24 @@ class UserBlockedError extends ErrorPageError {
/**
* Shows a generic "user is not logged in" error page.
*
- * This is essentially an ErrorPageError exception which by default use the
+ * This is essentially an ErrorPageError exception which by default uses the
* 'exception-nologin' as a title and 'exception-nologin-text' for the message.
* @see bug 37627
* @since 1.20
*
* @par Example:
* @code
- * if( $user->isAnon ) {
+ * if( $user->isAnon() ) {
* throw new UserNotLoggedIn();
* }
* @endcode
*
- * Please note the parameters are mixed up compared to ErrorPageError, this
- * is done to be able to simply specify a reason whitout overriding the default
- * title.
+ * Note the parameter order differs from ErrorPageError, this allows you to
+ * simply specify a reason without overriding the default title.
*
* @par Example:
* @code
- * if( $user->isAnon ) {
+ * if( $user->isAnon() ) {
* throw new UserNotLoggedIn( 'action-require-loggedin' );
* }
* @endcode
@@ -533,11 +532,11 @@ class UserNotLoggedIn extends ErrorPageError {
* @param $titleMsg A message key to set the page title.
* Optional, default: 'exception-nologin'
* @param $params Parameters to wfMessage().
- * Optiona, default: null
+ * Optional, default: null
*/
public function __construct(
$reasonMsg = 'exception-nologin-text',
- $titleMsg = 'exception-nologin',
+ $titleMsg = 'exception-nologin',
$params = null
) {
parent::__construct( $titleMsg, $reasonMsg, $params );
@@ -558,24 +557,48 @@ class HttpError extends MWException {
* Constructor
*
* @param $httpCode Integer: HTTP status code to send to the client
- * @param $content String|Message: content of the message
- * @param $header String|Message: content of the header (\<title\> and \<h1\>)
+ * @param string|Message $content content of the message
+ * @param string|Message $header content of the header (\<title\> and \<h1\>)
*/
- public function __construct( $httpCode, $content, $header = null ){
+ public function __construct( $httpCode, $content, $header = null ) {
parent::__construct( $content );
$this->httpCode = (int)$httpCode;
$this->header = $header;
$this->content = $content;
}
+ /**
+ * Returns the HTTP status code supplied to the constructor.
+ *
+ * @return int
+ */
+ public function getStatusCode() {
+ return $this->httpCode;
+ }
+
+ /**
+ * Report the HTTP error.
+ * Sends the appropriate HTTP status code and outputs an
+ * HTML page with an error message.
+ */
public function report() {
$httpMessage = HttpStatus::getMessage( $this->httpCode );
- header( "Status: {$this->httpCode} {$httpMessage}" );
+ header( "Status: {$this->httpCode} {$httpMessage}", true, $this->httpCode );
header( 'Content-type: text/html; charset=utf-8' );
+ print $this->getHTML();
+ }
+
+ /**
+ * Returns HTML for reporting the HTTP error.
+ * This will be a minimal but complete HTML document.
+ *
+ * @return string HTML
+ */
+ public function getHTML() {
if ( $this->header === null ) {
- $header = $httpMessage;
+ $header = HttpStatus::getMessage( $this->httpCode );
} elseif ( $this->header instanceof Message ) {
$header = $this->header->escaped();
} else {
@@ -588,7 +611,7 @@ class HttpError extends MWException {
$content = htmlspecialchars( $this->content );
}
- print "<!DOCTYPE html>\n".
+ return "<!DOCTYPE html>\n".
"<html><head><title>$header</title></head>\n" .
"<body><h1>$header</h1><p>$content</p></body></html>\n";
}
@@ -661,7 +684,7 @@ class MWExceptionHandler {
* Print a message, if possible to STDERR.
* Use this in command line mode only (see isCommandLine)
*
- * @param $message string Failure text
+ * @param string $message Failure text
*/
public static function printError( $message ) {
# NOTE: STDERR may not be available, especially if php-cgi is used from the command line (bug #15602).