Completed
Branch master (227f0c)
by
unknown
30:54
created

NewFilesPager::getForm()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
rs 9.2258
cc 3
eloc 34
nc 4
nop 0
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
 * @ingroup Pager
20
 */
21
22
/**
23
 * @ingroup Pager
24
 */
25
class NewFilesPager extends ReverseChronologicalPager {
26
27
	/**
28
	 * @var ImageGallery
29
	 */
30
	protected $gallery;
31
32
	/**
33
	 * @var bool
34
	 */
35
	protected $showBots;
36
37
	/**
38
	 * @var bool
39
	 */
40
	protected $hidePatrolled;
41
42
	function __construct( IContextSource $context, $par = null ) {
43
		$this->like = $context->getRequest()->getText( 'like' );
0 ignored issues
show
Bug introduced by
The property like does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
		$this->showBots = $context->getRequest()->getBool( 'showbots', 0 );
45
		$this->hidePatrolled = $context->getRequest()->getBool( 'hidepatrolled', 0 );
46
		if ( is_numeric( $par ) ) {
47
			$this->setLimit( $par );
48
		}
49
50
		parent::__construct( $context );
51
	}
52
53
	function getQueryInfo() {
54
		$conds = $jconds = [];
55
		$tables = [ 'image' ];
56
		$fields = [ 'img_name', 'img_user', 'img_timestamp' ];
57
		$options = [];
58
59 View Code Duplication
		if ( !$this->showBots ) {
60
			$groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
61
62
			if ( count( $groupsWithBotPermission ) ) {
63
				$tables[] = 'user_groups';
64
				$conds[] = 'ug_group IS NULL';
65
				$jconds['user_groups'] = [
66
					'LEFT JOIN',
67
					[
68
						'ug_group' => $groupsWithBotPermission,
69
						'ug_user = img_user'
70
					]
71
				];
72
			}
73
		}
74
75
		if ( $this->hidePatrolled ) {
76
			$tables[] = 'recentchanges';
77
			$conds['rc_type'] = RC_LOG;
78
			$conds['rc_log_type'] = 'upload';
79
			$conds['rc_patrolled'] = 0;
80
			$conds['rc_namespace'] = NS_FILE;
81
			$jconds['recentchanges'] = [
82
				'INNER JOIN',
83
				[
84
					'rc_title = img_name',
85
					'rc_user = img_user',
86
					'rc_timestamp = img_timestamp'
87
				]
88
			];
89
			// We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
90
			// It sometimes decides to query `recentchanges` first and filesort the result set later
91
			// to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
92
			$options[] = 'STRAIGHT_JOIN';
93
		}
94
95
		if ( !$this->getConfig()->get( 'MiserMode' ) && $this->like !== null ) {
96
			$dbr = wfGetDB( DB_SLAVE );
97
			$likeObj = Title::newFromText( $this->like );
98
			if ( $likeObj instanceof Title ) {
99
				$like = $dbr->buildLike(
100
					$dbr->anyString(),
101
					strtolower( $likeObj->getDBkey() ),
102
					$dbr->anyString()
103
				);
104
				$conds[] = "LOWER(img_name) $like";
105
			}
106
		}
107
108
		$query = [
109
			'tables' => $tables,
110
			'fields' => $fields,
111
			'join_conds' => $jconds,
112
			'conds' => $conds,
113
			'options' => $options,
114
		];
115
116
		return $query;
117
	}
118
119
	function getIndexField() {
120
		return 'img_timestamp';
121
	}
122
123
	function getStartBody() {
124
		if ( !$this->gallery ) {
125
			// Note that null for mode is taken to mean use default.
126
			$mode = $this->getRequest()->getVal( 'gallerymode', null );
127
			try {
128
				$this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
129
			} catch ( Exception $e ) {
130
				// User specified something invalid, fallback to default.
131
				$this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
132
			}
133
		}
134
135
		return '';
136
	}
137
138
	function getEndBody() {
139
		return $this->gallery->toHTML();
140
	}
141
142
	function formatRow( $row ) {
143
		$name = $row->img_name;
144
		$user = User::newFromId( $row->img_user );
145
146
		$title = Title::makeTitle( NS_FILE, $name );
147
		$ul = Linker::link( $user->getUserPage(), $user->getName() );
148
		$time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
149
150
		$this->gallery->add(
151
			$title,
152
			"$ul<br />\n<i>"
153
			. htmlspecialchars( $time )
154
			. "</i><br />\n"
155
		);
156
	}
157
158
	function getForm() {
159
		$fields = [
160
			'like' => [
161
				'type' => 'text',
162
				'label-message' => 'newimages-label',
163
				'name' => 'like',
164
			],
165
			'showbots' => [
166
				'type' => 'check',
167
				'label-message' => 'newimages-showbots',
168
				'name' => 'showbots',
169
			],
170
			'hidepatrolled' => [
171
				'type' => 'check',
172
				'label-message' => 'newimages-hidepatrolled',
173
				'name' => 'hidepatrolled',
174
			],
175
			'limit' => [
176
				'type' => 'hidden',
177
				'default' => $this->mLimit,
178
				'name' => 'limit',
179
			],
180
			'offset' => [
181
				'type' => 'hidden',
182
				'default' => $this->getRequest()->getText( 'offset' ),
183
				'name' => 'offset',
184
			],
185
		];
186
187
		if ( $this->getConfig()->get( 'MiserMode' ) ) {
188
			unset( $fields['like'] );
189
		}
190
191
		if ( !$this->getUser()->useFilePatrol() ) {
192
			unset( $fields['hidepatrolled'] );
193
		}
194
195
		$context = new DerivativeContext( $this->getContext() );
196
		$context->setTitle( $this->getTitle() ); // Remove subpage
0 ignored issues
show
Bug introduced by
It seems like $this->getTitle() can be null; however, setTitle() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
197
		$form = new HTMLForm( $fields, $context );
198
199
		$form->setSubmitTextMsg( 'ilsubmit' );
200
		$form->setSubmitProgressive();
201
202
		$form->setMethod( 'get' );
203
		$form->setWrapperLegendMsg( 'newimages-legend' );
204
205
		return $form;
206
	}
207
}
208