wiki.techinc.nl/includes/api/ApiRawMessage.php
Bartosz Dziewoński 159dad8abb language: Improve behavior of RawMessage used as MessageSpecifier
We have some code that expects that if you disassemble a
MessageSpecifier using its getKey() and getParams() methods,
then assemble a new MessageSpecifier using the return values,
you will get (at least approximately) the same message.

This was not the case with RawMessage, even though it implements
the MessageSpecifier interface, because its "keys" are not real
message keys, and this operation would mess it up.

Override the MessageSpecifier methods on RawMessage so that they're
compatible with how everyone expects a MessageSpecifier to work.

Add some tests for OutputPage, Message\Converter and Status
to verify some scenarios that would previously have failed.

Depends-On: I41991989515b4791bc1746f26bd404bf4f17dbdb
Depends-On: I612361dd20ff8aad4c0069f1c5af78e3e13b9692
Change-Id: Iddd14efa8b9536277c372257d5a7be135f26a540
2024-07-16 15:06:26 +01:00

65 lines
2 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
use MediaWiki\Language\RawMessage;
/**
* Extension of RawMessage implementing IApiMessage
* @newable
* @since 1.25
* @ingroup API
*/
class ApiRawMessage extends RawMessage implements IApiMessage {
use ApiMessageTrait;
/**
* @stable to call
* @param RawMessage|string|array $msg
* - RawMessage: is cloned
* - array: first element is $key, rest are $params to RawMessage::__construct
* - string: passed to RawMessage::__construct
* @param string|null $code
* @param array|null $data
*/
public function __construct( $msg, $code = null, array $data = null ) {
if ( $msg instanceof RawMessage ) {
foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
if ( isset( $msg->$key ) ) {
$this->$key = $msg->$key;
}
}
} elseif ( is_array( $msg ) ) {
$key = array_shift( $msg );
parent::__construct( $key, $msg );
} else {
parent::__construct( $msg );
}
$this->setApiCode( $code, $data );
}
public function getApiCode() {
if ( $this->apiCode === null ) {
// Copied from ApiMessageTrait to avoid changing the error codes. This causes T350248,
// but there's nothing better we can do when a RawMessage is used.
$this->apiCode = preg_replace( '/[^a-zA-Z0-9_-]/', '_', $this->getTextOfRawMessage() );
}
return $this->apiCode;
}
}