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