r88800 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88799‎ | r88800 | r88801 >
Date:17:03, 25 May 2011
Author:reedy
Status:ok
Tags:
Comment:
Move DB(.*?)Error classes into own file

Updated AutoLoader to match
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/db/Database.php (modified) (history)
  • /trunk/phase3/includes/db/DatabaseError.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseError.php
@@ -0,0 +1,276 @@
 2+<?php
 3+/******************************************************************************
 4+ * Error classes
 5+ *****************************************************************************/
 6+
 7+/**
 8+ * Database error base class
 9+ * @ingroup Database
 10+ */
 11+class DBError extends MWException {
 12+ public $db;
 13+
 14+ /**
 15+ * Construct a database error
 16+ * @param $db Database object which threw the error
 17+ * @param $error String A simple error message to be used for debugging
 18+ */
 19+ function __construct( DatabaseBase &$db, $error ) {
 20+ $this->db =& $db;
 21+ parent::__construct( $error );
 22+ }
 23+
 24+ protected function getContentMessage( $html ) {
 25+ if ( $html ) {
 26+ return nl2br( htmlspecialchars( $this->getMessage() ) );
 27+ } else {
 28+ return $this->getMessage();
 29+ }
 30+ }
 31+
 32+ function getText() {
 33+ global $wgShowDBErrorBacktrace;
 34+
 35+ $s = $this->getContentMessage( false ) . "\n";
 36+
 37+ if ( $wgShowDBErrorBacktrace ) {
 38+ $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
 39+ }
 40+
 41+ return $s;
 42+ }
 43+
 44+ function getHTML() {
 45+ global $wgShowDBErrorBacktrace;
 46+
 47+ $s = $this->getContentMessage( true );
 48+
 49+ if ( $wgShowDBErrorBacktrace ) {
 50+ $s .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
 51+ }
 52+
 53+ return $s;
 54+ }
 55+}
 56+
 57+/**
 58+ * @ingroup Database
 59+ */
 60+class DBConnectionError extends DBError {
 61+ public $error;
 62+
 63+ function __construct( DatabaseBase &$db, $error = 'unknown error' ) {
 64+ $msg = 'DB connection error';
 65+
 66+ if ( trim( $error ) != '' ) {
 67+ $msg .= ": $error";
 68+ }
 69+
 70+ $this->error = $error;
 71+
 72+ parent::__construct( $db, $msg );
 73+ }
 74+
 75+ function useOutputPage() {
 76+ // Not likely to work
 77+ return false;
 78+ }
 79+
 80+ function msg( $key, $fallback /*[, params...] */ ) {
 81+ global $wgLang;
 82+
 83+ $args = array_slice( func_get_args(), 2 );
 84+
 85+ if ( $this->useMessageCache() ) {
 86+ $message = $wgLang->getMessage( $key );
 87+ } else {
 88+ $message = $fallback;
 89+ }
 90+ return wfMsgReplaceArgs( $message, $args );
 91+ }
 92+
 93+ function getLogMessage() {
 94+ # Don't send to the exception log
 95+ return false;
 96+ }
 97+
 98+ function getPageTitle() {
 99+ global $wgSitename;
 100+ return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
 101+ }
 102+
 103+ function getHTML() {
 104+ global $wgShowDBErrorBacktrace;
 105+
 106+ $sorry = htmlspecialchars( $this->msg( 'dberr-problems', 'Sorry! This site is experiencing technical difficulties.' ) );
 107+ $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
 108+ $info = htmlspecialchars( $this->msg( 'dberr-info', '(Can\'t contact the database server: $1)' ) );
 109+
 110+ # No database access
 111+ MessageCache::singleton()->disable();
 112+
 113+ if ( trim( $this->error ) == '' ) {
 114+ $this->error = $this->db->getProperty( 'mServer' );
 115+ }
 116+
 117+ $this->error = Html::element( 'span', array( 'dir' => 'ltr' ), $this->error );
 118+
 119+ $noconnect = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
 120+ $text = str_replace( '$1', $this->error, $noconnect );
 121+
 122+ if ( $wgShowDBErrorBacktrace ) {
 123+ $text .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
 124+ }
 125+
 126+ $extra = $this->searchForm();
 127+
 128+ return "$text<hr />$extra";
 129+ }
 130+
 131+ public function reportHTML(){
 132+ global $wgUseFileCache;
 133+
 134+ # Check whether we can serve a file-cached copy of the page with the error underneath
 135+ if ( $wgUseFileCache ) {
 136+ try {
 137+ $cache = $this->fileCachedPage();
 138+ # Cached version on file system?
 139+ if ( $cache !== null ) {
 140+ # Hack: extend the body for error messages
 141+ $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
 142+ # Add cache notice...
 143+ $cache .= '<div style="color:red;font-size:150%;font-weight:bold;">'.
 144+ htmlspecialchars( $this->msg( 'dberr-cachederror',
 145+ 'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
 146+ '</div>';
 147+
 148+ # Output cached page with notices on bottom and re-close body
 149+ echo "{$cache}<hr />{$this->getHTML()}</body></html>";
 150+ return;
 151+ }
 152+ } catch ( MWException $e ) {
 153+ // Do nothing, just use the default page
 154+ }
 155+ }
 156+
 157+ # We can't, cough and die in the usual fashion
 158+ return parent::reportHTML();
 159+ }
 160+
 161+ function searchForm() {
 162+ global $wgSitename, $wgServer;
 163+
 164+ $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
 165+ $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
 166+ $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
 167+
 168+ $search = htmlspecialchars( @$_REQUEST['search'] );
 169+
 170+ $server = htmlspecialchars( $wgServer );
 171+ $sitename = htmlspecialchars( $wgSitename );
 172+
 173+ $trygoogle = <<<EOT
 174+<div style="margin: 1.5em">$usegoogle<br />
 175+<small>$outofdate</small></div>
 176+<!-- SiteSearch Google -->
 177+<form method="get" action="http://www.google.com/search" id="googlesearch">
 178+ <input type="hidden" name="domains" value="$server" />
 179+ <input type="hidden" name="num" value="50" />
 180+ <input type="hidden" name="ie" value="UTF-8" />
 181+ <input type="hidden" name="oe" value="UTF-8" />
 182+
 183+ <input type="text" name="q" size="31" maxlength="255" value="$search" />
 184+ <input type="submit" name="btnG" value="$googlesearch" />
 185+ <div>
 186+ <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
 187+ <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
 188+ </div>
 189+</form>
 190+<!-- SiteSearch Google -->
 191+EOT;
 192+ return $trygoogle;
 193+ }
 194+
 195+ private function fileCachedPage() {
 196+ global $wgTitle, $wgOut;
 197+
 198+ if ( $wgOut->isDisabled() ) {
 199+ return; // Done already?
 200+ }
 201+
 202+ if ( $wgTitle ) {
 203+ $t =& $wgTitle;
 204+ } else {
 205+ $t = Title::newFromText( $this->msg( 'mainpage', 'Main Page' ) );
 206+ }
 207+
 208+ $cache = new HTMLFileCache( $t );
 209+ if ( $cache->isFileCached() ) {
 210+ return $cache->fetchPageText();
 211+ } else {
 212+ return '';
 213+ }
 214+ }
 215+}
 216+
 217+/**
 218+ * @ingroup Database
 219+ */
 220+class DBQueryError extends DBError {
 221+ public $error, $errno, $sql, $fname;
 222+
 223+ function __construct( DatabaseBase &$db, $error, $errno, $sql, $fname ) {
 224+ $message = "A database error has occurred. Did you forget to run maintenance/update.php after upgrading? See: http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
 225+ "Query: $sql\n" .
 226+ "Function: $fname\n" .
 227+ "Error: $errno $error\n";
 228+ global $wgShowDBErrorBacktrace;
 229+ if( $wgShowDBErrorBacktrace ) {
 230+ $message .= $this->getTraceAsString();
 231+ }
 232+ parent::__construct( $db, $message );
 233+
 234+ $this->error = $error;
 235+ $this->errno = $errno;
 236+ $this->sql = $sql;
 237+ $this->fname = $fname;
 238+ }
 239+
 240+ function getContentMessage( $html ) {
 241+ if ( $this->useMessageCache() ) {
 242+ $msg = $html ? 'dberrortext' : 'dberrortextcl';
 243+ $ret = wfMsg( $msg, $this->getSQL(),
 244+ $this->fname, $this->errno, $this->error );
 245+ if ( $html ) {
 246+ $ret = htmlspecialchars( $ret );
 247+ }
 248+ return $ret;
 249+ } else {
 250+ return parent::getContentMessage( $html );
 251+ }
 252+ }
 253+
 254+ function getSQL() {
 255+ global $wgShowSQLErrors;
 256+
 257+ if ( !$wgShowSQLErrors ) {
 258+ return $this->msg( 'sqlhidden', 'SQL hidden' );
 259+ } else {
 260+ return $this->sql;
 261+ }
 262+ }
 263+
 264+ function getLogMessage() {
 265+ # Don't send to the exception log
 266+ return false;
 267+ }
 268+
 269+ function getPageTitle() {
 270+ return $this->msg( 'databaseerror', 'Database error' );
 271+ }
 272+}
 273+
 274+/**
 275+ * @ingroup Database
 276+ */
 277+class DBUnexpectedError extends DBError {}
Property changes on: trunk/phase3/includes/db/DatabaseError.php
___________________________________________________________________
Added: svn:mergeinfo
1278 Merged /branches/new-installer/phase3/includes/db/Database.php:r43664-66004
2279 Merged /branches/querypage-work2/phase3/includes/db/Database.php:r65475
3280 Merged /branches/wmf-deployment/includes/db/Database.php:r53381
4281 Merged /branches/REL1_15/phase3/includes/db/Database.php:r51646
5282 Merged /branches/wmf/1.16wmf4/includes/db/Database.php:r67177
6283 Merged /branches/sqlite/includes/db/Database.php:r58211-58321
Added: svn:eol-style
7284 + native
Added: svn:keywords
8285 + Author Date Id Revision
Index: trunk/phase3/includes/db/Database.php
@@ -2862,284 +2862,7 @@
28632863 function isNullable();
28642864 }
28652865
2866 -/******************************************************************************
2867 - * Error classes
2868 - *****************************************************************************/
2869 -
28702866 /**
2871 - * Database error base class
2872 - * @ingroup Database
2873 - */
2874 -class DBError extends MWException {
2875 - public $db;
2876 -
2877 - /**
2878 - * Construct a database error
2879 - * @param $db Database object which threw the error
2880 - * @param $error String A simple error message to be used for debugging
2881 - */
2882 - function __construct( DatabaseBase &$db, $error ) {
2883 - $this->db =& $db;
2884 - parent::__construct( $error );
2885 - }
2886 -
2887 - protected function getContentMessage( $html ) {
2888 - if ( $html ) {
2889 - return nl2br( htmlspecialchars( $this->getMessage() ) );
2890 - } else {
2891 - return $this->getMessage();
2892 - }
2893 - }
2894 -
2895 - function getText() {
2896 - global $wgShowDBErrorBacktrace;
2897 -
2898 - $s = $this->getContentMessage( false ) . "\n";
2899 -
2900 - if ( $wgShowDBErrorBacktrace ) {
2901 - $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
2902 - }
2903 -
2904 - return $s;
2905 - }
2906 -
2907 - function getHTML() {
2908 - global $wgShowDBErrorBacktrace;
2909 -
2910 - $s = $this->getContentMessage( true );
2911 -
2912 - if ( $wgShowDBErrorBacktrace ) {
2913 - $s .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
2914 - }
2915 -
2916 - return $s;
2917 - }
2918 -}
2919 -
2920 -/**
2921 - * @ingroup Database
2922 - */
2923 -class DBConnectionError extends DBError {
2924 - public $error;
2925 -
2926 - function __construct( DatabaseBase &$db, $error = 'unknown error' ) {
2927 - $msg = 'DB connection error';
2928 -
2929 - if ( trim( $error ) != '' ) {
2930 - $msg .= ": $error";
2931 - }
2932 -
2933 - $this->error = $error;
2934 -
2935 - parent::__construct( $db, $msg );
2936 - }
2937 -
2938 - function useOutputPage() {
2939 - // Not likely to work
2940 - return false;
2941 - }
2942 -
2943 - function msg( $key, $fallback /*[, params...] */ ) {
2944 - global $wgLang;
2945 -
2946 - $args = array_slice( func_get_args(), 2 );
2947 -
2948 - if ( $this->useMessageCache() ) {
2949 - $message = $wgLang->getMessage( $key );
2950 - } else {
2951 - $message = $fallback;
2952 - }
2953 - return wfMsgReplaceArgs( $message, $args );
2954 - }
2955 -
2956 - function getLogMessage() {
2957 - # Don't send to the exception log
2958 - return false;
2959 - }
2960 -
2961 - function getPageTitle() {
2962 - global $wgSitename;
2963 - return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
2964 - }
2965 -
2966 - function getHTML() {
2967 - global $wgShowDBErrorBacktrace;
2968 -
2969 - $sorry = htmlspecialchars( $this->msg( 'dberr-problems', 'Sorry! This site is experiencing technical difficulties.' ) );
2970 - $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
2971 - $info = htmlspecialchars( $this->msg( 'dberr-info', '(Can\'t contact the database server: $1)' ) );
2972 -
2973 - # No database access
2974 - MessageCache::singleton()->disable();
2975 -
2976 - if ( trim( $this->error ) == '' ) {
2977 - $this->error = $this->db->getProperty( 'mServer' );
2978 - }
2979 -
2980 - $this->error = Html::element( 'span', array( 'dir' => 'ltr' ), $this->error );
2981 -
2982 - $noconnect = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
2983 - $text = str_replace( '$1', $this->error, $noconnect );
2984 -
2985 - if ( $wgShowDBErrorBacktrace ) {
2986 - $text .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
2987 - }
2988 -
2989 - $extra = $this->searchForm();
2990 -
2991 - return "$text<hr />$extra";
2992 - }
2993 -
2994 - public function reportHTML(){
2995 - global $wgUseFileCache;
2996 -
2997 - # Check whether we can serve a file-cached copy of the page with the error underneath
2998 - if ( $wgUseFileCache ) {
2999 - try {
3000 - $cache = $this->fileCachedPage();
3001 - # Cached version on file system?
3002 - if ( $cache !== null ) {
3003 - # Hack: extend the body for error messages
3004 - $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
3005 - # Add cache notice...
3006 - $cache .= '<div style="color:red;font-size:150%;font-weight:bold;">'.
3007 - htmlspecialchars( $this->msg( 'dberr-cachederror',
3008 - 'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
3009 - '</div>';
3010 -
3011 - # Output cached page with notices on bottom and re-close body
3012 - echo "{$cache}<hr />{$this->getHTML()}</body></html>";
3013 - return;
3014 - }
3015 - } catch ( MWException $e ) {
3016 - // Do nothing, just use the default page
3017 - }
3018 - }
3019 -
3020 - # We can't, cough and die in the usual fashion
3021 - return parent::reportHTML();
3022 - }
3023 -
3024 - function searchForm() {
3025 - global $wgSitename, $wgServer;
3026 -
3027 - $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
3028 - $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
3029 - $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
3030 -
3031 - $search = htmlspecialchars( @$_REQUEST['search'] );
3032 -
3033 - $server = htmlspecialchars( $wgServer );
3034 - $sitename = htmlspecialchars( $wgSitename );
3035 -
3036 - $trygoogle = <<<EOT
3037 -<div style="margin: 1.5em">$usegoogle<br />
3038 -<small>$outofdate</small></div>
3039 -<!-- SiteSearch Google -->
3040 -<form method="get" action="http://www.google.com/search" id="googlesearch">
3041 - <input type="hidden" name="domains" value="$server" />
3042 - <input type="hidden" name="num" value="50" />
3043 - <input type="hidden" name="ie" value="UTF-8" />
3044 - <input type="hidden" name="oe" value="UTF-8" />
3045 -
3046 - <input type="text" name="q" size="31" maxlength="255" value="$search" />
3047 - <input type="submit" name="btnG" value="$googlesearch" />
3048 - <div>
3049 - <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
3050 - <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
3051 - </div>
3052 -</form>
3053 -<!-- SiteSearch Google -->
3054 -EOT;
3055 - return $trygoogle;
3056 - }
3057 -
3058 - private function fileCachedPage() {
3059 - global $wgTitle, $wgOut;
3060 -
3061 - if ( $wgOut->isDisabled() ) {
3062 - return; // Done already?
3063 - }
3064 -
3065 - if ( $wgTitle ) {
3066 - $t =& $wgTitle;
3067 - } else {
3068 - $t = Title::newFromText( $this->msg( 'mainpage', 'Main Page' ) );
3069 - }
3070 -
3071 - $cache = new HTMLFileCache( $t );
3072 - if ( $cache->isFileCached() ) {
3073 - return $cache->fetchPageText();
3074 - } else {
3075 - return '';
3076 - }
3077 - }
3078 -}
3079 -
3080 -/**
3081 - * @ingroup Database
3082 - */
3083 -class DBQueryError extends DBError {
3084 - public $error, $errno, $sql, $fname;
3085 -
3086 - function __construct( DatabaseBase &$db, $error, $errno, $sql, $fname ) {
3087 - $message = "A database error has occurred. Did you forget to run maintenance/update.php after upgrading? See: http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
3088 - "Query: $sql\n" .
3089 - "Function: $fname\n" .
3090 - "Error: $errno $error\n";
3091 - global $wgShowDBErrorBacktrace;
3092 - if( $wgShowDBErrorBacktrace ) {
3093 - $message .= $this->getTraceAsString();
3094 - }
3095 - parent::__construct( $db, $message );
3096 -
3097 - $this->error = $error;
3098 - $this->errno = $errno;
3099 - $this->sql = $sql;
3100 - $this->fname = $fname;
3101 - }
3102 -
3103 - function getContentMessage( $html ) {
3104 - if ( $this->useMessageCache() ) {
3105 - $msg = $html ? 'dberrortext' : 'dberrortextcl';
3106 - $ret = wfMsg( $msg, $this->getSQL(),
3107 - $this->fname, $this->errno, $this->error );
3108 - if ( $html ) {
3109 - $ret = htmlspecialchars( $ret );
3110 - }
3111 - return $ret;
3112 - } else {
3113 - return parent::getContentMessage( $html );
3114 - }
3115 - }
3116 -
3117 - function getSQL() {
3118 - global $wgShowSQLErrors;
3119 -
3120 - if ( !$wgShowSQLErrors ) {
3121 - return $this->msg( 'sqlhidden', 'SQL hidden' );
3122 - } else {
3123 - return $this->sql;
3124 - }
3125 - }
3126 -
3127 - function getLogMessage() {
3128 - # Don't send to the exception log
3129 - return false;
3130 - }
3131 -
3132 - function getPageTitle() {
3133 - return $this->msg( 'databaseerror', 'Database error' );
3134 - }
3135 -}
3136 -
3137 -/**
3138 - * @ingroup Database
3139 - */
3140 -class DBUnexpectedError extends DBError {}
3141 -
3142 -
3143 -/**
31442867 * Result wrapper for grabbing data queried by someone else
31452868 * @ingroup Database
31462869 */
Index: trunk/phase3/includes/AutoLoader.php
@@ -380,11 +380,11 @@
381381 'DatabaseSqlite' => 'includes/db/DatabaseSqlite.php',
382382 'DatabaseSqliteStandalone' => 'includes/db/DatabaseSqlite.php',
383383 'DatabaseType' => 'includes/db/Database.php',
384 - 'DBConnectionError' => 'includes/db/Database.php',
385 - 'DBError' => 'includes/db/Database.php',
 384+ 'DBConnectionError' => 'includes/db/DatabaseError.php',
 385+ 'DBError' => 'includes/db/DatabaseError.php',
386386 'DBObject' => 'includes/db/Database.php',
387 - 'DBQueryError' => 'includes/db/Database.php',
388 - 'DBUnexpectedError' => 'includes/db/Database.php',
 387+ 'DBQueryError' => 'includes/db/DatabaseError.php',
 388+ 'DBUnexpectedError' => 'includes/db/DatabaseError.php',
389389 'FakeResultWrapper' => 'includes/db/Database.php',
390390 'Field' => 'includes/db/Database.php',
391391 'IBM_DB2Blob' => 'includes/db/DatabaseIbm_db2.php',

Status & tagging log