Debian Codegraph @

includes/api/ApiMessageTrait.php

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