Special:Upload should not crash on failing previews
[mediawiki.git] / includes / specials / pagers / BlockListPager.php
bloba4124db5f4b2dbfb4314905d512a0e62345de146
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 use MediaWiki\MediaWikiServices;
27 class BlockListPager extends TablePager {
29 protected $conds;
30 protected $page;
32 /**
33 * @param SpecialPage $page
34 * @param array $conds
36 function __construct( $page, $conds ) {
37 $this->page = $page;
38 $this->conds = $conds;
39 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
40 parent::__construct( $page->getContext() );
43 function getFieldNames() {
44 static $headers = null;
46 if ( $headers === null ) {
47 $headers = [
48 'ipb_timestamp' => 'blocklist-timestamp',
49 'ipb_target' => 'blocklist-target',
50 'ipb_expiry' => 'blocklist-expiry',
51 'ipb_by' => 'blocklist-by',
52 'ipb_params' => 'blocklist-params',
53 'ipb_reason' => 'blocklist-reason',
55 foreach ( $headers as $key => $val ) {
56 $headers[$key] = $this->msg( $val )->text();
60 return $headers;
63 function formatValue( $name, $value ) {
64 static $msg = null;
65 if ( $msg === null ) {
66 $keys = [
67 'anononlyblock',
68 'createaccountblock',
69 'noautoblockblock',
70 'emailblock',
71 'blocklist-nousertalk',
72 'unblocklink',
73 'change-blocklink',
76 foreach ( $keys as $key ) {
77 $msg[$key] = $this->msg( $key )->text();
81 /** @var $row object */
82 $row = $this->mCurrentRow;
84 $language = $this->getLanguage();
86 $formatted = '';
88 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
90 switch ( $name ) {
91 case 'ipb_timestamp':
92 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
93 break;
95 case 'ipb_target':
96 if ( $row->ipb_auto ) {
97 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
98 } else {
99 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
100 switch ( $type ) {
101 case Block::TYPE_USER:
102 case Block::TYPE_IP:
103 $formatted = Linker::userLink( $target->getId(), $target );
104 $formatted .= Linker::userToolLinks(
105 $target->getId(),
106 $target,
107 false,
108 Linker::TOOL_LINKS_NOBLOCK
110 break;
111 case Block::TYPE_RANGE:
112 $formatted = htmlspecialchars( $target );
115 break;
117 case 'ipb_expiry':
118 $formatted = htmlspecialchars( $language->formatExpiry(
119 $value,
120 /* User preference timezone */true
121 ) );
122 if ( $this->getUser()->isAllowed( 'block' ) ) {
123 if ( $row->ipb_auto ) {
124 $links[] = $linkRenderer->makeKnownLink(
125 SpecialPage::getTitleFor( 'Unblock' ),
126 $msg['unblocklink'],
128 [ 'wpTarget' => "#{$row->ipb_id}" ]
130 } else {
131 $links[] = $linkRenderer->makeKnownLink(
132 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
133 $msg['unblocklink']
135 $links[] = $linkRenderer->makeKnownLink(
136 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
137 $msg['change-blocklink']
140 $formatted .= ' ' . Html::rawElement(
141 'span',
142 [ 'class' => 'mw-blocklist-actions' ],
143 $this->msg( 'parentheses' )->rawParams(
144 $language->pipeList( $links ) )->escaped()
147 if ( $value !== 'infinity' ) {
148 $timestamp = new MWTimestamp( $value );
149 $formatted .= '<br />' . $this->msg(
150 'ipb-blocklist-duration-left',
151 $language->formatDuration(
152 $timestamp->getTimestamp() - time(),
153 // reasonable output
155 'minutes',
156 'hours',
157 'days',
158 'years',
161 )->escaped();
163 break;
165 case 'ipb_by':
166 if ( isset( $row->by_user_name ) ) {
167 $formatted = Linker::userLink( $value, $row->by_user_name );
168 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
169 } else {
170 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
172 break;
174 case 'ipb_reason':
175 $formatted = Linker::formatComment( $value );
176 break;
178 case 'ipb_params':
179 $properties = [];
180 if ( $row->ipb_anon_only ) {
181 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
183 if ( $row->ipb_create_account ) {
184 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
186 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
187 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
190 if ( $row->ipb_block_email ) {
191 $properties[] = htmlspecialchars( $msg['emailblock'] );
194 if ( !$row->ipb_allow_usertalk ) {
195 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
198 $formatted = $language->commaList( $properties );
199 break;
201 default:
202 $formatted = "Unable to format $name";
203 break;
206 return $formatted;
209 function getQueryInfo() {
210 $info = [
211 'tables' => [ 'ipblocks', 'user' ],
212 'fields' => [
213 'ipb_id',
214 'ipb_address',
215 'ipb_user',
216 'ipb_by',
217 'ipb_by_text',
218 'by_user_name' => 'user_name',
219 'ipb_reason',
220 'ipb_timestamp',
221 'ipb_auto',
222 'ipb_anon_only',
223 'ipb_create_account',
224 'ipb_enable_autoblock',
225 'ipb_expiry',
226 'ipb_range_start',
227 'ipb_range_end',
228 'ipb_deleted',
229 'ipb_block_email',
230 'ipb_allow_usertalk',
232 'conds' => $this->conds,
233 'join_conds' => [ 'user' => [ 'LEFT JOIN', 'user_id = ipb_by' ] ]
236 # Filter out any expired blocks
237 $db = $this->getDatabase();
238 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
240 # Is the user allowed to see hidden blocks?
241 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
242 $info['conds']['ipb_deleted'] = 0;
245 return $info;
248 protected function getTableClass() {
249 return parent::getTableClass() . ' mw-blocklist';
252 function getIndexField() {
253 return 'ipb_timestamp';
256 function getDefaultSort() {
257 return 'ipb_timestamp';
260 function isFieldSortable( $name ) {
261 return false;
265 * Do a LinkBatch query to minimise database load when generating all these links
266 * @param ResultWrapper $result
268 function preprocessResults( $result ) {
269 # Do a link batch query
270 $lb = new LinkBatch;
271 $lb->setCaller( __METHOD__ );
273 foreach ( $result as $row ) {
274 $lb->add( NS_USER, $row->ipb_address );
275 $lb->add( NS_USER_TALK, $row->ipb_address );
277 if ( isset( $row->by_user_name ) ) {
278 $lb->add( NS_USER, $row->by_user_name );
279 $lb->add( NS_USER_TALK, $row->by_user_name );
283 $lb->execute();