Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
ApiMessageTrait | |
100.00% |
28 / 28 |
|
100.00% |
6 / 6 |
14 | |
100.00% |
1 / 1 |
getApiCode | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |||
setApiCode | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
getApiData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setApiData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__serialize | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
__unserialize | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | /** |
22 | * Trait to implement the IApiMessage interface for Message subclasses |
23 | * @since 1.27 |
24 | * @ingroup API |
25 | * @phan-file-suppress PhanTraitParentReference |
26 | * @phan-file-suppress PhanUndeclaredMethod |
27 | */ |
28 | trait ApiMessageTrait { |
29 | |
30 | /** |
31 | * Compatibility code mappings for various MW messages. |
32 | * @todo Ideally anything relying on this should be changed to use ApiMessage. |
33 | */ |
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 | } |