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