Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / api / ApiMessage.php
index db826a6..ae66778 100644 (file)
 
 /**
  * Interface for messages with machine-readable data for use by the API
+ *
+ * The idea is that it's a Message that has some extra data for the API to use when interpreting it
+ * as an error (or, in the future, as a warning). Internals of MediaWiki often use messages (or
+ * message keys, or Status objects containing messages) to pass information about errors to the user
+ * (see e.g. Title::getUserPermissionsErrors()) and the API has to make do with that.
+ *
  * @since 1.25
  * @ingroup API
  */
@@ -57,15 +63,57 @@ interface IApiMessage extends MessageSpecifier {
        public function setApiData( array $data );
 }
 
+/**
+ * Trait to implement the IApiMessage interface for Message subclasses
+ * @since 1.27
+ * @ingroup API
+ */
+trait ApiMessageTrait {
+       protected $apiCode = null;
+       protected $apiData = [];
+
+       public function getApiCode() {
+               return $this->apiCode === null ? $this->getKey() : $this->apiCode;
+       }
+
+       public function setApiCode( $code, array $data = null ) {
+               $this->apiCode = $code;
+               if ( $data !== null ) {
+                       $this->setApiData( $data );
+               }
+       }
+
+       public function getApiData() {
+               return $this->apiData;
+       }
+
+       public function setApiData( array $data ) {
+               $this->apiData = $data;
+       }
+
+       public function serialize() {
+               return serialize( [
+                       'parent' => parent::serialize(),
+                       'apiCode' => $this->apiCode,
+                       'apiData' => $this->apiData,
+               ] );
+       }
+
+       public function unserialize( $serialized ) {
+               $data = unserialize( $serialized );
+               parent::unserialize( $data['parent'] );
+               $this->apiCode = $data['apiCode'];
+               $this->apiData = $data['apiData'];
+       }
+}
+
 /**
  * Extension of Message implementing IApiMessage
  * @since 1.25
  * @ingroup API
- * @todo: Would be nice to use a Trait here to avoid code duplication
  */
 class ApiMessage extends Message implements IApiMessage {
-       protected $apiCode = null;
-       protected $apiData = array();
+       use ApiMessageTrait;
 
        /**
         * Create an IApiMessage for the message
@@ -113,51 +161,15 @@ class ApiMessage extends Message implements IApiMessage {
                $this->apiCode = $code;
                $this->apiData = (array)$data;
        }
-
-       public function getApiCode() {
-               return $this->apiCode === null ? $this->getKey() : $this->apiCode;
-       }
-
-       public function setApiCode( $code, array $data = null ) {
-               $this->apiCode = $code;
-               if ( $data !== null ) {
-                       $this->setApiData( $data );
-               }
-       }
-
-       public function getApiData() {
-               return $this->apiData;
-       }
-
-       public function setApiData( array $data ) {
-               $this->apiData = $data;
-       }
-
-       public function serialize() {
-               return serialize( array(
-                       'parent' => parent::serialize(),
-                       'apiCode' => $this->apiCode,
-                       'apiData' => $this->apiData,
-               ) );
-       }
-
-       public function unserialize( $serialized ) {
-               $data = unserialize( $serialized );
-               parent::unserialize( $data['parent'] );
-               $this->apiCode = $data['apiCode'];
-               $this->apiData = $data['apiData'];
-       }
 }
 
 /**
  * Extension of RawMessage implementing IApiMessage
  * @since 1.25
  * @ingroup API
- * @todo: Would be nice to use a Trait here to avoid code duplication
  */
 class ApiRawMessage extends RawMessage implements IApiMessage {
-       protected $apiCode = null;
-       protected $apiData = array();
+       use ApiMessageTrait;
 
        /**
         * @param RawMessage|string|array $msg
@@ -166,7 +178,6 @@ class ApiRawMessage extends RawMessage implements IApiMessage {
         *  - string: passed to RawMessage::__construct
         * @param string|null $code
         * @param array|null $data
-        * @return ApiMessage
         */
        public function __construct( $msg, $code = null, array $data = null ) {
                if ( $msg instanceof RawMessage ) {
@@ -184,38 +195,4 @@ class ApiRawMessage extends RawMessage implements IApiMessage {
                $this->apiCode = $code;
                $this->apiData = (array)$data;
        }
-
-       public function getApiCode() {
-               return $this->apiCode === null ? $this->getKey() : $this->apiCode;
-       }
-
-       public function setApiCode( $code, array $data = null ) {
-               $this->apiCode = $code;
-               if ( $data !== null ) {
-                       $this->setApiData( $data );
-               }
-       }
-
-       public function getApiData() {
-               return $this->apiData;
-       }
-
-       public function setApiData( array $data ) {
-               $this->apiData = $data;
-       }
-
-       public function serialize() {
-               return serialize( array(
-                       'parent' => parent::serialize(),
-                       'apiCode' => $this->apiCode,
-                       'apiData' => $this->apiData,
-               ) );
-       }
-
-       public function unserialize( $serialized ) {
-               $data = unserialize( $serialized );
-               parent::unserialize( $data['parent'] );
-               $this->apiCode = $data['apiCode'];
-               $this->apiData = $data['apiData'];
-       }
 }