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\Block\Restriction\PageRestriction
;
28 use MediaWiki\Block\Restriction\NamespaceRestriction
;
29 use MediaWiki\MediaWikiServices
;
30 use Wikimedia\Rdbms\IResultWrapper
;
32 class BlockListPager
extends TablePager
{
38 * Array of restrictions.
42 protected $restrictions = [];
45 * @param SpecialPage $page
48 public function __construct( $page, $conds ) {
50 $this->conds
= $conds;
51 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
52 parent
::__construct( $page->getContext() );
55 function getFieldNames() {
56 static $headers = null;
58 if ( $headers === null ) {
60 'ipb_timestamp' => 'blocklist-timestamp',
61 'ipb_target' => 'blocklist-target',
62 'ipb_expiry' => 'blocklist-expiry',
63 'ipb_by' => 'blocklist-by',
64 'ipb_params' => 'blocklist-params',
65 'ipb_reason' => 'blocklist-reason',
67 foreach ( $headers as $key => $val ) {
68 $headers[$key] = $this->msg( $val )->text();
75 function formatValue( $name, $value ) {
77 if ( $msg === null ) {
83 'blocklist-nousertalk',
87 'blocklist-editing-sitewide',
90 foreach ( $keys as $key ) {
91 $msg[$key] = $this->msg( $key )->text();
95 /** @var object $row */
96 $row = $this->mCurrentRow
;
98 $language = $this->getLanguage();
102 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
105 case 'ipb_timestamp':
106 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
110 if ( $row->ipb_auto
) {
111 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
113 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
115 case Block
::TYPE_USER
:
117 $formatted = Linker
::userLink( $target->getId(), $target );
118 $formatted .= Linker
::userToolLinks(
122 Linker
::TOOL_LINKS_NOBLOCK
125 case Block
::TYPE_RANGE
:
126 $formatted = htmlspecialchars( $target );
132 $formatted = htmlspecialchars( $language->formatExpiry(
134 /* User preference timezone */true
136 if ( $this->getUser()->isAllowed( 'block' ) ) {
137 if ( $row->ipb_auto
) {
138 $links[] = $linkRenderer->makeKnownLink(
139 SpecialPage
::getTitleFor( 'Unblock' ),
142 [ 'wpTarget' => "#{$row->ipb_id}" ]
145 $links[] = $linkRenderer->makeKnownLink(
146 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
149 $links[] = $linkRenderer->makeKnownLink(
150 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
151 $msg['change-blocklink']
154 $formatted .= ' ' . Html
::rawElement(
156 [ 'class' => 'mw-blocklist-actions' ],
157 $this->msg( 'parentheses' )->rawParams(
158 $language->pipeList( $links ) )->escaped()
161 if ( $value !== 'infinity' ) {
162 $timestamp = new MWTimestamp( $value );
163 $formatted .= '<br />' . $this->msg(
164 'ipb-blocklist-duration-left',
165 $language->formatDuration(
166 $timestamp->getTimestamp() - time(),
180 if ( isset( $row->by_user_name
) ) {
181 $formatted = Linker
::userLink( $value, $row->by_user_name
);
182 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
184 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
189 $value = CommentStore
::getStore()->getComment( 'ipb_reason', $row )->text
;
190 $formatted = Linker
::formatComment( $value );
196 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
197 if ( $row->ipb_sitewide
) {
198 $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
202 if ( !$row->ipb_sitewide
&& $this->restrictions
) {
203 $list = $this->getRestrictionListHTML( $row );
205 $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
209 if ( $row->ipb_anon_only
) {
210 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
212 if ( $row->ipb_create_account
) {
213 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
215 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
216 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
219 if ( $row->ipb_block_email
) {
220 $properties[] = htmlspecialchars( $msg['emailblock'] );
223 if ( !$row->ipb_allow_usertalk
) {
224 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
227 $formatted = Html
::rawElement(
230 implode( '', array_map( function ( $prop ) {
231 return Html
::rawElement(
241 $formatted = "Unable to format $name";
249 * Get Restriction List HTML
251 * @param stdClass $row
255 private function getRestrictionListHTML( stdClass
$row ) {
258 foreach ( $this->restrictions
as $restriction ) {
259 if ( $restriction->getBlockId() !== (int)$row->ipb_id
) {
263 switch ( $restriction->getType() ) {
264 case PageRestriction
::TYPE
:
265 if ( $restriction->getTitle() ) {
266 $items[$restriction->getType()][] = HTML
::rawElement(
269 Linker
::link( $restriction->getTitle() )
273 case NamespaceRestriction
::TYPE
:
274 $text = $restriction->getValue() === NS_MAIN
275 ?
$this->msg( 'blanknamespace' )
276 : $this->getLanguage()->getFormattedNsText(
277 $restriction->getValue()
279 $items[$restriction->getType()][] = HTML
::rawElement(
283 SpecialPage
::getTitleValueFor( 'Allpages' ),
287 'namespace' => $restriction->getValue()
295 if ( empty( $items ) ) {
300 foreach ( $items as $key => $value ) {
301 $sets[] = Html
::rawElement(
304 $this->msg( 'blocklist-editing-' . $key ) . Html
::rawElement(
307 implode( '', $value )
312 return Html
::rawElement(
319 function getQueryInfo() {
320 $commentQuery = CommentStore
::getStore()->getJoin( 'ipb_reason' );
321 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ipb_by' );
324 'tables' => array_merge(
325 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
331 'by_user_name' => 'user_name',
335 'ipb_create_account',
336 'ipb_enable_autoblock',
342 'ipb_allow_usertalk',
344 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
345 'conds' => $this->conds
,
347 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
348 ] +
$commentQuery['joins'] +
$actorQuery['joins']
351 # Filter out any expired blocks
352 $db = $this->getDatabase();
353 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
355 # Is the user allowed to see hidden blocks?
356 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
357 $info['conds']['ipb_deleted'] = 0;
364 * Get total number of autoblocks at any given time
366 * @return int Total number of unexpired active autoblocks
368 function getTotalAutoblocks() {
369 $dbr = $this->getDatabase();
370 $res = $dbr->selectField( 'ipblocks',
371 [ 'COUNT(*) AS totalautoblocks' ],
374 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
380 return 0; // We found nothing
383 protected function getTableClass() {
384 return parent
::getTableClass() . ' mw-blocklist';
387 function getIndexField() {
388 return 'ipb_timestamp';
391 function getDefaultSort() {
392 return 'ipb_timestamp';
395 function isFieldSortable( $name ) {
400 * Do a LinkBatch query to minimise database load when generating all these links
401 * @param IResultWrapper $result
403 function preprocessResults( $result ) {
404 # Do a link batch query
406 $lb->setCaller( __METHOD__
);
409 foreach ( $result as $row ) {
410 $lb->add( NS_USER
, $row->ipb_address
);
411 $lb->add( NS_USER_TALK
, $row->ipb_address
);
413 if ( isset( $row->by_user_name
) ) {
414 $lb->add( NS_USER
, $row->by_user_name
);
415 $lb->add( NS_USER_TALK
, $row->by_user_name
);
418 if ( !$row->ipb_sitewide
) {
419 $partialBlocks[] = $row->ipb_id
;
423 if ( $partialBlocks ) {
424 // Mutations to the $row object are not persisted. The restrictions will
425 // need be stored in a separate store.
426 $this->restrictions
= BlockRestriction
::loadByBlockId( $partialBlocks );