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