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