[JobQueue] Avoid spamming the runJobs log with large blobs.
[lhc/web/wiklou.git] / includes / upload / PublishStashedFile.php
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 Maintenance
22 */
23 require_once( __DIR__ . '/../../maintenance/Maintenance.php' );
24 set_time_limit( 3600 ); // 1 hour
25
26 /**
27 * Upload a file from the upload stash into the local file repo.
28 *
29 * @ingroup Maintenance
30 */
31 class PublishStashedFile extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Upload stashed file into the local file repo";
35 $this->addOption( 'filename', "Desired file name", true, true );
36 $this->addOption( 'filekey', "Upload stash file key", true, true );
37 $this->addOption( 'userid', "Upload owner user ID", true, true );
38 $this->addOption( 'comment', "Upload comment", true, true );
39 $this->addOption( 'text', "Upload description", true, true );
40 $this->addOption( 'watch', "Whether the uploader should watch the page", true, true );
41 $this->addOption( 'sessionid', "Upload owner session ID", true, true );
42 }
43
44 public function execute() {
45 wfSetupSession( $this->getOption( 'sessionid' ) );
46 try {
47 $user = User::newFromId( $this->getOption( 'userid' ) );
48 if ( !$user ) {
49 throw new MWException( "No user with ID " . $this->getOption( 'userid' ) . "." );
50 }
51
52 UploadBase::setSessionStatus(
53 $this->getOption( 'filekey' ),
54 array( 'result' => 'Poll', 'stage' => 'publish', 'status' => Status::newGood() )
55 );
56
57 $upload = new UploadFromStash( $user );
58 // @TODO: initialize() causes a GET, ideally we could frontload the antivirus
59 // checks and anything else to the stash stage (which includes concatenation and
60 // the local file is thus already there). That way, instead of GET+PUT, there could
61 // just be a COPY operation from the stash to the public zone.
62 $upload->initialize( $this->getOption( 'filekey' ), $this->getOption( 'filename' ) );
63
64 // Check if the local file checks out (this is generally a no-op)
65 $verification = $upload->verifyUpload();
66 if ( $verification['status'] !== UploadBase::OK ) {
67 $status = Status::newFatal( 'verification-error' );
68 $status->value = array( 'verification' => $verification );
69 UploadBase::setSessionStatus(
70 $this->getOption( 'filekey' ),
71 array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
72 );
73 $this->error( "Could not verify upload.\n", 1 ); // die
74 }
75
76 // Upload the stashed file to a permanent location
77 $status = $upload->performUpload(
78 $this->getOption( 'comment' ),
79 $this->getOption( 'text' ),
80 $this->getOption( 'watch' ),
81 $user
82 );
83 if ( !$status->isGood() ) {
84 UploadBase::setSessionStatus(
85 $this->getOption( 'filekey' ),
86 array( 'result' => 'Failure', 'stage' => 'publish', 'status' => $status )
87 );
88 $this->error( $status->getWikiText() . "\n", 1 ); // die
89 }
90
91 // Build the image info array while we have the local reference handy
92 $apiMain = new ApiMain(); // dummy object (XXX)
93 $imageInfo = $upload->getImageInfo( $apiMain->getResult() );
94
95 // Cleanup any temporary local file
96 $upload->cleanupTempFile();
97
98 // Cache the info so the user doesn't have to wait forever to get the final info
99 UploadBase::setSessionStatus(
100 $this->getOption( 'filekey' ),
101 array(
102 'result' => 'Success',
103 'stage' => 'publish',
104 'filename' => $upload->getLocalFile()->getName(),
105 'imageinfo' => $imageInfo,
106 'status' => Status::newGood()
107 )
108 );
109 } catch ( MWException $e ) {
110 UploadBase::setSessionStatus(
111 $this->getOption( 'filekey' ),
112 array(
113 'result' => 'Failure',
114 'stage' => 'publish',
115 'status' => Status::newFatal( 'api-error-publishfailed' )
116 )
117 );
118 throw $e;
119 }
120 session_write_close();
121 }
122 }
123
124 $maintClass = "PublishStashedFile";
125 require_once( RUN_MAINTENANCE_IF_MAIN );