Merge "LoadBalancer::getLaggedSlaveMode needs to know about the wiki"
[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 * 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 ( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
324 $diffLink = $this->message['diff'];
325 } elseif ( !self::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
326 $diffLink = $this->message['diff'];
327 } else {
328 $query = array(
329 'curid' => $rc->mAttribs['rc_cur_id'],
330 'diff' => $rc->mAttribs['rc_this_oldid'],
331 'oldid' => $rc->mAttribs['rc_last_oldid']
332 );
333
334 $diffLink = Linker::linkKnown(
335 $rc->getTitle(),
336 $this->message['diff'],
337 array( 'tabindex' => $rc->counter ),
338 $query
339 );
340 }
341 $diffhist = $diffLink . $this->message['pipe-separator'];
342 # History link
343 $diffhist .= Linker::linkKnown(
344 $rc->getTitle(),
345 $this->message['hist'],
346 array(),
347 array(
348 'curid' => $rc->mAttribs['rc_cur_id'],
349 'action' => 'history'
350 )
351 );
352 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
353 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
354 ' <span class="mw-changeslist-separator">. .</span> ';
355 }
356
357 /**
358 * @param string $s HTML to update
359 * @param RecentChange $rc
360 * @param bool $unpatrolled
361 * @param bool $watched
362 */
363 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
364 $params = array();
365 if ( $rc->getTitle()->isRedirect() ) {
366 $params = array( 'redirect' => 'no' );
367 }
368
369 $articlelink = Linker::linkKnown(
370 $rc->getTitle(),
371 null,
372 array( 'class' => 'mw-changeslist-title' ),
373 $params
374 );
375 if ( $this->isDeleted( $rc, Revision::DELETED_TEXT ) ) {
376 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
377 }
378 # To allow for boldening pages watched by this user
379 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
380 # RTL/LTR marker
381 $articlelink .= $this->getLanguage()->getDirMark();
382
383 Hooks::run( 'ChangesListInsertArticleLink',
384 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
385
386 $s .= " $articlelink";
387 }
388
389 /**
390 * @param RecentChange $rc
391 * @param bool $unpatrolled
392 * @param bool $watched
393 * @return string
394 * @since 1.26
395 */
396 public function getArticleLink( RecentChange $rc, $unpatrolled, $watched ) {
397 $s = '';
398 $this->insertArticleLink( $s, $rc, $unpatrolled, $watched );
399 return $s;
400 }
401
402 /**
403 * Get the timestamp from $rc formatted with current user's settings
404 * and a separator
405 *
406 * @param RecentChange $rc
407 * @return string HTML fragment
408 */
409 public function getTimestamp( $rc ) {
410 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
411 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
412 $this->getLanguage()->userTime(
413 $rc->mAttribs['rc_timestamp'],
414 $this->getUser()
415 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
416 }
417
418 /**
419 * Insert time timestamp string from $rc into $s
420 *
421 * @param string $s HTML to update
422 * @param RecentChange $rc
423 */
424 public function insertTimestamp( &$s, $rc ) {
425 $s .= $this->getTimestamp( $rc );
426 }
427
428 /**
429 * Insert links to user page, user talk page and eventually a blocking link
430 *
431 * @param string &$s HTML to update
432 * @param RecentChange &$rc
433 */
434 public function insertUserRelatedLinks( &$s, &$rc ) {
435 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
436 $s .= ' <span class="history-deleted">' .
437 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
438 } else {
439 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
440 $rc->mAttribs['rc_user_text'] );
441 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
442 }
443 }
444
445 /**
446 * Insert a formatted action
447 *
448 * @param RecentChange $rc
449 * @return string
450 */
451 public function insertLogEntry( $rc ) {
452 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
453 $formatter->setContext( $this->getContext() );
454 $formatter->setShowUserToolLinks( true );
455 $mark = $this->getLanguage()->getDirMark();
456
457 return $formatter->getActionText() . " $mark" . $formatter->getComment();
458 }
459
460 /**
461 * Insert a formatted comment
462 * @param RecentChange $rc
463 * @return string
464 */
465 public function insertComment( $rc ) {
466 if ( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
467 return ' <span class="history-deleted">' .
468 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
469 } else {
470 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
471 }
472 }
473
474 /**
475 * Check whether to enable recent changes patrol features
476 *
477 * @deprecated since 1.22
478 * @return bool
479 */
480 public static function usePatrol() {
481 global $wgUser;
482
483 wfDeprecated( __METHOD__, '1.22' );
484
485 return $wgUser->useRCPatrol();
486 }
487
488 /**
489 * Returns the string which indicates the number of watching users
490 * @param int $count Number of user watching a page
491 * @return string
492 */
493 protected function numberofWatchingusers( $count ) {
494 $cache = $this->watchingCache;
495 if ( $count > 0 ) {
496 if ( !$cache->has( $count ) ) {
497 $cache->set( $count, $this->msg( 'number_of_watching_users_RCview' )
498 ->numParams( $count )->escaped() );
499 }
500
501 return $cache->get( $count );
502 } else {
503 return '';
504 }
505 }
506
507 /**
508 * Determine if said field of a revision is hidden
509 * @param RCCacheEntry|RecentChange $rc
510 * @param int $field One of DELETED_* bitfield constants
511 * @return bool
512 */
513 public static function isDeleted( $rc, $field ) {
514 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
515 }
516
517 /**
518 * Determine if the current user is allowed to view a particular
519 * field of this revision, if it's marked as deleted.
520 * @param RCCacheEntry|RecentChange $rc
521 * @param int $field
522 * @param User $user User object to check, or null to use $wgUser
523 * @return bool
524 */
525 public static function userCan( $rc, $field, User $user = null ) {
526 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
527 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
528 } else {
529 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
530 }
531 }
532
533 /**
534 * @param string $link
535 * @param bool $watched
536 * @return string
537 */
538 protected function maybeWatchedLink( $link, $watched = false ) {
539 if ( $watched ) {
540 return '<strong class="mw-watched">' . $link . '</strong>';
541 } else {
542 return '<span class="mw-rc-unwatched">' . $link . '</span>';
543 }
544 }
545
546 /** Inserts a rollback link
547 *
548 * @param string $s
549 * @param RecentChange $rc
550 */
551 public function insertRollback( &$s, &$rc ) {
552 if ( $rc->mAttribs['rc_type'] == RC_EDIT
553 && $rc->mAttribs['rc_this_oldid']
554 && $rc->mAttribs['rc_cur_id']
555 ) {
556 $page = $rc->getTitle();
557 /** Check for rollback and edit permissions, disallow special pages, and only
558 * show a link on the top-most revision */
559 if ( $this->getUser()->isAllowed( 'rollback' )
560 && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid']
561 ) {
562 $rev = new Revision( array(
563 'title' => $page,
564 'id' => $rc->mAttribs['rc_this_oldid'],
565 'user' => $rc->mAttribs['rc_user'],
566 'user_text' => $rc->mAttribs['rc_user_text'],
567 'deleted' => $rc->mAttribs['rc_deleted']
568 ) );
569 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
570 }
571 }
572 }
573
574 /**
575 * @param RecentChange $rc
576 * @return string
577 * @since 1.26
578 */
579 public function getRollback( RecentChange $rc ) {
580 $s = '';
581 $this->insertRollback( $s, $rc );
582 return $s;
583 }
584
585 /**
586 * @param string $s
587 * @param RecentChange $rc
588 * @param array $classes
589 */
590 public function insertTags( &$s, &$rc, &$classes ) {
591 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
592 return;
593 }
594
595 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
596 $rc->mAttribs['ts_tags'],
597 'changeslist'
598 );
599 $classes = array_merge( $classes, $newClasses );
600 $s .= ' ' . $tagSummary;
601 }
602
603 /**
604 * @param RecentChange $rc
605 * @param array $classes
606 * @return string
607 * @since 1.26
608 */
609 public function getTags( RecentChange $rc, array &$classes ) {
610 $s = '';
611 $this->insertTags( $s, $rc, $classes );
612 return $s;
613 }
614
615 public function insertExtra( &$s, &$rc, &$classes ) {
616 // Empty, used for subclasses to add anything special.
617 }
618
619 protected function showAsUnpatrolled( RecentChange $rc ) {
620 return self::isUnpatrolled( $rc, $this->getUser() );
621 }
622
623 /**
624 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
625 * @param User $user
626 * @return bool
627 */
628 public static function isUnpatrolled( $rc, User $user ) {
629 if ( $rc instanceof RecentChange ) {
630 $isPatrolled = $rc->mAttribs['rc_patrolled'];
631 $rcType = $rc->mAttribs['rc_type'];
632 } else {
633 $isPatrolled = $rc->rc_patrolled;
634 $rcType = $rc->rc_type;
635 }
636
637 if ( !$isPatrolled ) {
638 if ( $user->useRCPatrol() ) {
639 return true;
640 }
641 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
642 return true;
643 }
644 }
645
646 return false;
647 }
648 }