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