Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / changes / EnhancedChangesList.php
blob2cee72630827d1454df0beb8fd32a8f11c920197
1 <?php
2 /**
3 * Generates a list of changes using an Enhanced system (uses javascript).
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.
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.
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
20 * @file
23 class EnhancedChangesList extends ChangesList {
25 /**
26 * @var RCCacheEntryFactory
28 protected $cacheEntryFactory;
30 /**
31 * @var array Array of array of RCCacheEntry
33 protected $rc_cache;
35 /**
36 * @param IContextSource|Skin $obj
37 * @throws MWException
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.' );
49 $context = $obj;
52 parent::__construct( $context );
54 // message is set by the parent ChangesList class
55 $this->cacheEntryFactory = new RCCacheEntryFactory(
56 $context,
57 $this->message
61 /**
62 * Add the JavaScript file for enhanced changeslist
63 * @return string
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 ) );
80 return '<div class="mw-changeslist">';
83 /**
84 * Format a line for enhanced recentchange (aka with javascript and block of lines).
86 * @param RecentChange $baseRC
87 * @param bool $watched
89 * @return string
91 public function recentChangesLine( &$baseRC, $watched = false ) {
93 $date = $this->getLanguage()->userDate(
94 $baseRC->mAttribs['rc_timestamp'],
95 $this->getUser()
98 $ret = '';
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;
109 $cacheEntry = $this->cacheEntryFactory->newFromRecentChange( $baseRC, $watched );
110 $this->addCacheEntry( $cacheEntry );
112 return $ret;
116 * Put accumulated information into the cache, for later display.
117 * Page moves go on their own line.
119 * @param RCCacheEntry $cacheEntry
121 protected function addCacheEntry( RCCacheEntry $cacheEntry ) {
122 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
124 if ( !isset( $this->rc_cache[$cacheGroupingKey] ) ) {
125 $this->rc_cache[$cacheGroupingKey] = array();
128 array_push( $this->rc_cache[$cacheGroupingKey], $cacheEntry );
132 * @todo use rc_source to group, if set; fallback to rc_type
134 * @param RCCacheEntry $cacheEntry
136 * @return string
138 protected function makeCacheGroupingKey( RCCacheEntry $cacheEntry ) {
139 $title = $cacheEntry->getTitle();
140 $cacheGroupingKey = $title->getPrefixedDBkey();
142 $type = $cacheEntry->mAttribs['rc_type'];
144 if ( $type == RC_LOG ) {
145 // Group by log type
146 $cacheGroupingKey = SpecialPage::getTitleFor(
147 'Log',
148 $cacheEntry->mAttribs['rc_log_type']
149 )->getPrefixedDBkey();
152 return $cacheGroupingKey;
156 * Enhanced RC group
157 * @param RCCacheEntry[] $block
158 * @return string
160 protected function recentChangesBlockGroup( $block ) {
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'] );
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' );
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 = $currentRevision = 0;
185 # Some catalyst variables...
186 $namehidden = true;
187 $allLogs = true;
188 $oldid = '';
189 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
190 foreach ( $block as $rcObj ) {
191 $oldid = $rcObj->mAttribs['rc_last_oldid'];
192 if ( $rcObj->mAttribs['rc_type'] == RC_NEW ) {
193 $isnew = true;
195 // If all log actions to this page were hidden, then don't
196 // give the name of the affected page for this block!
197 if ( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
198 $namehidden = false;
200 $u = $rcObj->userlink;
201 if ( !isset( $userlinks[$u] ) ) {
202 $userlinks[$u] = 0;
204 if ( $rcObj->unpatrolled ) {
205 $unpatrolled = true;
207 if ( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
208 $allLogs = false;
210 # Get the latest entry with a page_id and oldid
211 # since logs may not have these.
212 if ( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
213 $curId = $rcObj->mAttribs['rc_cur_id'];
215 if ( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
216 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
219 if ( !$rcObj->mAttribs['rc_bot'] ) {
220 $allBots = false;
222 if ( !$rcObj->mAttribs['rc_minor'] ) {
223 $allMinors = false;
226 $userlinks[$u]++;
229 # Sort the list and convert to text
230 krsort( $userlinks );
231 asort( $userlinks );
232 $users = array();
233 foreach ( $userlinks as $userlink => $count ) {
234 $text = $userlink;
235 $text .= $this->getLanguage()->getDirMark();
236 if ( $count > 1 ) {
237 // @todo FIXME: Hardcoded '×'. Should be a message.
238 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
239 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
241 array_push( $users, $text );
244 $users = ' <span class="changedby">'
245 . $this->msg( 'brackets' )->rawParams(
246 implode( $this->message['semicolon-separator'], $users )
247 )->escaped() . '</span>';
249 $tl = '<span class="mw-collapsible-toggle mw-collapsible-arrow ' .
250 'mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
251 $r .= "<td>$tl</td>";
253 # Main line
254 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
255 'newpage' => $isnew, # show, when one have this flag
256 'minor' => $allMinors, # show only, when all have this flag
257 'unpatrolled' => $unpatrolled, # show, when one have this flag
258 'bot' => $allBots, # show only, when all have this flag
259 ) );
261 # Timestamp
262 $r .= '&#160;' . $block[0]->timestamp . '&#160;</td><td>';
264 # Article link
265 if ( $namehidden ) {
266 $r .= ' <span class="history-deleted">' .
267 $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
268 } elseif ( $allLogs ) {
269 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
270 } else {
271 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
274 $r .= $this->getLanguage()->getDirMark();
276 $queryParams['curid'] = $curId;
278 # Changes message
279 static $nchanges = array();
280 static $sinceLastVisitMsg = array();
282 $n = count( $block );
283 if ( !isset( $nchanges[$n] ) ) {
284 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
287 $sinceLast = 0;
288 $unvisitedOldid = null;
289 /** @var $rcObj RCCacheEntry */
290 foreach ( $block as $rcObj ) {
291 // Same logic as below inside main foreach
292 if ( $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched ) {
293 $sinceLast++;
294 $unvisitedOldid = $rcObj->mAttribs['rc_last_oldid'];
297 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
298 $sinceLastVisitMsg[$sinceLast] =
299 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
302 # Total change link
303 $r .= ' ';
304 $logtext = '';
305 /** @var $block0 RecentChange */
306 $block0 = $block[0];
307 if ( !$allLogs ) {
308 if ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
309 $logtext .= $nchanges[$n];
310 } elseif ( $isnew ) {
311 $logtext .= $nchanges[$n];
312 } else {
313 $logtext .= Linker::link(
314 $block0->getTitle(),
315 $nchanges[$n],
316 array(),
317 $queryParams + array(
318 'diff' => $currentRevision,
319 'oldid' => $oldid,
321 array( 'known', 'noclasses' )
323 if ( $sinceLast > 0 && $sinceLast < $n ) {
324 $logtext .= $this->message['pipe-separator'] . Linker::link(
325 $block0->getTitle(),
326 $sinceLastVisitMsg[$sinceLast],
327 array(),
328 $queryParams + array(
329 'diff' => $currentRevision,
330 'oldid' => $unvisitedOldid,
332 array( 'known', 'noclasses' )
338 # History
339 if ( $allLogs ) {
340 // don't show history link for logs
341 } elseif ( $namehidden || !$block0->getTitle()->exists() ) {
342 $logtext .= $this->message['pipe-separator'] . $this->message['enhancedrc-history'];
343 } else {
344 $params = $queryParams;
345 $params['action'] = 'history';
347 $logtext .= $this->message['pipe-separator'] .
348 Linker::linkKnown(
349 $block0->getTitle(),
350 $this->message['enhancedrc-history'],
351 array(),
352 $params
356 if ( $logtext !== '' ) {
357 $r .= $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
360 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
362 # Character difference (does not apply if only log items)
363 if ( $RCShowChangedSize && !$allLogs ) {
364 $last = 0;
365 $first = count( $block ) - 1;
366 # Some events (like logs) have an "empty" size, so we need to skip those...
367 while ( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
368 $last++;
370 while ( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
371 $first--;
373 # Get net change
374 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
376 if ( $chardiff == '' ) {
377 $r .= ' ';
378 } else {
379 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
383 $r .= $users;
384 $r .= $this->numberofWatchingusers( $block0->numberofWatchingusers );
385 $r .= '</td></tr>';
387 # Sub-entries
388 foreach ( $block as $rcObj ) {
389 # Classes to apply -- TODO implement
390 $classes = array();
391 $type = $rcObj->mAttribs['rc_type'];
393 $trClass = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
394 ? ' class="mw-enhanced-watched"' : '';
396 $r .= '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
397 $r .= $this->recentChangesFlags( array(
398 'newpage' => $type == RC_NEW,
399 'minor' => $rcObj->mAttribs['rc_minor'],
400 'unpatrolled' => $rcObj->unpatrolled,
401 'bot' => $rcObj->mAttribs['rc_bot'],
402 ) );
403 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
405 $params = $queryParams;
407 if ( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
408 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
411 # Log timestamp
412 if ( $type == RC_LOG ) {
413 $link = $rcObj->timestamp;
414 # Revision link
415 } elseif ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
416 $link = '<span class="history-deleted">' . $rcObj->timestamp . '</span> ';
417 } else {
419 $link = Linker::linkKnown(
420 $rcObj->getTitle(),
421 $rcObj->timestamp,
422 array(),
423 $params
425 if ( $this->isDeleted( $rcObj, Revision::DELETED_TEXT ) ) {
426 $link = '<span class="history-deleted">' . $link . '</span> ';
429 $r .= $link . '</span>';
431 if ( !$type == RC_LOG || $type == RC_NEW ) {
432 $r .= ' ' . $this->msg( 'parentheses' )->rawParams(
433 $rcObj->curlink .
434 $this->message['pipe-separator'] .
435 $rcObj->lastlink
436 )->escaped();
438 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
440 # Character diff
441 if ( $RCShowChangedSize ) {
442 $cd = $this->formatCharacterDifference( $rcObj );
443 if ( $cd !== '' ) {
444 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
448 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
449 $r .= $this->insertLogEntry( $rcObj );
450 } else {
451 # User links
452 $r .= $rcObj->userlink;
453 $r .= $rcObj->usertalklink;
454 $r .= $this->insertComment( $rcObj );
457 # Rollback
458 $this->insertRollback( $r, $rcObj );
459 # Tags
460 $this->insertTags( $r, $rcObj, $classes );
462 $r .= "</td></tr>\n";
464 $r .= "</table>\n";
466 $this->rcCacheIndex++;
468 return $r;
472 * Enhanced RC ungrouped line.
474 * @param RecentChange|RCCacheEntry $rcObj
475 * @return string A HTML formatted line (generated using $r)
477 protected function recentChangesBlockLine( $rcObj ) {
478 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
480 $type = $rcObj->mAttribs['rc_type'];
481 $logType = $rcObj->mAttribs['rc_log_type'];
482 $classes = array( 'mw-enhanced-rc' );
483 if ( $logType ) {
484 # Log entry
485 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
486 } else {
487 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
488 $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
490 $classes[] = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
491 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
492 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
493 Html::openElement( 'tr' );
495 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
496 # Flag and Timestamp
497 $r .= $this->recentChangesFlags( array(
498 'newpage' => $type == RC_NEW,
499 'minor' => $rcObj->mAttribs['rc_minor'],
500 'unpatrolled' => $rcObj->unpatrolled,
501 'bot' => $rcObj->mAttribs['rc_bot'],
502 ) );
503 $r .= '&#160;' . $rcObj->timestamp . '&#160;</td><td>';
504 # Article or log link
505 if ( $logType ) {
506 $logPage = new LogPage( $logType );
507 $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
508 $logName = $logPage->getName()->escaped();
509 $r .= $this->msg( 'parentheses' )
510 ->rawParams( Linker::linkKnown( $logTitle, $logName ) )->escaped();
511 } else {
512 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
514 # Diff and hist links
515 if ( $type != RC_LOG ) {
516 $query['action'] = 'history';
517 $r .= ' ' . $this->msg( 'parentheses' )
518 ->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
519 $rcObj->getTitle(),
520 $this->message['hist'],
521 array(),
522 $query
523 ) )->escaped();
525 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
526 # Character diff
527 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
528 $cd = $this->formatCharacterDifference( $rcObj );
529 if ( $cd !== '' ) {
530 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
534 if ( $type == RC_LOG ) {
535 $r .= $this->insertLogEntry( $rcObj );
536 } else {
537 $r .= ' ' . $rcObj->userlink . $rcObj->usertalklink;
538 $r .= $this->insertComment( $rcObj );
539 $this->insertRollback( $r, $rcObj );
542 # Tags
543 $this->insertTags( $r, $rcObj, $classes );
544 # Show how many people are watching this if enabled
545 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
547 $r .= "</td></tr></table>\n";
549 return $r;
553 * If enhanced RC is in use, this function takes the previously cached
554 * RC lines, arranges them, and outputs the HTML
556 * @return string
558 protected function recentChangesBlock() {
559 if ( count( $this->rc_cache ) == 0 ) {
560 return '';
563 $blockOut = '';
564 foreach ( $this->rc_cache as $block ) {
565 if ( count( $block ) < 2 ) {
566 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
567 } else {
568 $blockOut .= $this->recentChangesBlockGroup( $block );
572 return '<div>' . $blockOut . '</div>';
576 * Returns text for the end of RC
577 * If enhanced RC is in use, returns pretty much all the text
578 * @return string
580 public function endRecentChangesList() {
581 return $this->recentChangesBlock() . '</div>';