Merge "Adding python 3 support for the Makefile.py file for the zhtable"
[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 * As we use the same small set of messages in various methods and that
85 * they are called often, we call them once and save them in $this->message
86 */
87 private function preCacheMessages() {
88 if ( !isset( $this->message ) ) {
89 foreach ( array(
90 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
91 'semicolon-separator', 'pipe-separator' ) as $msg
92 ) {
93 $this->message[$msg] = $this->msg( $msg )->escaped();
94 }
95 }
96 }
97
98 /**
99 * Returns the appropriate flags for new page, minor change and patrolling
100 * @param array $flags Associative array of 'flag' => Bool
101 * @param string $nothing to use for empty space
102 * @return string
103 */
104 public function recentChangesFlags( $flags, $nothing = '&#160;' ) {
105 global $wgRecentChangesFlags;
106 $f = '';
107 foreach ( array_keys( $wgRecentChangesFlags ) as $flag ) {
108 $f .= isset( $flags[$flag] ) && $flags[$flag]
109 ? self::flag( $flag )
110 : $nothing;
111 }
112
113 return $f;
114 }
115
116 /**
117 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
118 * namely the flag indicating a new page, a minor edit, a bot edit, or an
119 * unpatrolled edit. By default in English it will contain "N", "m", "b",
120 * "!" respectively, plus it will have an appropriate title and class.
121 *
122 * @param string $flag One key of $wgRecentChangesFlags
123 * @return string Raw HTML
124 */
125 public static function flag( $flag ) {
126 static $flagInfos = null;
127 if ( is_null( $flagInfos ) ) {
128 global $wgRecentChangesFlags;
129 $flagInfos = array();
130 foreach ( $wgRecentChangesFlags as $key => $value ) {
131 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
132 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
133 // Allow customized class name, fall back to flag name
134 $flagInfos[$key]['class'] = Sanitizer::escapeClass(
135 isset( $value['class'] ) ? $value['class'] : $key );
136 }
137 }
138
139 // Inconsistent naming, bleh, kepted for b/c
140 $map = array(
141 'minoredit' => 'minor',
142 'botedit' => 'bot',
143 );
144 if ( isset( $map[$flag] ) ) {
145 $flag = $map[$flag];
146 }
147
148 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
149 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
150 '</abbr>';
151 }
152
153 /**
154 * Returns text for the start of the tabular part of RC
155 * @return string
156 */
157 public function beginRecentChangesList() {
158 $this->rc_cache = array();
159 $this->rcMoveIndex = 0;
160 $this->rcCacheIndex = 0;
161 $this->lastdate = '';
162 $this->rclistOpen = false;
163 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
164
165 return '<div class="mw-changeslist">';
166 }
167
168 /**
169 * Show formatted char difference
170 * @param int $old Number of bytes
171 * @param int $new Number of bytes
172 * @param IContextSource $context
173 * @return string
174 */
175 public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
176 global $wgRCChangedSizeThreshold, $wgMiserMode;
177
178 if ( !$context ) {
179 $context = RequestContext::getMain();
180 }
181
182 $new = (int)$new;
183 $old = (int)$old;
184 $szdiff = $new - $old;
185
186 $lang = $context->getLanguage();
187 $code = $lang->getCode();
188 static $fastCharDiff = array();
189 if ( !isset( $fastCharDiff[$code] ) ) {
190 $fastCharDiff[$code] = $wgMiserMode || $context->msg( 'rc-change-size' )->plain() === '$1';
191 }
192
193 $formattedSize = $lang->formatNum( $szdiff );
194
195 if ( !$fastCharDiff[$code] ) {
196 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
197 }
198
199 if ( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
200 $tag = 'strong';
201 } else {
202 $tag = 'span';
203 }
204
205 if ( $szdiff === 0 ) {
206 $formattedSizeClass = 'mw-plusminus-null';
207 } elseif ( $szdiff > 0 ) {
208 $formattedSize = '+' . $formattedSize;
209 $formattedSizeClass = 'mw-plusminus-pos';
210 } else {
211 $formattedSizeClass = 'mw-plusminus-neg';
212 }
213
214 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
215
216 return Html::element( $tag,
217 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
218 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
219 }
220
221 /**
222 * Format the character difference of one or several changes.
223 *
224 * @param $old RecentChange
225 * @param $new RecentChange last change to use, if not provided, $old will be used
226 * @return string HTML fragment
227 */
228 public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
229 $oldlen = $old->mAttribs['rc_old_len'];
230
231 if ( $new ) {
232 $newlen = $new->mAttribs['rc_new_len'];
233 } else {
234 $newlen = $old->mAttribs['rc_new_len'];
235 }
236
237 if ( $oldlen === null || $newlen === null ) {
238 return '';
239 }
240
241 return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
242 }
243
244 /**
245 * Returns text for the end of RC
246 * @return String
247 */
248 public function endRecentChangesList() {
249 $out = $this->rclistOpen ? "</ul>\n" : '';
250 $out .= '</div>';
251 return $out;
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 * @param int $count Number of user watching a page
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' )
452 ->numParams( $count )->escaped();
453 }
454
455 return $cache[$count];
456 } else {
457 return '';
458 }
459 }
460
461 /**
462 * Determine if said field of a revision is hidden
463 * @param RCCacheEntry|RecentChange $rc
464 * @param int $field One of DELETED_* bitfield constants
465 * @return bool
466 */
467 public static function isDeleted( $rc, $field ) {
468 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
469 }
470
471 /**
472 * Determine if the current user is allowed to view a particular
473 * field of this revision, if it's marked as deleted.
474 * @param RCCacheEntry|RecentChange $rc
475 * @param int $field
476 * @param User $user User object to check, or null to use $wgUser
477 * @return bool
478 */
479 public static function userCan( $rc, $field, User $user = null ) {
480 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
481 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
482 } else {
483 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
484 }
485 }
486
487 /**
488 * @param $link string
489 * @param $watched bool
490 * @return string
491 */
492 protected function maybeWatchedLink( $link, $watched = false ) {
493 if ( $watched ) {
494 return '<strong class="mw-watched">' . $link . '</strong>';
495 } else {
496 return '<span class="mw-rc-unwatched">' . $link . '</span>';
497 }
498 }
499
500 /** Inserts a rollback link
501 *
502 * @param $s string
503 * @param $rc RecentChange
504 */
505 public function insertRollback( &$s, &$rc ) {
506 if ( $rc->mAttribs['rc_type'] == RC_EDIT
507 && $rc->mAttribs['rc_this_oldid']
508 && $rc->mAttribs['rc_cur_id']
509 ) {
510 $page = $rc->getTitle();
511 /** Check for rollback and edit permissions, disallow special pages, and only
512 * show a link on the top-most revision */
513 if ( $this->getUser()->isAllowed( 'rollback' )
514 && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid']
515 ) {
516 $rev = new Revision( array(
517 'title' => $page,
518 'id' => $rc->mAttribs['rc_this_oldid'],
519 'user' => $rc->mAttribs['rc_user'],
520 'user_text' => $rc->mAttribs['rc_user_text'],
521 'deleted' => $rc->mAttribs['rc_deleted']
522 ) );
523 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
524 }
525 }
526 }
527
528 /**
529 * @param $s string
530 * @param $rc RecentChange
531 * @param $classes
532 */
533 public function insertTags( &$s, &$rc, &$classes ) {
534 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
535 return;
536 }
537
538 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
539 $rc->mAttribs['ts_tags'],
540 'changeslist'
541 );
542 $classes = array_merge( $classes, $newClasses );
543 $s .= ' ' . $tagSummary;
544 }
545
546 public function insertExtra( &$s, &$rc, &$classes ) {
547 // Empty, used for subclasses to add anything special.
548 }
549
550 protected function showAsUnpatrolled( RecentChange $rc ) {
551 return self::isUnpatrolled( $rc, $this->getUser() );
552 }
553
554 /**
555 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
556 * @param User $user
557 * @return bool
558 */
559 public static function isUnpatrolled( $rc, User $user ) {
560 if ( $rc instanceof RecentChange ) {
561 $isPatrolled = $rc->mAttribs['rc_patrolled'];
562 $rcType = $rc->mAttribs['rc_type'];
563 } else {
564 $isPatrolled = $rc->rc_patrolled;
565 $rcType = $rc->rc_type;
566 }
567
568 if ( !$isPatrolled ) {
569 if ( $user->useRCPatrol() ) {
570 return true;
571 }
572 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
573 return true;
574 }
575 }
576
577 return false;
578 }
579 }