Merge "i18n: Add line-break in email notificaton for minor edits"
[lhc/web/wiklou.git] / includes / specials / pagers / BlockListPager.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Pager
20 */
21
22 /**
23 * @ingroup Pager
24 */
25 use MediaWiki\Block\BlockRestriction;
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;
31
32 class BlockListPager extends TablePager {
33
34 protected $conds;
35
36 /**
37 * Array of restrictions.
38 *
39 * @var Restriction[]
40 */
41 protected $restrictions = [];
42
43 /**
44 * @param SpecialPage $page
45 * @param array $conds
46 */
47 public function __construct( $page, $conds ) {
48 $this->conds = $conds;
49 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
50 parent::__construct( $page->getContext() );
51 }
52
53 function getFieldNames() {
54 static $headers = null;
55
56 if ( $headers === null ) {
57 $headers = [
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',
64 ];
65 foreach ( $headers as $key => $val ) {
66 $headers[$key] = $this->msg( $val )->text();
67 }
68 }
69
70 return $headers;
71 }
72
73 function formatValue( $name, $value ) {
74 static $msg = null;
75 if ( $msg === null ) {
76 $keys = [
77 'anononlyblock',
78 'createaccountblock',
79 'noautoblockblock',
80 'emailblock',
81 'blocklist-nousertalk',
82 'unblocklink',
83 'change-blocklink',
84 'blocklist-editing',
85 'blocklist-editing-sitewide',
86 ];
87
88 foreach ( $keys as $key ) {
89 $msg[$key] = $this->msg( $key )->text();
90 }
91 }
92
93 /** @var object $row */
94 $row = $this->mCurrentRow;
95
96 $language = $this->getLanguage();
97
98 $formatted = '';
99
100 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
101
102 switch ( $name ) {
103 case 'ipb_timestamp':
104 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
105 break;
106
107 case 'ipb_target':
108 if ( $row->ipb_auto ) {
109 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
110 } else {
111 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
112 switch ( $type ) {
113 case Block::TYPE_USER:
114 case Block::TYPE_IP:
115 $formatted = Linker::userLink( $target->getId(), $target );
116 $formatted .= Linker::userToolLinks(
117 $target->getId(),
118 $target,
119 false,
120 Linker::TOOL_LINKS_NOBLOCK
121 );
122 break;
123 case Block::TYPE_RANGE:
124 $formatted = htmlspecialchars( $target );
125 }
126 }
127 break;
128
129 case 'ipb_expiry':
130 $formatted = htmlspecialchars( $language->formatExpiry(
131 $value,
132 /* User preference timezone */true
133 ) );
134 if ( $this->getUser()->isAllowed( 'block' ) ) {
135 if ( $row->ipb_auto ) {
136 $links[] = $linkRenderer->makeKnownLink(
137 SpecialPage::getTitleFor( 'Unblock' ),
138 $msg['unblocklink'],
139 [],
140 [ 'wpTarget' => "#{$row->ipb_id}" ]
141 );
142 } else {
143 $links[] = $linkRenderer->makeKnownLink(
144 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
145 $msg['unblocklink']
146 );
147 $links[] = $linkRenderer->makeKnownLink(
148 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
149 $msg['change-blocklink']
150 );
151 }
152 $formatted .= ' ' . Html::rawElement(
153 'span',
154 [ 'class' => 'mw-blocklist-actions' ],
155 $this->msg( 'parentheses' )->rawParams(
156 $language->pipeList( $links ) )->escaped()
157 );
158 }
159 if ( $value !== 'infinity' ) {
160 $timestamp = new MWTimestamp( $value );
161 $formatted .= '<br />' . $this->msg(
162 'ipb-blocklist-duration-left',
163 $language->formatDuration(
164 $timestamp->getTimestamp() - time(),
165 // reasonable output
166 [
167 'minutes',
168 'hours',
169 'days',
170 'years',
171 ]
172 )
173 )->escaped();
174 }
175 break;
176
177 case 'ipb_by':
178 if ( isset( $row->by_user_name ) ) {
179 $formatted = Linker::userLink( $value, $row->by_user_name );
180 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
181 } else {
182 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
183 }
184 break;
185
186 case 'ipb_reason':
187 $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text;
188 $formatted = Linker::formatComment( $value );
189 break;
190
191 case 'ipb_params':
192 $properties = [];
193
194 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
195 if ( $row->ipb_sitewide ) {
196 $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
197 }
198 }
199
200 if ( !$row->ipb_sitewide && $this->restrictions ) {
201 $list = $this->getRestrictionListHTML( $row );
202 if ( $list ) {
203 $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
204 }
205 }
206
207 if ( $row->ipb_anon_only ) {
208 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
209 }
210 if ( $row->ipb_create_account ) {
211 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
212 }
213 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
214 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
215 }
216
217 if ( $row->ipb_block_email ) {
218 $properties[] = htmlspecialchars( $msg['emailblock'] );
219 }
220
221 if ( !$row->ipb_allow_usertalk ) {
222 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
223 }
224
225 $formatted = Html::rawElement(
226 'ul',
227 [],
228 implode( '', array_map( function ( $prop ) {
229 return Html::rawElement(
230 'li',
231 [],
232 $prop
233 );
234 }, $properties ) )
235 );
236 break;
237
238 default:
239 $formatted = "Unable to format $name";
240 break;
241 }
242
243 return $formatted;
244 }
245
246 /**
247 * Get Restriction List HTML
248 *
249 * @param stdClass $row
250 *
251 * @return string
252 */
253 private function getRestrictionListHTML( stdClass $row ) {
254 $items = [];
255
256 foreach ( $this->restrictions as $restriction ) {
257 if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
258 continue;
259 }
260
261 switch ( $restriction->getType() ) {
262 case PageRestriction::TYPE:
263 if ( $restriction->getTitle() ) {
264 $items[$restriction->getType()][] = Html::rawElement(
265 'li',
266 [],
267 Linker::link( $restriction->getTitle() )
268 );
269 }
270 break;
271 case NamespaceRestriction::TYPE:
272 $text = $restriction->getValue() === NS_MAIN
273 ? $this->msg( 'blanknamespace' )
274 : $this->getLanguage()->getFormattedNsText(
275 $restriction->getValue()
276 );
277 $items[$restriction->getType()][] = Html::rawElement(
278 'li',
279 [],
280 Linker::link(
281 SpecialPage::getTitleValueFor( 'Allpages' ),
282 $text,
283 [],
284 [
285 'namespace' => $restriction->getValue()
286 ]
287 )
288 );
289 break;
290 }
291 }
292
293 if ( empty( $items ) ) {
294 return '';
295 }
296
297 $sets = [];
298 foreach ( $items as $key => $value ) {
299 $sets[] = Html::rawElement(
300 'li',
301 [],
302 $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
303 'ul',
304 [],
305 implode( '', $value )
306 )
307 );
308 }
309
310 return Html::rawElement(
311 'ul',
312 [],
313 implode( '', $sets )
314 );
315 }
316
317 function getQueryInfo() {
318 $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' );
319 $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' );
320
321 $info = [
322 'tables' => array_merge(
323 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
324 ),
325 'fields' => [
326 'ipb_id',
327 'ipb_address',
328 'ipb_user',
329 'by_user_name' => 'user_name',
330 'ipb_timestamp',
331 'ipb_auto',
332 'ipb_anon_only',
333 'ipb_create_account',
334 'ipb_enable_autoblock',
335 'ipb_expiry',
336 'ipb_range_start',
337 'ipb_range_end',
338 'ipb_deleted',
339 'ipb_block_email',
340 'ipb_allow_usertalk',
341 'ipb_sitewide',
342 ] + $commentQuery['fields'] + $actorQuery['fields'],
343 'conds' => $this->conds,
344 'join_conds' => [
345 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
346 ] + $commentQuery['joins'] + $actorQuery['joins']
347 ];
348
349 # Filter out any expired blocks
350 $db = $this->getDatabase();
351 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
352
353 # Is the user allowed to see hidden blocks?
354 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
355 $info['conds']['ipb_deleted'] = 0;
356 }
357
358 return $info;
359 }
360
361 /**
362 * Get total number of autoblocks at any given time
363 *
364 * @return int Total number of unexpired active autoblocks
365 */
366 function getTotalAutoblocks() {
367 $dbr = $this->getDatabase();
368 $res = $dbr->selectField( 'ipblocks',
369 [ 'COUNT(*) AS totalautoblocks' ],
370 [
371 'ipb_auto' => '1',
372 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
373 ]
374 );
375 if ( $res ) {
376 return $res;
377 }
378 return 0; // We found nothing
379 }
380
381 protected function getTableClass() {
382 return parent::getTableClass() . ' mw-blocklist';
383 }
384
385 function getIndexField() {
386 return 'ipb_timestamp';
387 }
388
389 function getDefaultSort() {
390 return 'ipb_timestamp';
391 }
392
393 function isFieldSortable( $name ) {
394 return false;
395 }
396
397 /**
398 * Do a LinkBatch query to minimise database load when generating all these links
399 * @param IResultWrapper $result
400 */
401 function preprocessResults( $result ) {
402 # Do a link batch query
403 $lb = new LinkBatch;
404 $lb->setCaller( __METHOD__ );
405
406 $partialBlocks = [];
407 foreach ( $result as $row ) {
408 $lb->add( NS_USER, $row->ipb_address );
409 $lb->add( NS_USER_TALK, $row->ipb_address );
410
411 if ( isset( $row->by_user_name ) ) {
412 $lb->add( NS_USER, $row->by_user_name );
413 $lb->add( NS_USER_TALK, $row->by_user_name );
414 }
415
416 if ( !$row->ipb_sitewide ) {
417 $partialBlocks[] = $row->ipb_id;
418 }
419 }
420
421 if ( $partialBlocks ) {
422 // Mutations to the $row object are not persisted. The restrictions will
423 // need be stored in a separate store.
424 $this->restrictions = BlockRestriction::loadByBlockId( $partialBlocks );
425 }
426
427 $lb->execute();
428 }
429
430 }