wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / api / ApiMessage.php
blob5c3434f95ca5adeb9e42cbd4f45fc2f65cd28b89
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.
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.
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.
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
21 * @file
24 /**
25 * Interface for messages with machine-readable data for use by the API
27 * The idea is that it's a Message that has some extra data for the API to use when interpreting it
28 * as an error (or, in the future, as a warning). Internals of MediaWiki often use messages (or
29 * message keys, or Status objects containing messages) to pass information about errors to the user
30 * (see e.g. Title::getUserPermissionsErrors()) and the API has to make do with that.
32 * @since 1.25
33 * @ingroup API
35 interface IApiMessage extends MessageSpecifier {
36 /**
37 * Returns a machine-readable code for use by the API
39 * The message key is often sufficient, but sometimes there are multiple
40 * messages used for what is really the same underlying condition (e.g.
41 * badaccess-groups and badaccess-group0)
42 * @return string
44 public function getApiCode();
46 /**
47 * Returns additional machine-readable data about the error condition
48 * @return array
50 public function getApiData();
52 /**
53 * Sets the machine-readable code for use by the API
54 * @param string|null $code If null, the message key should be returned by self::getApiCode()
55 * @param array|null $data If non-null, passed to self::setApiData()
57 public function setApiCode( $code, array $data = null );
59 /**
60 * Sets additional machine-readable data about the error condition
61 * @param array $data
63 public function setApiData( array $data );
66 /**
67 * Extension of Message implementing IApiMessage
68 * @since 1.25
69 * @ingroup API
70 * @todo: Would be nice to use a Trait here to avoid code duplication
72 class ApiMessage extends Message implements IApiMessage {
73 protected $apiCode = null;
74 protected $apiData = array();
76 /**
77 * Create an IApiMessage for the message
79 * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
80 * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
82 * @param Message|RawMessage|array|string $msg
83 * @param string|null $code
84 * @param array|null $data
85 * @return ApiMessage
87 public static function create( $msg, $code = null, array $data = null ) {
88 if ( $msg instanceof IApiMessage ) {
89 return $msg;
90 } elseif ( $msg instanceof RawMessage ) {
91 return new ApiRawMessage( $msg, $code, $data );
92 } else {
93 return new ApiMessage( $msg, $code, $data );
97 /**
98 * @param Message|string|array $msg
99 * - Message: is cloned
100 * - array: first element is $key, rest are $params to Message::__construct
101 * - string: passed to Message::__construct
102 * @param string|null $code
103 * @param array|null $data
104 * @return ApiMessage
106 public function __construct( $msg, $code = null, array $data = null ) {
107 if ( $msg instanceof Message ) {
108 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
109 if ( isset( $msg->$key ) ) {
110 $this->$key = $msg->$key;
113 } elseif ( is_array( $msg ) ) {
114 $key = array_shift( $msg );
115 parent::__construct( $key, $msg );
116 } else {
117 parent::__construct( $msg );
119 $this->apiCode = $code;
120 $this->apiData = (array)$data;
123 public function getApiCode() {
124 return $this->apiCode === null ? $this->getKey() : $this->apiCode;
127 public function setApiCode( $code, array $data = null ) {
128 $this->apiCode = $code;
129 if ( $data !== null ) {
130 $this->setApiData( $data );
134 public function getApiData() {
135 return $this->apiData;
138 public function setApiData( array $data ) {
139 $this->apiData = $data;
142 public function serialize() {
143 return serialize( array(
144 'parent' => parent::serialize(),
145 'apiCode' => $this->apiCode,
146 'apiData' => $this->apiData,
147 ) );
150 public function unserialize( $serialized ) {
151 $data = unserialize( $serialized );
152 parent::unserialize( $data['parent'] );
153 $this->apiCode = $data['apiCode'];
154 $this->apiData = $data['apiData'];
159 * Extension of RawMessage implementing IApiMessage
160 * @since 1.25
161 * @ingroup API
162 * @todo: Would be nice to use a Trait here to avoid code duplication
164 class ApiRawMessage extends RawMessage implements IApiMessage {
165 protected $apiCode = null;
166 protected $apiData = array();
169 * @param RawMessage|string|array $msg
170 * - RawMessage: is cloned
171 * - array: first element is $key, rest are $params to RawMessage::__construct
172 * - string: passed to RawMessage::__construct
173 * @param string|null $code
174 * @param array|null $data
176 public function __construct( $msg, $code = null, array $data = null ) {
177 if ( $msg instanceof RawMessage ) {
178 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
179 if ( isset( $msg->$key ) ) {
180 $this->$key = $msg->$key;
183 } elseif ( is_array( $msg ) ) {
184 $key = array_shift( $msg );
185 parent::__construct( $key, $msg );
186 } else {
187 parent::__construct( $msg );
189 $this->apiCode = $code;
190 $this->apiData = (array)$data;
193 public function getApiCode() {
194 return $this->apiCode === null ? $this->getKey() : $this->apiCode;
197 public function setApiCode( $code, array $data = null ) {
198 $this->apiCode = $code;
199 if ( $data !== null ) {
200 $this->setApiData( $data );
204 public function getApiData() {
205 return $this->apiData;
208 public function setApiData( array $data ) {
209 $this->apiData = $data;
212 public function serialize() {
213 return serialize( array(
214 'parent' => parent::serialize(),
215 'apiCode' => $this->apiCode,
216 'apiData' => $this->apiData,
217 ) );
220 public function unserialize( $serialized ) {
221 $data = unserialize( $serialized );
222 parent::unserialize( $data['parent'] );
223 $this->apiCode = $data['apiCode'];
224 $this->apiData = $data['apiData'];