Remove obvious function-level profiling
[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
113 return $ret;
114 }
115
116 /**
117 * Put accumulated information into the cache, for later display.
118 * Page moves go on their own line.
119 *
120 * @param RCCacheEntry $cacheEntry
121 */
122 protected function addCacheEntry( RCCacheEntry $cacheEntry ) {
123 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
124
125 if ( !isset( $this->rc_cache[$cacheGroupingKey] ) ) {
126 $this->rc_cache[$cacheGroupingKey] = array();
127 }
128
129 array_push( $this->rc_cache[$cacheGroupingKey], $cacheEntry );
130 }
131
132 /**
133 * @todo use rc_source to group, if set; fallback to rc_type
134 *
135 * @param RCCacheEntry $cacheEntry
136 *
137 * @return string
138 */
139 protected function makeCacheGroupingKey( RCCacheEntry $cacheEntry ) {
140 $title = $cacheEntry->getTitle();
141 $cacheGroupingKey = $title->getPrefixedDBkey();
142
143 $type = $cacheEntry->mAttribs['rc_type'];
144
145 if ( $type == RC_LOG ) {
146 // Group by log type
147 $cacheGroupingKey = SpecialPage::getTitleFor(
148 'Log',
149 $cacheEntry->mAttribs['rc_log_type']
150 )->getPrefixedDBkey();
151 }
152
153 return $cacheGroupingKey;
154 }
155
156 /**
157 * Enhanced RC group
158 * @param RCCacheEntry[] $block
159 * @return string
160 */
161 protected function recentChangesBlockGroup( $block ) {
162
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'] );
172 }
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' );
177
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;
195 }
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;
200 }
201 $u = $rcObj->userlink;
202 if ( !isset( $userlinks[$u] ) ) {
203 $userlinks[$u] = 0;
204 }
205 if ( $rcObj->unpatrolled ) {
206 $unpatrolled = true;
207 }
208 if ( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
209 $allLogs = false;
210 }
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'];
215 }
216 if ( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
217 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
218 }
219
220 if ( !$rcObj->mAttribs['rc_bot'] ) {
221 $allBots = false;
222 }
223 if ( !$rcObj->mAttribs['rc_minor'] ) {
224 $allMinors = false;
225 }
226
227 $userlinks[$u]++;
228 }
229
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();
241 }
242 array_push( $users, $text );
243 }
244
245 $users = ' <span class="changedby">'
246 . $this->msg( 'brackets' )->rawParams(
247 implode( $this->message['semicolon-separator'], $users )
248 )->escaped() . '</span>';
249
250 $tl = '<span class="mw-collapsible-toggle mw-collapsible-arrow ' .
251 'mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
252 $r .= "<td>$tl</td>";
253
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 ) );
261
262 # Timestamp
263 $r .= '&#160;' . $block[0]->timestamp . '&#160;</td><td>';
264
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 );
273 }
274
275 $r .= $this->getLanguage()->getDirMark();
276
277 $queryParams['curid'] = $curId;
278
279 # Changes message
280 static $nchanges = array();
281 static $sinceLastVisitMsg = array();
282
283 $n = count( $block );
284 if ( !isset( $nchanges[$n] ) ) {
285 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
286 }
287
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'];
296 }
297 }
298 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
299 $sinceLastVisitMsg[$sinceLast] =
300 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
301 }
302
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,
321 ),
322 array( 'known', 'noclasses' )
323 );
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,
332 ),
333 array( 'known', 'noclasses' )
334 );
335 }
336 }
337 }
338
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';
347
348 $logtext .= $this->message['pipe-separator'] .
349 Linker::linkKnown(
350 $block0->getTitle(),
351 $this->message['enhancedrc-history'],
352 array(),
353 $params
354 );
355 }
356
357 if ( $logtext !== '' ) {
358 $r .= $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
359 }
360
361 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
362
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++;
370 }
371 while ( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
372 $first--;
373 }
374 # Get net change
375 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
376
377 if ( $chardiff == '' ) {
378 $r .= ' ';
379 } else {
380 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
381 }
382 }
383
384 $r .= $users;
385 $r .= $this->numberofWatchingusers( $block0->numberofWatchingusers );
386 $r .= '</td></tr>';
387
388 # Sub-entries
389 foreach ( $block as $rcObj ) {
390 # Classes to apply -- TODO implement
391 $classes = array();
392 $type = $rcObj->mAttribs['rc_type'];
393
394 $trClass = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
395 ? ' class="mw-enhanced-watched"' : '';
396
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">';
405
406 $params = $queryParams;
407
408 if ( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
409 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
410 }
411
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 {
419
420 $link = Linker::linkKnown(
421 $rcObj->getTitle(),
422 $rcObj->timestamp,
423 array(),
424 $params
425 );
426 if ( $this->isDeleted( $rcObj, Revision::DELETED_TEXT ) ) {
427 $link = '<span class="history-deleted">' . $link . '</span> ';
428 }
429 }
430 $r .= $link . '</span>';
431
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();
438 }
439 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
440
441 # Character diff
442 if ( $RCShowChangedSize ) {
443 $cd = $this->formatCharacterDifference( $rcObj );
444 if ( $cd !== '' ) {
445 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
446 }
447 }
448
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 );
456 }
457
458 # Rollback
459 $this->insertRollback( $r, $rcObj );
460 # Tags
461 $this->insertTags( $r, $rcObj, $classes );
462
463 $r .= "</td></tr>\n";
464 }
465 $r .= "</table>\n";
466
467 $this->rcCacheIndex++;
468
469
470 return $r;
471 }
472
473 /**
474 * Enhanced RC ungrouped line.
475 *
476 * @param RecentChange|RCCacheEntry $rcObj
477 * @return string A HTML formatted line (generated using $r)
478 */
479 protected function recentChangesBlockLine( $rcObj ) {
480 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
481
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'] );
491 }
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' );
496
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 );
515 }
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();
526 }
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> ';
533 }
534 }
535
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 );
542 }
543
544 # Tags
545 $this->insertTags( $r, $rcObj, $classes );
546 # Show how many people are watching this if enabled
547 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
548
549 $r .= "</td></tr></table>\n";
550
551
552 return $r;
553 }
554
555 /**
556 * If enhanced RC is in use, this function takes the previously cached
557 * RC lines, arranges them, and outputs the HTML
558 *
559 * @return string
560 */
561 protected function recentChangesBlock() {
562 if ( count( $this->rc_cache ) == 0 ) {
563 return '';
564 }
565
566
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 );
573 }
574 }
575
576
577 return '<div>' . $blockOut . '</div>';
578 }
579
580 /**
581 * Returns text for the end of RC
582 * If enhanced RC is in use, returns pretty much all the text
583 * @return string
584 */
585 public function endRecentChangesList() {
586 return $this->recentChangesBlock() . '</div>';
587 }
588 }