Merge "Fix Status::getMessage accidentially returning string instead of Message"
[lhc/web/wiklou.git] / includes / changes / ChangesList.php
1 <?php
2 /**
3 * Base class for all changes lists.
4 *
5 * The class is used for formatting recent changes, related changes and watchlist.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 class ChangesList extends ContextSource {
26 /**
27 * @var Skin
28 */
29 public $skin;
30
31 protected $watchlist = false;
32 protected $lastdate;
33 protected $message;
34 protected $rc_cache;
35 protected $rcCacheIndex;
36 protected $rclistOpen;
37 protected $rcMoveIndex;
38
39 /**
40 * Changeslist constructor
41 *
42 * @param Skin|IContextSource $obj
43 */
44 public function __construct( $obj ) {
45 if ( $obj instanceof IContextSource ) {
46 $this->setContext( $obj );
47 $this->skin = $obj->getSkin();
48 } else {
49 $this->setContext( $obj->getContext() );
50 $this->skin = $obj;
51 }
52 $this->preCacheMessages();
53 }
54
55 /**
56 * Fetch an appropriate changes list class for the specified context
57 * Some users might want to use an enhanced list format, for instance
58 *
59 * @param IContextSource $context
60 * @return ChangesList derivative
61 */
62 public static function newFromContext( IContextSource $context ) {
63 $user = $context->getUser();
64 $sk = $context->getSkin();
65 $list = null;
66 if ( wfRunHooks( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
67 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
68
69 return $new ? new EnhancedChangesList( $context ) : new OldChangesList( $context );
70 } else {
71 return $list;
72 }
73 }
74
75 /**
76 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
77 * @param $value Boolean
78 */
79 public function setWatchlistDivs( $value = true ) {
80 $this->watchlist = $value;
81 }
82
83 /**
84 * @return bool true when setWatchlistDivs has been called
85 * @since 1.23
86 */
87 public function isWatchlist() {
88 return (bool)$this->watchlist;
89 }
90
91 /**
92 * As we use the same small set of messages in various methods and that
93 * they are called often, we call them once and save them in $this->message
94 */
95 private function preCacheMessages() {
96 if ( !isset( $this->message ) ) {
97 foreach ( array(
98 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
99 'semicolon-separator', 'pipe-separator' ) as $msg
100 ) {
101 $this->message[$msg] = $this->msg( $msg )->escaped();
102 }
103 }
104 }
105
106 /**
107 * Returns the appropriate flags for new page, minor change and patrolling
108 * @param array $flags Associative array of 'flag' => Bool
109 * @param string $nothing to use for empty space
110 * @return string
111 */
112 public function recentChangesFlags( $flags, $nothing = '&#160;' ) {
113 global $wgRecentChangesFlags;
114 $f = '';
115 foreach ( array_keys( $wgRecentChangesFlags ) as $flag ) {
116 $f .= isset( $flags[$flag] ) && $flags[$flag]
117 ? self::flag( $flag )
118 : $nothing;
119 }
120
121 return $f;
122 }
123
124 /**
125 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
126 * namely the flag indicating a new page, a minor edit, a bot edit, or an
127 * unpatrolled edit. By default in English it will contain "N", "m", "b",
128 * "!" respectively, plus it will have an appropriate title and class.
129 *
130 * @param string $flag One key of $wgRecentChangesFlags
131 * @return string Raw HTML
132 */
133 public static function flag( $flag ) {
134 static $flagInfos = null;
135 if ( is_null( $flagInfos ) ) {
136 global $wgRecentChangesFlags;
137 $flagInfos = array();
138 foreach ( $wgRecentChangesFlags as $key => $value ) {
139 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
140 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
141 // Allow customized class name, fall back to flag name
142 $flagInfos[$key]['class'] = Sanitizer::escapeClass(
143 isset( $value['class'] ) ? $value['class'] : $key );
144 }
145 }
146
147 // Inconsistent naming, bleh, kepted for b/c
148 $map = array(
149 'minoredit' => 'minor',
150 'botedit' => 'bot',
151 );
152 if ( isset( $map[$flag] ) ) {
153 $flag = $map[$flag];
154 }
155
156 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
157 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
158 '</abbr>';
159 }
160
161 /**
162 * Returns text for the start of the tabular part of RC
163 * @return string
164 */
165 public function beginRecentChangesList() {
166 $this->rc_cache = array();
167 $this->rcMoveIndex = 0;
168 $this->rcCacheIndex = 0;
169 $this->lastdate = '';
170 $this->rclistOpen = false;
171 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
172
173 return '<div class="mw-changeslist">';
174 }
175
176 /**
177 * Show formatted char difference
178 * @param int $old Number of bytes
179 * @param int $new Number of bytes
180 * @param IContextSource $context
181 * @return string
182 */
183 public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
184 global $wgRCChangedSizeThreshold, $wgMiserMode;
185
186 if ( !$context ) {
187 $context = RequestContext::getMain();
188 }
189
190 $new = (int)$new;
191 $old = (int)$old;
192 $szdiff = $new - $old;
193
194 $lang = $context->getLanguage();
195 $code = $lang->getCode();
196 static $fastCharDiff = array();
197 if ( !isset( $fastCharDiff[$code] ) ) {
198 $fastCharDiff[$code] = $wgMiserMode || $context->msg( 'rc-change-size' )->plain() === '$1';
199 }
200
201 $formattedSize = $lang->formatNum( $szdiff );
202
203 if ( !$fastCharDiff[$code] ) {
204 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
205 }
206
207 if ( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
208 $tag = 'strong';
209 } else {
210 $tag = 'span';
211 }
212
213 if ( $szdiff === 0 ) {
214 $formattedSizeClass = 'mw-plusminus-null';
215 } elseif ( $szdiff > 0 ) {
216 $formattedSize = '+' . $formattedSize;
217 $formattedSizeClass = 'mw-plusminus-pos';
218 } else {
219 $formattedSizeClass = 'mw-plusminus-neg';
220 }
221
222 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
223
224 return Html::element( $tag,
225 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
226 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
227 }
228
229 /**
230 * Format the character difference of one or several changes.
231 *
232 * @param $old RecentChange
233 * @param $new RecentChange last change to use, if not provided, $old will be used
234 * @return string HTML fragment
235 */
236 public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
237 $oldlen = $old->mAttribs['rc_old_len'];
238
239 if ( $new ) {
240 $newlen = $new->mAttribs['rc_new_len'];
241 } else {
242 $newlen = $old->mAttribs['rc_new_len'];
243 }
244
245 if ( $oldlen === null || $newlen === null ) {
246 return '';
247 }
248
249 return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
250 }
251
252 /**
253 * Returns text for the end of RC
254 * @return String
255 */
256 public function endRecentChangesList() {
257 $out = $this->rclistOpen ? "</ul>\n" : '';
258 $out .= '</div>';
259
260 return $out;
261 }
262
263 /**
264 * @param string $s HTML to update
265 * @param $rc_timestamp mixed
266 */
267 public function insertDateHeader( &$s, $rc_timestamp ) {
268 # Make date header if necessary
269 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
270 if ( $date != $this->lastdate ) {
271 if ( $this->lastdate != '' ) {
272 $s .= "</ul>\n";
273 }
274 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
275 $this->lastdate = $date;
276 $this->rclistOpen = true;
277 }
278 }
279
280 /**
281 * @param string $s HTML to update
282 * @param $title Title
283 * @param $logtype string
284 */
285 public function insertLog( &$s, $title, $logtype ) {
286 $page = new LogPage( $logtype );
287 $logname = $page->getName()->escaped();
288 $s .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $title, $logname ) )->escaped();
289 }
290
291 /**
292 * @param string $s HTML to update
293 * @param $rc RecentChange
294 * @param $unpatrolled
295 */
296 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
297 # Diff link
298 if ( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
299 $diffLink = $this->message['diff'];
300 } elseif ( !self::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
301 $diffLink = $this->message['diff'];
302 } else {
303 $query = array(
304 'curid' => $rc->mAttribs['rc_cur_id'],
305 'diff' => $rc->mAttribs['rc_this_oldid'],
306 'oldid' => $rc->mAttribs['rc_last_oldid']
307 );
308
309 $diffLink = Linker::linkKnown(
310 $rc->getTitle(),
311 $this->message['diff'],
312 array( 'tabindex' => $rc->counter ),
313 $query
314 );
315 }
316 $diffhist = $diffLink . $this->message['pipe-separator'];
317 # History link
318 $diffhist .= Linker::linkKnown(
319 $rc->getTitle(),
320 $this->message['hist'],
321 array(),
322 array(
323 'curid' => $rc->mAttribs['rc_cur_id'],
324 'action' => 'history'
325 )
326 );
327 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
328 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
329 ' <span class="mw-changeslist-separator">. .</span> ';
330 }
331
332 /**
333 * @param string $s HTML to update
334 * @param $rc RecentChange
335 * @param $unpatrolled
336 * @param $watched
337 */
338 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
339 $params = array();
340
341 $articlelink = Linker::linkKnown(
342 $rc->getTitle(),
343 null,
344 array( 'class' => 'mw-changeslist-title' ),
345 $params
346 );
347 if ( $this->isDeleted( $rc, Revision::DELETED_TEXT ) ) {
348 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
349 }
350 # To allow for boldening pages watched by this user
351 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
352 # RTL/LTR marker
353 $articlelink .= $this->getLanguage()->getDirMark();
354
355 wfRunHooks( 'ChangesListInsertArticleLink',
356 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
357
358 $s .= " $articlelink";
359 }
360
361 /**
362 * Get the timestamp from $rc formatted with current user's settings
363 * and a separator
364 *
365 * @param $rc RecentChange
366 * @return string HTML fragment
367 */
368 public function getTimestamp( $rc ) {
369 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
370 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
371 $this->getLanguage()->userTime(
372 $rc->mAttribs['rc_timestamp'],
373 $this->getUser()
374 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
375 }
376
377 /**
378 * Insert time timestamp string from $rc into $s
379 *
380 * @param string $s HTML to update
381 * @param $rc RecentChange
382 */
383 public function insertTimestamp( &$s, $rc ) {
384 $s .= $this->getTimestamp( $rc );
385 }
386
387 /**
388 * Insert links to user page, user talk page and eventually a blocking link
389 *
390 * @param &$s String HTML to update
391 * @param &$rc RecentChange
392 */
393 public function insertUserRelatedLinks( &$s, &$rc ) {
394 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
395 $s .= ' <span class="history-deleted">' .
396 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
397 } else {
398 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
399 $rc->mAttribs['rc_user_text'] );
400 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
401 }
402 }
403
404 /**
405 * Insert a formatted action
406 *
407 * @param $rc RecentChange
408 * @return string
409 */
410 public function insertLogEntry( $rc ) {
411 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
412 $formatter->setContext( $this->getContext() );
413 $formatter->setShowUserToolLinks( true );
414 $mark = $this->getLanguage()->getDirMark();
415
416 return $formatter->getActionText() . " $mark" . $formatter->getComment();
417 }
418
419 /**
420 * Insert a formatted comment
421 * @param $rc RecentChange
422 * @return string
423 */
424 public function insertComment( $rc ) {
425 if ( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
426 if ( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
427 return ' <span class="history-deleted">' .
428 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
429 } else {
430 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
431 }
432 }
433
434 return '';
435 }
436
437 /**
438 * Check whether to enable recent changes patrol features
439 *
440 * @deprecated since 1.22
441 * @return Boolean
442 */
443 public static function usePatrol() {
444 global $wgUser;
445
446 wfDeprecated( __METHOD__, '1.22' );
447
448 return $wgUser->useRCPatrol();
449 }
450
451 /**
452 * Returns the string which indicates the number of watching users
453 * @param int $count Number of user watching a page
454 * @return string
455 */
456 protected function numberofWatchingusers( $count ) {
457 static $cache = array();
458 if ( $count > 0 ) {
459 if ( !isset( $cache[$count] ) ) {
460 $cache[$count] = $this->msg( 'number_of_watching_users_RCview' )
461 ->numParams( $count )->escaped();
462 }
463
464 return $cache[$count];
465 } else {
466 return '';
467 }
468 }
469
470 /**
471 * Determine if said field of a revision is hidden
472 * @param RCCacheEntry|RecentChange $rc
473 * @param int $field One of DELETED_* bitfield constants
474 * @return bool
475 */
476 public static function isDeleted( $rc, $field ) {
477 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
478 }
479
480 /**
481 * Determine if the current user is allowed to view a particular
482 * field of this revision, if it's marked as deleted.
483 * @param RCCacheEntry|RecentChange $rc
484 * @param int $field
485 * @param User $user User object to check, or null to use $wgUser
486 * @return bool
487 */
488 public static function userCan( $rc, $field, User $user = null ) {
489 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
490 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
491 } else {
492 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
493 }
494 }
495
496 /**
497 * @param $link string
498 * @param $watched bool
499 * @return string
500 */
501 protected function maybeWatchedLink( $link, $watched = false ) {
502 if ( $watched ) {
503 return '<strong class="mw-watched">' . $link . '</strong>';
504 } else {
505 return '<span class="mw-rc-unwatched">' . $link . '</span>';
506 }
507 }
508
509 /** Inserts a rollback link
510 *
511 * @param $s string
512 * @param $rc RecentChange
513 */
514 public function insertRollback( &$s, &$rc ) {
515 if ( $rc->mAttribs['rc_type'] == RC_EDIT
516 && $rc->mAttribs['rc_this_oldid']
517 && $rc->mAttribs['rc_cur_id']
518 ) {
519 $page = $rc->getTitle();
520 /** Check for rollback and edit permissions, disallow special pages, and only
521 * show a link on the top-most revision */
522 if ( $this->getUser()->isAllowed( 'rollback' )
523 && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid']
524 ) {
525 $rev = new Revision( array(
526 'title' => $page,
527 'id' => $rc->mAttribs['rc_this_oldid'],
528 'user' => $rc->mAttribs['rc_user'],
529 'user_text' => $rc->mAttribs['rc_user_text'],
530 'deleted' => $rc->mAttribs['rc_deleted']
531 ) );
532 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
533 }
534 }
535 }
536
537 /**
538 * @param $s string
539 * @param $rc RecentChange
540 * @param $classes
541 */
542 public function insertTags( &$s, &$rc, &$classes ) {
543 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
544 return;
545 }
546
547 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
548 $rc->mAttribs['ts_tags'],
549 'changeslist'
550 );
551 $classes = array_merge( $classes, $newClasses );
552 $s .= ' ' . $tagSummary;
553 }
554
555 public function insertExtra( &$s, &$rc, &$classes ) {
556 // Empty, used for subclasses to add anything special.
557 }
558
559 protected function showAsUnpatrolled( RecentChange $rc ) {
560 return self::isUnpatrolled( $rc, $this->getUser() );
561 }
562
563 /**
564 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
565 * @param User $user
566 * @return bool
567 */
568 public static function isUnpatrolled( $rc, User $user ) {
569 if ( $rc instanceof RecentChange ) {
570 $isPatrolled = $rc->mAttribs['rc_patrolled'];
571 $rcType = $rc->mAttribs['rc_type'];
572 } else {
573 $isPatrolled = $rc->rc_patrolled;
574 $rcType = $rc->rc_type;
575 }
576
577 if ( !$isPatrolled ) {
578 if ( $user->useRCPatrol() ) {
579 return true;
580 }
581 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
582 return true;
583 }
584 }
585
586 return false;
587 }
588 }