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