AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / specials / pagers / BlockListPager.php
blobd8229761b749f3e643c466e0dd71da255ad5e1d6
1 <?php
2 /**
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
18 * @file
19 * @ingroup Pager
22 /**
23 * @ingroup Pager
25 class BlockListPager extends TablePager {
27 protected $conds;
28 protected $page;
30 /**
31 * @param SpecialPage $page
32 * @param array $conds
34 function __construct( $page, $conds ) {
35 $this->page = $page;
36 $this->conds = $conds;
37 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
38 parent::__construct( $page->getContext() );
41 function getFieldNames() {
42 static $headers = null;
44 if ( $headers === null ) {
45 $headers = [
46 'ipb_timestamp' => 'blocklist-timestamp',
47 'ipb_target' => 'blocklist-target',
48 'ipb_expiry' => 'blocklist-expiry',
49 'ipb_by' => 'blocklist-by',
50 'ipb_params' => 'blocklist-params',
51 'ipb_reason' => 'blocklist-reason',
53 foreach ( $headers as $key => $val ) {
54 $headers[$key] = $this->msg( $val )->text();
58 return $headers;
61 function formatValue( $name, $value ) {
62 static $msg = null;
63 if ( $msg === null ) {
64 $keys = [
65 'anononlyblock',
66 'createaccountblock',
67 'noautoblockblock',
68 'emailblock',
69 'blocklist-nousertalk',
70 'unblocklink',
71 'change-blocklink',
74 foreach ( $keys as $key ) {
75 $msg[$key] = $this->msg( $key )->escaped();
79 /** @var $row object */
80 $row = $this->mCurrentRow;
82 $language = $this->getLanguage();
84 $formatted = '';
86 switch ( $name ) {
87 case 'ipb_timestamp':
88 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
89 break;
91 case 'ipb_target':
92 if ( $row->ipb_auto ) {
93 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
94 } else {
95 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
96 switch ( $type ) {
97 case Block::TYPE_USER:
98 case Block::TYPE_IP:
99 $formatted = Linker::userLink( $target->getId(), $target );
100 $formatted .= Linker::userToolLinks(
101 $target->getId(),
102 $target,
103 false,
104 Linker::TOOL_LINKS_NOBLOCK
106 break;
107 case Block::TYPE_RANGE:
108 $formatted = htmlspecialchars( $target );
111 break;
113 case 'ipb_expiry':
114 $formatted = htmlspecialchars( $language->formatExpiry(
115 $value,
116 /* User preference timezone */true
117 ) );
118 if ( $this->getUser()->isAllowed( 'block' ) ) {
119 if ( $row->ipb_auto ) {
120 $links[] = Linker::linkKnown(
121 SpecialPage::getTitleFor( 'Unblock' ),
122 $msg['unblocklink'],
124 [ 'wpTarget' => "#{$row->ipb_id}" ]
126 } else {
127 $links[] = Linker::linkKnown(
128 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
129 $msg['unblocklink']
131 $links[] = Linker::linkKnown(
132 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
133 $msg['change-blocklink']
136 $formatted .= ' ' . Html::rawElement(
137 'span',
138 [ 'class' => 'mw-blocklist-actions' ],
139 $this->msg( 'parentheses' )->rawParams(
140 $language->pipeList( $links ) )->escaped()
143 if ( $value !== 'infinity' ) {
144 $timestamp = new MWTimestamp( $value );
145 $formatted .= '<br />' . $this->msg(
146 'ipb-blocklist-duration-left',
147 $language->formatDuration(
148 $timestamp->getTimestamp() - time(),
149 // reasonable output
151 'minutes',
152 'hours',
153 'days',
154 'years',
157 )->escaped();
159 break;
161 case 'ipb_by':
162 if ( isset( $row->by_user_name ) ) {
163 $formatted = Linker::userLink( $value, $row->by_user_name );
164 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
165 } else {
166 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
168 break;
170 case 'ipb_reason':
171 $formatted = Linker::formatComment( $value );
172 break;
174 case 'ipb_params':
175 $properties = [];
176 if ( $row->ipb_anon_only ) {
177 $properties[] = $msg['anononlyblock'];
179 if ( $row->ipb_create_account ) {
180 $properties[] = $msg['createaccountblock'];
182 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
183 $properties[] = $msg['noautoblockblock'];
186 if ( $row->ipb_block_email ) {
187 $properties[] = $msg['emailblock'];
190 if ( !$row->ipb_allow_usertalk ) {
191 $properties[] = $msg['blocklist-nousertalk'];
194 $formatted = $language->commaList( $properties );
195 break;
197 default:
198 $formatted = "Unable to format $name";
199 break;
202 return $formatted;
205 function getQueryInfo() {
206 $info = [
207 'tables' => [ 'ipblocks', 'user' ],
208 'fields' => [
209 'ipb_id',
210 'ipb_address',
211 'ipb_user',
212 'ipb_by',
213 'ipb_by_text',
214 'by_user_name' => 'user_name',
215 'ipb_reason',
216 'ipb_timestamp',
217 'ipb_auto',
218 'ipb_anon_only',
219 'ipb_create_account',
220 'ipb_enable_autoblock',
221 'ipb_expiry',
222 'ipb_range_start',
223 'ipb_range_end',
224 'ipb_deleted',
225 'ipb_block_email',
226 'ipb_allow_usertalk',
228 'conds' => $this->conds,
229 'join_conds' => [ 'user' => [ 'LEFT JOIN', 'user_id = ipb_by' ] ]
232 # Filter out any expired blocks
233 $db = $this->getDatabase();
234 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
236 # Is the user allowed to see hidden blocks?
237 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
238 $info['conds']['ipb_deleted'] = 0;
241 return $info;
244 protected function getTableClass() {
245 return parent::getTableClass() . ' mw-blocklist';
248 function getIndexField() {
249 return 'ipb_timestamp';
252 function getDefaultSort() {
253 return 'ipb_timestamp';
256 function isFieldSortable( $name ) {
257 return false;
261 * Do a LinkBatch query to minimise database load when generating all these links
262 * @param ResultWrapper $result
264 function preprocessResults( $result ) {
265 # Do a link batch query
266 $lb = new LinkBatch;
267 $lb->setCaller( __METHOD__ );
269 foreach ( $result as $row ) {
270 $lb->add( NS_USER, $row->ipb_address );
271 $lb->add( NS_USER_TALK, $row->ipb_address );
273 if ( isset( $row->by_user_name ) ) {
274 $lb->add( NS_USER, $row->by_user_name );
275 $lb->add( NS_USER_TALK, $row->by_user_name );
279 $lb->execute();