| 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 | namespace MediaWiki\Api; |
| 22 | |
| 23 | use MediaWiki\Language\RawMessage; |
| 24 | use MediaWiki\Message\Message; |
| 25 | use Wikimedia\Message\MessageSpecifier; |
| 26 | |
| 27 | /** |
| 28 | * Extension of Message implementing IApiMessage |
| 29 | * @newable |
| 30 | * @since 1.25 |
| 31 | * @ingroup API |
| 32 | */ |
| 33 | class ApiMessage extends Message implements IApiMessage { |
| 34 | use ApiMessageTrait; |
| 35 | |
| 36 | /** |
| 37 | * Create an IApiMessage for the message |
| 38 | * |
| 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. |
| 41 | * |
| 42 | * @stable to call |
| 43 | * @param MessageSpecifier|array|string $msg |
| 44 | * @param string|null $code |
| 45 | * @param array|null $data |
| 46 | * @return IApiMessage |
| 47 | * @param-taint $msg tainted |
| 48 | */ |
| 49 | public static function create( $msg, $code = null, ?array $data = null ) { |
| 50 | if ( is_array( $msg ) ) { |
| 51 | // From StatusValue |
| 52 | if ( isset( $msg['message'] ) ) { |
| 53 | $msg = [ $msg['message'], ...$msg['params'] ?? [] ]; |
| 54 | } |
| 55 | |
| 56 | // Weirdness that comes in sometimes, including the above |
| 57 | if ( $msg[0] instanceof MessageSpecifier ) { |
| 58 | $msg = $msg[0]; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if ( $msg instanceof IApiMessage ) { |
| 63 | return $msg; |
| 64 | } elseif ( $msg instanceof RawMessage ) { |
| 65 | return new ApiRawMessage( $msg, $code, $data ); |
| 66 | } else { |
| 67 | return new ApiMessage( $msg, $code, $data ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 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 |
| 78 | */ |
| 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; |
| 84 | } |
| 85 | } |
| 86 | } elseif ( is_array( $msg ) ) { |
| 87 | $key = array_shift( $msg ); |
| 88 | parent::__construct( $key, $msg ); |
| 89 | } else { |
| 90 | parent::__construct( $msg ); |
| 91 | } |
| 92 | $this->setApiCode( $code, $data ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** @deprecated class alias since 1.43 */ |
| 97 | class_alias( ApiMessage::class, 'ApiMessage' ); |