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