summaryrefslogtreecommitdiff
blob: 30a3c58e693bd6151f19f96c1b363527b322da84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Pager
 */

namespace MediaWiki\CheckUser;

use CentralAuthUtils;
use ExtensionRegistry;
use Html;
use IContextSource;
use MediaWiki\Linker\LinkRenderer;
use NamespaceInfo;
use TablePager;
use WikiMap;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IDatabase;

/**
 * @ingroup Pager
 */
class PreliminaryCheckPager extends TablePager {
	/** @var NamespaceInfo */
	private $namespaceInfo;

	/** @var ExtensionRegistry */
	private $extensionRegistry;

	/** @var array */
	protected $tokenData;

	/** @var TokenQueryManager */
	private $tokenQueryManager;

	/** @var PreliminaryCheckService */
	private $preliminaryCheckService;

	/** @var string[] Array of column name to translated table header message */
	private $fieldNames;

	/**
	 * @param IContextSource $context
	 * @param LinkRenderer $linkRenderer
	 * @param NamespaceInfo $namespaceInfo
	 * @param TokenQueryManager $tokenQueryManager
	 * @param ExtensionRegistry $extensionRegistry
	 * @param PreliminaryCheckService $preliminaryCheckService
	 */
	public function __construct(
		IContextSource $context,
		LinkRenderer $linkRenderer,
		NamespaceInfo $namespaceInfo,
		TokenQueryManager $tokenQueryManager,
		ExtensionRegistry $extensionRegistry,
		PreliminaryCheckService $preliminaryCheckService
	) {
		// This must be done before getIndexField is called by the TablePager constructor
		$this->extensionRegistry = $extensionRegistry;
		if ( $this->isGlobalCheck() ) {
			// @phan-suppress-next-line PhanPossiblyNullTypeMismatchProperty
			$this->mDb = $this->getCentralReplicaDB();
		}

		parent::__construct( $context, $linkRenderer );
		$this->namespaceInfo = $namespaceInfo;
		$this->preliminaryCheckService = $preliminaryCheckService;
		$this->tokenQueryManager = $tokenQueryManager;

		$this->tokenData = $tokenQueryManager->getDataFromRequest( $context->getRequest() );
		$this->mOffset = $this->tokenData['offset'] ?? '';
	}

	/**
	 * @inheritDoc
	 */
	protected function getTableClass() {
		return parent::getTableClass() .
			' ext-checkuser-investigate-table' .
			' ext-checkuser-investigate-table-preliminary-check';
	}

	/**
	 * @inheritDoc
	 */
	public function getCellAttrs( $field, $value ) {
		$attributes = parent::getCellAttrs( $field, $value );
		$attributes['class'] = $attributes['class'] ?? '';

		switch ( $field ) {
			case 'wiki':
				$attributes['class'] .= ' ext-checkuser-investigate-table-cell-interactive';
				$attributes['class'] .= ' ext-checkuser-investigate-table-cell-pinnable';
				$attributes['data-field'] = $field;
				$attributes['data-value'] = $value;
				break;
			case 'registration':
				$attributes['class'] .= ' ext-checkuser-investigate-table-cell-interactive';
				$attributes['class'] .= ' ext-checkuser-investigate-table-cell-pinnable';
				$date = $this->getLanguage()->userDate(
					$value,
					$this->getUser(),
					[ 'format' => 'ISO 8601' ]
				);
				$attributes['data-field'] = $field;
				$attributes['data-value'] = $date;
				break;
		}

		// Add each cell to the tab index.
		$attributes['tabindex'] = 0;

		return $attributes;
	}

	/**
	 * @param string $name
	 * @param mixed $value
	 * @return string
	 */
	public function formatValue( $name, $value ) {
		$language = $this->getLanguage();
		$row = $this->mCurrentRow;

		$formatted = '';
		switch ( $name ) {
			case 'name':
				$formatted = htmlspecialchars( $value );
				break;
			case 'registration':
				$formatted = htmlspecialchars(
					$language->userTimeAndDate( $value, $this->getUser() )
				);
				break;
			case 'wiki':
				$wiki = WikiMap::getWiki( $row->wiki );
				if ( $wiki ) {
					$formatted = Html::element(
						'a',
						[
							'href' => $wiki->getFullUrl(
								$this->namespaceInfo->getCanonicalName( NS_USER ) . ':' . $row->name
							),
						],
						$wiki->getDisplayName()
					);
				} else {
					$formatted = $this->msg( 'checkuser-investigate-preliminary-table-cell-wiki-nowiki' )->text();
				}
				break;
			case 'editcount':
				$wiki = WikiMap::getWiki( $row->wiki );
				if ( $wiki ) {
					$formatted = Html::rawElement(
						'a',
						[
							'href' => $wiki->getFullUrl(
								$this->namespaceInfo->getCanonicalName( NS_SPECIAL ) . ':Contributions/' . $row->name
							),
						],
						$this->msg(
							'checkuser-investigate-preliminary-table-cell-edits',
							$value
						)->parse()
					);
				} else {
					$formatted = $this->getLinkRenderer()->makeKnownLink(
						\SpecialPage::getTitleFor( 'Contributions', $row->name ),
						$this->msg(
							'checkuser-investigate-preliminary-table-cell-edits',
							$value
						)->text()
					);
				}
				break;
			case 'blocked':
				if ( $value ) {
					$formatted = $this->msg(
						'checkuser-investigate-preliminary-table-cell-blocked'
					)->parse();
				} else {
					$formatted = $this->msg(
						'checkuser-investigate-preliminary-table-cell-unblocked'
					)->parse();
				}
				break;
			case 'groups':
				$formatted = htmlspecialchars( implode( ', ', $value ) );
				break;
		}

		return $formatted;
	}

	/**
	 * @inheritDoc
	 */
	public function getIndexField() {
		return $this->isGlobalCheck() ? [ [ 'lu_name', 'lu_wiki' ] ] : 'user_name';
	}

	/**
	 * @inheritDoc
	 */
	public function getFieldNames() {
		if ( $this->fieldNames === null ) {
			$fullFieldNames = [
				'name' => 'checkuser-investigate-preliminary-table-header-name',
				'registration' => 'checkuser-investigate-preliminary-table-header-registration',
				'wiki' => 'checkuser-investigate-preliminary-table-header-wiki',
				'editcount' => 'checkuser-investigate-preliminary-table-header-editcount',
				'blocked' => 'checkuser-investigate-preliminary-table-header-blocked',
				'groups' => 'checkuser-investigate-preliminary-table-header-groups',
			];

			if ( $this->isGlobalCheck() ) {
				$this->fieldNames = $fullFieldNames;
			} else {
				unset( $fullFieldNames['wiki'] );
				$this->fieldNames = $fullFieldNames;
			}

			foreach ( $this->fieldNames as $key => $val ) {
				$this->fieldNames[$key] = $this->msg( $val )->text();
			}
		}
		return $this->fieldNames;
	}

	/**
	 * @inheritDoc
	 */
	public function getQueryInfo() {
		$targets = $this->tokenData['targets'] ?? [];
		$users = array_filter( array_map( 'User::newFromName', $targets ), function ( $user ) {
			return (bool)$user;
		} );

		return $this->preliminaryCheckService->getQueryInfo( $users );
	}

	/**
	 * @inheritDoc
	 */
	public function preprocessResults( $result ) {
		$this->mResult = new FakeResultWrapper(
			$this->preliminaryCheckService->preprocessResults( $result )
		);
	}

	/**
	 * @return bool
	 */
	public function isGlobalCheck(): bool {
		return $this->extensionRegistry->isLoaded( 'CentralAuth' )
			&& is_callable( [ CentralAuthUtils::class, 'getCentralReplicaDB' ] );
	}

	/**
	 * @return IDatabase|null
	 */
	protected function getCentralReplicaDB(): ?IDatabase {
		if ( is_callable( [ CentralAuthUtils::class, 'getCentralReplicaDB' ] ) ) {
			return CentralAuthUtils::getCentralReplicaDB();
		}
		return null;
	}

	/**
	 * @inheritDoc
	 */
	public function isFieldSortable( $field ) {
		return false;
	}

	/**
	 * @inheritDoc
	 */
	public function getDefaultSort() {
		return '';
	}

	/**
	 * @inheritDoc
	 *
	 * Conceal the offset which may reveal private data.
	 */
	public function getPagingQueries() {
		return $this->tokenQueryManager->getPagingQueries(
			$this->getRequest(), parent::getPagingQueries()
		);
	}
}