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