Merge "Fix positioning of jQuery.tipsy tooltip arrows"
[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 /** @var BagOStuff */
40 protected $watchMsgCache;
41
42 /**
43 * Changeslist constructor
44 *
45 * @param Skin|IContextSource $obj
46 */
47 public function __construct( $obj ) {
48 if ( $obj instanceof IContextSource ) {
49 $this->setContext( $obj );
50 $this->skin = $obj->getSkin();
51 } else {
52 $this->setContext( $obj->getContext() );
53 $this->skin = $obj;
54 }
55 $this->preCacheMessages();
56 $this->watchMsgCache = new HashBagOStuff( array( 'maxKeys' => 50 ) );
57 }
58
59 /**
60 * Fetch an appropriate changes list class for the specified context
61 * Some users might want to use an enhanced list format, for instance
62 *
63 * @param IContextSource $context
64 * @return ChangesList
65 */
66 public static function newFromContext( IContextSource $context ) {
67 $user = $context->getUser();
68 $sk = $context->getSkin();
69 $list = null;
70 if ( Hooks::run( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
71 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
72
73 return $new ? new EnhancedChangesList( $context ) : new OldChangesList( $context );
74 } else {
75 return $list;
76 }
77 }
78
79 /**
80 * Format a line
81 *
82 * @since 1.27
83 *
84 * @param RecentChange $rc Passed by reference
85 * @param bool $watched (default false)
86 * @param int $linenumber (default null)
87 *
88 * @return string|bool
89 */
90 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
91 throw new RuntimeException( 'recentChangesLine should be implemented' );
92 }
93
94 /**
95 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
96 * @param bool $value
97 */
98 public function setWatchlistDivs( $value = true ) {
99 $this->watchlist = $value;
100 }
101
102 /**
103 * @return bool True when setWatchlistDivs has been called
104 * @since 1.23
105 */
106 public function isWatchlist() {
107 return (bool)$this->watchlist;
108 }
109
110 /**
111 * As we use the same small set of messages in various methods and that
112 * they are called often, we call them once and save them in $this->message
113 */
114 private function preCacheMessages() {
115 if ( !isset( $this->message ) ) {
116 foreach ( array(
117 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
118 'semicolon-separator', 'pipe-separator' ) as $msg
119 ) {
120 $this->message[$msg] = $this->msg( $msg )->escaped();
121 }
122 }
123 }
124
125 /**
126 * Returns the appropriate flags for new page, minor change and patrolling
127 * @param array $flags Associative array of 'flag' => Bool
128 * @param string $nothing To use for empty space
129 * @return string
130 */
131 public function recentChangesFlags( $flags, $nothing = '&#160;' ) {
132 $f = '';
133 foreach ( array_keys( $this->getConfig()->get( 'RecentChangesFlags' ) ) as $flag ) {
134 $f .= isset( $flags[$flag] ) && $flags[$flag]
135 ? self::flag( $flag )
136 : $nothing;
137 }
138
139 return $f;
140 }
141
142 /**
143 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
144 * namely the flag indicating a new page, a minor edit, a bot edit, or an
145 * unpatrolled edit. By default in English it will contain "N", "m", "b",
146 * "!" respectively, plus it will have an appropriate title and class.
147 *
148 * @param string $flag One key of $wgRecentChangesFlags
149 * @return string Raw HTML
150 */
151 public static function flag( $flag ) {
152 static $flagInfos = null;
153 if ( is_null( $flagInfos ) ) {
154 global $wgRecentChangesFlags;
155 $flagInfos = array();
156 foreach ( $wgRecentChangesFlags as $key => $value ) {
157 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
158 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
159 // Allow customized class name, fall back to flag name
160 $flagInfos[$key]['class'] = Sanitizer::escapeClass(
161 isset( $value['class'] ) ? $value['class'] : $key );
162 }
163 }
164
165 // Inconsistent naming, bleh, kepted for b/c
166 $map = array(
167 'minoredit' => 'minor',
168 'botedit' => 'bot',
169 );
170 if ( isset( $map[$flag] ) ) {
171 $flag = $map[$flag];
172 }
173
174 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
175 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
176 '</abbr>';
177 }
178
179 /**
180 * Returns text for the start of the tabular part of RC
181 * @return string
182 */
183 public function beginRecentChangesList() {
184 $this->rc_cache = array();
185 $this->rcMoveIndex = 0;
186 $this->rcCacheIndex = 0;
187 $this->lastdate = '';
188 $this->rclistOpen = false;
189 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
190
191 return '<div class="mw-changeslist">';
192 }
193
194 /**
195 * @param ResultWrapper|array $rows
196 */
197 public function initChangesListRows( $rows ) {
198 Hooks::run( 'ChangesListInitRows', array( $this, $rows ) );
199 }
200
201 /**
202 * Show formatted char difference
203 * @param int $old Number of bytes
204 * @param int $new Number of bytes
205 * @param IContextSource $context
206 * @return string
207 */
208 public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
209 if ( !$context ) {
210 $context = RequestContext::getMain();
211 }
212
213 $new = (int)$new;
214 $old = (int)$old;
215 $szdiff = $new - $old;
216
217 $lang = $context->getLanguage();
218 $config = $context->getConfig();
219 $code = $lang->getCode();
220 static $fastCharDiff = array();
221 if ( !isset( $fastCharDiff[$code] ) ) {
222 $fastCharDiff[$code] = $config->get( 'MiserMode' )
223 || $context->msg( 'rc-change-size' )->plain() === '$1';
224 }
225
226 $formattedSize = $lang->formatNum( $szdiff );
227
228 if ( !$fastCharDiff[$code] ) {
229 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
230 }
231
232 if ( abs( $szdiff ) > abs( $config->get( 'RCChangedSizeThreshold' ) ) ) {
233 $tag = 'strong';
234 } else {
235 $tag = 'span';
236 }
237
238 if ( $szdiff === 0 ) {
239 $formattedSizeClass = 'mw-plusminus-null';
240 } elseif ( $szdiff > 0 ) {
241 $formattedSize = '+' . $formattedSize;
242 $formattedSizeClass = 'mw-plusminus-pos';
243 } else {
244 $formattedSizeClass = 'mw-plusminus-neg';
245 }
246
247 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
248
249 return Html::element( $tag,
250 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
251 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
252 }
253
254 /**
255 * Format the character difference of one or several changes.
256 *
257 * @param RecentChange $old
258 * @param RecentChange $new Last change to use, if not provided, $old will be used
259 * @return string HTML fragment
260 */
261 public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
262 $oldlen = $old->mAttribs['rc_old_len'];
263
264 if ( $new ) {
265 $newlen = $new->mAttribs['rc_new_len'];
266 } else {
267 $newlen = $old->mAttribs['rc_new_len'];
268 }
269
270 if ( $oldlen === null || $newlen === null ) {
271 return '';
272 }
273
274 return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
275 }
276
277 /**
278 * Returns text for the end of RC
279 * @return string
280 */
281 public function endRecentChangesList() {
282 $out = $this->rclistOpen ? "</ul>\n" : '';
283 $out .= '</div>';
284
285 return $out;
286 }
287
288 /**
289 * @param string $s HTML to update
290 * @param mixed $rc_timestamp
291 */
292 public function insertDateHeader( &$s, $rc_timestamp ) {
293 # Make date header if necessary
294 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
295 if ( $date != $this->lastdate ) {
296 if ( $this->lastdate != '' ) {
297 $s .= "</ul>\n";
298 }
299 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
300 $this->lastdate = $date;
301 $this->rclistOpen = true;
302 }
303 }
304
305 /**
306 * @param string $s HTML to update
307 * @param Title $title
308 * @param string $logtype
309 */
310 public function insertLog( &$s, $title, $logtype ) {
311 $page = new LogPage( $logtype );
312 $logname = $page->getName()->escaped();
313 $s .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $title, $logname ) )->escaped();
314 }
315
316 /**
317 * @param string $s HTML to update
318 * @param RecentChange $rc
319 * @param bool $unpatrolled
320 */
321 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
322 # Diff link
323 if (
324 $rc->mAttribs['rc_type'] == RC_NEW ||
325 $rc->mAttribs['rc_type'] == RC_LOG ||
326 $rc->mAttribs['rc_type'] == RC_CATEGORIZE
327 ) {
328 $diffLink = $this->message['diff'];
329 } elseif ( !self::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
330 $diffLink = $this->message['diff'];
331 } else {
332 $query = array(
333 'curid' => $rc->mAttribs['rc_cur_id'],
334 'diff' => $rc->mAttribs['rc_this_oldid'],
335 'oldid' => $rc->mAttribs['rc_last_oldid']
336 );
337
338 $diffLink = Linker::linkKnown(
339 $rc->getTitle(),
340 $this->message['diff'],
341 array( 'tabindex' => $rc->counter ),
342 $query
343 );
344 }
345 if ( $rc->mAttribs['rc_type'] == RC_CATEGORIZE ) {
346 $diffhist = $diffLink . $this->message['pipe-separator'] . $this->message['hist'];
347 } else {
348 $diffhist = $diffLink . $this->message['pipe-separator'];
349 # History link
350 $diffhist .= Linker::linkKnown(
351 $rc->getTitle(),
352 $this->message['hist'],
353 array(),
354 array(
355 'curid' => $rc->mAttribs['rc_cur_id'],
356 'action' => 'history'
357 )
358 );
359 }
360
361 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
362 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
363 ' <span class="mw-changeslist-separator">. .</span> ';
364 }
365
366 /**
367 * @param string $s Article link will be appended to this string, in place.
368 * @param RecentChange $rc
369 * @param bool $unpatrolled
370 * @param bool $watched
371 * @deprecated since 1.27, use getArticleLink instead.
372 */
373 public function insertArticleLink( &$s, RecentChange $rc, $unpatrolled, $watched ) {
374 $s .= $this->getArticleLink( $rc, $unpatrolled, $watched );
375 }
376
377 /**
378 * @param RecentChange $rc
379 * @param bool $unpatrolled
380 * @param bool $watched
381 * @return string HTML
382 * @since 1.26
383 */
384 public function getArticleLink( &$rc, $unpatrolled, $watched ) {
385 $params = array();
386 if ( $rc->getTitle()->isRedirect() ) {
387 $params = array( 'redirect' => 'no' );
388 }
389
390 $articlelink = Linker::linkKnown(
391 $rc->getTitle(),
392 null,
393 array( 'class' => 'mw-changeslist-title' ),
394 $params
395 );
396 if ( $this->isDeleted( $rc, Revision::DELETED_TEXT ) ) {
397 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
398 }
399 # To allow for boldening pages watched by this user
400 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
401 # RTL/LTR marker
402 $articlelink .= $this->getLanguage()->getDirMark();
403
404 # TODO: Deprecate the $s argument, it seems happily unused.
405 $s = '';
406 Hooks::run( 'ChangesListInsertArticleLink',
407 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
408
409 return "{$s} {$articlelink}";
410 }
411
412 /**
413 * Get the timestamp from $rc formatted with current user's settings
414 * and a separator
415 *
416 * @param RecentChange $rc
417 * @return string HTML fragment
418 */
419 public function getTimestamp( $rc ) {
420 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
421 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
422 $this->getLanguage()->userTime(
423 $rc->mAttribs['rc_timestamp'],
424 $this->getUser()
425 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
426 }
427
428 /**
429 * Insert time timestamp string from $rc into $s
430 *
431 * @param string $s HTML to update
432 * @param RecentChange $rc
433 */
434 public function insertTimestamp( &$s, $rc ) {
435 $s .= $this->getTimestamp( $rc );
436 }
437
438 /**
439 * Insert links to user page, user talk page and eventually a blocking link
440 *
441 * @param string &$s HTML to update
442 * @param RecentChange &$rc
443 */
444 public function insertUserRelatedLinks( &$s, &$rc ) {
445 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
446 $s .= ' <span class="history-deleted">' .
447 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
448 } else {
449 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
450 $rc->mAttribs['rc_user_text'] );
451 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
452 }
453 }
454
455 /**
456 * Insert a formatted action
457 *
458 * @param RecentChange $rc
459 * @return string
460 */
461 public function insertLogEntry( $rc ) {
462 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
463 $formatter->setContext( $this->getContext() );
464 $formatter->setShowUserToolLinks( true );
465 $mark = $this->getLanguage()->getDirMark();
466
467 return $formatter->getActionText() . " $mark" . $formatter->getComment();
468 }
469
470 /**
471 * Insert a formatted comment
472 * @param RecentChange $rc
473 * @return string
474 */
475 public function insertComment( $rc ) {
476 if ( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
477 return ' <span class="history-deleted">' .
478 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
479 } else {
480 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
481 }
482 }
483
484 /**
485 * Check whether to enable recent changes patrol features
486 *
487 * @deprecated since 1.22
488 * @return bool
489 */
490 public static function usePatrol() {
491 global $wgUser;
492
493 wfDeprecated( __METHOD__, '1.22' );
494
495 return $wgUser->useRCPatrol();
496 }
497
498 /**
499 * Returns the string which indicates the number of watching users
500 * @param int $count Number of user watching a page
501 * @return string
502 */
503 protected function numberofWatchingusers( $count ) {
504 if ( $count <= 0 ) {
505 return '';
506 }
507 $cache = $this->watchMsgCache;
508 $that = $this;
509 return $cache->getWithSetCallback( $count, $cache::TTL_INDEFINITE,
510 function () use ( $that, $count ) {
511 return $that->msg( 'number_of_watching_users_RCview' )
512 ->numParams( $count )->escaped();
513 }
514 );
515 }
516
517 /**
518 * Determine if said field of a revision is hidden
519 * @param RCCacheEntry|RecentChange $rc
520 * @param int $field One of DELETED_* bitfield constants
521 * @return bool
522 */
523 public static function isDeleted( $rc, $field ) {
524 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
525 }
526
527 /**
528 * Determine if the current user is allowed to view a particular
529 * field of this revision, if it's marked as deleted.
530 * @param RCCacheEntry|RecentChange $rc
531 * @param int $field
532 * @param User $user User object to check, or null to use $wgUser
533 * @return bool
534 */
535 public static function userCan( $rc, $field, User $user = null ) {
536 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
537 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
538 } else {
539 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
540 }
541 }
542
543 /**
544 * @param string $link
545 * @param bool $watched
546 * @return string
547 */
548 protected function maybeWatchedLink( $link, $watched = false ) {
549 if ( $watched ) {
550 return '<strong class="mw-watched">' . $link . '</strong>';
551 } else {
552 return '<span class="mw-rc-unwatched">' . $link . '</span>';
553 }
554 }
555
556 /** Inserts a rollback link
557 *
558 * @param string $s
559 * @param RecentChange $rc
560 */
561 public function insertRollback( &$s, &$rc ) {
562 if ( $rc->mAttribs['rc_type'] == RC_EDIT
563 && $rc->mAttribs['rc_this_oldid']
564 && $rc->mAttribs['rc_cur_id']
565 ) {
566 $page = $rc->getTitle();
567 /** Check for rollback and edit permissions, disallow special pages, and only
568 * show a link on the top-most revision */
569 if ( $this->getUser()->isAllowed( 'rollback' )
570 && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid']
571 ) {
572 $rev = new Revision( array(
573 'title' => $page,
574 'id' => $rc->mAttribs['rc_this_oldid'],
575 'user' => $rc->mAttribs['rc_user'],
576 'user_text' => $rc->mAttribs['rc_user_text'],
577 'deleted' => $rc->mAttribs['rc_deleted']
578 ) );
579 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
580 }
581 }
582 }
583
584 /**
585 * @param RecentChange $rc
586 * @return string
587 * @since 1.26
588 */
589 public function getRollback( RecentChange $rc ) {
590 $s = '';
591 $this->insertRollback( $s, $rc );
592 return $s;
593 }
594
595 /**
596 * @param string $s
597 * @param RecentChange $rc
598 * @param array $classes
599 */
600 public function insertTags( &$s, &$rc, &$classes ) {
601 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
602 return;
603 }
604
605 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
606 $rc->mAttribs['ts_tags'],
607 'changeslist'
608 );
609 $classes = array_merge( $classes, $newClasses );
610 $s .= ' ' . $tagSummary;
611 }
612
613 /**
614 * @param RecentChange $rc
615 * @param array $classes
616 * @return string
617 * @since 1.26
618 */
619 public function getTags( RecentChange $rc, array &$classes ) {
620 $s = '';
621 $this->insertTags( $s, $rc, $classes );
622 return $s;
623 }
624
625 public function insertExtra( &$s, &$rc, &$classes ) {
626 // Empty, used for subclasses to add anything special.
627 }
628
629 protected function showAsUnpatrolled( RecentChange $rc ) {
630 return self::isUnpatrolled( $rc, $this->getUser() );
631 }
632
633 /**
634 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
635 * @param User $user
636 * @return bool
637 */
638 public static function isUnpatrolled( $rc, User $user ) {
639 if ( $rc instanceof RecentChange ) {
640 $isPatrolled = $rc->mAttribs['rc_patrolled'];
641 $rcType = $rc->mAttribs['rc_type'];
642 } else {
643 $isPatrolled = $rc->rc_patrolled;
644 $rcType = $rc->rc_type;
645 }
646
647 if ( !$isPatrolled ) {
648 if ( $user->useRCPatrol() ) {
649 return true;
650 }
651 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
652 return true;
653 }
654 }
655
656 return false;
657 }
658
659 /**
660 * Determines whether a revision is linked to this change; this may not be the case
661 * when the categorization wasn't done by an edit but a conditional parser function
662 *
663 * @since 1.27
664 *
665 * @param RecentChange|RCCacheEntry $rcObj
666 * @return bool
667 */
668 protected function isCategorizationWithoutRevision( $rcObj ) {
669 return intval( $rcObj->getAttribute( 'rc_type' ) ) === RC_CATEGORIZE
670 && intval( $rcObj->getAttribute( 'rc_this_oldid' ) ) === 0;
671 }
672
673 }