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.
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.
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
25 use MediaWiki\Block\BlockRestriction
;
26 use MediaWiki\Block\Restriction\Restriction
;
27 use MediaWiki\MediaWikiServices
;
28 use Wikimedia\Rdbms\IResultWrapper
;
30 class BlockListPager
extends TablePager
{
36 * Array of restrictions.
40 protected $restrictions = [];
43 * @param SpecialPage $page
46 function __construct( $page, $conds ) {
48 $this->conds
= $conds;
49 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
50 parent
::__construct( $page->getContext() );
53 function getFieldNames() {
54 static $headers = null;
56 if ( $headers === null ) {
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',
65 foreach ( $headers as $key => $val ) {
66 $headers[$key] = $this->msg( $val )->text();
73 function formatValue( $name, $value ) {
75 if ( $msg === null ) {
81 'blocklist-nousertalk',
85 'blocklist-editing-sitewide',
88 foreach ( $keys as $key ) {
89 $msg[$key] = $this->msg( $key )->text();
93 /** @var object $row */
94 $row = $this->mCurrentRow
;
96 $language = $this->getLanguage();
100 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
103 case 'ipb_timestamp':
104 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
108 if ( $row->ipb_auto
) {
109 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
111 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
113 case Block
::TYPE_USER
:
115 $formatted = Linker
::userLink( $target->getId(), $target );
116 $formatted .= Linker
::userToolLinks(
120 Linker
::TOOL_LINKS_NOBLOCK
123 case Block
::TYPE_RANGE
:
124 $formatted = htmlspecialchars( $target );
130 $formatted = htmlspecialchars( $language->formatExpiry(
132 /* User preference timezone */true
134 if ( $this->getUser()->isAllowed( 'block' ) ) {
135 if ( $row->ipb_auto
) {
136 $links[] = $linkRenderer->makeKnownLink(
137 SpecialPage
::getTitleFor( 'Unblock' ),
140 [ 'wpTarget' => "#{$row->ipb_id}" ]
143 $links[] = $linkRenderer->makeKnownLink(
144 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
147 $links[] = $linkRenderer->makeKnownLink(
148 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
149 $msg['change-blocklink']
152 $formatted .= ' ' . Html
::rawElement(
154 [ 'class' => 'mw-blocklist-actions' ],
155 $this->msg( 'parentheses' )->rawParams(
156 $language->pipeList( $links ) )->escaped()
159 if ( $value !== 'infinity' ) {
160 $timestamp = new MWTimestamp( $value );
161 $formatted .= '<br />' . $this->msg(
162 'ipb-blocklist-duration-left',
163 $language->formatDuration(
164 $timestamp->getTimestamp() - time(),
178 if ( isset( $row->by_user_name
) ) {
179 $formatted = Linker
::userLink( $value, $row->by_user_name
);
180 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
182 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
187 $value = CommentStore
::getStore()->getComment( 'ipb_reason', $row )->text
;
188 $formatted = Linker
::formatComment( $value );
194 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
195 if ( $row->ipb_sitewide
) {
196 $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
200 if ( !$row->ipb_sitewide
&& $this->restrictions
) {
201 $list = $this->getRestrictionListHTML( $this->restrictions
, $row );
202 $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
205 if ( $row->ipb_anon_only
) {
206 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
208 if ( $row->ipb_create_account
) {
209 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
211 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
212 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
215 if ( $row->ipb_block_email
) {
216 $properties[] = htmlspecialchars( $msg['emailblock'] );
219 if ( !$row->ipb_allow_usertalk
) {
220 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
223 $formatted = Html
::rawElement(
226 implode( '', array_map( function ( $prop ) {
227 return Html
::rawElement(
237 $formatted = "Unable to format $name";
245 * Get Restriction List HTML
247 * @param Restriction[] $restrictions
248 * @param stdClass $row
252 private static function getRestrictionListHTML(
258 foreach ( $restrictions as $restriction ) {
259 if ( $restriction->getBlockId() !== (int)$row->ipb_id
) {
263 if ( $restriction->getType() !== 'page' ) {
267 $items[] = Html
::rawElement(
270 Linker
::link( $restriction->getTitle() )
274 if ( empty( $items ) ) {
278 return Html
::rawElement(
281 implode( '', $items )
285 function getQueryInfo() {
286 $commentQuery = CommentStore
::getStore()->getJoin( 'ipb_reason' );
287 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ipb_by' );
290 'tables' => array_merge(
291 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
297 'by_user_name' => 'user_name',
301 'ipb_create_account',
302 'ipb_enable_autoblock',
308 'ipb_allow_usertalk',
310 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
311 'conds' => $this->conds
,
313 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
314 ] +
$commentQuery['joins'] +
$actorQuery['joins']
317 # Filter out any expired blocks
318 $db = $this->getDatabase();
319 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
321 # Is the user allowed to see hidden blocks?
322 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
323 $info['conds']['ipb_deleted'] = 0;
330 * Get total number of autoblocks at any given time
332 * @return int Total number of unexpired active autoblocks
334 function getTotalAutoblocks() {
335 $dbr = $this->getDatabase();
336 $res = $dbr->selectField( 'ipblocks',
337 [ 'COUNT(*) AS totalautoblocks' ],
340 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
346 return 0; // We found nothing
349 protected function getTableClass() {
350 return parent
::getTableClass() . ' mw-blocklist';
353 function getIndexField() {
354 return 'ipb_timestamp';
357 function getDefaultSort() {
358 return 'ipb_timestamp';
361 function isFieldSortable( $name ) {
366 * Do a LinkBatch query to minimise database load when generating all these links
367 * @param IResultWrapper $result
369 function preprocessResults( $result ) {
370 # Do a link batch query
372 $lb->setCaller( __METHOD__
);
375 foreach ( $result as $row ) {
376 $lb->add( NS_USER
, $row->ipb_address
);
377 $lb->add( NS_USER_TALK
, $row->ipb_address
);
379 if ( isset( $row->by_user_name
) ) {
380 $lb->add( NS_USER
, $row->by_user_name
);
381 $lb->add( NS_USER_TALK
, $row->by_user_name
);
384 if ( !$row->ipb_sitewide
) {
385 $partialBlocks[] = $row->ipb_id
;
389 if ( $partialBlocks ) {
390 // Mutations to the $row object are not persisted. The restrictions will
391 // need be stored in a separate store.
392 $this->restrictions
= BlockRestriction
::loadByBlockId( $partialBlocks );