ThumbnailRenderJob::hitThumbUrl()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 5
nop 3
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Job for asynchronous rendering of thumbnails.
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 JobQueue
22
 */
23
24
/**
25
 * Job for asynchronous rendering of thumbnails.
26
 *
27
 * @ingroup JobQueue
28
 */
29
class ThumbnailRenderJob extends Job {
30
	public function __construct( Title $title, array $params ) {
31
		parent::__construct( 'ThumbnailRender', $title, $params );
32
	}
33
34
	public function run() {
35
		global $wgUploadThumbnailRenderMethod;
36
37
		$transformParams = $this->params['transformParams'];
38
39
		$file = wfLocalFile( $this->title );
40
		$file->load( File::READ_LATEST );
41
42
		if ( $file && $file->exists() ) {
43
			if ( $wgUploadThumbnailRenderMethod === 'jobqueue' ) {
44
				$thumb = $file->transform( $transformParams, File::RENDER_NOW );
45
46
				if ( $thumb && !$thumb->isError() ) {
47
					return true;
48
				} else {
49
					$this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated' );
50
					return false;
51
				}
52
			} elseif ( $wgUploadThumbnailRenderMethod === 'http' ) {
53
				$thumbUrl = '';
54
				$status = $this->hitThumbUrl( $file, $transformParams, $thumbUrl );
55
56
				wfDebug( __METHOD__ . ": received status {$status}\n" );
57
58
				// 400 happens when requesting a size greater or equal than the original
59
				if ( $status === 200 || $status === 301 || $status === 302 || $status === 400 ) {
60
					return true;
61
				} elseif ( $status ) {
62
					$this->setLastError( __METHOD__ . ': incorrect HTTP status ' .
63
						$status . ' when hitting ' . $thumbUrl );
64
					return false;
65
				} else {
66
					$this->setLastError( __METHOD__ . ': HTTP request failure' );
67
					return false;
68
				}
69
			} else {
70
				$this->setLastError( __METHOD__ . ': unknown thumbnail render method ' .
71
					$wgUploadThumbnailRenderMethod );
72
				return false;
73
			}
74
		} else {
75
			$this->setLastError( __METHOD__ . ': file doesn\'t exist' );
76
			return false;
77
		}
78
	}
79
80
	protected function hitThumbUrl( $file, $transformParams, &$thumbUrl ) {
81
		global $wgUploadThumbnailRenderHttpCustomHost, $wgUploadThumbnailRenderHttpCustomDomain;
82
83
		$thumbName = $file->thumbName( $transformParams );
84
		$thumbUrl = $file->getThumbUrl( $thumbName );
85
86
		if ( $wgUploadThumbnailRenderHttpCustomDomain ) {
87
			$parsedUrl = wfParseUrl( $thumbUrl );
88
89
			if ( !$parsedUrl || !isset( $parsedUrl['path'] ) || !strlen( $parsedUrl['path'] ) ) {
90
				return false;
91
			}
92
93
			$thumbUrl = '//' . $wgUploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
94
		}
95
96
		wfDebug( __METHOD__ . ": hitting url {$thumbUrl}\n" );
97
98
		$request = MWHttpRequest::factory( $thumbUrl,
99
			[ 'method' => 'HEAD', 'followRedirects' => true ],
100
			__METHOD__
101
		);
102
103
		if ( $wgUploadThumbnailRenderHttpCustomHost ) {
104
			$request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
105
		}
106
107
		$status = $request->execute();
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
109
		return $request->getStatus();
110
	}
111
}
112