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\MediaWikiServices
;
26 use Wikimedia\Rdbms\ResultWrapper
;
28 class BlockListPager
extends TablePager
{
34 * @param SpecialPage $page
37 function __construct( $page, $conds ) {
39 $this->conds
= $conds;
40 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
41 parent
::__construct( $page->getContext() );
44 function getFieldNames() {
45 static $headers = null;
47 if ( $headers === null ) {
49 'ipb_timestamp' => 'blocklist-timestamp',
50 'ipb_target' => 'blocklist-target',
51 'ipb_expiry' => 'blocklist-expiry',
52 'ipb_by' => 'blocklist-by',
53 'ipb_params' => 'blocklist-params',
54 'ipb_reason' => 'blocklist-reason',
56 foreach ( $headers as $key => $val ) {
57 $headers[$key] = $this->msg( $val )->text();
64 function formatValue( $name, $value ) {
66 if ( $msg === null ) {
72 'blocklist-nousertalk',
77 foreach ( $keys as $key ) {
78 $msg[$key] = $this->msg( $key )->text();
82 /** @var object $row */
83 $row = $this->mCurrentRow
;
85 $language = $this->getLanguage();
89 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
93 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
97 if ( $row->ipb_auto
) {
98 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
100 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
102 case Block
::TYPE_USER
:
104 $formatted = Linker
::userLink( $target->getId(), $target );
105 $formatted .= Linker
::userToolLinks(
109 Linker
::TOOL_LINKS_NOBLOCK
112 case Block
::TYPE_RANGE
:
113 $formatted = htmlspecialchars( $target );
119 $formatted = htmlspecialchars( $language->formatExpiry(
121 /* User preference timezone */true
123 if ( $this->getUser()->isAllowed( 'block' ) ) {
124 if ( $row->ipb_auto
) {
125 $links[] = $linkRenderer->makeKnownLink(
126 SpecialPage
::getTitleFor( 'Unblock' ),
129 [ 'wpTarget' => "#{$row->ipb_id}" ]
132 $links[] = $linkRenderer->makeKnownLink(
133 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
136 $links[] = $linkRenderer->makeKnownLink(
137 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
138 $msg['change-blocklink']
141 $formatted .= ' ' . Html
::rawElement(
143 [ 'class' => 'mw-blocklist-actions' ],
144 $this->msg( 'parentheses' )->rawParams(
145 $language->pipeList( $links ) )->escaped()
148 if ( $value !== 'infinity' ) {
149 $timestamp = new MWTimestamp( $value );
150 $formatted .= '<br />' . $this->msg(
151 'ipb-blocklist-duration-left',
152 $language->formatDuration(
153 $timestamp->getTimestamp() - time(),
167 if ( isset( $row->by_user_name
) ) {
168 $formatted = Linker
::userLink( $value, $row->by_user_name
);
169 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
171 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
176 $value = CommentStore
::getStore()->getComment( 'ipb_reason', $row )->text
;
177 $formatted = Linker
::formatComment( $value );
182 if ( $row->ipb_anon_only
) {
183 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
185 if ( $row->ipb_create_account
) {
186 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
188 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
189 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
192 if ( $row->ipb_block_email
) {
193 $properties[] = htmlspecialchars( $msg['emailblock'] );
196 if ( !$row->ipb_allow_usertalk
) {
197 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
200 $formatted = $language->commaList( $properties );
204 $formatted = "Unable to format $name";
211 function getQueryInfo() {
212 $commentQuery = CommentStore
::getStore()->getJoin( 'ipb_reason' );
213 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ipb_by' );
216 'tables' => array_merge(
217 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
223 'by_user_name' => 'user_name',
227 'ipb_create_account',
228 'ipb_enable_autoblock',
234 'ipb_allow_usertalk',
235 ] +
$commentQuery['fields'], $actorQuery['fields'],
236 'conds' => $this->conds
,
238 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
239 ] +
$commentQuery['joins'] +
$actorQuery['joins']
242 # Filter out any expired blocks
243 $db = $this->getDatabase();
244 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
246 # Is the user allowed to see hidden blocks?
247 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
248 $info['conds']['ipb_deleted'] = 0;
255 * Get total number of autoblocks at any given time
257 * @return int Total number of unexpired active autoblocks
259 function getTotalAutoblocks() {
260 $dbr = $this->getDatabase();
261 $res = $dbr->selectField( 'ipblocks',
262 [ 'COUNT(*) AS totalautoblocks' ],
265 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
271 return 0; // We found nothing
274 protected function getTableClass() {
275 return parent
::getTableClass() . ' mw-blocklist';
278 function getIndexField() {
279 return 'ipb_timestamp';
282 function getDefaultSort() {
283 return 'ipb_timestamp';
286 function isFieldSortable( $name ) {
291 * Do a LinkBatch query to minimise database load when generating all these links
292 * @param ResultWrapper $result
294 function preprocessResults( $result ) {
295 # Do a link batch query
297 $lb->setCaller( __METHOD__
);
299 foreach ( $result as $row ) {
300 $lb->add( NS_USER
, $row->ipb_address
);
301 $lb->add( NS_USER_TALK
, $row->ipb_address
);
303 if ( isset( $row->by_user_name
) ) {
304 $lb->add( NS_USER
, $row->by_user_name
);
305 $lb->add( NS_USER_TALK
, $row->by_user_name
);