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