Remove obsolete RC_MOVE and RC_MOVE_OVER_REDIRECT rc types
[lhc/web/wiklou.git] / includes / changes / EnhancedChangesList.php
1 <?php
2 /**
3 * Generates a list of changes using an Enhanced system (uses javascript).
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 class EnhancedChangesList extends ChangesList {
24
25 /**
26 * @var RCCacheEntryFactory
27 */
28 protected $cacheEntryFactory;
29
30 /**
31 * @var array Array of array of RCCacheEntry
32 */
33 protected $rc_cache;
34
35 /**
36 * @param IContextSource|Skin $obj
37 */
38 public function __construct( $obj ) {
39 if ( $obj instanceof Skin ) {
40 // @todo: deprecate constructing with Skin
41 $context = $obj->getContext();
42 } else {
43 if ( ! $obj instanceof IContextSource ) {
44 throw new MWException( 'EnhancedChangesList must be constructed with a '
45 . 'context source or skin.' );
46 }
47
48 $context = $obj;
49 }
50
51 parent::__construct( $context );
52
53 // message is set by the parent ChangesList class
54 $this->cacheEntryFactory = new RCCacheEntryFactory(
55 $context,
56 $this->message
57 );
58 }
59
60 /**
61 * Add the JavaScript file for enhanced changeslist
62 * @return string
63 */
64 public function beginRecentChangesList() {
65 $this->rc_cache = array();
66 $this->rcMoveIndex = 0;
67 $this->rcCacheIndex = 0;
68 $this->lastdate = '';
69 $this->rclistOpen = false;
70 $this->getOutput()->addModuleStyles( array(
71 'mediawiki.special.changeslist',
72 'mediawiki.special.changeslist.enhanced',
73 ) );
74 $this->getOutput()->addModules( array(
75 'jquery.makeCollapsible',
76 'mediawiki.icon',
77 ) );
78
79 return '<div class="mw-changeslist">';
80 }
81
82 /**
83 * Format a line for enhanced recentchange (aka with javascript and block of lines).
84 *
85 * @param RecentChange $baseRC
86 * @param bool $watched
87 *
88 * @return string
89 */
90 public function recentChangesLine( &$baseRC, $watched = false ) {
91 wfProfileIn( __METHOD__ );
92
93 $date = $this->getLanguage()->userDate(
94 $baseRC->mAttribs['rc_timestamp'],
95 $this->getUser()
96 );
97
98 $ret = '';
99
100 # If it's a new day, add the headline and flush the cache
101 if ( $date != $this->lastdate ) {
102 # Process current cache
103 $ret = $this->recentChangesBlock();
104 $this->rc_cache = array();
105 $ret .= Xml::element( 'h4', null, $date ) . "\n";
106 $this->lastdate = $date;
107 }
108
109 $cacheEntry = $this->cacheEntryFactory->newFromRecentChange( $baseRC, $watched );
110 $this->addCacheEntry( $cacheEntry );
111
112 wfProfileOut( __METHOD__ );
113
114 return $ret;
115 }
116
117 /**
118 * Put accumulated information into the cache, for later display.
119 * Page moves go on their own line.
120 *
121 * @param RCCacheEntry $cacheEntry
122 */
123 protected function addCacheEntry( RCCacheEntry $cacheEntry ) {
124 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
125
126 if ( !isset( $this->rc_cache[$cacheGroupingKey] ) ) {
127 $this->rc_cache[$cacheGroupingKey] = array();
128 }
129
130 array_push( $this->rc_cache[$cacheGroupingKey], $cacheEntry );
131 }
132
133 /**
134 * @todo use rc_source to group, if set; fallback to rc_type
135 *
136 * @param RCCacheEntry $cacheEntry
137 *
138 * @return string
139 */
140 protected function makeCacheGroupingKey( RCCacheEntry $cacheEntry ) {
141 $title = $cacheEntry->getTitle();
142 $cacheGroupingKey = $title->getPrefixedDBkey();
143
144 $type = $cacheEntry->mAttribs['rc_type'];
145
146 if ( $type == RC_LOG ) {
147 // Group by log type
148 $cacheGroupingKey = SpecialPage::getTitleFor(
149 'Log',
150 $cacheEntry->mAttribs['rc_log_type']
151 )->getPrefixedDBkey();
152 }
153
154 return $cacheGroupingKey;
155 }
156
157 /**
158 * Enhanced RC group
159 * @param RCCacheEntry[] $block
160 * @return string
161 */
162 protected function recentChangesBlockGroup( $block ) {
163 global $wgRCShowChangedSize;
164
165 wfProfileIn( __METHOD__ );
166
167 # Add the namespace and title of the block as part of the class
168 $classes = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' );
169 if ( $block[0]->mAttribs['rc_log_type'] ) {
170 # Log entry
171 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-'
172 . $block[0]->mAttribs['rc_log_type'] );
173 } else {
174 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns'
175 . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
176 }
177 $classes[] = $block[0]->watched && $block[0]->mAttribs['rc_timestamp'] >= $block[0]->watched
178 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
179 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
180 Html::openElement( 'tr' );
181
182 # Collate list of users
183 $userlinks = array();
184 # Other properties
185 $unpatrolled = false;
186 $isnew = false;
187 $allBots = true;
188 $allMinors = true;
189 $curId = $currentRevision = 0;
190 # Some catalyst variables...
191 $namehidden = true;
192 $allLogs = true;
193 $oldid = '';
194 foreach ( $block as $rcObj ) {
195 $oldid = $rcObj->mAttribs['rc_last_oldid'];
196 if ( $rcObj->mAttribs['rc_type'] == RC_NEW ) {
197 $isnew = true;
198 }
199 // If all log actions to this page were hidden, then don't
200 // give the name of the affected page for this block!
201 if ( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
202 $namehidden = false;
203 }
204 $u = $rcObj->userlink;
205 if ( !isset( $userlinks[$u] ) ) {
206 $userlinks[$u] = 0;
207 }
208 if ( $rcObj->unpatrolled ) {
209 $unpatrolled = true;
210 }
211 if ( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
212 $allLogs = false;
213 }
214 # Get the latest entry with a page_id and oldid
215 # since logs may not have these.
216 if ( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
217 $curId = $rcObj->mAttribs['rc_cur_id'];
218 }
219 if ( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
220 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
221 }
222
223 if ( !$rcObj->mAttribs['rc_bot'] ) {
224 $allBots = false;
225 }
226 if ( !$rcObj->mAttribs['rc_minor'] ) {
227 $allMinors = false;
228 }
229
230 $userlinks[$u]++;
231 }
232
233 # Sort the list and convert to text
234 krsort( $userlinks );
235 asort( $userlinks );
236 $users = array();
237 foreach ( $userlinks as $userlink => $count ) {
238 $text = $userlink;
239 $text .= $this->getLanguage()->getDirMark();
240 if ( $count > 1 ) {
241 // @todo FIXME: Hardcoded '×'. Should be a message.
242 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
243 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
244 }
245 array_push( $users, $text );
246 }
247
248 $users = ' <span class="changedby">'
249 . $this->msg( 'brackets' )->rawParams(
250 implode( $this->message['semicolon-separator'], $users )
251 )->escaped() . '</span>';
252
253 $tl = '<span class="mw-collapsible-toggle mw-collapsible-arrow ' .
254 'mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
255 $r .= "<td>$tl</td>";
256
257 # Main line
258 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
259 'newpage' => $isnew, # show, when one have this flag
260 'minor' => $allMinors, # show only, when all have this flag
261 'unpatrolled' => $unpatrolled, # show, when one have this flag
262 'bot' => $allBots, # show only, when all have this flag
263 ) );
264
265 # Timestamp
266 $r .= '&#160;' . $block[0]->timestamp . '&#160;</td><td>';
267
268 # Article link
269 if ( $namehidden ) {
270 $r .= ' <span class="history-deleted">' .
271 $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
272 } elseif ( $allLogs ) {
273 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
274 } else {
275 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
276 }
277
278 $r .= $this->getLanguage()->getDirMark();
279
280 $queryParams['curid'] = $curId;
281
282 # Changes message
283 static $nchanges = array();
284 static $sinceLastVisitMsg = array();
285
286 $n = count( $block );
287 if ( !isset( $nchanges[$n] ) ) {
288 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
289 }
290
291 $sinceLast = 0;
292 $unvisitedOldid = null;
293 /** @var $rcObj RCCacheEntry */
294 foreach ( $block as $rcObj ) {
295 // Same logic as below inside main foreach
296 if ( $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched ) {
297 $sinceLast++;
298 $unvisitedOldid = $rcObj->mAttribs['rc_last_oldid'];
299 }
300 }
301 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
302 $sinceLastVisitMsg[$sinceLast] =
303 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
304 }
305
306 # Total change link
307 $r .= ' ';
308 $logtext = '';
309 /** @var $block0 RecentChange */
310 $block0 = $block[0];
311 if ( !$allLogs ) {
312 if ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
313 $logtext .= $nchanges[$n];
314 } elseif ( $isnew ) {
315 $logtext .= $nchanges[$n];
316 } else {
317 $logtext .= Linker::link(
318 $block0->getTitle(),
319 $nchanges[$n],
320 array(),
321 $queryParams + array(
322 'diff' => $currentRevision,
323 'oldid' => $oldid,
324 ),
325 array( 'known', 'noclasses' )
326 );
327 if ( $sinceLast > 0 && $sinceLast < $n ) {
328 $logtext .= $this->message['pipe-separator'] . Linker::link(
329 $block0->getTitle(),
330 $sinceLastVisitMsg[$sinceLast],
331 array(),
332 $queryParams + array(
333 'diff' => $currentRevision,
334 'oldid' => $unvisitedOldid,
335 ),
336 array( 'known', 'noclasses' )
337 );
338 }
339 }
340 }
341
342 # History
343 if ( $allLogs ) {
344 // don't show history link for logs
345 } elseif ( $namehidden || !$block0->getTitle()->exists() ) {
346 $logtext .= $this->message['pipe-separator'] . $this->message['enhancedrc-history'];
347 } else {
348 $params = $queryParams;
349 $params['action'] = 'history';
350
351 $logtext .= $this->message['pipe-separator'] .
352 Linker::linkKnown(
353 $block0->getTitle(),
354 $this->message['enhancedrc-history'],
355 array(),
356 $params
357 );
358 }
359
360 if ( $logtext !== '' ) {
361 $r .= $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
362 }
363
364 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
365
366 # Character difference (does not apply if only log items)
367 if ( $wgRCShowChangedSize && !$allLogs ) {
368 $last = 0;
369 $first = count( $block ) - 1;
370 # Some events (like logs) have an "empty" size, so we need to skip those...
371 while ( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
372 $last++;
373 }
374 while ( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
375 $first--;
376 }
377 # Get net change
378 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
379
380 if ( $chardiff == '' ) {
381 $r .= ' ';
382 } else {
383 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
384 }
385 }
386
387 $r .= $users;
388 $r .= $this->numberofWatchingusers( $block0->numberofWatchingusers );
389 $r .= '</td></tr>';
390
391 # Sub-entries
392 foreach ( $block as $rcObj ) {
393 # Classes to apply -- TODO implement
394 $classes = array();
395 $type = $rcObj->mAttribs['rc_type'];
396
397 $trClass = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
398 ? ' class="mw-enhanced-watched"' : '';
399
400 $r .= '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
401 $r .= $this->recentChangesFlags( array(
402 'newpage' => $type == RC_NEW,
403 'minor' => $rcObj->mAttribs['rc_minor'],
404 'unpatrolled' => $rcObj->unpatrolled,
405 'bot' => $rcObj->mAttribs['rc_bot'],
406 ) );
407 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
408
409 $params = $queryParams;
410
411 if ( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
412 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
413 }
414
415 # Log timestamp
416 if ( $type == RC_LOG ) {
417 $link = $rcObj->timestamp;
418 # Revision link
419 } elseif ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
420 $link = '<span class="history-deleted">' . $rcObj->timestamp . '</span> ';
421 } else {
422
423 $link = Linker::linkKnown(
424 $rcObj->getTitle(),
425 $rcObj->timestamp,
426 array(),
427 $params
428 );
429 if ( $this->isDeleted( $rcObj, Revision::DELETED_TEXT ) ) {
430 $link = '<span class="history-deleted">' . $link . '</span> ';
431 }
432 }
433 $r .= $link . '</span>';
434
435 if ( !$type == RC_LOG || $type == RC_NEW ) {
436 $r .= ' ' . $this->msg( 'parentheses' )->rawParams(
437 $rcObj->curlink .
438 $this->message['pipe-separator'] .
439 $rcObj->lastlink
440 )->escaped();
441 }
442 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
443
444 # Character diff
445 if ( $wgRCShowChangedSize ) {
446 $cd = $this->formatCharacterDifference( $rcObj );
447 if ( $cd !== '' ) {
448 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
449 }
450 }
451
452 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
453 $r .= $this->insertLogEntry( $rcObj );
454 } else {
455 # User links
456 $r .= $rcObj->userlink;
457 $r .= $rcObj->usertalklink;
458 $r .= $this->insertComment( $rcObj );
459 }
460
461 # Rollback
462 $this->insertRollback( $r, $rcObj );
463 # Tags
464 $this->insertTags( $r, $rcObj, $classes );
465
466 $r .= "</td></tr>\n";
467 }
468 $r .= "</table>\n";
469
470 $this->rcCacheIndex++;
471
472 wfProfileOut( __METHOD__ );
473
474 return $r;
475 }
476
477 /**
478 * Generate HTML for an arrow or placeholder graphic
479 * @param string $dir One of '', 'd', 'l', 'r'
480 * @param string $alt
481 * @param string $title
482 * @return string HTML "<img>" tag
483 */
484 protected function arrow( $dir, $alt = '', $title = '' ) {
485 global $wgStylePath;
486 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
487 $encAlt = htmlspecialchars( $alt );
488 $encTitle = htmlspecialchars( $title );
489
490 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" title=\"$encTitle\" />";
491 }
492
493 /**
494 * Generate HTML for a right- or left-facing arrow,
495 * depending on language direction.
496 * @return string HTML "<img>" tag
497 */
498 protected function sideArrow() {
499 $dir = $this->getLanguage()->isRTL() ? 'l' : 'r';
500
501 return $this->arrow( $dir, '+', $this->msg( 'rc-enhanced-expand' )->text() );
502 }
503
504 /**
505 * Generate HTML for a down-facing arrow
506 * depending on language direction.
507 * @return string HTML "<img>" tag
508 */
509 protected function downArrow() {
510 return $this->arrow( 'd', '-', $this->msg( 'rc-enhanced-hide' )->text() );
511 }
512
513 /**
514 * Generate HTML for a spacer image
515 * @return string HTML "<img>" tag
516 */
517 protected function spacerArrow() {
518 return $this->arrow( '', codepointToUtf8( 0xa0 ) ); // non-breaking space
519 }
520
521 /**
522 * Enhanced RC ungrouped line.
523 *
524 * @param RecentChange|RCCacheEntry $rcObj
525 * @return string A HTML formatted line (generated using $r)
526 */
527 protected function recentChangesBlockLine( $rcObj ) {
528 global $wgRCShowChangedSize;
529
530 wfProfileIn( __METHOD__ );
531 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
532
533 $type = $rcObj->mAttribs['rc_type'];
534 $logType = $rcObj->mAttribs['rc_log_type'];
535 $classes = array( 'mw-enhanced-rc' );
536 if ( $logType ) {
537 # Log entry
538 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
539 } else {
540 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
541 $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
542 }
543 $classes[] = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
544 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
545 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
546 Html::openElement( 'tr' );
547
548 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
549 # Flag and Timestamp
550 $r .= $this->recentChangesFlags( array(
551 'newpage' => $type == RC_NEW,
552 'minor' => $rcObj->mAttribs['rc_minor'],
553 'unpatrolled' => $rcObj->unpatrolled,
554 'bot' => $rcObj->mAttribs['rc_bot'],
555 ) );
556 $r .= '&#160;' . $rcObj->timestamp . '&#160;</td><td>';
557 # Article or log link
558 if ( $logType ) {
559 $logPage = new LogPage( $logType );
560 $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
561 $logName = $logPage->getName()->escaped();
562 $r .= $this->msg( 'parentheses' )
563 ->rawParams( Linker::linkKnown( $logTitle, $logName ) )->escaped();
564 } else {
565 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
566 }
567 # Diff and hist links
568 if ( $type != RC_LOG ) {
569 $query['action'] = 'history';
570 $r .= ' ' . $this->msg( 'parentheses' )
571 ->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
572 $rcObj->getTitle(),
573 $this->message['hist'],
574 array(),
575 $query
576 ) )->escaped();
577 }
578 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
579 # Character diff
580 if ( $wgRCShowChangedSize ) {
581 $cd = $this->formatCharacterDifference( $rcObj );
582 if ( $cd !== '' ) {
583 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
584 }
585 }
586
587 if ( $type == RC_LOG ) {
588 $r .= $this->insertLogEntry( $rcObj );
589 } else {
590 $r .= ' ' . $rcObj->userlink . $rcObj->usertalklink;
591 $r .= $this->insertComment( $rcObj );
592 $this->insertRollback( $r, $rcObj );
593 }
594
595 # Tags
596 $this->insertTags( $r, $rcObj, $classes );
597 # Show how many people are watching this if enabled
598 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
599
600 $r .= "</td></tr></table>\n";
601
602 wfProfileOut( __METHOD__ );
603
604 return $r;
605 }
606
607 /**
608 * If enhanced RC is in use, this function takes the previously cached
609 * RC lines, arranges them, and outputs the HTML
610 *
611 * @return string
612 */
613 protected function recentChangesBlock() {
614 if ( count( $this->rc_cache ) == 0 ) {
615 return '';
616 }
617
618 wfProfileIn( __METHOD__ );
619
620 $blockOut = '';
621 foreach ( $this->rc_cache as $block ) {
622 if ( count( $block ) < 2 ) {
623 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
624 } else {
625 $blockOut .= $this->recentChangesBlockGroup( $block );
626 }
627 }
628
629 wfProfileOut( __METHOD__ );
630
631 return '<div>' . $blockOut . '</div>';
632 }
633
634 /**
635 * Returns text for the end of RC
636 * If enhanced RC is in use, returns pretty much all the text
637 * @return string
638 */
639 public function endRecentChangesList() {
640 return $this->recentChangesBlock() . '</div>';
641 }
642 }