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\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
;
32 class BlockListPager
extends TablePager
{
37 * Array of restrictions.
41 protected $restrictions = [];
44 * @param SpecialPage $page
47 public function __construct( $page, $conds ) {
48 parent
::__construct( $page->getContext(), $page->getLinkRenderer() );
49 $this->conds
= $conds;
50 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
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();
75 * @param string $value
77 * @suppress PhanTypeArraySuspiciousNullable
79 function formatValue( $name, $value ) {
81 if ( $msg === null ) {
87 'blocklist-nousertalk',
91 'blocklist-editing-sitewide',
94 foreach ( $keys as $key ) {
95 $msg[$key] = $this->msg( $key )->text();
99 /** @var object $row */
100 $row = $this->mCurrentRow
;
102 $language = $this->getLanguage();
106 $linkRenderer = $this->getLinkRenderer();
109 case 'ipb_timestamp':
110 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
114 if ( $row->ipb_auto
) {
115 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
117 list( $target, $type ) = DatabaseBlock
::parseTarget( $row->ipb_address
);
119 case DatabaseBlock
::TYPE_USER
:
120 case DatabaseBlock
::TYPE_IP
:
121 $formatted = Linker
::userLink( $target->getId(), $target );
122 $formatted .= Linker
::userToolLinks(
126 Linker
::TOOL_LINKS_NOBLOCK
129 case DatabaseBlock
::TYPE_RANGE
:
130 $formatted = htmlspecialchars( $target );
136 $formatted = htmlspecialchars( $language->formatExpiry(
138 /* User preference timezone */true
140 if ( $this->getUser()->isAllowed( 'block' ) ) {
141 if ( $row->ipb_auto
) {
142 $links[] = $linkRenderer->makeKnownLink(
143 SpecialPage
::getTitleFor( 'Unblock' ),
146 [ 'wpTarget' => "#{$row->ipb_id}" ]
149 $links[] = $linkRenderer->makeKnownLink(
150 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
153 $links[] = $linkRenderer->makeKnownLink(
154 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
155 $msg['change-blocklink']
158 $formatted .= ' ' . Html
::rawElement(
160 [ 'class' => 'mw-blocklist-actions' ],
161 $this->msg( 'parentheses' )->rawParams(
162 $language->pipeList( $links ) )->escaped()
165 if ( $value !== 'infinity' ) {
166 $timestamp = new MWTimestamp( $value );
167 $formatted .= '<br />' . $this->msg(
168 'ipb-blocklist-duration-left',
169 $language->formatDuration(
170 $timestamp->getTimestamp() - MWTimestamp
::time(),
184 if ( isset( $row->by_user_name
) ) {
185 $formatted = Linker
::userLink( $value, $row->by_user_name
);
186 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
188 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
193 $value = CommentStore
::getStore()->getComment( 'ipb_reason', $row )->text
;
194 $formatted = Linker
::formatComment( $value );
200 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) && $row->ipb_sitewide
) {
201 $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
204 if ( !$row->ipb_sitewide
&& $this->restrictions
) {
205 $list = $this->getRestrictionListHTML( $row );
207 $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
211 if ( $row->ipb_anon_only
) {
212 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
214 if ( $row->ipb_create_account
) {
215 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
217 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
218 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
221 if ( $row->ipb_block_email
) {
222 $properties[] = htmlspecialchars( $msg['emailblock'] );
225 if ( !$row->ipb_allow_usertalk
) {
226 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
229 $formatted = Html
::rawElement(
232 implode( '', array_map( function ( $prop ) {
233 return Html
::rawElement(
243 $formatted = "Unable to format $name";
251 * Get Restriction List HTML
253 * @param stdClass $row
257 private function getRestrictionListHTML( stdClass
$row ) {
259 $linkRenderer = $this->getLinkRenderer();
261 foreach ( $this->restrictions
as $restriction ) {
262 if ( $restriction->getBlockId() !== (int)$row->ipb_id
) {
266 switch ( $restriction->getType() ) {
267 case PageRestriction
::TYPE
:
268 if ( $restriction->getTitle() ) {
269 $items[$restriction->getType()][] = Html
::rawElement(
272 $linkRenderer->makeLink( $restriction->getTitle() )
276 case NamespaceRestriction
::TYPE
:
277 $text = $restriction->getValue() === NS_MAIN
278 ?
$this->msg( 'blanknamespace' )->text()
279 : $this->getLanguage()->getFormattedNsText(
280 $restriction->getValue()
282 $items[$restriction->getType()][] = Html
::rawElement(
285 $linkRenderer->makeLink(
286 SpecialPage
::getTitleValueFor( 'Allpages' ),
290 'namespace' => $restriction->getValue()
298 if ( empty( $items ) ) {
303 foreach ( $items as $key => $value ) {
304 $sets[] = Html
::rawElement(
307 $this->msg( 'blocklist-editing-' . $key ) . Html
::rawElement(
310 implode( '', $value )
315 return Html
::rawElement(
322 function getQueryInfo() {
323 $commentQuery = CommentStore
::getStore()->getJoin( 'ipb_reason' );
324 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ipb_by' );
327 'tables' => array_merge(
328 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
334 'by_user_name' => 'user_name',
338 'ipb_create_account',
339 'ipb_enable_autoblock',
345 'ipb_allow_usertalk',
347 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
348 'conds' => $this->conds
,
350 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
351 ] +
$commentQuery['joins'] +
$actorQuery['joins']
354 # Filter out any expired blocks
355 $db = $this->getDatabase();
356 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
358 # Is the user allowed to see hidden blocks?
359 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
360 $info['conds']['ipb_deleted'] = 0;
367 * Get total number of autoblocks at any given time
369 * @return int Total number of unexpired active autoblocks
371 function getTotalAutoblocks() {
372 $dbr = $this->getDatabase();
373 $res = $dbr->selectField( 'ipblocks',
377 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
383 return 0; // We found nothing
386 protected function getTableClass() {
387 return parent
::getTableClass() . ' mw-blocklist';
390 function getIndexField() {
391 return 'ipb_timestamp';
394 function getDefaultSort() {
395 return 'ipb_timestamp';
398 function isFieldSortable( $name ) {
403 * Do a LinkBatch query to minimise database load when generating all these links
404 * @param IResultWrapper $result
406 function preprocessResults( $result ) {
407 # Do a link batch query
409 $lb->setCaller( __METHOD__
);
412 foreach ( $result as $row ) {
413 $lb->add( NS_USER
, $row->ipb_address
);
414 $lb->add( NS_USER_TALK
, $row->ipb_address
);
416 if ( isset( $row->by_user_name
) ) {
417 $lb->add( NS_USER
, $row->by_user_name
);
418 $lb->add( NS_USER_TALK
, $row->by_user_name
);
421 if ( !$row->ipb_sitewide
) {
422 $partialBlocks[] = $row->ipb_id
;
426 if ( $partialBlocks ) {
427 // Mutations to the $row object are not persisted. The restrictions will
428 // need be stored in a separate store.
429 $blockRestrictionStore = MediaWikiServices
::getInstance()->getBlockRestrictionStore();
430 $this->restrictions
= $blockRestrictionStore->loadByBlockId( $partialBlocks );