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' );
215 'tables' => [ 'ipblocks', 'user' ] +
$commentQuery['tables'],
222 'by_user_name' => 'user_name',
226 'ipb_create_account',
227 'ipb_enable_autoblock',
233 'ipb_allow_usertalk',
234 ] +
$commentQuery['fields'],
235 'conds' => $this->conds
,
236 'join_conds' => [ 'user' => [ 'LEFT JOIN', 'user_id = ipb_by' ] ] +
$commentQuery['joins']
239 # Filter out any expired blocks
240 $db = $this->getDatabase();
241 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
243 # Is the user allowed to see hidden blocks?
244 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
245 $info['conds']['ipb_deleted'] = 0;
252 * Get total number of autoblocks at any given time
254 * @return int Total number of unexpired active autoblocks
256 function getTotalAutoblocks() {
257 $dbr = $this->getDatabase();
258 $res = $dbr->selectField( 'ipblocks',
259 [ 'COUNT(*) AS totalautoblocks' ],
262 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
268 return 0; // We found nothing
271 protected function getTableClass() {
272 return parent
::getTableClass() . ' mw-blocklist';
275 function getIndexField() {
276 return 'ipb_timestamp';
279 function getDefaultSort() {
280 return 'ipb_timestamp';
283 function isFieldSortable( $name ) {
288 * Do a LinkBatch query to minimise database load when generating all these links
289 * @param ResultWrapper $result
291 function preprocessResults( $result ) {
292 # Do a link batch query
294 $lb->setCaller( __METHOD__
);
296 foreach ( $result as $row ) {
297 $lb->add( NS_USER
, $row->ipb_address
);
298 $lb->add( NS_USER_TALK
, $row->ipb_address
);
300 if ( isset( $row->by_user_name
) ) {
301 $lb->add( NS_USER
, $row->by_user_name
);
302 $lb->add( NS_USER_TALK
, $row->by_user_name
);