Drop zh-tw message "saveprefs"
[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 $success = Hooks::run( 'EnhancedChangesListModifyLineData',
382 array( $this, &$data, $block, $rcObj ) );
383 if ( !$success ) {
384 // skip entry if hook aborted it
385 continue;
386 }
387
388 $line = '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
389 if ( isset( $data['recentChangesFlags'] ) ) {
390 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
391 unset( $data['recentChangesFlags'] );
392 }
393 $line .= '&#160;</td><td class="mw-enhanced-rc-nested">';
394
395 if ( isset( $data['timestampLink'] ) ) {
396 $line .= '<span class="mw-enhanced-rc-time">' . $data['timestampLink'] . '</span>';
397 unset( $data['timestampLink'] );
398 }
399
400 // everything else: makes it easier for extensions to add or remove data
401 $line .= implode( '', $data );
402
403 $line .= "</td></tr>\n";
404
405 $r .= $line;
406 }
407 $r .= "</table>\n";
408
409 $this->rcCacheIndex++;
410
411 return $r;
412 }
413
414 /**
415 * Generates amount of changes (linking to diff ) & link to history.
416 *
417 * @param array $block
418 * @param array $queryParams
419 * @param bool $allLogs
420 * @param bool $isnew
421 * @param bool $namehidden
422 * @return string
423 */
424 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
425 # Changes message
426 static $nchanges = array();
427 static $sinceLastVisitMsg = array();
428
429 $n = count( $block );
430 if ( !isset( $nchanges[$n] ) ) {
431 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
432 }
433
434 $sinceLast = 0;
435 $unvisitedOldid = null;
436 /** @var $rcObj RCCacheEntry */
437 foreach ( $block as $rcObj ) {
438 // Same logic as below inside main foreach
439 if ( $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched ) {
440 $sinceLast++;
441 $unvisitedOldid = $rcObj->mAttribs['rc_last_oldid'];
442 }
443 }
444 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
445 $sinceLastVisitMsg[$sinceLast] =
446 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
447 }
448
449 $currentRevision = 0;
450 foreach ( $block as $rcObj ) {
451 if ( !$currentRevision ) {
452 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
453 }
454 }
455
456 # Total change link
457 $links = array();
458 /** @var $block0 RecentChange */
459 $block0 = $block[0];
460 $last = $block[count( $block ) - 1];
461 if ( !$allLogs ) {
462 if ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
463 $links['total-changes'] = $nchanges[$n];
464 } elseif ( $isnew ) {
465 $links['total-changes'] = $nchanges[$n];
466 } else {
467 $links['total-changes'] = Linker::link(
468 $block0->getTitle(),
469 $nchanges[$n],
470 array(),
471 $queryParams + array(
472 'diff' => $currentRevision,
473 'oldid' => $last->mAttribs['rc_last_oldid'],
474 ),
475 array( 'known', 'noclasses' )
476 );
477 if ( $sinceLast > 0 && $sinceLast < $n ) {
478 $links['total-changes-since-last'] = Linker::link(
479 $block0->getTitle(),
480 $sinceLastVisitMsg[$sinceLast],
481 array(),
482 $queryParams + array(
483 'diff' => $currentRevision,
484 'oldid' => $unvisitedOldid,
485 ),
486 array( 'known', 'noclasses' )
487 );
488 }
489 }
490 }
491
492 # History
493 if ( $allLogs ) {
494 // don't show history link for logs
495 } elseif ( $namehidden || !$block0->getTitle()->exists() ) {
496 $links['history'] = $this->message['enhancedrc-history'];
497 } else {
498 $params = $queryParams;
499 $params['action'] = 'history';
500
501 $links['history'] = Linker::linkKnown(
502 $block0->getTitle(),
503 $this->message['enhancedrc-history'],
504 array(),
505 $params
506 );
507 }
508
509 # Allow others to alter, remove or add to these links
510 Hooks::run( 'EnhancedChangesList::getLogText',
511 array( $this, &$links, $block ) );
512
513 if ( !$links ) {
514 return '';
515 }
516
517 $logtext = implode( $this->message['pipe-separator'], $links );
518 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
519 return ' ' . $logtext;
520 }
521
522 /**
523 * Enhanced RC ungrouped line.
524 *
525 * @param RecentChange|RCCacheEntry $rcObj
526 * @return string A HTML formatted line (generated using $r)
527 */
528 protected function recentChangesBlockLine( $rcObj ) {
529 $data = array();
530
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
546 # Flag and Timestamp
547 $data['recentChangesFlags'] = array(
548 'newpage' => $type == RC_NEW,
549 'minor' => $rcObj->mAttribs['rc_minor'],
550 'unpatrolled' => $rcObj->unpatrolled,
551 'bot' => $rcObj->mAttribs['rc_bot'],
552 );
553 // timestamp is not really a link here, but is called timestampLink
554 // for consistency with EnhancedChangesListModifyLineData
555 $data['timestampLink'] = $rcObj->timestamp;
556
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 $data['logLink'] = $this->msg( 'parentheses' )
563 ->rawParams( Linker::linkKnown( $logTitle, $logName ) )->escaped();
564 } else {
565 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled, $rcObj->watched );
566 }
567
568 # Diff and hist links
569 if ( $type != RC_LOG ) {
570 $query['action'] = 'history';
571 $data['historyLink'] = ' ' . $this->msg( 'parentheses' )
572 ->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
573 $rcObj->getTitle(),
574 $this->message['hist'],
575 array(),
576 $query
577 ) )->escaped();
578 }
579 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
580
581 # Character diff
582 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
583 $cd = $this->formatCharacterDifference( $rcObj );
584 if ( $cd !== '' ) {
585 $data['characterDiff'] = $cd;
586 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
587 }
588 }
589
590 if ( $type == RC_LOG ) {
591 $data['logEntry'] = $this->insertLogEntry( $rcObj );
592 } else {
593 $data['userLink'] = $rcObj->userlink;
594 $data['userTalkLink'] = $rcObj->usertalklink;
595 $data['comment'] = $this->insertComment( $rcObj );
596 $data['rollback'] = $this->getRollback( $rcObj );
597 }
598
599 # Tags
600 $data['tags'] = $this->getTags( $rcObj, $classes );
601
602 # Show how many people are watching this if enabled
603 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
604
605 // give the hook a chance to modify the data
606 $success = Hooks::run( 'EnhancedChangesListModifyBlockLineData',
607 array( $this, &$data, $rcObj ) );
608 if ( !$success ) {
609 // skip entry if hook aborted it
610 return '';
611 }
612
613 $line = Html::openElement( 'table', array( 'class' => $classes ) ) .
614 Html::openElement( 'tr' );
615 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
616
617 if ( isset( $data['recentChangesFlags'] ) ) {
618 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
619 unset( $data['recentChangesFlags'] );
620 }
621
622 if ( isset( $data['timestampLink'] ) ) {
623 $line .= '&#160;' . $data['timestampLink'];
624 unset( $data['timestampLink'] );
625 }
626 $line .= '&#160;</td><td>';
627
628 // everything else: makes it easier for extensions to add or remove data
629 $line .= implode( '', $data );
630
631 $line .= "</td></tr></table>\n";
632
633 return $line;
634 }
635
636 /**
637 * If enhanced RC is in use, this function takes the previously cached
638 * RC lines, arranges them, and outputs the HTML
639 *
640 * @return string
641 */
642 protected function recentChangesBlock() {
643 if ( count( $this->rc_cache ) == 0 ) {
644 return '';
645 }
646
647 $blockOut = '';
648 foreach ( $this->rc_cache as $block ) {
649 if ( count( $block ) < 2 ) {
650 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
651 } else {
652 $blockOut .= $this->recentChangesBlockGroup( $block );
653 }
654 }
655
656 return '<div>' . $blockOut . '</div>';
657 }
658
659 /**
660 * Returns text for the end of RC
661 * If enhanced RC is in use, returns pretty much all the text
662 * @return string
663 */
664 public function endRecentChangesList() {
665 return $this->recentChangesBlock() . '</div>';
666 }
667 }