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