ApiMessage.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. * @ingroup API
  33. */
  34. interface IApiMessage extends MessageSpecifier {
  35. /**
  36. * Returns a machine-readable code for use by the API
  37. *
  38. * If no code was specifically set, the message key is used as the code
  39. * after removing "apiwarn-" or "apierror-" prefixes and applying
  40. * backwards-compatibility mappings.
  41. *
  42. * @return string
  43. */
  44. public function getApiCode();
  45. /**
  46. * Returns additional machine-readable data about the error condition
  47. * @return array
  48. */
  49. public function getApiData();
  50. /**
  51. * Sets the machine-readable code for use by the API
  52. * @param string|null $code If null, uses the default (see self::getApiCode())
  53. * @param array|null $data If non-null, passed to self::setApiData()
  54. */
  55. public function setApiCode( $code, array $data = null );
  56. /**
  57. * Sets additional machine-readable data about the error condition
  58. * @param array $data
  59. */
  60. public function setApiData( array $data );
  61. }
  62. /**
  63. * Trait to implement the IApiMessage interface for Message subclasses
  64. * @since 1.27
  65. * @ingroup API
  66. */
  67. trait ApiMessageTrait {
  68. /**
  69. * Compatibility code mappings for various MW messages.
  70. * @todo Ideally anything relying on this should be changed to use ApiMessage.
  71. */
  72. protected static $messageMap = [
  73. 'actionthrottledtext' => 'ratelimited',
  74. 'autoblockedtext' => 'autoblocked',
  75. 'badaccess-group0' => 'permissiondenied',
  76. 'badaccess-groups' => 'permissiondenied',
  77. 'badipaddress' => 'invalidip',
  78. 'blankpage' => 'emptypage',
  79. 'blockedtext' => 'blocked',
  80. 'cannotdelete' => 'cantdelete',
  81. 'cannotundelete' => 'cantundelete',
  82. 'cantmove-titleprotected' => 'protectedtitle',
  83. 'cantrollback' => 'onlyauthor',
  84. 'confirmedittext' => 'confirmemail',
  85. 'content-not-allowed-here' => 'contentnotallowedhere',
  86. 'deleteprotected' => 'cantedit',
  87. 'delete-toobig' => 'bigdelete',
  88. 'edit-conflict' => 'editconflict',
  89. 'imagenocrossnamespace' => 'nonfilenamespace',
  90. 'imagetypemismatch' => 'filetypemismatch',
  91. 'importbadinterwiki' => 'badinterwiki',
  92. 'importcantopen' => 'cantopenfile',
  93. 'import-noarticle' => 'badinterwiki',
  94. 'importnofile' => 'nofile',
  95. 'importuploaderrorpartial' => 'partialupload',
  96. 'importuploaderrorsize' => 'filetoobig',
  97. 'importuploaderrortemp' => 'notempdir',
  98. 'ipb_already_blocked' => 'alreadyblocked',
  99. 'ipb_blocked_as_range' => 'blockedasrange',
  100. 'ipb_cant_unblock' => 'cantunblock',
  101. 'ipb_expiry_invalid' => 'invalidexpiry',
  102. 'ip_range_invalid' => 'invalidrange',
  103. 'mailnologin' => 'cantsend',
  104. 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
  105. 'movenologintext' => 'cantmove-anon',
  106. 'movenotallowed' => 'cantmove',
  107. 'movenotallowedfile' => 'cantmovefile',
  108. 'namespaceprotected' => 'protectednamespace',
  109. 'nocreate-loggedin' => 'cantcreate',
  110. 'nocreatetext' => 'cantcreate-anon',
  111. 'noname' => 'invaliduser',
  112. 'nosuchusershort' => 'nosuchuser',
  113. 'notanarticle' => 'missingtitle',
  114. 'nouserspecified' => 'invaliduser',
  115. 'ns-specialprotected' => 'unsupportednamespace',
  116. 'protect-cantedit' => 'cantedit',
  117. 'protectedinterface' => 'protectednamespace-interface',
  118. 'protectedpagetext' => 'protectedpage',
  119. 'range_block_disabled' => 'rangedisabled',
  120. 'rcpatroldisabled' => 'patroldisabled',
  121. 'readonlytext' => 'readonly',
  122. 'sessionfailure' => 'badtoken',
  123. 'systemblockedtext' => 'blocked',
  124. 'titleprotected' => 'protectedtitle',
  125. 'undo-failure' => 'undofailure',
  126. 'userrights-nodatabase' => 'nosuchdatabase',
  127. 'userrights-no-interwiki' => 'nointerwikiuserrights',
  128. ];
  129. protected $apiCode = null;
  130. protected $apiData = [];
  131. public function getApiCode() {
  132. if ( $this->apiCode === null ) {
  133. $key = $this->getKey();
  134. if ( isset( self::$messageMap[$key] ) ) {
  135. $this->apiCode = self::$messageMap[$key];
  136. } elseif ( $key === 'apierror-missingparam' ) {
  137. /// @todo: Kill this case along with ApiBase::$messageMap
  138. $this->apiCode = 'no' . $this->getParams()[0];
  139. } elseif ( substr( $key, 0, 8 ) === 'apiwarn-' ) {
  140. $this->apiCode = substr( $key, 8 );
  141. } elseif ( substr( $key, 0, 9 ) === 'apierror-' ) {
  142. $this->apiCode = substr( $key, 9 );
  143. } else {
  144. $this->apiCode = $key;
  145. }
  146. }
  147. return $this->apiCode;
  148. }
  149. public function setApiCode( $code, array $data = null ) {
  150. if ( $code !== null && !( is_string( $code ) && $code !== '' ) ) {
  151. throw new InvalidArgumentException( "Invalid code \"$code\"" );
  152. }
  153. $this->apiCode = $code;
  154. if ( $data !== null ) {
  155. $this->setApiData( $data );
  156. }
  157. }
  158. public function getApiData() {
  159. return $this->apiData;
  160. }
  161. public function setApiData( array $data ) {
  162. $this->apiData = $data;
  163. }
  164. public function serialize() {
  165. return serialize( [
  166. 'parent' => parent::serialize(),
  167. 'apiCode' => $this->apiCode,
  168. 'apiData' => $this->apiData,
  169. ] );
  170. }
  171. public function unserialize( $serialized ) {
  172. $data = unserialize( $serialized );
  173. parent::unserialize( $data['parent'] );
  174. $this->apiCode = $data['apiCode'];
  175. $this->apiData = $data['apiData'];
  176. }
  177. }
  178. /**
  179. * Extension of Message implementing IApiMessage
  180. * @since 1.25
  181. * @ingroup API
  182. */
  183. class ApiMessage extends Message implements IApiMessage {
  184. use ApiMessageTrait;
  185. /**
  186. * Create an IApiMessage for the message
  187. *
  188. * This returns $msg if it's an IApiMessage, calls 'new ApiRawMessage' if
  189. * $msg is a RawMessage, or calls 'new ApiMessage' in all other cases.
  190. *
  191. * @param Message|RawMessage|array|string $msg
  192. * @param string|null $code
  193. * @param array|null $data
  194. * @return IApiMessage
  195. */
  196. public static function create( $msg, $code = null, array $data = null ) {
  197. if ( is_array( $msg ) ) {
  198. // From StatusValue
  199. if ( isset( $msg['message'] ) ) {
  200. if ( isset( $msg['params'] ) ) {
  201. $msg = array_merge( [ $msg['message'] ], $msg['params'] );
  202. } else {
  203. $msg = [ $msg['message'] ];
  204. }
  205. }
  206. // Weirdness that comes in sometimes, including the above
  207. if ( $msg[0] instanceof MessageSpecifier ) {
  208. $msg = $msg[0];
  209. }
  210. }
  211. if ( $msg instanceof IApiMessage ) {
  212. return $msg;
  213. } elseif ( $msg instanceof RawMessage ) {
  214. return new ApiRawMessage( $msg, $code, $data );
  215. } else {
  216. return new ApiMessage( $msg, $code, $data );
  217. }
  218. }
  219. /**
  220. * @param Message|string|array $msg
  221. * - Message: is cloned
  222. * - array: first element is $key, rest are $params to Message::__construct
  223. * - string: passed to Message::__construct
  224. * @param string|null $code
  225. * @param array|null $data
  226. */
  227. public function __construct( $msg, $code = null, array $data = null ) {
  228. if ( $msg instanceof Message ) {
  229. foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
  230. if ( isset( $msg->$key ) ) {
  231. $this->$key = $msg->$key;
  232. }
  233. }
  234. } elseif ( is_array( $msg ) ) {
  235. $key = array_shift( $msg );
  236. parent::__construct( $key, $msg );
  237. } else {
  238. parent::__construct( $msg );
  239. }
  240. $this->setApiCode( $code, $data );
  241. }
  242. }
  243. /**
  244. * Extension of RawMessage implementing IApiMessage
  245. * @since 1.25
  246. * @ingroup API
  247. */
  248. class ApiRawMessage extends RawMessage implements IApiMessage {
  249. use ApiMessageTrait;
  250. /**
  251. * @param RawMessage|string|array $msg
  252. * - RawMessage: is cloned
  253. * - array: first element is $key, rest are $params to RawMessage::__construct
  254. * - string: passed to RawMessage::__construct
  255. * @param string|null $code
  256. * @param array|null $data
  257. */
  258. public function __construct( $msg, $code = null, array $data = null ) {
  259. if ( $msg instanceof RawMessage ) {
  260. foreach ( get_class_vars( get_class( $this ) ) as $key => $value ) {
  261. if ( isset( $msg->$key ) ) {
  262. $this->$key = $msg->$key;
  263. }
  264. }
  265. } elseif ( is_array( $msg ) ) {
  266. $key = array_shift( $msg );
  267. parent::__construct( $key, $msg );
  268. } else {
  269. parent::__construct( $msg );
  270. }
  271. $this->setApiCode( $code, $data );
  272. }
  273. }