Don't check namespace in SpecialWantedtemplates
[lhc/web/wiklou.git] / includes / api / ApiMessage.php
1 <?php
2 /**
3 * Defines an interface for messages with additional machine-readable data for
4 * use by the API, and provides concrete implementations of that interface.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * Interface for messages with machine-readable data for use by the API
26 * @since 1.25
27 * @ingroup API
28 */
29 interface IApiMessage extends MessageSpecifier {
30 /**
31 * Returns a machine-readable code for use by the API
32 *
33 * The message key is often sufficient, but sometimes there are multiple
34 * messages used for what is really the same underlying condition (e.g.
35 * badaccess-groups and badaccess-group0)
36 * @return string
37 */
38 public function getApiCode();
39
40 /**
41 * Returns additional machine-readable data about the error condition
42 * @return array
43 */
44 public function getApiData();
45
46 /**
47 * Sets the machine-readable code for use by the API
48 * @param string|null $code If null, the message key should be returned by self::getApiCode()
49 * @param array|null $data If non-null, passed to self::setApiData()
50 */
51 public function setApiCode( $code, array $data = null );
52
53 /**
54 * Sets additional machine-readable data about the error condition
55 * @param array $data
56 */
57 public function setApiData( array $data );
58 }
59
60 /**
61 * Extension of Message implementing IApiMessage
62 * @since 1.25
63 * @ingroup API
64 * @todo: Would be nice to use a Trait here to avoid code duplication
65 */
66 class ApiMessage extends Message implements IApiMessage {
67 protected $apiCode = null;
68 protected $apiData = array();
69
70 /**
71 * Create an IApiMessage for the message
72 *
73 * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
74 * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
75 *
76 * @param Message|RawMessage|array|string $msg
77 * @param string|null $code
78 * @param array|null $data
79 * @return ApiMessage
80 */
81 public static function create( $msg, $code = null, array $data = null ) {
82 if ( $msg instanceof IApiMessage ) {
83 return $msg;
84 } elseif ( $msg instanceof RawMessage ) {
85 return new ApiRawMessage( $msg, $code, $data );
86 } else {
87 return new ApiMessage( $msg, $code, $data );
88 }
89 }
90
91 /**
92 * @param Message|string|array $msg
93 * - Message: is cloned
94 * - array: first element is $key, rest are $params to Message::__construct
95 * - string: passed to Message::__construct
96 * @param string|null $code
97 * @param array|null $data
98 * @return ApiMessage
99 */
100 public function __construct( $msg, $code = null, array $data = null ) {
101 if ( $msg instanceof Message ) {
102 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
103 if ( isset( $msg->$key ) ) {
104 $this->$key = $msg->$key;
105 }
106 }
107 } elseif ( is_array( $msg ) ) {
108 $key = array_shift( $msg );
109 parent::__construct( $key, $msg );
110 } else {
111 parent::__construct( $msg );
112 }
113 $this->apiCode = $code;
114 $this->apiData = (array)$data;
115 }
116
117 public function getApiCode() {
118 return $this->apiCode === null ? $this->getKey() : $this->apiCode;
119 }
120
121 public function setApiCode( $code, array $data = null ) {
122 $this->apiCode = $code;
123 if ( $data !== null ) {
124 $this->setApiData( $data );
125 }
126 }
127
128 public function getApiData() {
129 return $this->apiData;
130 }
131
132 public function setApiData( array $data ) {
133 $this->apiData = $data;
134 }
135 }
136
137 /**
138 * Extension of RawMessage implementing IApiMessage
139 * @since 1.25
140 * @ingroup API
141 * @todo: Would be nice to use a Trait here to avoid code duplication
142 */
143 class ApiRawMessage extends RawMessage implements IApiMessage {
144 protected $apiCode = null;
145 protected $apiData = array();
146
147 /**
148 * @param RawMessage|string|array $msg
149 * - RawMessage: is cloned
150 * - array: first element is $key, rest are $params to RawMessage::__construct
151 * - string: passed to RawMessage::__construct
152 * @param string|null $code
153 * @param array|null $data
154 * @return ApiMessage
155 */
156 public function __construct( $msg, $code = null, array $data = null ) {
157 if ( $msg instanceof RawMessage ) {
158 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
159 if ( isset( $msg->$key ) ) {
160 $this->$key = $msg->$key;
161 }
162 }
163 } elseif ( is_array( $msg ) ) {
164 $key = array_shift( $msg );
165 parent::__construct( $key, $msg );
166 } else {
167 parent::__construct( $msg );
168 }
169 $this->apiCode = $code;
170 $this->apiData = (array)$data;
171 }
172
173 public function getApiCode() {
174 return $this->apiCode === null ? $this->getKey() : $this->apiCode;
175 }
176
177 public function setApiCode( $code, array $data = null ) {
178 $this->apiCode = $code;
179 if ( $data !== null ) {
180 $this->setApiData( $data );
181 }
182 }
183
184 public function getApiData() {
185 return $this->apiData;
186 }
187
188 public function setApiData( array $data ) {
189 $this->apiData = $data;
190 }
191 }