Restructured upload-by-url:
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2 /**
3 * @file
4 * @ingroup upload
5 *
6 * Implements uploading from a HTTP resource.
7 *
8 * @author Bryan Tong Minh
9 * @author Michael Dale
10 */
11 class UploadFromUrl extends UploadBase {
12 protected $mAsync, $mUrl;
13
14 /**
15 * Checks if the user is allowed to use the upload-by-URL feature. If the
16 * user is allowed, pass on permissions checking to the parent.
17 */
18 public static function isAllowed( $user ) {
19 if ( !$user->isAllowed( 'upload_by_url' ) )
20 return 'upload_by_url';
21 return parent::isAllowed( $user );
22 }
23
24 /**
25 * Checks if the upload from URL feature is enabled
26 * @return bool
27 */
28 public static function isEnabled() {
29 global $wgAllowCopyUploads;
30 return $wgAllowCopyUploads && parent::isEnabled();
31 }
32
33 /**
34 * Entry point for API upload
35 *
36 * @param $name string
37 * @param $url string
38 * @param $async mixed Whether the download should be performed
39 * asynchronous. False for synchronous, async or async-leavemessage for
40 * asynchronous download.
41 */
42 public function initialize( $name, $url, $async = false ) {
43 global $wgUser;
44
45 $this->mUrl = $url;
46 $this->mAsync = $async;
47
48 $tempPath = $async ? null : $this->makeTemporaryFile();
49 # File size and removeTempFile will be filled in later
50 $this->initializePathInfo( $name, $tempPath, 0, false );
51 }
52
53 /**
54 * Entry point for SpecialUpload
55 * @param $request Object: WebRequest object
56 */
57 public function initializeFromRequest( &$request ) {
58 $desiredDestName = $request->getText( 'wpDestFile' );
59 if ( !$desiredDestName )
60 $desiredDestName = $request->getText( 'wpUploadFileURL' );
61 return $this->initialize(
62 $desiredDestName,
63 $request->getVal( 'wpUploadFileURL' ),
64 false
65 );
66 }
67
68 /**
69 * @param $request Object: WebRequest object
70 */
71 public static function isValidRequest( $request ) {
72 global $wgUser;
73
74 $url = $request->getVal( 'wpUploadFileURL' );
75 return !empty( $url )
76 && Http::isValidURI( $url )
77 && $wgUser->isAllowed( 'upload_by_url' );
78 }
79
80
81 public function fetchFile() {
82 if ( !Http::isValidURI( $this->mUrl ) ) {
83 return Status::newFatal( 'http-invalid-url' );
84 }
85
86 if ( !$this->mAsync ) {
87 return $this->reallyFetchFile();
88 }
89 return Status::newGood();
90 }
91 protected function makeTemporaryFile() {
92 return tempnam( wfTempDir(), 'URL' );
93 }
94 private function saveTempFile( $req ) {
95 if ( $this->mTempPath === false ) {
96 return Status::newFatal( 'tmp-create-error' );
97 }
98 if ( file_put_contents( $this->mTempPath, $req->getContent() ) === false ) {
99 return Status::newFatal( 'tmp-write-error' );
100 }
101
102 $this->mFileSize = filesize( $this->mTempPath );
103
104 return Status::newGood();
105 }
106 protected function reallyFetchFile() {
107 $req = HttpRequest::factory( $this->mUrl );
108 $status = $req->execute();
109
110 if ( !$status->isOk() ) {
111 return $status;
112 }
113
114 $status = $this->saveTempFile( $req );
115 if ( !$status->isGood() ) {
116 return $status;
117 }
118 $this->mRemoveTempFile = true;
119
120 return $status;
121 }
122
123 public function performUpload( $comment, $pageText, $watch, $user ) {
124 if ( $this->mAsync ) {
125 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
126
127 $status = new Status;
128 $status->error( 'async', $sessionKey );
129 return $status;
130 }
131
132 return parent::performUpload( $comment, $pageText, $watch, $user );
133 }
134
135
136 protected function insertJob( $comment, $pageText, $watch, $user ) {
137 $sessionKey = $this->getSessionKey();
138 $job = new UploadFromUrlJob( $this->getTitle(), array(
139 'url' => $this->mUrl,
140 'comment' => $comment,
141 'pageText' => $pageText,
142 'watch' => $watch,
143 'userName' => $user->getName(),
144 'leaveMessage' => $this->mAsync == 'async-leavemessage',
145 'ignoreWarnings' => $this->mIgnoreWarnings,
146 'sessionKey' => $sessionKey,
147 ) );
148 $job->insert();
149 return $sessionKey;
150 }
151
152
153 }