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