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