Remove MediaWiki mentions from /rdbms error strings
[lhc/web/wiklou.git] / includes / libs / rdbms / exception / DBError.php
1 <?php
2 /**
3 * This file contains database error classes.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database error base class
26 * @ingroup Database
27 */
28 class DBError extends Exception {
29 /** @var IDatabase|null */
30 public $db;
31
32 /**
33 * Construct a database error
34 * @param IDatabase $db Object which threw the error
35 * @param string $error A simple error message to be used for debugging
36 */
37 function __construct( IDatabase $db = null, $error ) {
38 $this->db = $db;
39 parent::__construct( $error );
40 }
41 }
42
43 /**
44 * Base class for the more common types of database errors. These are known to occur
45 * frequently, so we try to give friendly error messages for them.
46 *
47 * @ingroup Database
48 * @since 1.23
49 */
50 class DBExpectedError extends DBError implements MessageSpecifier {
51 /** @var string[] Message parameters */
52 protected $params;
53
54 function __construct( IDatabase $db = null, $error, array $params = [] ) {
55 parent::__construct( $db, $error );
56 $this->params = $params;
57 }
58
59 public function getKey() {
60 return 'databaseerror-text';
61 }
62
63 public function getParams() {
64 return $this->params;
65 }
66 }
67
68 /**
69 * @ingroup Database
70 */
71 class DBConnectionError extends DBExpectedError {
72 /**
73 * @param IDatabase $db Object throwing the error
74 * @param string $error Error text
75 */
76 function __construct( IDatabase $db = null, $error = 'unknown error' ) {
77 $msg = 'Cannot access the database';
78 if ( trim( $error ) != '' ) {
79 $msg .= ": $error";
80 }
81
82 parent::__construct( $db, $msg );
83 }
84 }
85
86 /**
87 * @ingroup Database
88 */
89 class DBQueryError extends DBExpectedError {
90 /** @var string */
91 public $error;
92 /** @var integer */
93 public $errno;
94 /** @var string */
95 public $sql;
96 /** @var string */
97 public $fname;
98
99 /**
100 * @param IDatabase $db
101 * @param string $error
102 * @param int|string $errno
103 * @param string $sql
104 * @param string $fname
105 */
106 function __construct( IDatabase $db, $error, $errno, $sql, $fname ) {
107 if ( $db instanceof DatabaseBase && $db->wasConnectionError( $errno ) ) {
108 $message = "A connection error occured. \n" .
109 "Query: $sql\n" .
110 "Function: $fname\n" .
111 "Error: $errno $error\n";
112 } else {
113 $message = "A database query error has occurred. Did you forget to run " .
114 "your application's database schema updater after upgrading? \n" .
115 "Query: $sql\n" .
116 "Function: $fname\n" .
117 "Error: $errno $error\n";
118 }
119 parent::__construct( $db, $message );
120
121 $this->error = $error;
122 $this->errno = $errno;
123 $this->sql = $sql;
124 $this->fname = $fname;
125 }
126 }
127
128 /**
129 * @ingroup Database
130 */
131 class DBReadOnlyError extends DBExpectedError {
132 }
133
134 /**
135 * @ingroup Database
136 */
137 class DBTransactionError extends DBExpectedError {
138 }
139
140 /**
141 * @ingroup Database
142 */
143 class DBTransactionSizeError extends DBTransactionError {
144 function getKey() {
145 return 'transaction-duration-limit-exceeded';
146 }
147 }
148
149 /**
150 * Exception class for replica DB wait timeouts
151 * @ingroup Database
152 */
153 class DBReplicationWaitError extends DBExpectedError {
154 }
155
156 /**
157 * @ingroup Database
158 */
159 class DBUnexpectedError extends DBError {
160 }
161
162 /**
163 * Exception class for attempted DB access
164 * @ingroup Database
165 */
166 class DBAccessError extends DBUnexpectedError {
167 public function __construct() {
168 parent::__construct( "Access to the database has been disabled." );
169 }
170 }
171