ApiMessage.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Defines an interface for messages with additional machine-readable data for
  4. * use by the API, and provides concrete implementations of that interface.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. */
  23. /**
  24. * Interface for messages with machine-readable data for use by the API
  25. *
  26. * The idea is that it's a Message that has some extra data for the API to use when interpreting it
  27. * as an error (or, in the future, as a warning). Internals of MediaWiki often use messages (or
  28. * message keys, or Status objects containing messages) to pass information about errors to the user
  29. * (see e.g. Title::getUserPermissionsErrors()) and the API has to make do with that.
  30. *
  31. * @since 1.25
  32. * @note This interface exists to work around PHP's inheritance, so ApiMessage
  33. * can extend Message and ApiRawMessage can extend RawMessage while still
  34. * allowing an instanceof check for a Message object including this
  35. * functionality. If for some reason you feel the need to implement this
  36. * interface on some other class, that class must also implement all the
  37. * public methods the Message class provides (not just those from
  38. * MessageSpecifier, which as written is fairly useless).
  39. * @ingroup API
  40. */
  41. interface IApiMessage extends MessageSpecifier {
  42. /**
  43. * Returns a machine-readable code for use by the API
  44. *
  45. * If no code was specifically set, the message key is used as the code
  46. * after removing "apiwarn-" or "apierror-" prefixes and applying
  47. * backwards-compatibility mappings.
  48. *
  49. * @return string
  50. */
  51. public function getApiCode();
  52. /**
  53. * Returns additional machine-readable data about the error condition
  54. * @return array
  55. */
  56. public function getApiData();
  57. /**
  58. * Sets the machine-readable code for use by the API
  59. * @param string|null $code If null, uses the default (see self::getApiCode())
  60. * @param array|null $data If non-null, passed to self::setApiData()
  61. */
  62. public function setApiCode( $code, array $data = null );
  63. /**
  64. * Sets additional machine-readable data about the error condition
  65. * @param array $data
  66. */
  67. public function setApiData( array $data );
  68. }
  69. /**
  70. * Trait to implement the IApiMessage interface for Message subclasses
  71. * @since 1.27
  72. * @ingroup API
  73. */
  74. trait ApiMessageTrait {
  75. /**
  76. * Compatibility code mappings for various MW messages.
  77. * @todo Ideally anything relying on this should be changed to use ApiMessage.
  78. */
  79. protected static $messageMap = [
  80. 'actionthrottledtext' => 'ratelimited',
  81. 'autoblockedtext' => 'autoblocked',
  82. 'badaccess-group0' => 'permissiondenied',
  83. 'badaccess-groups' => 'permissiondenied',
  84. 'badipaddress' => 'invalidip',
  85. 'blankpage' => 'emptypage',
  86. 'blockedtext' => 'blocked',
  87. 'cannotdelete' => 'cantdelete',
  88. 'cannotundelete' => 'cantundelete',
  89. 'cantmove-titleprotected' => 'protectedtitle',
  90. 'cantrollback' => 'onlyauthor',
  91. 'confirmedittext' => 'confirmemail',
  92. 'content-not-allowed-here' => 'contentnotallowedhere',
  93. 'deleteprotected' => 'cantedit',
  94. 'delete-toobig' => 'bigdelete',
  95. 'edit-conflict' => 'editconflict',
  96. 'imagenocrossnamespace' => 'nonfilenamespace',
  97. 'imagetypemismatch' => 'filetypemismatch',
  98. 'importbadinterwiki' => 'badinterwiki',
  99. 'importcantopen' => 'cantopenfile',
  100. 'import-noarticle' => 'badinterwiki',
  101. 'importnofile' => 'nofile',
  102. 'importuploaderrorpartial' => 'partialupload',
  103. 'importuploaderrorsize' => 'filetoobig',
  104. 'importuploaderrortemp' => 'notempdir',
  105. 'ipb_already_blocked' => 'alreadyblocked',
  106. 'ipb_blocked_as_range' => 'blockedasrange',
  107. 'ipb_cant_unblock' => 'cantunblock',
  108. 'ipb_expiry_invalid' => 'invalidexpiry',
  109. 'ip_range_invalid' => 'invalidrange',
  110. 'mailnologin' => 'cantsend',
  111. 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
  112. 'movenologintext' => 'cantmove-anon',
  113. 'movenotallowed' => 'cantmove',
  114. 'movenotallowedfile' => 'cantmovefile',
  115. 'namespaceprotected' => 'protectednamespace',
  116. 'nocreate-loggedin' => 'cantcreate',
  117. 'nocreatetext' => 'cantcreate-anon',
  118. 'noname' => 'invaliduser',
  119. 'nosuchusershort' => 'nosuchuser',
  120. 'notanarticle' => 'missingtitle',
  121. 'nouserspecified' => 'invaliduser',
  122. 'ns-specialprotected' => 'unsupportednamespace',
  123. 'protect-cantedit' => 'cantedit',
  124. 'protectedinterface' => 'protectednamespace-interface',
  125. 'protectedpagetext' => 'protectedpage',
  126. 'range_block_disabled' => 'rangedisabled',
  127. 'rcpatroldisabled' => 'patroldisabled',
  128. 'readonlytext' => 'readonly',
  129. 'sessionfailure' => 'badtoken',
  130. 'systemblockedtext' => 'blocked',
  131. 'titleprotected' => 'protectedtitle',
  132. 'undo-failure' => 'undofailure',
  133. 'userrights-nodatabase' => 'nosuchdatabase',
  134. 'userrights-no-interwiki' => 'nointerwikiuserrights',
  135. ];
  136. protected $apiCode = null;
  137. protected $apiData = [];
  138. public function getApiCode() {
  139. if ( $this->apiCode === null ) {
  140. $key = $this->getKey();
  141. if ( isset( self::$messageMap[$key] ) ) {
  142. $this->apiCode = self::$messageMap[$key];
  143. } elseif ( $key === 'apierror-missingparam' ) {
  144. /// @todo: Kill this case along with ApiBase::$messageMap
  145. $this->apiCode = 'no' . $this->getParams()[0];
  146. } elseif ( substr( $key, 0, 8 ) === 'apiwarn-' ) {
  147. $this->apiCode = substr( $key, 8 );
  148. } elseif ( substr( $key, 0, 9 ) === 'apierror-' ) {
  149. $this->apiCode = substr( $key, 9 );
  150. } else {
  151. $this->apiCode = $key;
  152. }
  153. }
  154. return $this->apiCode;
  155. }
  156. public function setApiCode( $code, array $data = null ) {
  157. if ( $code !== null && !( is_string( $code ) && $code !== '' ) ) {
  158. throw new InvalidArgumentException( "Invalid code \"$code\"" );
  159. }
  160. $this->apiCode = $code;
  161. if ( $data !== null ) {
  162. $this->setApiData( $data );
  163. }
  164. }
  165. public function getApiData() {
  166. return $this->apiData;
  167. }
  168. public function setApiData( array $data ) {
  169. $this->apiData = $data;
  170. }
  171. public function serialize() {
  172. return serialize( [
  173. 'parent' => parent::serialize(),
  174. 'apiCode' => $this->apiCode,
  175. 'apiData' => $this->apiData,
  176. ] );
  177. }
  178. public function unserialize( $serialized ) {
  179. $data = unserialize( $serialized );
  180. parent::unserialize( $data['parent'] );
  181. $this->apiCode = $data['apiCode'];
  182. $this->apiData = $data['apiData'];
  183. }
  184. }
  185. /**
  186. * Extension of Message implementing IApiMessage
  187. * @since 1.25
  188. * @ingroup API
  189. */
  190. class ApiMessage extends Message implements IApiMessage {
  191. use ApiMessageTrait;
  192. /**
  193. * Create an IApiMessage for the message
  194. *
  195. * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
  196. * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
  197. *
  198. * @param Message|RawMessage|array|string $msg
  199. * @param string|null $code
  200. * @param array|null $data
  201. * @return IApiMessage
  202. */
  203. public static function create( $msg, $code = null, array $data = null ) {
  204. if ( is_array( $msg ) ) {
  205. // From StatusValue
  206. if ( isset( $msg['message'] ) ) {
  207. if ( isset( $msg['params'] ) ) {
  208. $msg = array_merge( [ $msg['message'] ], $msg['params'] );
  209. } else {
  210. $msg = [ $msg['message'] ];
  211. }
  212. }
  213. // Weirdness that comes in sometimes, including the above
  214. if ( $msg[0] instanceof MessageSpecifier ) {
  215. $msg = $msg[0];
  216. }
  217. }
  218. if ( $msg instanceof IApiMessage ) {
  219. return $msg;
  220. } elseif ( $msg instanceof RawMessage ) {
  221. return new ApiRawMessage( $msg, $code, $data );
  222. } else {
  223. return new ApiMessage( $msg, $code, $data );
  224. }
  225. }
  226. /**
  227. * @param Message|string|array $msg
  228. * - Message: is cloned
  229. * - array: first element is $key, rest are $params to Message::__construct
  230. * - string: passed to Message::__construct
  231. * @param string|null $code
  232. * @param array|null $data
  233. */
  234. public function __construct( $msg, $code = null, array $data = null ) {
  235. if ( $msg instanceof Message ) {
  236. foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
  237. if ( isset( $msg->$key ) ) {
  238. $this->$key = $msg->$key;
  239. }
  240. }
  241. } elseif ( is_array( $msg ) ) {
  242. $key = array_shift( $msg );
  243. parent::__construct( $key, $msg );
  244. } else {
  245. parent::__construct( $msg );
  246. }
  247. $this->setApiCode( $code, $data );
  248. }
  249. }
  250. /**
  251. * Extension of RawMessage implementing IApiMessage
  252. * @since 1.25
  253. * @ingroup API
  254. */
  255. class ApiRawMessage extends RawMessage implements IApiMessage {
  256. use ApiMessageTrait;
  257. /**
  258. * @param RawMessage|string|array $msg
  259. * - RawMessage: is cloned
  260. * - array: first element is $key, rest are $params to RawMessage::__construct
  261. * - string: passed to RawMessage::__construct
  262. * @param string|null $code
  263. * @param array|null $data
  264. */
  265. public function __construct( $msg, $code = null, array $data = null ) {
  266. if ( $msg instanceof RawMessage ) {
  267. foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
  268. if ( isset( $msg->$key ) ) {
  269. $this->$key = $msg->$key;
  270. }
  271. }
  272. } elseif ( is_array( $msg ) ) {
  273. $key = array_shift( $msg );
  274. parent::__construct( $key, $msg );
  275. } else {
  276. parent::__construct( $msg );
  277. }
  278. $this->setApiCode( $code, $data );
  279. }
  280. }