Merge "Only try to show character difference if it isn't empty"
[lhc/web/wiklou.git] / includes / changes / EnhancedChangesList.php
index d8aeb77..6c7666f 100644 (file)
@@ -91,27 +91,28 @@ class EnhancedChangesList extends ChangesList {
        /**
         * Format a line for enhanced recentchange (aka with javascript and block of lines).
         *
-        * @param RecentChange $rc
+        * @param RecentChange &$rc
         * @param bool $watched
         * @param int $linenumber (default null)
         *
         * @return string
         */
        public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
-
                $date = $this->getLanguage()->userDate(
                        $rc->mAttribs['rc_timestamp'],
                        $this->getUser()
                );
+               if ( $this->lastdate === '' ) {
+                       $this->lastdate = $date;
+               }
 
                $ret = '';
 
-               # If it's a new day, add the headline and flush the cache
-               if ( $date != $this->lastdate ) {
-                       # Process current cache
+               # If it's a new day, flush the cache and update $this->lastdate
+               if ( $date !== $this->lastdate ) {
+                       # Process current cache (uses $this->lastdate to generate a heading)
                        $ret = $this->recentChangesBlock();
                        $this->rc_cache = [];
-                       $ret .= Xml::element( 'h4', null, $date ) . "\n";
                        $this->lastdate = $date;
                }
 
@@ -171,12 +172,14 @@ class EnhancedChangesList extends ChangesList {
                $recentChangesFlags = $this->getConfig()->get( 'RecentChangesFlags' );
 
                # Add the namespace and title of the block as part of the class
-               $tableClasses = [ 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' ];
+               $tableClasses = [ 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc', 'mw-changeslist-line' ];
                if ( $block[0]->mAttribs['rc_log_type'] ) {
                        # Log entry
+                       $tableClasses[] = 'mw-changeslist-log';
                        $tableClasses[] = Sanitizer::escapeClass( 'mw-changeslist-log-'
                                . $block[0]->mAttribs['rc_log_type'] );
                } else {
+                       $tableClasses[] = 'mw-changeslist-edit';
                        $tableClasses[] = Sanitizer::escapeClass( 'mw-changeslist-ns'
                                . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
                }
@@ -243,8 +246,7 @@ class EnhancedChangesList extends ChangesList {
                        $text = $userlink;
                        $text .= $this->getLanguage()->getDirMark();
                        if ( $count > 1 ) {
-                               // @todo FIXME: Hardcoded '×'. Should be a message.
-                               $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
+                               $formattedCount = $this->msg( 'ntimes' )->numParams( $count )->escaped();
                                $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
                        }
                        array_push( $users, $text );
@@ -330,6 +332,11 @@ class EnhancedChangesList extends ChangesList {
                        implode( $this->message['semicolon-separator'], $users )
                )->escaped();
 
+               $prefix = '';
+               if ( is_callable( $this->changeLinePrefixer ) ) {
+                       $prefix = call_user_func( $this->changeLinePrefixer, $block[0], $this, true );
+               }
+
                $templateParams = [
                        'articleLink' => $articleLink,
                        'charDifference' => $charDifference,
@@ -338,9 +345,11 @@ class EnhancedChangesList extends ChangesList {
                        'lines' => $lines,
                        'logText' => $logText,
                        'numberofWatchingusers' => $numberofWatchingusers,
+                       'prefix' => $prefix,
                        'rev-deleted-event' => $revDeletedMsg,
                        'tableClasses' => $tableClasses,
                        'timestamp' => $block[0]->timestamp,
+                       'fullTimestamp' => $block[0]->getAttribute( 'rc_timestamp' ),
                        'users' => $usersList,
                ];
 
@@ -366,7 +375,7 @@ class EnhancedChangesList extends ChangesList {
 
                $type = $rcObj->mAttribs['rc_type'];
                $data = [];
-               $lineParams = [];
+               $lineParams = [ 'targetTitle' => $rcObj->getTitle() ];
 
                $classes = [ 'mw-enhanced-rc' ];
                if ( $rcObj->watched
@@ -374,7 +383,7 @@ class EnhancedChangesList extends ChangesList {
                ) {
                        $classes[] = 'mw-enhanced-watched';
                }
-               $classes = array_merge( $classes, $this->getHTMLClassesForFilters( $rcObj ) );
+               $classes = array_merge( $classes, $this->getHTMLClasses( $rcObj, $rcObj->watched ) );
 
                $separator = ' <span class="mw-changeslist-separator">. .</span> ';
 
@@ -447,13 +456,16 @@ class EnhancedChangesList extends ChangesList {
                # Tags
                $data['tags'] = $this->getTags( $rcObj, $classes );
 
+               $attribs = $this->getDataAttributes( $rcObj );
+
                // give the hook a chance to modify the data
                $success = Hooks::run( 'EnhancedChangesListModifyLineData',
-                       [ $this, &$data, $block, $rcObj, &$classes ] );
+                       [ $this, &$data, $block, $rcObj, &$classes, &$attribs ] );
                if ( !$success ) {
                        // skip entry if hook aborted it
                        return [];
                }
+               $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
 
                $lineParams['recentChangesFlagsRaw'] = [];
                if ( isset( $data['recentChangesFlags'] ) ) {
@@ -469,6 +481,7 @@ class EnhancedChangesList extends ChangesList {
                }
 
                $lineParams['classes'] = array_values( $classes );
+               $lineParams['attribs'] = Html::expandAttributes( $attribs );
 
                // everything else: makes it easier for extensions to add or remove data
                $lineParams['data'] = array_values( $data );
@@ -502,7 +515,7 @@ class EnhancedChangesList extends ChangesList {
 
                $sinceLast = 0;
                $unvisitedOldid = null;
-               /** @var $rcObj RCCacheEntry */
+               /** @var RCCacheEntry $rcObj */
                foreach ( $block as $rcObj ) {
                        // Same logic as below inside main foreach
                        if ( $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched ) {
@@ -524,7 +537,7 @@ class EnhancedChangesList extends ChangesList {
 
                # Total change link
                $links = [];
-               /** @var $block0 RecentChange */
+               /** @var RecentChange $block0 */
                $block0 = $block[0];
                $last = $block[count( $block ) - 1];
                if ( !$allLogs ) {
@@ -605,8 +618,10 @@ class EnhancedChangesList extends ChangesList {
 
                if ( $logType ) {
                        # Log entry
+                       $classes[] = 'mw-changeslist-log';
                        $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
                } else {
+                       $classes[] = 'mw-changeslist-edit';
                        $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
                                $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
                }
@@ -671,6 +686,8 @@ class EnhancedChangesList extends ChangesList {
                # Show how many people are watching this if enabled
                $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
 
+               $data['attribs'] = array_merge( $this->getDataAttributes( $rcObj ), [ 'class' => $classes ] );
+
                // give the hook a chance to modify the data
                $success = Hooks::run( 'EnhancedChangesListModifyBlockLineData',
                        [ $this, &$data, $rcObj ] );
@@ -678,10 +695,21 @@ class EnhancedChangesList extends ChangesList {
                        // skip entry if hook aborted it
                        return '';
                }
+               $attribs = $data['attribs'];
+               unset( $data['attribs'] );
+               $attribs = wfArrayFilterByKey( $attribs, function ( $key ) {
+                       return $key === 'class' || Sanitizer::isReservedDataAttribute( $key );
+               } );
+
+               $prefix = '';
+               if ( is_callable( $this->changeLinePrefixer ) ) {
+                       $prefix = call_user_func( $this->changeLinePrefixer, $rcObj, $this, false );
+               }
 
-               $line = Html::openElement( 'table', [ 'class' => $classes ] ) .
-                       Html::openElement( 'tr' );
-               $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
+               $line = Html::openElement( 'table', $attribs ) . Html::openElement( 'tr' );
+               $line .= Html::rawElement( 'td', [], '<span class="mw-enhancedchanges-arrow-space"></span>' );
+               $line .= Html::rawElement( 'td', [ 'class' => 'mw-changeslist-line-prefix' ], $prefix );
+               $line .= '<td class="mw-enhanced-rc">';
 
                if ( isset( $data['recentChangesFlags'] ) ) {
                        $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
@@ -692,7 +720,12 @@ class EnhancedChangesList extends ChangesList {
                        $line .= '&#160;' . $data['timestampLink'];
                        unset( $data['timestampLink'] );
                }
-               $line .= '&#160;</td><td>';
+               $line .= '&#160;</td>';
+               $line .= Html::openElement( 'td', [
+                       'class' => 'mw-changeslist-line-inner',
+                       // Used for reliable determination of the affiliated page
+                       'data-target-page' => $rcObj->getTitle(),
+               ] );
 
                // everything else: makes it easier for extensions to add or remove data
                $line .= implode( '', $data );
@@ -755,7 +788,11 @@ class EnhancedChangesList extends ChangesList {
                        }
                }
 
-               return '<div>' . $blockOut . '</div>';
+               if ( $blockOut === '' ) {
+                       return '';
+               }
+               // $this->lastdate is kept up to date by recentChangesLine()
+               return Xml::element( 'h4', null, $this->lastdate ) . "\n<div>" . $blockOut . '</div>';
        }
 
        /**