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