r97314 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97313‎ | r97314 | r97315 >
Date:18:50, 16 September 2011
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
* Added HttpError exception as replacement for wfHttpError(); changed alls core calls to it except AjaxDispatcher.php
* Changed FeedUtils' call to it to be similar than feeds are completely disabled
* Use local context instead of global variables in Special:Userlogout
Modified paths:
  • /trunk/phase3/includes/Exception.php (modified) (history)
  • /trunk/phase3/includes/FeedUtils.php (modified) (history)
  • /trunk/phase3/includes/Metadata.php (modified) (history)
  • /trunk/phase3/includes/WebRequest.php (modified) (history)
  • /trunk/phase3/includes/Wiki.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialUploadStash.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialUserlogout.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/FeedUtils.php
@@ -31,16 +31,15 @@
3232 * @return Boolean
3333 */
3434 public static function checkFeedOutput( $type ) {
35 - global $wgFeed, $wgFeedClasses;
 35+ global $wgOut, $wgFeed, $wgFeedClasses;
3636
3737 if ( !$wgFeed ) {
38 - global $wgOut;
3938 $wgOut->addWikiMsg( 'feed-unavailable' );
4039 return false;
4140 }
4241
4342 if( !isset( $wgFeedClasses[$type] ) ) {
44 - wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
 43+ $wgOut->addWikiMsg( 'feed-invalid' );
4544 return false;
4645 }
4746
Index: trunk/phase3/includes/WebRequest.php
@@ -874,7 +874,7 @@
875875 return false;
876876 }
877877 }
878 - wfHttpError( 403, 'Forbidden',
 878+ throw new HttpError( 403,
879879 'Invalid file extension found in the path info or query string.' );
880880
881881 return false;
Index: trunk/phase3/includes/Wiki.php
@@ -210,7 +210,7 @@
211211 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
212212 "to true.";
213213 }
214 - wfHttpError( 500, "Internal error", $message );
 214+ throw new HttpError( 500, $message );
215215 } else {
216216 $output->setSquidMaxage( 1200 );
217217 $output->redirect( $targetUrl, '301' );
Index: trunk/phase3/includes/specials/SpecialUserlogout.php
@@ -33,31 +33,30 @@
3434 }
3535
3636 function execute( $par ) {
37 - global $wgUser, $wgOut;
38 -
3937 /**
4038 * Some satellite ISPs use broken precaching schemes that log people out straight after
4139 * they're logged in (bug 17790). Luckily, there's a way to detect such requests.
4240 */
4341 if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], '&' ) !== false ) {
4442 wfDebug( "Special:Userlogout request {$_SERVER['REQUEST_URI']} looks suspicious, denying.\n" );
45 - wfHttpError( 400, wfMsg( 'loginerror' ), wfMsg( 'suspicious-userlogout' ) );
46 - return;
 43+ throw new HttpError( 400, wfMessage( 'suspicious-userlogout' ), wfMessage( 'loginerror' ) );
4744 }
4845
4946 $this->setHeaders();
5047 $this->outputHeader();
5148
52 - $oldName = $wgUser->getName();
53 - $wgUser->logout();
 49+ $user = $this->getUser();
 50+ $oldName = $user->getName();
 51+ $user->logout();
5452
55 - $wgOut->addWikiMsg( 'logouttext' );
 53+ $out = $this->getOutput();
 54+ $out->addWikiMsg( 'logouttext' );
5655
5756 // Hook.
5857 $injected_html = '';
59 - wfRunHooks( 'UserLogoutComplete', array( &$wgUser, &$injected_html, $oldName ) );
60 - $wgOut->addHTML( $injected_html );
 58+ wfRunHooks( 'UserLogoutComplete', array( &$user, &$injected_html, $oldName ) );
 59+ $out->addHTML( $injected_html );
6160
62 - $wgOut->returnToMain();
 61+ $out->returnToMain();
6362 }
6463 }
Index: trunk/phase3/includes/specials/SpecialUploadStash.php
@@ -95,8 +95,7 @@
9696 $message = $e->getMessage();
9797 }
9898
99 - wfHttpError( $code, HttpStatus::getMessage( $code ), $message );
100 - return false;
 99+ throw new HttpError( $code, $message );
101100 }
102101
103102 /**
Index: trunk/phase3/includes/Metadata.php
@@ -41,14 +41,13 @@
4242 $rdftype = wfNegotiateType( wfAcceptToPrefs( $httpaccept ), wfAcceptToPrefs( self::RDF_TYPE_PREFS ) );
4343
4444 if( !$rdftype ){
45 - wfHttpError( 406, 'Not Acceptable', wfMsg( 'notacceptable' ) );
46 - return false;
47 - } else {
48 - $wgOut->disable();
49 - $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
50 - $wgOut->sendCacheControl();
51 - return true;
 45+ throw new HttpError( 406, wfMessage( 'notacceptable' ) );
5246 }
 47+
 48+ $wgOut->disable();
 49+ $wgRequest->response()->header( "Content-type: {$rdftype}; charset=utf-8" );
 50+ $wgOut->sendCacheControl();
 51+ return true;
5352 }
5453
5554 protected function reallyFullUrl() {
Index: trunk/phase3/includes/Exception.php
@@ -367,6 +367,55 @@
368368 }
369369
370370 /**
 371+ * Show an error that looks like an HTTP server error.
 372+ * Replacement for wfHttpError().
 373+ *
 374+ * @ingroup Exception
 375+ */
 376+class HttpError extends MWException {
 377+ private $httpCode, $header, $content;
 378+
 379+ /**
 380+ * Constructor
 381+ *
 382+ * @param $httpCode Integer: HTTP status code to send to the client
 383+ * @param $content String|Message: content of the message
 384+ * @param $header String|Message: content of the header (\<title\> and \<h1\>)
 385+ */
 386+ public function __construct( $httpCode, $content, $header = null ){
 387+ parent::__construct( $content );
 388+ $this->httpCode = (int)$httpCode;
 389+ $this->header = $header;
 390+ $this->content = $content;
 391+ }
 392+
 393+ public function reportHTML() {
 394+ $httpMessage = HttpStatus::getMessage( $this->httpCode );
 395+
 396+ header( "Status: {$this->httpCode} {$httpMessage}" );
 397+ header( 'Content-type: text/html; charset=utf-8' );
 398+
 399+ if ( $this->header === null ) {
 400+ $header = $httpMessage;
 401+ } elseif ( $this->header instanceof Message ) {
 402+ $header = $this->header->escaped();
 403+ } else {
 404+ $header = htmlspecialchars( $this->header );
 405+ }
 406+
 407+ if ( $this->content instanceof Message ) {
 408+ $content = $this->content->escaped();
 409+ } else {
 410+ $content = htmlspecialchars( $this->content );
 411+ }
 412+
 413+ print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n".
 414+ "<html><head><title>$header</title></head>\n" .
 415+ "<body><h1>$header</h1><p>$content</p></body></html>\n";
 416+ }
 417+}
 418+
 419+/**
371420 * Handler class for MWExceptions
372421 * @ingroup Exception
373422 */

Follow-up revisions

RevisionCommit summaryAuthorDate
r100113Fix for r97314: add HttpError to the AutoLoaderialex13:53, 18 October 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   20:55, 28 September 2011

Looks good.

#Comment by Platonides (talk | contribs)   20:57, 17 October 2011

Needs to be added to the AutoLoader

#Comment by IAlex (talk | contribs)   13:53, 18 October 2011

Done in r100113.

Status & tagging log