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.
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.
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
21 namespace MediaWiki\Api
;
23 use MediaWiki\Language\RawMessage
;
24 use MediaWiki\Message\Message
;
25 use Wikimedia\Message\MessageSpecifier
;
28 * Extension of Message implementing IApiMessage
33 class ApiMessage
extends Message
implements IApiMessage
{
37 * Create an IApiMessage for the message
39 * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
40 * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
43 * @param MessageSpecifier|array|string $msg
44 * @param string|null $code
45 * @param array|null $data
47 * @param-taint $msg tainted
49 public static function create( $msg, $code = null, ?
array $data = null ) {
50 if ( is_array( $msg ) ) {
52 if ( isset( $msg['message'] ) ) {
53 $msg = [ $msg['message'], ...$msg['params'] ??
[] ];
56 // Weirdness that comes in sometimes, including the above
57 if ( $msg[0] instanceof MessageSpecifier
) {
62 if ( $msg instanceof IApiMessage
) {
64 } elseif ( $msg instanceof RawMessage
) {
65 return new ApiRawMessage( $msg, $code, $data );
67 return new ApiMessage( $msg, $code, $data );
72 * @param MessageSpecifier|string|array $msg
73 * - Message: is cloned
74 * - array: first element is $key, rest are $params to Message::__construct
75 * - string, any other MessageSpecifier: passed to Message::__construct
76 * @param string|null $code
77 * @param array|null $data
79 public function __construct( $msg, $code = null, ?
array $data = null ) {
80 if ( $msg instanceof Message
) {
81 foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
82 if ( isset( $msg->$key ) ) {
83 $this->$key = $msg->$key;
86 } elseif ( is_array( $msg ) ) {
87 $key = array_shift( $msg );
88 parent
::__construct( $key, $msg );
90 parent
::__construct( $msg );
92 $this->setApiCode( $code, $data );
96 /** @deprecated class alias since 1.43 */
97 class_alias( ApiMessage
::class, 'ApiMessage' );