PublishStashedFileJob.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Upload a file from the upload stash into the local file repo.
  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. * @ingroup Upload
  22. * @ingroup JobQueue
  23. */
  24. use Wikimedia\ScopedCallback;
  25. /**
  26. * Upload a file from the upload stash into the local file repo.
  27. *
  28. * @ingroup Upload
  29. * @ingroup JobQueue
  30. */
  31. class PublishStashedFileJob extends Job {
  32. public function __construct( Title $title, array $params ) {
  33. parent::__construct( 'PublishStashedFile', $title, $params );
  34. $this->removeDuplicates = true;
  35. }
  36. public function run() {
  37. $scope = RequestContext::importScopedSession( $this->params['session'] );
  38. $this->addTeardownCallback( function () use ( &$scope ) {
  39. ScopedCallback::consume( $scope ); // T126450
  40. } );
  41. $context = RequestContext::getMain();
  42. $user = $context->getUser();
  43. try {
  44. if ( !$user->isLoggedIn() ) {
  45. $this->setLastError( "Could not load the author user from session." );
  46. return false;
  47. }
  48. UploadBase::setSessionStatus(
  49. $user,
  50. $this->params['filekey'],
  51. [ 'result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood() ]
  52. );
  53. $upload = new UploadFromStash( $user );
  54. // @todo initialize() causes a GET, ideally we could frontload the antivirus
  55. // checks and anything else to the stash stage (which includes concatenation and
  56. // the local file is thus already there). That way, instead of GET+PUT, there could
  57. // just be a COPY operation from the stash to the public zone.
  58. $upload->initialize( $this->params['filekey'], $this->params['filename'] );
  59. // Check if the local file checks out (this is generally a no-op)
  60. $verification = $upload->verifyUpload();
  61. if ( $verification['status'] !== UploadBase::OK ) {
  62. $status = Status::newFatal( 'verification-error' );
  63. $status->value = [ 'verification' => $verification ];
  64. UploadBase::setSessionStatus(
  65. $user,
  66. $this->params['filekey'],
  67. [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
  68. );
  69. $this->setLastError( "Could not verify upload." );
  70. return false;
  71. }
  72. // Upload the stashed file to a permanent location
  73. $status = $upload->performUpload(
  74. $this->params['comment'],
  75. $this->params['text'],
  76. $this->params['watch'],
  77. $user,
  78. $this->params['tags'] ?? []
  79. );
  80. if ( !$status->isGood() ) {
  81. UploadBase::setSessionStatus(
  82. $user,
  83. $this->params['filekey'],
  84. [ 'result' => 'Failure', 'stage' => 'publish', 'status' => $status ]
  85. );
  86. $this->setLastError( $status->getWikiText( false, false, 'en' ) );
  87. return false;
  88. }
  89. // Build the image info array while we have the local reference handy
  90. $apiMain = new ApiMain(); // dummy object (XXX)
  91. $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
  92. // Cleanup any temporary local file
  93. $upload->cleanupTempFile();
  94. // Cache the info so the user doesn't have to wait forever to get the final info
  95. UploadBase::setSessionStatus(
  96. $user,
  97. $this->params['filekey'],
  98. [
  99. 'result' => 'Success',
  100. 'stage' => 'publish',
  101. 'filename' => $upload->getLocalFile()->getName(),
  102. 'imageinfo' => $imageInfo,
  103. 'status' => Status::newGood()
  104. ]
  105. );
  106. } catch ( Exception $e ) {
  107. UploadBase::setSessionStatus(
  108. $user,
  109. $this->params['filekey'],
  110. [
  111. 'result' => 'Failure',
  112. 'stage' => 'publish',
  113. 'status' => Status::newFatal( 'api-error-publishfailed' )
  114. ]
  115. );
  116. $this->setLastError( get_class( $e ) . ": " . $e->getMessage() );
  117. // To prevent potential database referential integrity issues.
  118. // See T34551.
  119. MWExceptionHandler::rollbackMasterChangesAndLog( $e );
  120. return false;
  121. }
  122. return true;
  123. }
  124. public function getDeduplicationInfo() {
  125. $info = parent::getDeduplicationInfo();
  126. if ( is_array( $info['params'] ) ) {
  127. $info['params'] = [ 'filekey' => $info['params']['filekey'] ];
  128. }
  129. return $info;
  130. }
  131. public function allowRetries() {
  132. return false;
  133. }
  134. }