Merge "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 public function serialize() {
137 return serialize( array(
138 'parent' => parent::serialize(),
139 'apiCode' => $this->apiCode,
140 'apiData' => $this->apiData,
141 ) );
142 }
143
144 public function unserialize( $serialized ) {
145 $data = unserialize( $serialized );
146 parent::unserialize( $data['parent'] );
147 $this->apiCode = $data['apiCode'];
148 $this->apiData = $data['apiData'];
149 }
150 }
151
152 /**
153 * Extension of RawMessage implementing IApiMessage
154 * @since 1.25
155 * @ingroup API
156 * @todo: Would be nice to use a Trait here to avoid code duplication
157 */
158 class ApiRawMessage extends RawMessage implements IApiMessage {
159 protected $apiCode = null;
160 protected $apiData = array();
161
162 /**
163 * @param RawMessage|string|array $msg
164 * - RawMessage: is cloned
165 * - array: first element is $key, rest are $params to RawMessage::__construct
166 * - string: passed to RawMessage::__construct
167 * @param string|null $code
168 * @param array|null $data
169 * @return ApiMessage
170 */
171 public function __construct( $msg, $code = null, array $data = null ) {
172 if ( $msg instanceof RawMessage ) {
173 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
174 if ( isset( $msg->$key ) ) {
175 $this->$key = $msg->$key;
176 }
177 }
178 } elseif ( is_array( $msg ) ) {
179 $key = array_shift( $msg );
180 parent::__construct( $key, $msg );
181 } else {
182 parent::__construct( $msg );
183 }
184 $this->apiCode = $code;
185 $this->apiData = (array)$data;
186 }
187
188 public function getApiCode() {
189 return $this->apiCode === null ? $this->getKey() : $this->apiCode;
190 }
191
192 public function setApiCode( $code, array $data = null ) {
193 $this->apiCode = $code;
194 if ( $data !== null ) {
195 $this->setApiData( $data );
196 }
197 }
198
199 public function getApiData() {
200 return $this->apiData;
201 }
202
203 public function setApiData( array $data ) {
204 $this->apiData = $data;
205 }
206
207 public function serialize() {
208 return serialize( array(
209 'parent' => parent::serialize(),
210 'apiCode' => $this->apiCode,
211 'apiData' => $this->apiData,
212 ) );
213 }
214
215 public function unserialize( $serialized ) {
216 $data = unserialize( $serialized );
217 parent::unserialize( $data['parent'] );
218 $this->apiCode = $data['apiCode'];
219 $this->apiData = $data['apiData'];
220 }
221 }