MediaWiki master
ApiMessageTrait.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Api;
22
23use InvalidArgumentException;
24
33
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',
99 ];
100
102 protected $apiCode = null;
104 protected $apiData = [];
105
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;
120 }
121
122 // Ensure the code is actually valid
123 $this->apiCode = preg_replace( '/[^a-zA-Z0-9_-]/', '_', $this->apiCode );
124 }
125 return $this->apiCode;
126 }
127
128 public function setApiCode( $code, ?array $data = null ) {
129 if ( $code !== null && !ApiErrorFormatter::isValidApiCode( $code ) ) {
130 throw new InvalidArgumentException( "Invalid code \"$code\"" );
131 }
132
133 $this->apiCode = $code;
134 if ( $data !== null ) {
135 $this->setApiData( $data );
136 }
137 }
138
139 public function getApiData() {
140 return $this->apiData;
141 }
142
143 public function setApiData( array $data ) {
144 $this->apiData = $data;
145 }
146
147 public function __serialize() {
148 return [
149 'parent' => parent::__serialize(),
150 'apiCode' => $this->apiCode,
151 'apiData' => $this->apiData,
152 ];
153 }
154
155 public function __unserialize( $data ) {
156 parent::__unserialize( $data['parent'] );
157 $this->apiCode = $data['apiCode'];
158 $this->apiData = $data['apiData'];
159 }
160}
161
163class_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