Allow partially blocked users to tag unrelated revisions
[lhc/web/wiklou.git] / includes / api / ApiMessage.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 * Extension of Message implementing IApiMessage
23 * @since 1.25
24 * @ingroup API
25 */
26 class ApiMessage extends Message implements IApiMessage {
27 use ApiMessageTrait;
28
29 /**
30 * Create an IApiMessage for the message
31 *
32 * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
33 * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
34 *
35 * @param Message|RawMessage|array|string $msg
36 * @param string|null $code
37 * @param array|null $data
38 * @return IApiMessage
39 */
40 public static function create( $msg, $code = null, array $data = null ) {
41 if ( is_array( $msg ) ) {
42 // From StatusValue
43 if ( isset( $msg['message'] ) ) {
44 if ( isset( $msg['params'] ) ) {
45 $msg = array_merge( [ $msg['message'] ], $msg['params'] );
46 } else {
47 $msg = [ $msg['message'] ];
48 }
49 }
50
51 // Weirdness that comes in sometimes, including the above
52 if ( $msg[0] instanceof MessageSpecifier ) {
53 $msg = $msg[0];
54 }
55 }
56
57 if ( $msg instanceof IApiMessage ) {
58 return $msg;
59 } elseif ( $msg instanceof RawMessage ) {
60 return new ApiRawMessage( $msg, $code, $data );
61 } else {
62 return new ApiMessage( $msg, $code, $data );
63 }
64 }
65
66 /**
67 * @param Message|string|array $msg
68 * - Message: is cloned
69 * - array: first element is $key, rest are $params to Message::__construct
70 * - string: passed to Message::__construct
71 * @param string|null $code
72 * @param array|null $data
73 */
74 public function __construct( $msg, $code = null, array $data = null ) {
75 if ( $msg instanceof Message ) {
76 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
77 if ( isset( $msg->$key ) ) {
78 $this->$key = $msg->$key;
79 }
80 }
81 } elseif ( is_array( $msg ) ) {
82 $key = array_shift( $msg );
83 parent::__construct( $key, $msg );
84 } else {
85 parent::__construct( $msg );
86 }
87 $this->setApiCode( $code, $data );
88 }
89 }