Completed
Branch master (54277f)
by
unknown
24:54
created

BlockListPager::getFieldNames()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 13
nc 2
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 BlockListPager extends TablePager {
26
27
	protected $conds;
28
	protected $page;
29
30
	/**
31
	 * @param SpecialPage $page
32
	 * @param array $conds
33
	 */
34
	function __construct( $page, $conds ) {
35
		$this->page = $page;
36
		$this->conds = $conds;
37
		$this->mDefaultDirection = IndexPager::DIR_DESCENDING;
38
		parent::__construct( $page->getContext() );
39
	}
40
41
	function getFieldNames() {
42
		static $headers = null;
43
44
		if ( $headers === null ) {
45
			$headers = [
46
				'ipb_timestamp' => 'blocklist-timestamp',
47
				'ipb_target' => 'blocklist-target',
48
				'ipb_expiry' => 'blocklist-expiry',
49
				'ipb_by' => 'blocklist-by',
50
				'ipb_params' => 'blocklist-params',
51
				'ipb_reason' => 'blocklist-reason',
52
			];
53
			foreach ( $headers as $key => $val ) {
54
				$headers[$key] = $this->msg( $val )->text();
55
			}
56
		}
57
58
		return $headers;
59
	}
60
61
	function formatValue( $name, $value ) {
62
		static $msg = null;
63
		if ( $msg === null ) {
64
			$keys = [
65
				'anononlyblock',
66
				'createaccountblock',
67
				'noautoblockblock',
68
				'emailblock',
69
				'blocklist-nousertalk',
70
				'unblocklink',
71
				'change-blocklink',
72
			];
73
74
			foreach ( $keys as $key ) {
75
				$msg[$key] = $this->msg( $key )->escaped();
76
			}
77
		}
78
79
		/** @var $row object */
80
		$row = $this->mCurrentRow;
81
82
		$language = $this->getLanguage();
83
84
		$formatted = '';
85
86
		switch ( $name ) {
87
			case 'ipb_timestamp':
88
				$formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
89
				break;
90
91
			case 'ipb_target':
92
				if ( $row->ipb_auto ) {
93
					$formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
94
				} else {
95
					list( $target, $type ) = Block::parseTarget( $row->ipb_address );
96
					switch ( $type ) {
97
						case Block::TYPE_USER:
98
						case Block::TYPE_IP:
99
							$formatted = Linker::userLink( $target->getId(), $target );
100
							$formatted .= Linker::userToolLinks(
101
								$target->getId(),
102
								$target,
103
								false,
104
								Linker::TOOL_LINKS_NOBLOCK
105
							);
106
							break;
107
						case Block::TYPE_RANGE:
108
							$formatted = htmlspecialchars( $target );
109
					}
110
				}
111
				break;
112
113
			case 'ipb_expiry':
114
				$formatted = htmlspecialchars( $language->formatExpiry(
115
					$value,
116
					/* User preference timezone */true
117
				) );
118
				if ( $this->getUser()->isAllowed( 'block' ) ) {
119
					if ( $row->ipb_auto ) {
120
						$links[] = Linker::linkKnown(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$links was never initialized. Although not strictly required by PHP, it is generally a good practice to add $links = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
121
							SpecialPage::getTitleFor( 'Unblock' ),
122
							$msg['unblocklink'],
123
							[],
124
							[ 'wpTarget' => "#{$row->ipb_id}" ]
125
						);
126
					} else {
127
						$links[] = Linker::linkKnown(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$links was never initialized. Although not strictly required by PHP, it is generally a good practice to add $links = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
128
							SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
129
							$msg['unblocklink']
130
						);
131
						$links[] = Linker::linkKnown(
132
							SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
133
							$msg['change-blocklink']
134
						);
135
					}
136
					$formatted .= ' ' . Html::rawElement(
137
							'span',
138
							[ 'class' => 'mw-blocklist-actions' ],
139
							$this->msg( 'parentheses' )->rawParams(
140
								$language->pipeList( $links ) )->escaped()
141
						);
142
				}
143
				if ( $value !== 'infinity' ) {
144
					$timestamp = new MWTimestamp( $value );
145
					$formatted .= '<br />' . $this->msg(
146
						'ipb-blocklist-duration-left',
147
						$language->formatDuration(
148
							$timestamp->getTimestamp() - time(),
149
							// reasonable output
150
							[
151
								'minutes',
152
								'hours',
153
								'days',
154
								'years',
155
							]
156
						)
157
					)->escaped();
158
				}
159
				break;
160
161
			case 'ipb_by':
162
				if ( isset( $row->by_user_name ) ) {
163
					$formatted = Linker::userLink( $value, $row->by_user_name );
164
					$formatted .= Linker::userToolLinks( $value, $row->by_user_name );
165
				} else {
166
					$formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
167
				}
168
				break;
169
170
			case 'ipb_reason':
171
				$formatted = Linker::formatComment( $value );
172
				break;
173
174
			case 'ipb_params':
175
				$properties = [];
176
				if ( $row->ipb_anon_only ) {
177
					$properties[] = $msg['anononlyblock'];
178
				}
179
				if ( $row->ipb_create_account ) {
180
					$properties[] = $msg['createaccountblock'];
181
				}
182
				if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
183
					$properties[] = $msg['noautoblockblock'];
184
				}
185
186
				if ( $row->ipb_block_email ) {
187
					$properties[] = $msg['emailblock'];
188
				}
189
190
				if ( !$row->ipb_allow_usertalk ) {
191
					$properties[] = $msg['blocklist-nousertalk'];
192
				}
193
194
				$formatted = $language->commaList( $properties );
195
				break;
196
197
			default:
198
				$formatted = "Unable to format $name";
199
				break;
200
		}
201
202
		return $formatted;
203
	}
204
205
	function getQueryInfo() {
206
		$info = [
207
			'tables' => [ 'ipblocks', 'user' ],
208
			'fields' => [
209
				'ipb_id',
210
				'ipb_address',
211
				'ipb_user',
212
				'ipb_by',
213
				'ipb_by_text',
214
				'by_user_name' => 'user_name',
215
				'ipb_reason',
216
				'ipb_timestamp',
217
				'ipb_auto',
218
				'ipb_anon_only',
219
				'ipb_create_account',
220
				'ipb_enable_autoblock',
221
				'ipb_expiry',
222
				'ipb_range_start',
223
				'ipb_range_end',
224
				'ipb_deleted',
225
				'ipb_block_email',
226
				'ipb_allow_usertalk',
227
			],
228
			'conds' => $this->conds,
229
			'join_conds' => [ 'user' => [ 'LEFT JOIN', 'user_id = ipb_by' ] ]
230
		];
231
232
		# Filter out any expired blocks
233
		$db = $this->getDatabase();
234
		$info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
0 ignored issues
show
Security Bug introduced by
It seems like $db->timestamp() targeting DatabaseBase::timestamp() can also be of type false; however, DatabaseBase::addQuotes() does only seem to accept string|object<Blob>, did you maybe forget to handle an error condition?
Loading history...
235
236
		# Is the user allowed to see hidden blocks?
237
		if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
238
			$info['conds']['ipb_deleted'] = 0;
239
		}
240
241
		return $info;
242
	}
243
244
	public function getTableClass() {
245
		return parent::getTableClass() . ' mw-blocklist';
246
	}
247
248
	function getIndexField() {
249
		return 'ipb_timestamp';
250
	}
251
252
	function getDefaultSort() {
253
		return 'ipb_timestamp';
254
	}
255
256
	function isFieldSortable( $name ) {
257
		return false;
258
	}
259
260
	/**
261
	 * Do a LinkBatch query to minimise database load when generating all these links
262
	 * @param ResultWrapper $result
263
	 */
264
	function preprocessResults( $result ) {
265
		# Do a link batch query
266
		$lb = new LinkBatch;
267
		$lb->setCaller( __METHOD__ );
268
269
		foreach ( $result as $row ) {
270
			$lb->add( NS_USER, $row->ipb_address );
271
			$lb->add( NS_USER_TALK, $row->ipb_address );
272
273
			if ( isset( $row->by_user_name ) ) {
274
				$lb->add( NS_USER, $row->by_user_name );
275
				$lb->add( NS_USER_TALK, $row->by_user_name );
276
			}
277
		}
278
279
		$lb->execute();
280
	}
281
282
}
283