ApiMessageTrait.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Trait to implement the IApiMessage interface for Message subclasses
  22. * @since 1.27
  23. * @ingroup API
  24. * @phan-file-suppress PhanTraitParentReference
  25. * @phan-file-suppress PhanUndeclaredMethod
  26. */
  27. trait ApiMessageTrait {
  28. /**
  29. * Compatibility code mappings for various MW messages.
  30. * @todo Ideally anything relying on this should be changed to use ApiMessage.
  31. */
  32. protected static $messageMap = [
  33. 'actionthrottledtext' => 'ratelimited',
  34. 'autoblockedtext' => 'autoblocked',
  35. 'badaccess-group0' => 'permissiondenied',
  36. 'badaccess-groups' => 'permissiondenied',
  37. 'badipaddress' => 'invalidip',
  38. 'blankpage' => 'emptypage',
  39. 'blockedtext' => 'blocked',
  40. 'blockedtext-composite' => 'blocked',
  41. 'blockedtext-partial' => 'blocked',
  42. 'cannotdelete' => 'cantdelete',
  43. 'cannotundelete' => 'cantundelete',
  44. 'cantmove-titleprotected' => 'protectedtitle',
  45. 'cantrollback' => 'onlyauthor',
  46. 'confirmedittext' => 'confirmemail',
  47. 'content-not-allowed-here' => 'contentnotallowedhere',
  48. 'deleteprotected' => 'cantedit',
  49. 'delete-toobig' => 'bigdelete',
  50. 'edit-conflict' => 'editconflict',
  51. 'imagenocrossnamespace' => 'nonfilenamespace',
  52. 'imagetypemismatch' => 'filetypemismatch',
  53. 'importbadinterwiki' => 'badinterwiki',
  54. 'importcantopen' => 'cantopenfile',
  55. 'import-noarticle' => 'badinterwiki',
  56. 'importnofile' => 'nofile',
  57. 'importuploaderrorpartial' => 'partialupload',
  58. 'importuploaderrorsize' => 'filetoobig',
  59. 'importuploaderrortemp' => 'notempdir',
  60. 'ipb_already_blocked' => 'alreadyblocked',
  61. 'ipb_blocked_as_range' => 'blockedasrange',
  62. 'ipb_cant_unblock' => 'cantunblock',
  63. 'ipb_expiry_invalid' => 'invalidexpiry',
  64. 'ip_range_invalid' => 'invalidrange',
  65. 'mailnologin' => 'cantsend',
  66. 'markedaspatrollederror-noautopatrol' => 'noautopatrol',
  67. 'movenologintext' => 'cantmove-anon',
  68. 'movenotallowed' => 'cantmove',
  69. 'movenotallowedfile' => 'cantmovefile',
  70. 'namespaceprotected' => 'protectednamespace',
  71. 'nocreate-loggedin' => 'cantcreate',
  72. 'nocreatetext' => 'cantcreate-anon',
  73. 'noname' => 'invaliduser',
  74. 'nosuchusershort' => 'nosuchuser',
  75. 'notanarticle' => 'missingtitle',
  76. 'nouserspecified' => 'invaliduser',
  77. 'ns-specialprotected' => 'unsupportednamespace',
  78. 'protect-cantedit' => 'cantedit',
  79. 'protectedinterface' => 'protectednamespace-interface',
  80. 'protectedpagetext' => 'protectedpage',
  81. 'range_block_disabled' => 'rangedisabled',
  82. 'rcpatroldisabled' => 'patroldisabled',
  83. 'readonlytext' => 'readonly',
  84. 'sessionfailure' => 'badtoken',
  85. 'systemblockedtext' => 'blocked',
  86. 'titleprotected' => 'protectedtitle',
  87. 'undo-failure' => 'undofailure',
  88. 'userrights-nodatabase' => 'nosuchdatabase',
  89. 'userrights-no-interwiki' => 'nointerwikiuserrights',
  90. ];
  91. protected $apiCode = null;
  92. protected $apiData = [];
  93. public function getApiCode() {
  94. if ( $this->apiCode === null ) {
  95. $key = $this->getKey();
  96. if ( isset( self::$messageMap[$key] ) ) {
  97. $this->apiCode = self::$messageMap[$key];
  98. } elseif ( $key === 'apierror-missingparam' ) {
  99. /// @todo: Kill this case along with ApiBase::$messageMap
  100. $this->apiCode = 'no' . $this->getParams()[0];
  101. } elseif ( substr( $key, 0, 8 ) === 'apiwarn-' ) {
  102. $this->apiCode = substr( $key, 8 );
  103. } elseif ( substr( $key, 0, 9 ) === 'apierror-' ) {
  104. $this->apiCode = substr( $key, 9 );
  105. } else {
  106. $this->apiCode = $key;
  107. }
  108. // Ensure the code is actually valid
  109. $this->apiCode = preg_replace( '/[^a-zA-Z0-9_-]/', '_', $this->apiCode );
  110. }
  111. return $this->apiCode;
  112. }
  113. public function setApiCode( $code, array $data = null ) {
  114. if ( $code !== null && !ApiErrorFormatter::isValidApiCode( $code ) ) {
  115. throw new InvalidArgumentException( "Invalid code \"$code\"" );
  116. }
  117. $this->apiCode = $code;
  118. if ( $data !== null ) {
  119. $this->setApiData( $data );
  120. }
  121. }
  122. public function getApiData() {
  123. return $this->apiData;
  124. }
  125. public function setApiData( array $data ) {
  126. $this->apiData = $data;
  127. }
  128. public function serialize() {
  129. return serialize( [
  130. 'parent' => parent::serialize(),
  131. 'apiCode' => $this->apiCode,
  132. 'apiData' => $this->apiData,
  133. ] );
  134. }
  135. public function unserialize( $serialized ) {
  136. $data = unserialize( $serialized );
  137. parent::unserialize( $data['parent'] );
  138. $this->apiCode = $data['apiCode'];
  139. $this->apiData = $data['apiData'];
  140. }
  141. }