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