Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / api / ApiMessageTrait.php
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 '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 ( substr( $key, 0, 8 ) === 'apiwarn-' ) {
106 $this->apiCode = substr( $key, 8 );
107 } elseif ( substr( $key, 0, 9 ) === '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 serialize( [
140 'parent' => parent::serialize(),
141 'apiCode' => $this->apiCode,
142 'apiData' => $this->apiData,
143 ] );
144 }
145
146 public function unserialize( $serialized ) {
147 $data = unserialize( $serialized );
148 parent::unserialize( $data['parent'] );
149 $this->apiCode = $data['apiCode'];
150 $this->apiData = $data['apiData'];
151 }
152 }