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