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