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