Implement extension registration from an extension.json file
[mediawiki.git] / includes / changes / EnhancedChangesList.php
blobb39bca3ba4e252f587a089c96af23e381a828893
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 );
113 return $ret;
117 * Put accumulated information into the cache, for later display.
118 * Page moves go on their own line.
120 * @param RCCacheEntry $cacheEntry
122 protected function addCacheEntry( RCCacheEntry $cacheEntry ) {
123 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
125 if ( !isset( $this->rc_cache[$cacheGroupingKey] ) ) {
126 $this->rc_cache[$cacheGroupingKey] = array();
129 array_push( $this->rc_cache[$cacheGroupingKey], $cacheEntry );
133 * @todo use rc_source to group, if set; fallback to rc_type
135 * @param RCCacheEntry $cacheEntry
137 * @return string
139 protected function makeCacheGroupingKey( RCCacheEntry $cacheEntry ) {
140 $title = $cacheEntry->getTitle();
141 $cacheGroupingKey = $title->getPrefixedDBkey();
143 $type = $cacheEntry->mAttribs['rc_type'];
145 if ( $type == RC_LOG ) {
146 // Group by log type
147 $cacheGroupingKey = SpecialPage::getTitleFor(
148 'Log',
149 $cacheEntry->mAttribs['rc_log_type']
150 )->getPrefixedDBkey();
153 return $cacheGroupingKey;
157 * Enhanced RC group
158 * @param RCCacheEntry[] $block
159 * @return string
161 protected function recentChangesBlockGroup( $block ) {
163 # Add the namespace and title of the block as part of the class
164 $classes = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' );
165 if ( $block[0]->mAttribs['rc_log_type'] ) {
166 # Log entry
167 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-'
168 . $block[0]->mAttribs['rc_log_type'] );
169 } else {
170 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns'
171 . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
173 $classes[] = $block[0]->watched && $block[0]->mAttribs['rc_timestamp'] >= $block[0]->watched
174 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
175 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
176 Html::openElement( 'tr' );
178 # Collate list of users
179 $userlinks = array();
180 # Other properties
181 $unpatrolled = false;
182 $isnew = false;
183 $allBots = true;
184 $allMinors = true;
185 $curId = $currentRevision = 0;
186 # Some catalyst variables...
187 $namehidden = true;
188 $allLogs = true;
189 $oldid = '';
190 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
191 foreach ( $block as $rcObj ) {
192 $oldid = $rcObj->mAttribs['rc_last_oldid'];
193 if ( $rcObj->mAttribs['rc_type'] == RC_NEW ) {
194 $isnew = true;
196 // If all log actions to this page were hidden, then don't
197 // give the name of the affected page for this block!
198 if ( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
199 $namehidden = false;
201 $u = $rcObj->userlink;
202 if ( !isset( $userlinks[$u] ) ) {
203 $userlinks[$u] = 0;
205 if ( $rcObj->unpatrolled ) {
206 $unpatrolled = true;
208 if ( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
209 $allLogs = false;
211 # Get the latest entry with a page_id and oldid
212 # since logs may not have these.
213 if ( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
214 $curId = $rcObj->mAttribs['rc_cur_id'];
216 if ( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
217 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
220 if ( !$rcObj->mAttribs['rc_bot'] ) {
221 $allBots = false;
223 if ( !$rcObj->mAttribs['rc_minor'] ) {
224 $allMinors = false;
227 $userlinks[$u]++;
230 # Sort the list and convert to text
231 krsort( $userlinks );
232 asort( $userlinks );
233 $users = array();
234 foreach ( $userlinks as $userlink => $count ) {
235 $text = $userlink;
236 $text .= $this->getLanguage()->getDirMark();
237 if ( $count > 1 ) {
238 // @todo FIXME: Hardcoded '×'. Should be a message.
239 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
240 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
242 array_push( $users, $text );
245 $users = ' <span class="changedby">'
246 . $this->msg( 'brackets' )->rawParams(
247 implode( $this->message['semicolon-separator'], $users )
248 )->escaped() . '</span>';
250 $tl = '<span class="mw-collapsible-toggle mw-collapsible-arrow ' .
251 'mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
252 $r .= "<td>$tl</td>";
254 # Main line
255 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
256 'newpage' => $isnew, # show, when one have this flag
257 'minor' => $allMinors, # show only, when all have this flag
258 'unpatrolled' => $unpatrolled, # show, when one have this flag
259 'bot' => $allBots, # show only, when all have this flag
260 ) );
262 # Timestamp
263 $r .= '&#160;' . $block[0]->timestamp . '&#160;</td><td>';
265 # Article link
266 if ( $namehidden ) {
267 $r .= ' <span class="history-deleted">' .
268 $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
269 } elseif ( $allLogs ) {
270 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
271 } else {
272 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
275 $r .= $this->getLanguage()->getDirMark();
277 $queryParams['curid'] = $curId;
279 # Changes message
280 static $nchanges = array();
281 static $sinceLastVisitMsg = array();
283 $n = count( $block );
284 if ( !isset( $nchanges[$n] ) ) {
285 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
288 $sinceLast = 0;
289 $unvisitedOldid = null;
290 /** @var $rcObj RCCacheEntry */
291 foreach ( $block as $rcObj ) {
292 // Same logic as below inside main foreach
293 if ( $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched ) {
294 $sinceLast++;
295 $unvisitedOldid = $rcObj->mAttribs['rc_last_oldid'];
298 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
299 $sinceLastVisitMsg[$sinceLast] =
300 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
303 # Total change link
304 $r .= ' ';
305 $logtext = '';
306 /** @var $block0 RecentChange */
307 $block0 = $block[0];
308 if ( !$allLogs ) {
309 if ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
310 $logtext .= $nchanges[$n];
311 } elseif ( $isnew ) {
312 $logtext .= $nchanges[$n];
313 } else {
314 $logtext .= Linker::link(
315 $block0->getTitle(),
316 $nchanges[$n],
317 array(),
318 $queryParams + array(
319 'diff' => $currentRevision,
320 'oldid' => $oldid,
322 array( 'known', 'noclasses' )
324 if ( $sinceLast > 0 && $sinceLast < $n ) {
325 $logtext .= $this->message['pipe-separator'] . Linker::link(
326 $block0->getTitle(),
327 $sinceLastVisitMsg[$sinceLast],
328 array(),
329 $queryParams + array(
330 'diff' => $currentRevision,
331 'oldid' => $unvisitedOldid,
333 array( 'known', 'noclasses' )
339 # History
340 if ( $allLogs ) {
341 // don't show history link for logs
342 } elseif ( $namehidden || !$block0->getTitle()->exists() ) {
343 $logtext .= $this->message['pipe-separator'] . $this->message['enhancedrc-history'];
344 } else {
345 $params = $queryParams;
346 $params['action'] = 'history';
348 $logtext .= $this->message['pipe-separator'] .
349 Linker::linkKnown(
350 $block0->getTitle(),
351 $this->message['enhancedrc-history'],
352 array(),
353 $params
357 if ( $logtext !== '' ) {
358 $r .= $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
361 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
363 # Character difference (does not apply if only log items)
364 if ( $RCShowChangedSize && !$allLogs ) {
365 $last = 0;
366 $first = count( $block ) - 1;
367 # Some events (like logs) have an "empty" size, so we need to skip those...
368 while ( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
369 $last++;
371 while ( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
372 $first--;
374 # Get net change
375 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
377 if ( $chardiff == '' ) {
378 $r .= ' ';
379 } else {
380 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
384 $r .= $users;
385 $r .= $this->numberofWatchingusers( $block0->numberofWatchingusers );
386 $r .= '</td></tr>';
388 # Sub-entries
389 foreach ( $block as $rcObj ) {
390 # Classes to apply -- TODO implement
391 $classes = array();
392 $type = $rcObj->mAttribs['rc_type'];
394 $trClass = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
395 ? ' class="mw-enhanced-watched"' : '';
397 $r .= '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
398 $r .= $this->recentChangesFlags( array(
399 'newpage' => $type == RC_NEW,
400 'minor' => $rcObj->mAttribs['rc_minor'],
401 'unpatrolled' => $rcObj->unpatrolled,
402 'bot' => $rcObj->mAttribs['rc_bot'],
403 ) );
404 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
406 $params = $queryParams;
408 if ( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
409 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
412 # Log timestamp
413 if ( $type == RC_LOG ) {
414 $link = $rcObj->timestamp;
415 # Revision link
416 } elseif ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
417 $link = '<span class="history-deleted">' . $rcObj->timestamp . '</span> ';
418 } else {
420 $link = Linker::linkKnown(
421 $rcObj->getTitle(),
422 $rcObj->timestamp,
423 array(),
424 $params
426 if ( $this->isDeleted( $rcObj, Revision::DELETED_TEXT ) ) {
427 $link = '<span class="history-deleted">' . $link . '</span> ';
430 $r .= $link . '</span>';
432 if ( !$type == RC_LOG || $type == RC_NEW ) {
433 $r .= ' ' . $this->msg( 'parentheses' )->rawParams(
434 $rcObj->curlink .
435 $this->message['pipe-separator'] .
436 $rcObj->lastlink
437 )->escaped();
439 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
441 # Character diff
442 if ( $RCShowChangedSize ) {
443 $cd = $this->formatCharacterDifference( $rcObj );
444 if ( $cd !== '' ) {
445 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
449 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
450 $r .= $this->insertLogEntry( $rcObj );
451 } else {
452 # User links
453 $r .= $rcObj->userlink;
454 $r .= $rcObj->usertalklink;
455 $r .= $this->insertComment( $rcObj );
458 # Rollback
459 $this->insertRollback( $r, $rcObj );
460 # Tags
461 $this->insertTags( $r, $rcObj, $classes );
463 $r .= "</td></tr>\n";
465 $r .= "</table>\n";
467 $this->rcCacheIndex++;
470 return $r;
474 * Enhanced RC ungrouped line.
476 * @param RecentChange|RCCacheEntry $rcObj
477 * @return string A HTML formatted line (generated using $r)
479 protected function recentChangesBlockLine( $rcObj ) {
480 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
482 $type = $rcObj->mAttribs['rc_type'];
483 $logType = $rcObj->mAttribs['rc_log_type'];
484 $classes = array( 'mw-enhanced-rc' );
485 if ( $logType ) {
486 # Log entry
487 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
488 } else {
489 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
490 $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
492 $classes[] = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
493 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
494 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
495 Html::openElement( 'tr' );
497 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
498 # Flag and Timestamp
499 $r .= $this->recentChangesFlags( array(
500 'newpage' => $type == RC_NEW,
501 'minor' => $rcObj->mAttribs['rc_minor'],
502 'unpatrolled' => $rcObj->unpatrolled,
503 'bot' => $rcObj->mAttribs['rc_bot'],
504 ) );
505 $r .= '&#160;' . $rcObj->timestamp . '&#160;</td><td>';
506 # Article or log link
507 if ( $logType ) {
508 $logPage = new LogPage( $logType );
509 $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
510 $logName = $logPage->getName()->escaped();
511 $r .= $this->msg( 'parentheses' )
512 ->rawParams( Linker::linkKnown( $logTitle, $logName ) )->escaped();
513 } else {
514 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
516 # Diff and hist links
517 if ( $type != RC_LOG ) {
518 $query['action'] = 'history';
519 $r .= ' ' . $this->msg( 'parentheses' )
520 ->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
521 $rcObj->getTitle(),
522 $this->message['hist'],
523 array(),
524 $query
525 ) )->escaped();
527 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
528 # Character diff
529 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
530 $cd = $this->formatCharacterDifference( $rcObj );
531 if ( $cd !== '' ) {
532 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
536 if ( $type == RC_LOG ) {
537 $r .= $this->insertLogEntry( $rcObj );
538 } else {
539 $r .= ' ' . $rcObj->userlink . $rcObj->usertalklink;
540 $r .= $this->insertComment( $rcObj );
541 $this->insertRollback( $r, $rcObj );
544 # Tags
545 $this->insertTags( $r, $rcObj, $classes );
546 # Show how many people are watching this if enabled
547 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
549 $r .= "</td></tr></table>\n";
552 return $r;
556 * If enhanced RC is in use, this function takes the previously cached
557 * RC lines, arranges them, and outputs the HTML
559 * @return string
561 protected function recentChangesBlock() {
562 if ( count( $this->rc_cache ) == 0 ) {
563 return '';
567 $blockOut = '';
568 foreach ( $this->rc_cache as $block ) {
569 if ( count( $block ) < 2 ) {
570 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
571 } else {
572 $blockOut .= $this->recentChangesBlockGroup( $block );
577 return '<div>' . $blockOut . '</div>';
581 * Returns text for the end of RC
582 * If enhanced RC is in use, returns pretty much all the text
583 * @return string
585 public function endRecentChangesList() {
586 return $this->recentChangesBlock() . '</div>';