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