Merge "Add .mw-editsection-like class, behavior same as .mw-editsection"
[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 '';
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 if ( $this->rclistOpen ) {
250 return "</ul>\n";
251 } else {
252 return '';
253 }
254 }
255
256 /**
257 * @param string $s HTML to update
258 * @param $rc_timestamp mixed
259 */
260 public function insertDateHeader( &$s, $rc_timestamp ) {
261 # Make date header if necessary
262 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
263 if ( $date != $this->lastdate ) {
264 if ( $this->lastdate != '' ) {
265 $s .= "</ul>\n";
266 }
267 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
268 $this->lastdate = $date;
269 $this->rclistOpen = true;
270 }
271 }
272
273 /**
274 * @param string $s HTML to update
275 * @param $title Title
276 * @param $logtype string
277 */
278 public function insertLog( &$s, $title, $logtype ) {
279 $page = new LogPage( $logtype );
280 $logname = $page->getName()->escaped();
281 $s .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $title, $logname ) )->escaped();
282 }
283
284 /**
285 * @param string $s HTML to update
286 * @param $rc RecentChange
287 * @param $unpatrolled
288 */
289 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
290 # Diff link
291 if ( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
292 $diffLink = $this->message['diff'];
293 } elseif ( !self::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
294 $diffLink = $this->message['diff'];
295 } else {
296 $query = array(
297 'curid' => $rc->mAttribs['rc_cur_id'],
298 'diff' => $rc->mAttribs['rc_this_oldid'],
299 'oldid' => $rc->mAttribs['rc_last_oldid']
300 );
301
302 $diffLink = Linker::linkKnown(
303 $rc->getTitle(),
304 $this->message['diff'],
305 array( 'tabindex' => $rc->counter ),
306 $query
307 );
308 }
309 $diffhist = $diffLink . $this->message['pipe-separator'];
310 # History link
311 $diffhist .= Linker::linkKnown(
312 $rc->getTitle(),
313 $this->message['hist'],
314 array(),
315 array(
316 'curid' => $rc->mAttribs['rc_cur_id'],
317 'action' => 'history'
318 )
319 );
320 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
321 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
322 ' <span class="mw-changeslist-separator">. .</span> ';
323 }
324
325 /**
326 * @param string $s HTML to update
327 * @param $rc RecentChange
328 * @param $unpatrolled
329 * @param $watched
330 */
331 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
332 $params = array();
333
334 $articlelink = Linker::linkKnown(
335 $rc->getTitle(),
336 null,
337 array( 'class' => 'mw-changeslist-title' ),
338 $params
339 );
340 if ( $this->isDeleted( $rc, Revision::DELETED_TEXT ) ) {
341 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
342 }
343 # To allow for boldening pages watched by this user
344 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
345 # RTL/LTR marker
346 $articlelink .= $this->getLanguage()->getDirMark();
347
348 wfRunHooks( 'ChangesListInsertArticleLink',
349 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
350
351 $s .= " $articlelink";
352 }
353
354 /**
355 * Get the timestamp from $rc formatted with current user's settings
356 * and a separator
357 *
358 * @param $rc RecentChange
359 * @return string HTML fragment
360 */
361 public function getTimestamp( $rc ) {
362 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
363 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
364 $this->getLanguage()->userTime(
365 $rc->mAttribs['rc_timestamp'],
366 $this->getUser()
367 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
368 }
369
370 /**
371 * Insert time timestamp string from $rc into $s
372 *
373 * @param string $s HTML to update
374 * @param $rc RecentChange
375 */
376 public function insertTimestamp( &$s, $rc ) {
377 $s .= $this->getTimestamp( $rc );
378 }
379
380 /**
381 * Insert links to user page, user talk page and eventually a blocking link
382 *
383 * @param &$s String HTML to update
384 * @param &$rc RecentChange
385 */
386 public function insertUserRelatedLinks( &$s, &$rc ) {
387 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
388 $s .= ' <span class="history-deleted">' .
389 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
390 } else {
391 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
392 $rc->mAttribs['rc_user_text'] );
393 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
394 }
395 }
396
397 /**
398 * Insert a formatted action
399 *
400 * @param $rc RecentChange
401 * @return string
402 */
403 public function insertLogEntry( $rc ) {
404 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
405 $formatter->setContext( $this->getContext() );
406 $formatter->setShowUserToolLinks( true );
407 $mark = $this->getLanguage()->getDirMark();
408
409 return $formatter->getActionText() . " $mark" . $formatter->getComment();
410 }
411
412 /**
413 * Insert a formatted comment
414 * @param $rc RecentChange
415 * @return string
416 */
417 public function insertComment( $rc ) {
418 if ( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
419 if ( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
420 return ' <span class="history-deleted">' .
421 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
422 } else {
423 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
424 }
425 }
426
427 return '';
428 }
429
430 /**
431 * Check whether to enable recent changes patrol features
432 *
433 * @deprecated since 1.22
434 * @return Boolean
435 */
436 public static function usePatrol() {
437 global $wgUser;
438
439 wfDeprecated( __METHOD__, '1.22' );
440
441 return $wgUser->useRCPatrol();
442 }
443
444 /**
445 * Returns the string which indicates the number of watching users
446 * @param int $count Number of user watching a page
447 * @return string
448 */
449 protected function numberofWatchingusers( $count ) {
450 static $cache = array();
451 if ( $count > 0 ) {
452 if ( !isset( $cache[$count] ) ) {
453 $cache[$count] = $this->msg( 'number_of_watching_users_RCview' )
454 ->numParams( $count )->escaped();
455 }
456
457 return $cache[$count];
458 } else {
459 return '';
460 }
461 }
462
463 /**
464 * Determine if said field of a revision is hidden
465 * @param RCCacheEntry|RecentChange $rc
466 * @param int $field One of DELETED_* bitfield constants
467 * @return bool
468 */
469 public static function isDeleted( $rc, $field ) {
470 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
471 }
472
473 /**
474 * Determine if the current user is allowed to view a particular
475 * field of this revision, if it's marked as deleted.
476 * @param RCCacheEntry|RecentChange $rc
477 * @param int $field
478 * @param User $user User object to check, or null to use $wgUser
479 * @return bool
480 */
481 public static function userCan( $rc, $field, User $user = null ) {
482 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
483 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
484 } else {
485 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
486 }
487 }
488
489 /**
490 * @param $link string
491 * @param $watched bool
492 * @return string
493 */
494 protected function maybeWatchedLink( $link, $watched = false ) {
495 if ( $watched ) {
496 return '<strong class="mw-watched">' . $link . '</strong>';
497 } else {
498 return '<span class="mw-rc-unwatched">' . $link . '</span>';
499 }
500 }
501
502 /** Inserts a rollback link
503 *
504 * @param $s string
505 * @param $rc RecentChange
506 */
507 public function insertRollback( &$s, &$rc ) {
508 if ( $rc->mAttribs['rc_type'] == RC_EDIT
509 && $rc->mAttribs['rc_this_oldid']
510 && $rc->mAttribs['rc_cur_id']
511 ) {
512 $page = $rc->getTitle();
513 /** Check for rollback and edit permissions, disallow special pages, and only
514 * show a link on the top-most revision */
515 if ( $this->getUser()->isAllowed( 'rollback' )
516 && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid']
517 ) {
518 $rev = new Revision( array(
519 'title' => $page,
520 'id' => $rc->mAttribs['rc_this_oldid'],
521 'user' => $rc->mAttribs['rc_user'],
522 'user_text' => $rc->mAttribs['rc_user_text'],
523 'deleted' => $rc->mAttribs['rc_deleted']
524 ) );
525 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
526 }
527 }
528 }
529
530 /**
531 * @param $s string
532 * @param $rc RecentChange
533 * @param $classes
534 */
535 public function insertTags( &$s, &$rc, &$classes ) {
536 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
537 return;
538 }
539
540 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
541 $rc->mAttribs['ts_tags'],
542 'changeslist'
543 );
544 $classes = array_merge( $classes, $newClasses );
545 $s .= ' ' . $tagSummary;
546 }
547
548 public function insertExtra( &$s, &$rc, &$classes ) {
549 // Empty, used for subclasses to add anything special.
550 }
551
552 protected function showAsUnpatrolled( RecentChange $rc ) {
553 return self::isUnpatrolled( $rc, $this->getUser() );
554 }
555
556 /**
557 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
558 * @param User $user
559 * @return bool
560 */
561 public static function isUnpatrolled( $rc, User $user ) {
562 if ( $rc instanceof RecentChange ) {
563 $isPatrolled = $rc->mAttribs['rc_patrolled'];
564 $rcType = $rc->mAttribs['rc_type'];
565 } else {
566 $isPatrolled = $rc->rc_patrolled;
567 $rcType = $rc->rc_type;
568 }
569
570 if ( !$isPatrolled ) {
571 if ( $user->useRCPatrol() ) {
572 return true;
573 }
574 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
575 return true;
576 }
577 }
578
579 return false;
580 }
581 }