Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UploadFromUrlJob
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getDeduplicationInfo
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getUpload
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 logJobParams
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\JobQueue\Jobs;
22
23use MediaWiki\JobQueue\GenericParameterJob;
24use MediaWiki\JobQueue\Job;
25use MediaWiki\Status\Status;
26use UploadBase;
27use UploadFromUrl;
28
29/**
30 * Upload a file by URL, via the jobqueue.
31 *
32 */
33class UploadFromUrlJob extends Job implements GenericParameterJob {
34    use UploadJobTrait;
35
36    public function __construct( array $params ) {
37        // @TODO: fix the invokation of Job::__construct in the parent class
38        parent::__construct( 'UploadFromUrl', $params );
39        $this->removeDuplicates = true;
40        $this->user = null;
41        $this->cacheKey = UploadFromUrl::getCacheKey( $this->params );
42    }
43
44    /**
45     * Deduplicate on title, url alone.
46     *
47     * Please note that this could cause some
48     * edge case failure, when the image at the
49     * same remote url is changed before the first upload
50     * is ran.
51     *
52     * @return array
53     */
54    public function getDeduplicationInfo() {
55        $info = parent::getDeduplicationInfo();
56        if ( is_array( $info['params'] ) ) {
57            $info['params'] = [ 'url' => $info['params']['url'], 'title' => $info['params']['filename'] ];
58        }
59
60        return $info;
61    }
62
63    protected function getUpload(): UploadBase {
64        if ( $this->upload === null ) {
65            $this->upload = new UploadFromUrl;
66            $this->upload->initialize( $this->params['filename'], $this->params['url'] );
67        }
68        return $this->upload;
69    }
70
71    /**
72     * Get the parameters for job logging
73     *
74     * @param Status[] $status
75     * @return array
76     */
77    public function logJobParams( $status ): array {
78        return [
79            'stage' => $status['stage'] ?? '-',
80            'result' => $status['result'] ?? '-',
81            'status' => (string)( $status['status'] ?? '-' ),
82            'url' => $this->params['url'],
83            'filename' => $this->params['filename'],
84            'user' => $this->user->getName(),
85        ];
86    }
87
88}
89
90/** @deprecated class alias since 1.44 */
91class_alias( UploadFromUrlJob::class, 'UploadFromUrlJob' );