ApiFileRevert.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on March 5, 2011
  6. *
  7. * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. /**
  27. * @ingroup API
  28. */
  29. class ApiFileRevert extends ApiBase {
  30. /** @var LocalFile */
  31. protected $file;
  32. /** @var string */
  33. protected $archiveName;
  34. /** @var array */
  35. protected $params;
  36. public function execute() {
  37. $this->useTransactionalTimeLimit();
  38. $this->params = $this->extractRequestParams();
  39. // Extract the file and archiveName from the request parameters
  40. $this->validateParameters();
  41. // Check whether we're allowed to revert this file
  42. $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
  43. $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
  44. $status = $this->file->upload(
  45. $sourceUrl,
  46. $this->params['comment'],
  47. $this->params['comment'],
  48. 0,
  49. false,
  50. false,
  51. $this->getUser()
  52. );
  53. if ( $status->isGood() ) {
  54. $result = [ 'result' => 'Success' ];
  55. } else {
  56. $result = [
  57. 'result' => 'Failure',
  58. 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
  59. ];
  60. }
  61. $this->getResult()->addValue( null, $this->getModuleName(), $result );
  62. }
  63. /**
  64. * Validate the user parameters and set $this->archiveName and $this->file.
  65. * Throws an error if validation fails
  66. */
  67. protected function validateParameters() {
  68. // Validate the input title
  69. $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
  70. if ( is_null( $title ) ) {
  71. $this->dieWithError(
  72. [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
  73. );
  74. }
  75. $localRepo = RepoGroup::singleton()->getLocalRepo();
  76. // Check if the file really exists
  77. $this->file = $localRepo->newFile( $title );
  78. if ( !$this->file->exists() ) {
  79. $this->dieWithError( 'apierror-missingtitle' );
  80. }
  81. // Check if the archivename is valid for this file
  82. $this->archiveName = $this->params['archivename'];
  83. $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
  84. if ( !$oldFile->exists() ) {
  85. $this->dieWithError( 'filerevert-badversion' );
  86. }
  87. }
  88. public function mustBePosted() {
  89. return true;
  90. }
  91. public function isWriteMode() {
  92. return true;
  93. }
  94. public function getAllowedParams() {
  95. return [
  96. 'filename' => [
  97. ApiBase::PARAM_TYPE => 'string',
  98. ApiBase::PARAM_REQUIRED => true,
  99. ],
  100. 'comment' => [
  101. ApiBase::PARAM_DFLT => '',
  102. ],
  103. 'archivename' => [
  104. ApiBase::PARAM_TYPE => 'string',
  105. ApiBase::PARAM_REQUIRED => true,
  106. ],
  107. ];
  108. }
  109. public function needsToken() {
  110. return 'csrf';
  111. }
  112. protected function getExamplesMessages() {
  113. return [
  114. 'action=filerevert&filename=Wiki.png&comment=Revert&' .
  115. 'archivename=20110305152740!Wiki.png&token=123ABC'
  116. => 'apihelp-filerevert-example-revert',
  117. ];
  118. }
  119. }