SECURITY: rate-limit and prevent blocked users from changing email
[lhc/web/wiklou.git] / includes / api / ApiMessageTrait.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 /**
22 * Trait to implement the IApiMessage interface for Message subclasses
23 * @since 1.27
24 * @ingroup API
25 */
26 trait ApiMessageTrait {
27
28 /**
29 * Compatibility code mappings for various MW messages.
30 * @todo Ideally anything relying on this should be changed to use ApiMessage.
31 */
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',
88 ];
89
90 protected $apiCode = null;
91 protected $apiData = [];
92
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 );
105 } else {
106 $this->apiCode = $key;
107 }
108
109 // Ensure the code is actually valid
110 $this->apiCode = preg_replace( '/[^a-zA-Z0-9_-]/', '_', $this->apiCode );
111 }
112 return $this->apiCode;
113 }
114
115 public function setApiCode( $code, array $data = null ) {
116 if ( $code !== null && !ApiErrorFormatter::isValidApiCode( $code ) ) {
117 throw new InvalidArgumentException( "Invalid code \"$code\"" );
118 }
119
120 $this->apiCode = $code;
121 if ( $data !== null ) {
122 $this->setApiData( $data );
123 }
124 }
125
126 public function getApiData() {
127 return $this->apiData;
128 }
129
130 public function setApiData( array $data ) {
131 $this->apiData = $data;
132 }
133
134 public function serialize() {
135 return serialize( [
136 'parent' => parent::serialize(),
137 'apiCode' => $this->apiCode,
138 'apiData' => $this->apiData,
139 ] );
140 }
141
142 public function unserialize( $serialized ) {
143 $data = unserialize( $serialized );
144 parent::unserialize( $data['parent'] );
145 $this->apiCode = $data['apiCode'];
146 $this->apiData = $data['apiData'];
147 }
148 }