Merge "Removed obsolete "containsOldMagic" code"
[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
308 $trClass = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
309 ? ' class="mw-enhanced-watched"' : '';
310
311 $r .= '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
312 $r .= $this->recentChangesFlags( array(
313 'newpage' => $type == RC_NEW,
314 'minor' => $rcObj->mAttribs['rc_minor'],
315 'unpatrolled' => $rcObj->unpatrolled,
316 'bot' => $rcObj->mAttribs['rc_bot'],
317 ) );
318 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
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
334 $link = Linker::linkKnown(
335 $rcObj->getTitle(),
336 $rcObj->timestamp,
337 array(),
338 $params
339 );
340 if ( $this->isDeleted( $rcObj, Revision::DELETED_TEXT ) ) {
341 $link = '<span class="history-deleted">' . $link . '</span> ';
342 }
343 }
344 $r .= $link . '</span>';
345
346 if ( !$type == RC_LOG || $type == RC_NEW ) {
347 $r .= ' ' . $this->msg( 'parentheses' )->rawParams(
348 $rcObj->curlink .
349 $this->message['pipe-separator'] .
350 $rcObj->lastlink
351 )->escaped();
352 }
353 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
354
355 # Character diff
356 if ( $RCShowChangedSize ) {
357 $cd = $this->formatCharacterDifference( $rcObj );
358 if ( $cd !== '' ) {
359 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
360 }
361 }
362
363 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
364 $r .= $this->insertLogEntry( $rcObj );
365 } else {
366 # User links
367 $r .= $rcObj->userlink;
368 $r .= $rcObj->usertalklink;
369 $r .= $this->insertComment( $rcObj );
370 }
371
372 # Rollback
373 $this->insertRollback( $r, $rcObj );
374 # Tags
375 $this->insertTags( $r, $rcObj, $classes );
376
377 $r .= "</td></tr>\n";
378 }
379 $r .= "</table>\n";
380
381 $this->rcCacheIndex++;
382
383 return $r;
384 }
385
386 /**
387 * Generates amount of changes (linking to diff ) & link to history.
388 *
389 * @param array $block
390 * @param array $queryParams
391 * @param bool $allLogs
392 * @param bool $isnew
393 * @param bool $namehidden
394 * @return string
395 */
396 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
397 # Changes message
398 static $nchanges = array();
399 static $sinceLastVisitMsg = array();
400
401 $n = count( $block );
402 if ( !isset( $nchanges[$n] ) ) {
403 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
404 }
405
406 $sinceLast = 0;
407 $unvisitedOldid = null;
408 /** @var $rcObj RCCacheEntry */
409 foreach ( $block as $rcObj ) {
410 // Same logic as below inside main foreach
411 if ( $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched ) {
412 $sinceLast++;
413 $unvisitedOldid = $rcObj->mAttribs['rc_last_oldid'];
414 }
415 }
416 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
417 $sinceLastVisitMsg[$sinceLast] =
418 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
419 }
420
421 $currentRevision = 0;
422 foreach ( $block as $rcObj ) {
423 if ( !$currentRevision ) {
424 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
425 }
426 }
427
428 # Total change link
429 $links = array();
430 /** @var $block0 RecentChange */
431 $block0 = $block[0];
432 $last = $block[count( $block ) - 1];
433 if ( !$allLogs ) {
434 if ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
435 $links['total-changes'] = $nchanges[$n];
436 } elseif ( $isnew ) {
437 $links['total-changes'] = $nchanges[$n];
438 } else {
439 $links['total-changes'] = Linker::link(
440 $block0->getTitle(),
441 $nchanges[$n],
442 array(),
443 $queryParams + array(
444 'diff' => $currentRevision,
445 'oldid' => $last->mAttribs['rc_last_oldid'],
446 ),
447 array( 'known', 'noclasses' )
448 );
449 if ( $sinceLast > 0 && $sinceLast < $n ) {
450 $links['total-changes-since-last'] = Linker::link(
451 $block0->getTitle(),
452 $sinceLastVisitMsg[$sinceLast],
453 array(),
454 $queryParams + array(
455 'diff' => $currentRevision,
456 'oldid' => $unvisitedOldid,
457 ),
458 array( 'known', 'noclasses' )
459 );
460 }
461 }
462 }
463
464 # History
465 if ( $allLogs ) {
466 // don't show history link for logs
467 } elseif ( $namehidden || !$block0->getTitle()->exists() ) {
468 $links['history'] = $this->message['enhancedrc-history'];
469 } else {
470 $params = $queryParams;
471 $params['action'] = 'history';
472
473 $links['history'] = Linker::linkKnown(
474 $block0->getTitle(),
475 $this->message['enhancedrc-history'],
476 array(),
477 $params
478 );
479 }
480
481 # Allow others to alter, remove or add to these links
482 Hooks::run( 'EnhancedChangesList::getLogText',
483 array( $this, &$links, $block ) );
484
485 if ( !$links ) {
486 return '';
487 }
488
489 $logtext = implode( $this->message['pipe-separator'], $links );
490 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
491 return ' ' . $logtext;
492 }
493
494 /**
495 * Enhanced RC ungrouped line.
496 *
497 * @param RecentChange|RCCacheEntry $rcObj
498 * @return string A HTML formatted line (generated using $r)
499 */
500 protected function recentChangesBlockLine( $rcObj ) {
501 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
502
503 $type = $rcObj->mAttribs['rc_type'];
504 $logType = $rcObj->mAttribs['rc_log_type'];
505 $classes = array( 'mw-enhanced-rc' );
506 if ( $logType ) {
507 # Log entry
508 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
509 } else {
510 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
511 $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
512 }
513 $classes[] = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
514 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
515 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
516 Html::openElement( 'tr' );
517
518 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
519 # Flag and Timestamp
520 $r .= $this->recentChangesFlags( array(
521 'newpage' => $type == RC_NEW,
522 'minor' => $rcObj->mAttribs['rc_minor'],
523 'unpatrolled' => $rcObj->unpatrolled,
524 'bot' => $rcObj->mAttribs['rc_bot'],
525 ) );
526 $r .= '&#160;' . $rcObj->timestamp . '&#160;</td><td>';
527 # Article or log link
528 if ( $logType ) {
529 $logPage = new LogPage( $logType );
530 $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
531 $logName = $logPage->getName()->escaped();
532 $r .= $this->msg( 'parentheses' )
533 ->rawParams( Linker::linkKnown( $logTitle, $logName ) )->escaped();
534 } else {
535 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
536 }
537 # Diff and hist links
538 if ( $type != RC_LOG ) {
539 $query['action'] = 'history';
540 $r .= ' ' . $this->msg( 'parentheses' )
541 ->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
542 $rcObj->getTitle(),
543 $this->message['hist'],
544 array(),
545 $query
546 ) )->escaped();
547 }
548 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
549 # Character diff
550 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
551 $cd = $this->formatCharacterDifference( $rcObj );
552 if ( $cd !== '' ) {
553 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
554 }
555 }
556
557 if ( $type == RC_LOG ) {
558 $r .= $this->insertLogEntry( $rcObj );
559 } else {
560 $r .= ' ' . $rcObj->userlink . $rcObj->usertalklink;
561 $r .= $this->insertComment( $rcObj );
562 $this->insertRollback( $r, $rcObj );
563 }
564
565 # Tags
566 $this->insertTags( $r, $rcObj, $classes );
567 # Show how many people are watching this if enabled
568 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
569
570 $r .= "</td></tr></table>\n";
571
572 return $r;
573 }
574
575 /**
576 * If enhanced RC is in use, this function takes the previously cached
577 * RC lines, arranges them, and outputs the HTML
578 *
579 * @return string
580 */
581 protected function recentChangesBlock() {
582 if ( count( $this->rc_cache ) == 0 ) {
583 return '';
584 }
585
586 $blockOut = '';
587 foreach ( $this->rc_cache as $block ) {
588 if ( count( $block ) < 2 ) {
589 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
590 } else {
591 $blockOut .= $this->recentChangesBlockGroup( $block );
592 }
593 }
594
595 return '<div>' . $blockOut . '</div>';
596 }
597
598 /**
599 * Returns text for the end of RC
600 * If enhanced RC is in use, returns pretty much all the text
601 * @return string
602 */
603 public function endRecentChangesList() {
604 return $this->recentChangesBlock() . '</div>';
605 }
606 }