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  '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' ) {
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 }
serialize()
trait $apiCode
$apiData
static isValidApiCode( $code)
Test whether a code is a valid API error code.
setApiCode( $code, array $data=null)
setApiData(array $data)
unserialize( $serialized)
getApiData()
foreach( $res as $row) $serialized
getApiCode()