MediaWiki REL1_40
ApiMessageTrait.php
Go to the documentation of this file.
1<?php
29
34 protected static $messageMap = [
35 'actionthrottledtext' => 'ratelimited',
36 'autoblockedtext' => 'autoblocked',
37 'badaccess-group0' => 'permissiondenied',
38 'badaccess-groups' => 'permissiondenied',
39 'badipaddress' => 'invalidip',
40 'blankpage' => 'emptypage',
41 'blockedtext' => 'blocked',
42 'blockedtext-composite' => 'blocked',
43 'blockedtext-partial' => 'blocked',
44 'cannotdelete' => 'cantdelete',
45 'cannotundelete' => 'cantundelete',
46 'cantmove-titleprotected' => 'protectedtitle',
47 'cantrollback' => 'onlyauthor',
48 'confirmedittext' => 'confirmemail',
49 'content-not-allowed-here' => 'contentnotallowedhere',
50 'deleteprotected' => 'cantedit',
51 'delete-toobig' => 'bigdelete',
52 'edit-conflict' => 'editconflict',
53 'imagenocrossnamespace' => 'nonfilenamespace',
54 'imagetypemismatch' => 'filetypemismatch',
55 'importbadinterwiki' => 'badinterwiki',
56 'importcantopen' => 'cantopenfile',
57 'import-noarticle' => 'badinterwiki',
58 'importnofile' => 'nofile',
59 'importuploaderrorpartial' => 'partialupload',
60 'importuploaderrorsize' => 'filetoobig',
61 'importuploaderrortemp' => 'notempdir',
62 'ipb_already_blocked' => 'alreadyblocked',
63 'ipb_blocked_as_range' => 'blockedasrange',
64 'ipb_cant_unblock' => 'cantunblock',
65 'ipb_expiry_invalid' => 'invalidexpiry',
66 'ip_range_invalid' => 'invalidrange',
67 'mailnologin' => 'cantsend',
68 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
69 'movenologintext' => 'cantmove-anon',
70 'movenotallowed' => 'cantmove',
71 'movenotallowedfile' => 'cantmovefile',
72 'namespaceprotected' => 'protectednamespace',
73 'nocreate-loggedin' => 'cantcreate',
74 'nocreatetext' => 'cantcreate-anon',
75 'noname' => 'invaliduser',
76 'nosuchusershort' => 'nosuchuser',
77 'notanarticle' => 'missingtitle',
78 'nouserspecified' => 'invaliduser',
79 'ns-specialprotected' => 'unsupportednamespace',
80 'protect-cantedit' => 'cantedit',
81 'protectedinterface' => 'protectednamespace-interface',
82 'protectedpagetext' => 'protectedpage',
83 'range_block_disabled' => 'rangedisabled',
84 'rcpatroldisabled' => 'patroldisabled',
85 'readonlytext' => 'readonly',
86 'sessionfailure' => 'badtoken',
87 'systemblockedtext' => 'blocked',
88 'titleprotected' => 'protectedtitle',
89 'undo-failure' => 'undofailure',
90 'userrights-nodatabase' => 'nosuchdatabase',
91 'userrights-no-interwiki' => 'nointerwikiuserrights',
92 ];
93
94 protected $apiCode = null;
95 protected $apiData = [];
96
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
119 public function setApiCode( $code, array $data = null ) {
120 if ( $code !== null && !ApiErrorFormatter::isValidApiCode( $code ) ) {
121 throw new InvalidArgumentException( "Invalid code \"$code\"" );
122 }
123
124 $this->apiCode = $code;
125 if ( $data !== null ) {
126 $this->setApiData( $data );
127 }
128 }
129
130 public function getApiData() {
131 return $this->apiData;
132 }
133
134 public function setApiData( array $data ) {
135 $this->apiData = $data;
136 }
137
138 public function __serialize() {
139 return [
140 'parent' => parent::__serialize(),
141 'apiCode' => $this->apiCode,
142 'apiData' => $this->apiData,
143 ];
144 }
145
146 public function __unserialize( $data ) {
147 parent::__unserialize( $data['parent'] );
148 $this->apiCode = $data['apiCode'];
149 $this->apiData = $data['apiData'];
150 }
151}
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.