ApiUndelete.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * @ingroup API
  24. */
  25. class ApiUndelete extends ApiBase {
  26. public function execute() {
  27. $this->useTransactionalTimeLimit();
  28. $params = $this->extractRequestParams();
  29. $user = $this->getUser();
  30. $block = $user->getBlock();
  31. if ( $block && $block->isSitewide() ) {
  32. $this->dieBlocked( $user->getBlock() );
  33. }
  34. $titleObj = Title::newFromText( $params['title'] );
  35. if ( !$titleObj || $titleObj->isExternal() ) {
  36. $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
  37. }
  38. if ( !$this->getPermissionManager()->userCan( 'undelete', $this->getUser(), $titleObj ) ) {
  39. $this->dieWithError( 'permdenied-undelete' );
  40. }
  41. // Check if user can add tags
  42. if ( !is_null( $params['tags'] ) ) {
  43. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  44. if ( !$ableToTag->isOK() ) {
  45. $this->dieStatus( $ableToTag );
  46. }
  47. }
  48. // Convert timestamps
  49. if ( !isset( $params['timestamps'] ) ) {
  50. $params['timestamps'] = [];
  51. }
  52. if ( !is_array( $params['timestamps'] ) ) {
  53. $params['timestamps'] = [ $params['timestamps'] ];
  54. }
  55. foreach ( $params['timestamps'] as $i => $ts ) {
  56. $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
  57. }
  58. $pa = new PageArchive( $titleObj, $this->getConfig() );
  59. $retval = $pa->undelete(
  60. ( $params['timestamps'] ?? [] ),
  61. $params['reason'],
  62. $params['fileids'],
  63. false,
  64. $user,
  65. $params['tags']
  66. );
  67. if ( !is_array( $retval ) ) {
  68. $this->dieWithError( 'apierror-cantundelete' );
  69. }
  70. if ( $retval[1] ) {
  71. Hooks::run( 'FileUndeleteComplete',
  72. [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
  73. }
  74. $this->setWatch( $params['watchlist'], $titleObj );
  75. $info = [
  76. 'title' => $titleObj->getPrefixedText(),
  77. 'revisions' => (int)$retval[0],
  78. 'fileversions' => (int)$retval[1],
  79. 'reason' => $retval[2]
  80. ];
  81. $this->getResult()->addValue( null, $this->getModuleName(), $info );
  82. }
  83. public function mustBePosted() {
  84. return true;
  85. }
  86. public function isWriteMode() {
  87. return true;
  88. }
  89. public function getAllowedParams() {
  90. return [
  91. 'title' => [
  92. ApiBase::PARAM_TYPE => 'string',
  93. ApiBase::PARAM_REQUIRED => true
  94. ],
  95. 'reason' => '',
  96. 'tags' => [
  97. ApiBase::PARAM_TYPE => 'tags',
  98. ApiBase::PARAM_ISMULTI => true,
  99. ],
  100. 'timestamps' => [
  101. ApiBase::PARAM_TYPE => 'timestamp',
  102. ApiBase::PARAM_ISMULTI => true,
  103. ],
  104. 'fileids' => [
  105. ApiBase::PARAM_TYPE => 'integer',
  106. ApiBase::PARAM_ISMULTI => true,
  107. ],
  108. 'watchlist' => [
  109. ApiBase::PARAM_DFLT => 'preferences',
  110. ApiBase::PARAM_TYPE => [
  111. 'watch',
  112. 'unwatch',
  113. 'preferences',
  114. 'nochange'
  115. ],
  116. ],
  117. ];
  118. }
  119. public function needsToken() {
  120. return 'csrf';
  121. }
  122. protected function getExamplesMessages() {
  123. return [
  124. 'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
  125. => 'apihelp-undelete-example-page',
  126. 'action=undelete&title=Main%20Page&token=123ABC' .
  127. '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
  128. => 'apihelp-undelete-example-revisions',
  129. ];
  130. }
  131. public function getHelpUrls() {
  132. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
  133. }
  134. }