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