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