ApiFileRevert.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@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 ApiFileRevert extends ApiBase {
  26. /** @var LocalFile */
  27. protected $file;
  28. /** @var string */
  29. protected $archiveName;
  30. /** @var array */
  31. protected $params;
  32. public function execute() {
  33. $this->useTransactionalTimeLimit();
  34. $this->params = $this->extractRequestParams();
  35. // Extract the file and archiveName from the request parameters
  36. $this->validateParameters();
  37. // Check whether we're allowed to revert this file
  38. $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
  39. $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
  40. $status = $this->file->upload(
  41. $sourceUrl,
  42. $this->params['comment'],
  43. $this->params['comment'],
  44. 0,
  45. false,
  46. false,
  47. $this->getUser()
  48. );
  49. if ( $status->isGood() ) {
  50. $result = [ 'result' => 'Success' ];
  51. } else {
  52. $result = [
  53. 'result' => 'Failure',
  54. 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
  55. ];
  56. }
  57. $this->getResult()->addValue( null, $this->getModuleName(), $result );
  58. }
  59. /**
  60. * Validate the user parameters and set $this->archiveName and $this->file.
  61. * Throws an error if validation fails
  62. */
  63. protected function validateParameters() {
  64. // Validate the input title
  65. $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
  66. if ( is_null( $title ) ) {
  67. $this->dieWithError(
  68. [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
  69. );
  70. }
  71. $localRepo = RepoGroup::singleton()->getLocalRepo();
  72. // Check if the file really exists
  73. $this->file = $localRepo->newFile( $title );
  74. if ( !$this->file->exists() ) {
  75. $this->dieWithError( 'apierror-missingtitle' );
  76. }
  77. // Check if the archivename is valid for this file
  78. $this->archiveName = $this->params['archivename'];
  79. $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
  80. if ( !$oldFile->exists() ) {
  81. $this->dieWithError( 'filerevert-badversion' );
  82. }
  83. }
  84. public function mustBePosted() {
  85. return true;
  86. }
  87. public function isWriteMode() {
  88. return true;
  89. }
  90. public function getAllowedParams() {
  91. return [
  92. 'filename' => [
  93. ApiBase::PARAM_TYPE => 'string',
  94. ApiBase::PARAM_REQUIRED => true,
  95. ],
  96. 'comment' => [
  97. ApiBase::PARAM_DFLT => '',
  98. ],
  99. 'archivename' => [
  100. ApiBase::PARAM_TYPE => 'string',
  101. ApiBase::PARAM_REQUIRED => true,
  102. ],
  103. ];
  104. }
  105. public function needsToken() {
  106. return 'csrf';
  107. }
  108. protected function getExamplesMessages() {
  109. return [
  110. 'action=filerevert&filename=Wiki.png&comment=Revert&' .
  111. 'archivename=20110305152740!Wiki.png&token=123ABC'
  112. => 'apihelp-filerevert-example-revert',
  113. ];
  114. }
  115. }