Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiMessageTrait.php
blob3cc2a6720dc5cc52038c9d5b314b76b8a7190ff3
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.
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
18 * @file
21 namespace MediaWiki\Api;
23 use InvalidArgumentException;
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
32 trait ApiMessageTrait {
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[]
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_already_blocked' => 'alreadyblocked',
70 'ipb_blocked_as_range' => 'blockedasrange',
71 'ipb_cant_unblock' => 'cantunblock',
72 'ipb_expiry_invalid' => 'invalidexpiry',
73 'ip_range_invalid' => 'invalidrange',
74 'mailnologin' => 'cantsend',
75 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
76 'movenologintext' => 'cantmove-anon',
77 'movenotallowed' => 'cantmove',
78 'movenotallowedfile' => 'cantmovefile',
79 'namespaceprotected' => 'protectednamespace',
80 'nocreate-loggedin' => 'cantcreate',
81 'nocreatetext' => 'cantcreate-anon',
82 'noname' => 'invaliduser',
83 'nosuchusershort' => 'nosuchuser',
84 'notanarticle' => 'missingtitle',
85 'nouserspecified' => 'invaliduser',
86 'ns-specialprotected' => 'unsupportednamespace',
87 'protect-cantedit' => 'cantedit',
88 'protectedinterface' => 'protectednamespace-interface',
89 'protectedpagetext' => 'protectedpage',
90 'range_block_disabled' => 'rangedisabled',
91 'rcpatroldisabled' => 'patroldisabled',
92 'readonlytext' => 'readonly',
93 'sessionfailure' => 'badtoken',
94 'systemblockedtext' => 'blocked',
95 'titleprotected' => 'protectedtitle',
96 'undo-failure' => 'undofailure',
97 'userrights-nodatabase' => 'nosuchdatabase',
98 'userrights-no-interwiki' => 'nointerwikiuserrights',
101 /** @var string|null */
102 protected $apiCode = null;
103 /** @var array */
104 protected $apiData = [];
106 public function getApiCode() {
107 if ( $this->apiCode === null ) {
108 $key = $this->getKey();
109 if ( isset( self::$messageMap[$key] ) ) {
110 $this->apiCode = self::$messageMap[$key];
111 } elseif ( $key === 'apierror-missingparam' ) {
112 // @todo: Kill this case along with ApiBase::$messageMap
113 $this->apiCode = 'no' . $this->getParams()[0];
114 } elseif ( str_starts_with( $key, 'apiwarn-' ) ) {
115 $this->apiCode = substr( $key, 8 );
116 } elseif ( str_starts_with( $key, 'apierror-' ) ) {
117 $this->apiCode = substr( $key, 9 );
118 } else {
119 $this->apiCode = $key;
122 // Ensure the code is actually valid
123 $this->apiCode = preg_replace( '/[^a-zA-Z0-9_-]/', '_', $this->apiCode );
125 return $this->apiCode;
128 public function setApiCode( $code, ?array $data = null ) {
129 if ( $code !== null && !ApiErrorFormatter::isValidApiCode( $code ) ) {
130 throw new InvalidArgumentException( "Invalid code \"$code\"" );
133 $this->apiCode = $code;
134 if ( $data !== null ) {
135 $this->setApiData( $data );
139 public function getApiData() {
140 return $this->apiData;
143 public function setApiData( array $data ) {
144 $this->apiData = $data;
147 public function __serialize() {
148 return [
149 'parent' => parent::__serialize(),
150 'apiCode' => $this->apiCode,
151 'apiData' => $this->apiData,
155 public function __unserialize( $data ) {
156 parent::__unserialize( $data['parent'] );
157 $this->apiCode = $data['apiCode'];
158 $this->apiData = $data['apiData'];
162 /** @deprecated class alias since 1.43 */
163 class_alias( ApiMessageTrait::class, 'ApiMessageTrait' );