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