Merge "Type hint against LinkTarget in WatchedItemStore"
[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 'blockedtext-composite' => 'blocked',
41 'blockedtext-partial' => 'blocked',
42 'cannotdelete' => 'cantdelete',
43 'cannotundelete' => 'cantundelete',
44 'cantmove-titleprotected' => 'protectedtitle',
45 'cantrollback' => 'onlyauthor',
46 'confirmedittext' => 'confirmemail',
47 'content-not-allowed-here' => 'contentnotallowedhere',
48 'deleteprotected' => 'cantedit',
49 'delete-toobig' => 'bigdelete',
50 'edit-conflict' => 'editconflict',
51 'imagenocrossnamespace' => 'nonfilenamespace',
52 'imagetypemismatch' => 'filetypemismatch',
53 'importbadinterwiki' => 'badinterwiki',
54 'importcantopen' => 'cantopenfile',
55 'import-noarticle' => 'badinterwiki',
56 'importnofile' => 'nofile',
57 'importuploaderrorpartial' => 'partialupload',
58 'importuploaderrorsize' => 'filetoobig',
59 'importuploaderrortemp' => 'notempdir',
60 'ipb_already_blocked' => 'alreadyblocked',
61 'ipb_blocked_as_range' => 'blockedasrange',
62 'ipb_cant_unblock' => 'cantunblock',
63 'ipb_expiry_invalid' => 'invalidexpiry',
64 'ip_range_invalid' => 'invalidrange',
65 'mailnologin' => 'cantsend',
66 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
67 'movenologintext' => 'cantmove-anon',
68 'movenotallowed' => 'cantmove',
69 'movenotallowedfile' => 'cantmovefile',
70 'namespaceprotected' => 'protectednamespace',
71 'nocreate-loggedin' => 'cantcreate',
72 'nocreatetext' => 'cantcreate-anon',
73 'noname' => 'invaliduser',
74 'nosuchusershort' => 'nosuchuser',
75 'notanarticle' => 'missingtitle',
76 'nouserspecified' => 'invaliduser',
77 'ns-specialprotected' => 'unsupportednamespace',
78 'protect-cantedit' => 'cantedit',
79 'protectedinterface' => 'protectednamespace-interface',
80 'protectedpagetext' => 'protectedpage',
81 'range_block_disabled' => 'rangedisabled',
82 'rcpatroldisabled' => 'patroldisabled',
83 'readonlytext' => 'readonly',
84 'sessionfailure' => 'badtoken',
85 'systemblockedtext' => 'blocked',
86 'titleprotected' => 'protectedtitle',
87 'undo-failure' => 'undofailure',
88 'userrights-nodatabase' => 'nosuchdatabase',
89 'userrights-no-interwiki' => 'nointerwikiuserrights',
90 ];
91
92 protected $apiCode = null;
93 protected $apiData = [];
94
95 public function getApiCode() {
96 if ( $this->apiCode === null ) {
97 $key = $this->getKey();
98 if ( isset( self::$messageMap[$key] ) ) {
99 $this->apiCode = self::$messageMap[$key];
100 } elseif ( $key === 'apierror-missingparam' ) {
101 /// @todo: Kill this case along with ApiBase::$messageMap
102 $this->apiCode = 'no' . $this->getParams()[0];
103 } elseif ( substr( $key, 0, 8 ) === 'apiwarn-' ) {
104 $this->apiCode = substr( $key, 8 );
105 } elseif ( substr( $key, 0, 9 ) === 'apierror-' ) {
106 $this->apiCode = substr( $key, 9 );
107 } else {
108 $this->apiCode = $key;
109 }
110
111 // Ensure the code is actually valid
112 $this->apiCode = preg_replace( '/[^a-zA-Z0-9_-]/', '_', $this->apiCode );
113 }
114 return $this->apiCode;
115 }
116
117 public function setApiCode( $code, array $data = null ) {
118 if ( $code !== null && !ApiErrorFormatter::isValidApiCode( $code ) ) {
119 throw new InvalidArgumentException( "Invalid code \"$code\"" );
120 }
121
122 $this->apiCode = $code;
123 if ( $data !== null ) {
124 $this->setApiData( $data );
125 }
126 }
127
128 public function getApiData() {
129 return $this->apiData;
130 }
131
132 public function setApiData( array $data ) {
133 $this->apiData = $data;
134 }
135
136 public function serialize() {
137 return serialize( [
138 'parent' => parent::serialize(),
139 'apiCode' => $this->apiCode,
140 'apiData' => $this->apiData,
141 ] );
142 }
143
144 public function unserialize( $serialized ) {
145 $data = unserialize( $serialized );
146 parent::unserialize( $data['parent'] );
147 $this->apiCode = $data['apiCode'];
148 $this->apiData = $data['apiData'];
149 }
150 }