Language: s/error_log/wfWarn/
[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 $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 wfProfileOut( __METHOD__ );
113
114 return $ret;
115 }
116
117 /**
118 * Put accumulated information into the cache, for later display.
119 * Page moves go on their own line.
120 *
121 * @param RCCacheEntry $cacheEntry
122 */
123 protected function addCacheEntry( RCCacheEntry $cacheEntry ) {
124 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
125
126 if ( !isset( $this->rc_cache[$cacheGroupingKey] ) ) {
127 $this->rc_cache[$cacheGroupingKey] = array();
128 }
129
130 array_push( $this->rc_cache[$cacheGroupingKey], $cacheEntry );
131 }
132
133 /**
134 * @todo use rc_source to group, if set; fallback to rc_type
135 *
136 * @param RCCacheEntry $cacheEntry
137 *
138 * @return string
139 */
140 protected function makeCacheGroupingKey( RCCacheEntry $cacheEntry ) {
141 $title = $cacheEntry->getTitle();
142 $cacheGroupingKey = $title->getPrefixedDBkey();
143
144 $type = $cacheEntry->mAttribs['rc_type'];
145
146 if ( $type == RC_LOG ) {
147 // Group by log type
148 $cacheGroupingKey = SpecialPage::getTitleFor(
149 'Log',
150 $cacheEntry->mAttribs['rc_log_type']
151 )->getPrefixedDBkey();
152 }
153
154 return $cacheGroupingKey;
155 }
156
157 /**
158 * Enhanced RC group
159 * @param RCCacheEntry[] $block
160 * @return string
161 */
162 protected function recentChangesBlockGroup( $block ) {
163 global $wgRCShowChangedSize;
164
165 wfProfileIn( __METHOD__ );
166
167 # Add the namespace and title of the block as part of the class
168 $classes = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' );
169 if ( $block[0]->mAttribs['rc_log_type'] ) {
170 # Log entry
171 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-'
172 . $block[0]->mAttribs['rc_log_type'] );
173 } else {
174 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns'
175 . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
176 }
177 $classes[] = $block[0]->watched && $block[0]->mAttribs['rc_timestamp'] >= $block[0]->watched
178 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
179 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
180 Html::openElement( 'tr' );
181
182 # Collate list of users
183 $userlinks = array();
184 # Other properties
185 $unpatrolled = false;
186 $isnew = false;
187 $allBots = true;
188 $allMinors = true;
189 $curId = $currentRevision = 0;
190 # Some catalyst variables...
191 $namehidden = true;
192 $allLogs = true;
193 $oldid = '';
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 ( $wgRCShowChangedSize && !$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 ( $wgRCShowChangedSize ) {
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 global $wgRCShowChangedSize;
485
486 wfProfileIn( __METHOD__ );
487 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
488
489 $type = $rcObj->mAttribs['rc_type'];
490 $logType = $rcObj->mAttribs['rc_log_type'];
491 $classes = array( 'mw-enhanced-rc' );
492 if ( $logType ) {
493 # Log entry
494 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
495 } else {
496 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
497 $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
498 }
499 $classes[] = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
500 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
501 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
502 Html::openElement( 'tr' );
503
504 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
505 # Flag and Timestamp
506 $r .= $this->recentChangesFlags( array(
507 'newpage' => $type == RC_NEW,
508 'minor' => $rcObj->mAttribs['rc_minor'],
509 'unpatrolled' => $rcObj->unpatrolled,
510 'bot' => $rcObj->mAttribs['rc_bot'],
511 ) );
512 $r .= '&#160;' . $rcObj->timestamp . '&#160;</td><td>';
513 # Article or log link
514 if ( $logType ) {
515 $logPage = new LogPage( $logType );
516 $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
517 $logName = $logPage->getName()->escaped();
518 $r .= $this->msg( 'parentheses' )
519 ->rawParams( Linker::linkKnown( $logTitle, $logName ) )->escaped();
520 } else {
521 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
522 }
523 # Diff and hist links
524 if ( $type != RC_LOG ) {
525 $query['action'] = 'history';
526 $r .= ' ' . $this->msg( 'parentheses' )
527 ->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
528 $rcObj->getTitle(),
529 $this->message['hist'],
530 array(),
531 $query
532 ) )->escaped();
533 }
534 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
535 # Character diff
536 if ( $wgRCShowChangedSize ) {
537 $cd = $this->formatCharacterDifference( $rcObj );
538 if ( $cd !== '' ) {
539 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
540 }
541 }
542
543 if ( $type == RC_LOG ) {
544 $r .= $this->insertLogEntry( $rcObj );
545 } else {
546 $r .= ' ' . $rcObj->userlink . $rcObj->usertalklink;
547 $r .= $this->insertComment( $rcObj );
548 $this->insertRollback( $r, $rcObj );
549 }
550
551 # Tags
552 $this->insertTags( $r, $rcObj, $classes );
553 # Show how many people are watching this if enabled
554 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
555
556 $r .= "</td></tr></table>\n";
557
558 wfProfileOut( __METHOD__ );
559
560 return $r;
561 }
562
563 /**
564 * If enhanced RC is in use, this function takes the previously cached
565 * RC lines, arranges them, and outputs the HTML
566 *
567 * @return string
568 */
569 protected function recentChangesBlock() {
570 if ( count( $this->rc_cache ) == 0 ) {
571 return '';
572 }
573
574 wfProfileIn( __METHOD__ );
575
576 $blockOut = '';
577 foreach ( $this->rc_cache as $block ) {
578 if ( count( $block ) < 2 ) {
579 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
580 } else {
581 $blockOut .= $this->recentChangesBlockGroup( $block );
582 }
583 }
584
585 wfProfileOut( __METHOD__ );
586
587 return '<div>' . $blockOut . '</div>';
588 }
589
590 /**
591 * Returns text for the end of RC
592 * If enhanced RC is in use, returns pretty much all the text
593 * @return string
594 */
595 public function endRecentChangesList() {
596 return $this->recentChangesBlock() . '</div>';
597 }
598 }