Declare dynamic properties
[lhc/web/wiklou.git] / includes / specials / pagers / BlockListPager.php
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 use MediaWiki\Block\DatabaseBlock;
26 use MediaWiki\Block\Restriction\Restriction;
27 use MediaWiki\Block\Restriction\PageRestriction;
28 use MediaWiki\Block\Restriction\NamespaceRestriction;
29 use MediaWiki\MediaWikiServices;
30 use Wikimedia\Rdbms\IResultWrapper;
31
32 class BlockListPager extends TablePager {
33
34 protected $conds;
35
36 /**
37 * Array of restrictions.
38 *
39 * @var Restriction[]
40 */
41 protected $restrictions = [];
42
43 /**
44 * @param SpecialPage $page
45 * @param array $conds
46 */
47 public function __construct( $page, $conds ) {
48 parent::__construct( $page->getContext(), $page->getLinkRenderer() );
49 $this->conds = $conds;
50 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
51 }
52
53 function getFieldNames() {
54 static $headers = null;
55
56 if ( $headers === null ) {
57 $headers = [
58 'ipb_timestamp' => 'blocklist-timestamp',
59 'ipb_target' => 'blocklist-target',
60 'ipb_expiry' => 'blocklist-expiry',
61 'ipb_by' => 'blocklist-by',
62 'ipb_params' => 'blocklist-params',
63 'ipb_reason' => 'blocklist-reason',
64 ];
65 foreach ( $headers as $key => $val ) {
66 $headers[$key] = $this->msg( $val )->text();
67 }
68 }
69
70 return $headers;
71 }
72
73 /**
74 * @param string $name
75 * @param string $value
76 * @return string
77 * @suppress PhanTypeArraySuspicious
78 */
79 function formatValue( $name, $value ) {
80 static $msg = null;
81 if ( $msg === null ) {
82 $keys = [
83 'anononlyblock',
84 'createaccountblock',
85 'noautoblockblock',
86 'emailblock',
87 'blocklist-nousertalk',
88 'unblocklink',
89 'change-blocklink',
90 'blocklist-editing',
91 'blocklist-editing-sitewide',
92 ];
93
94 foreach ( $keys as $key ) {
95 $msg[$key] = $this->msg( $key )->text();
96 }
97 }
98
99 /** @var object $row */
100 $row = $this->mCurrentRow;
101
102 $language = $this->getLanguage();
103
104 $formatted = '';
105
106 $linkRenderer = $this->getLinkRenderer();
107
108 switch ( $name ) {
109 case 'ipb_timestamp':
110 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
111 break;
112
113 case 'ipb_target':
114 if ( $row->ipb_auto ) {
115 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
116 } else {
117 list( $target, $type ) = DatabaseBlock::parseTarget( $row->ipb_address );
118 switch ( $type ) {
119 case DatabaseBlock::TYPE_USER:
120 case DatabaseBlock::TYPE_IP:
121 $formatted = Linker::userLink( $target->getId(), $target );
122 $formatted .= Linker::userToolLinks(
123 $target->getId(),
124 $target,
125 false,
126 Linker::TOOL_LINKS_NOBLOCK
127 );
128 break;
129 case DatabaseBlock::TYPE_RANGE:
130 $formatted = htmlspecialchars( $target );
131 }
132 }
133 break;
134
135 case 'ipb_expiry':
136 $formatted = htmlspecialchars( $language->formatExpiry(
137 $value,
138 /* User preference timezone */true
139 ) );
140 if ( $this->getUser()->isAllowed( 'block' ) ) {
141 $links = [];
142 if ( $row->ipb_auto ) {
143 $links[] = $linkRenderer->makeKnownLink(
144 SpecialPage::getTitleFor( 'Unblock' ),
145 $msg['unblocklink'],
146 [],
147 [ 'wpTarget' => "#{$row->ipb_id}" ]
148 );
149 } else {
150 $links[] = $linkRenderer->makeKnownLink(
151 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
152 $msg['unblocklink']
153 );
154 $links[] = $linkRenderer->makeKnownLink(
155 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
156 $msg['change-blocklink']
157 );
158 }
159 $formatted .= ' ' . Html::rawElement(
160 'span',
161 [ 'class' => 'mw-blocklist-actions' ],
162 $this->msg( 'parentheses' )->rawParams(
163 $language->pipeList( $links ) )->escaped()
164 );
165 }
166 if ( $value !== 'infinity' ) {
167 $timestamp = new MWTimestamp( $value );
168 $formatted .= '<br />' . $this->msg(
169 'ipb-blocklist-duration-left',
170 $language->formatDuration(
171 $timestamp->getTimestamp() - MWTimestamp::time(),
172 // reasonable output
173 [
174 'minutes',
175 'hours',
176 'days',
177 'years',
178 ]
179 )
180 )->escaped();
181 }
182 break;
183
184 case 'ipb_by':
185 if ( isset( $row->by_user_name ) ) {
186 $formatted = Linker::userLink( $value, $row->by_user_name );
187 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
188 } else {
189 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
190 }
191 break;
192
193 case 'ipb_reason':
194 $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text;
195 $formatted = Linker::formatComment( $value );
196 break;
197
198 case 'ipb_params':
199 $properties = [];
200
201 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) && $row->ipb_sitewide ) {
202 $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
203 }
204
205 if ( !$row->ipb_sitewide && $this->restrictions ) {
206 $list = $this->getRestrictionListHTML( $row );
207 if ( $list ) {
208 $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
209 }
210 }
211
212 if ( $row->ipb_anon_only ) {
213 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
214 }
215 if ( $row->ipb_create_account ) {
216 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
217 }
218 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
219 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
220 }
221
222 if ( $row->ipb_block_email ) {
223 $properties[] = htmlspecialchars( $msg['emailblock'] );
224 }
225
226 if ( !$row->ipb_allow_usertalk ) {
227 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
228 }
229
230 $formatted = Html::rawElement(
231 'ul',
232 [],
233 implode( '', array_map( function ( $prop ) {
234 return Html::rawElement(
235 'li',
236 [],
237 $prop
238 );
239 }, $properties ) )
240 );
241 break;
242
243 default:
244 $formatted = "Unable to format $name";
245 break;
246 }
247
248 return $formatted;
249 }
250
251 /**
252 * Get Restriction List HTML
253 *
254 * @param stdClass $row
255 *
256 * @return string
257 */
258 private function getRestrictionListHTML( stdClass $row ) {
259 $items = [];
260 $linkRenderer = $this->getLinkRenderer();
261
262 foreach ( $this->restrictions as $restriction ) {
263 if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
264 continue;
265 }
266
267 switch ( $restriction->getType() ) {
268 case PageRestriction::TYPE:
269 '@phan-var PageRestriction $restriction';
270 if ( $restriction->getTitle() ) {
271 $items[$restriction->getType()][] = Html::rawElement(
272 'li',
273 [],
274 $linkRenderer->makeLink( $restriction->getTitle() )
275 );
276 }
277 break;
278 case NamespaceRestriction::TYPE:
279 $text = $restriction->getValue() === NS_MAIN
280 ? $this->msg( 'blanknamespace' )->text()
281 : $this->getLanguage()->getFormattedNsText(
282 $restriction->getValue()
283 );
284 $items[$restriction->getType()][] = Html::rawElement(
285 'li',
286 [],
287 $linkRenderer->makeLink(
288 SpecialPage::getTitleValueFor( 'Allpages' ),
289 $text,
290 [],
291 [
292 'namespace' => $restriction->getValue()
293 ]
294 )
295 );
296 break;
297 }
298 }
299
300 if ( empty( $items ) ) {
301 return '';
302 }
303
304 $sets = [];
305 foreach ( $items as $key => $value ) {
306 $sets[] = Html::rawElement(
307 'li',
308 [],
309 $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
310 'ul',
311 [],
312 implode( '', $value )
313 )
314 );
315 }
316
317 return Html::rawElement(
318 'ul',
319 [],
320 implode( '', $sets )
321 );
322 }
323
324 function getQueryInfo() {
325 $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' );
326 $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' );
327
328 $info = [
329 'tables' => array_merge(
330 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
331 ),
332 'fields' => [
333 'ipb_id',
334 'ipb_address',
335 'ipb_user',
336 'by_user_name' => 'user_name',
337 'ipb_timestamp',
338 'ipb_auto',
339 'ipb_anon_only',
340 'ipb_create_account',
341 'ipb_enable_autoblock',
342 'ipb_expiry',
343 'ipb_range_start',
344 'ipb_range_end',
345 'ipb_deleted',
346 'ipb_block_email',
347 'ipb_allow_usertalk',
348 'ipb_sitewide',
349 ] + $commentQuery['fields'] + $actorQuery['fields'],
350 'conds' => $this->conds,
351 'join_conds' => [
352 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
353 ] + $commentQuery['joins'] + $actorQuery['joins']
354 ];
355
356 # Filter out any expired blocks
357 $db = $this->getDatabase();
358 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
359
360 # Is the user allowed to see hidden blocks?
361 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
362 $info['conds']['ipb_deleted'] = 0;
363 }
364
365 return $info;
366 }
367
368 /**
369 * Get total number of autoblocks at any given time
370 *
371 * @return int Total number of unexpired active autoblocks
372 */
373 function getTotalAutoblocks() {
374 $dbr = $this->getDatabase();
375 $res = $dbr->selectField( 'ipblocks',
376 'COUNT(*)',
377 [
378 'ipb_auto' => '1',
379 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
380 ]
381 );
382 if ( $res ) {
383 return $res;
384 }
385 return 0; // We found nothing
386 }
387
388 protected function getTableClass() {
389 return parent::getTableClass() . ' mw-blocklist';
390 }
391
392 function getIndexField() {
393 return 'ipb_timestamp';
394 }
395
396 function getDefaultSort() {
397 return 'ipb_timestamp';
398 }
399
400 function isFieldSortable( $name ) {
401 return false;
402 }
403
404 /**
405 * Do a LinkBatch query to minimise database load when generating all these links
406 * @param IResultWrapper $result
407 */
408 function preprocessResults( $result ) {
409 # Do a link batch query
410 $lb = new LinkBatch;
411 $lb->setCaller( __METHOD__ );
412
413 $partialBlocks = [];
414 foreach ( $result as $row ) {
415 $lb->add( NS_USER, $row->ipb_address );
416 $lb->add( NS_USER_TALK, $row->ipb_address );
417
418 if ( isset( $row->by_user_name ) ) {
419 $lb->add( NS_USER, $row->by_user_name );
420 $lb->add( NS_USER_TALK, $row->by_user_name );
421 }
422
423 if ( !$row->ipb_sitewide ) {
424 $partialBlocks[] = $row->ipb_id;
425 }
426 }
427
428 if ( $partialBlocks ) {
429 // Mutations to the $row object are not persisted. The restrictions will
430 // need be stored in a separate store.
431 $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
432 $this->restrictions = $blockRestrictionStore->loadByBlockId( $partialBlocks );
433 }
434
435 $lb->execute();
436 }
437
438 }