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.
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.
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
22 * Trait to implement the IApiMessage interface for Message subclasses
26 trait ApiMessageTrait
{
29 * Compatibility code mappings for various MW messages.
30 * @todo Ideally anything relying on this should be changed to use ApiMessage.
32 protected static $messageMap = [
33 'actionthrottledtext' => 'ratelimited',
34 'autoblockedtext' => 'autoblocked',
35 'badaccess-group0' => 'permissiondenied',
36 'badaccess-groups' => 'permissiondenied',
37 'badipaddress' => 'invalidip',
38 'blankpage' => 'emptypage',
39 'blockedtext' => 'blocked',
40 'cannotdelete' => 'cantdelete',
41 'cannotundelete' => 'cantundelete',
42 'cantmove-titleprotected' => 'protectedtitle',
43 'cantrollback' => 'onlyauthor',
44 'confirmedittext' => 'confirmemail',
45 'content-not-allowed-here' => 'contentnotallowedhere',
46 'deleteprotected' => 'cantedit',
47 'delete-toobig' => 'bigdelete',
48 'edit-conflict' => 'editconflict',
49 'imagenocrossnamespace' => 'nonfilenamespace',
50 'imagetypemismatch' => 'filetypemismatch',
51 'importbadinterwiki' => 'badinterwiki',
52 'importcantopen' => 'cantopenfile',
53 'import-noarticle' => 'badinterwiki',
54 'importnofile' => 'nofile',
55 'importuploaderrorpartial' => 'partialupload',
56 'importuploaderrorsize' => 'filetoobig',
57 'importuploaderrortemp' => 'notempdir',
58 'ipb_already_blocked' => 'alreadyblocked',
59 'ipb_blocked_as_range' => 'blockedasrange',
60 'ipb_cant_unblock' => 'cantunblock',
61 'ipb_expiry_invalid' => 'invalidexpiry',
62 'ip_range_invalid' => 'invalidrange',
63 'mailnologin' => 'cantsend',
64 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
65 'movenologintext' => 'cantmove-anon',
66 'movenotallowed' => 'cantmove',
67 'movenotallowedfile' => 'cantmovefile',
68 'namespaceprotected' => 'protectednamespace',
69 'nocreate-loggedin' => 'cantcreate',
70 'nocreatetext' => 'cantcreate-anon',
71 'noname' => 'invaliduser',
72 'nosuchusershort' => 'nosuchuser',
73 'notanarticle' => 'missingtitle',
74 'nouserspecified' => 'invaliduser',
75 'ns-specialprotected' => 'unsupportednamespace',
76 'protect-cantedit' => 'cantedit',
77 'protectedinterface' => 'protectednamespace-interface',
78 'protectedpagetext' => 'protectedpage',
79 'range_block_disabled' => 'rangedisabled',
80 'rcpatroldisabled' => 'patroldisabled',
81 'readonlytext' => 'readonly',
82 'sessionfailure' => 'badtoken',
83 'systemblockedtext' => 'blocked',
84 'titleprotected' => 'protectedtitle',
85 'undo-failure' => 'undofailure',
86 'userrights-nodatabase' => 'nosuchdatabase',
87 'userrights-no-interwiki' => 'nointerwikiuserrights',
90 protected $apiCode = null;
91 protected $apiData = [];
93 public function getApiCode() {
94 if ( $this->apiCode
=== null ) {
95 $key = $this->getKey();
96 if ( isset( self
::$messageMap[$key] ) ) {
97 $this->apiCode
= self
::$messageMap[$key];
98 } elseif ( $key === 'apierror-missingparam' ) {
99 /// @todo: Kill this case along with ApiBase::$messageMap
100 $this->apiCode
= 'no' . $this->getParams()[0];
101 } elseif ( substr( $key, 0, 8 ) === 'apiwarn-' ) {
102 $this->apiCode
= substr( $key, 8 );
103 } elseif ( substr( $key, 0, 9 ) === 'apierror-' ) {
104 $this->apiCode
= substr( $key, 9 );
106 $this->apiCode
= $key;
109 // Ensure the code is actually valid
110 $this->apiCode
= preg_replace( '/[^a-zA-Z0-9_-]/', '_', $this->apiCode
);
112 return $this->apiCode
;
115 public function setApiCode( $code, array $data = null ) {
116 if ( $code !== null && !ApiErrorFormatter
::isValidApiCode( $code ) ) {
117 throw new InvalidArgumentException( "Invalid code \"$code\"" );
120 $this->apiCode
= $code;
121 if ( $data !== null ) {
122 $this->setApiData( $data );
126 public function getApiData() {
127 return $this->apiData
;
130 public function setApiData( array $data ) {
131 $this->apiData
= $data;
134 public function serialize() {
136 'parent' => parent
::serialize(),
137 'apiCode' => $this->apiCode
,
138 'apiData' => $this->apiData
,
142 public function unserialize( $serialized ) {
143 $data = unserialize( $serialized );
144 parent
::unserialize( $data['parent'] );
145 $this->apiCode
= $data['apiCode'];
146 $this->apiData
= $data['apiData'];