Split ApiUsageException and UsageException to class per file
[lhc/web/wiklou.git] / includes / api / UsageException.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * This exception will be thrown when dieUsage is called to stop module execution.
23 *
24 * @ingroup API
25 * @deprecated since 1.29, use ApiUsageException instead
26 */
27 class UsageException extends MWException {
28
29 private $mCodestr;
30
31 /**
32 * @var null|array
33 */
34 private $mExtraData;
35
36 /**
37 * @param string $message
38 * @param string $codestr
39 * @param int $code
40 * @param array|null $extradata
41 */
42 public function __construct( $message, $codestr, $code = 0, $extradata = null ) {
43 parent::__construct( $message, $code );
44 $this->mCodestr = $codestr;
45 $this->mExtraData = $extradata;
46
47 if ( !$this instanceof ApiUsageException ) {
48 wfDeprecated( __METHOD__, '1.29' );
49 }
50
51 // This should never happen, so throw an exception about it that will
52 // hopefully get logged with a backtrace (T138585)
53 if ( !is_string( $codestr ) || $codestr === '' ) {
54 throw new InvalidArgumentException( 'Invalid $codestr, was ' .
55 ( $codestr === '' ? 'empty string' : gettype( $codestr ) )
56 );
57 }
58 }
59
60 /**
61 * @return string
62 */
63 public function getCodeString() {
64 wfDeprecated( __METHOD__, '1.29' );
65 return $this->mCodestr;
66 }
67
68 /**
69 * @return array
70 */
71 public function getMessageArray() {
72 wfDeprecated( __METHOD__, '1.29' );
73 $result = [
74 'code' => $this->mCodestr,
75 'info' => $this->getMessage()
76 ];
77 if ( is_array( $this->mExtraData ) ) {
78 $result = array_merge( $result, $this->mExtraData );
79 }
80
81 return $result;
82 }
83
84 /**
85 * @return string
86 */
87 public function __toString() {
88 return "{$this->getCodeString()}: {$this->getMessage()}";
89 }
90 }