From: Adam Roses Wight Date: Mon, 19 Oct 2015 08:33:54 +0000 (-0700) Subject: Use template to render EnhancedChangesList groups X-Git-Tag: 1.31.0-rc.0~8533 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/wiki/supprimer.php?a=commitdiff_plain;h=de8f8c8248341c007ab6a9365a3c40614bcf04dc;p=lhc%2Fweb%2Fwiklou.git Use template to render EnhancedChangesList groups Rewrite a chunky HTML string concatenation party as Mustache template rendering. This decouples the view from the controller. Bug: T120921 Change-Id: I3217b80168f89e7b91dbc33a7053865ad3408615 --- diff --git a/includes/changes/ChangesList.php b/includes/changes/ChangesList.php index 2494ef1098..9567700914 100644 --- a/includes/changes/ChangesList.php +++ b/includes/changes/ChangesList.php @@ -364,12 +364,24 @@ class ChangesList extends ContextSource { } /** - * @param string $s HTML to update + * @param string $s Article link will be appended to this string, in place. * @param RecentChange $rc * @param bool $unpatrolled * @param bool $watched + * @deprecated since 1.27, use getArticleLink instead. */ - public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) { + public function insertArticleLink( &$s, RecentChange $rc, $unpatrolled, $watched ) { + $s .= $this->getArticleLink( $rc, $unpatrolled, $watched ); + } + + /** + * @param RecentChange $rc + * @param bool $unpatrolled + * @param bool $watched + * @return string HTML + * @since 1.26 + */ + public function getArticleLink( &$rc, $unpatrolled, $watched ) { $params = array(); if ( $rc->getTitle()->isRedirect() ) { $params = array( 'redirect' => 'no' ); @@ -389,23 +401,12 @@ class ChangesList extends ContextSource { # RTL/LTR marker $articlelink .= $this->getLanguage()->getDirMark(); + # TODO: Deprecate the $s argument, it seems happily unused. + $s = ''; Hooks::run( 'ChangesListInsertArticleLink', array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) ); - $s .= " $articlelink"; - } - - /** - * @param RecentChange $rc - * @param bool $unpatrolled - * @param bool $watched - * @return string - * @since 1.26 - */ - public function getArticleLink( RecentChange $rc, $unpatrolled, $watched ) { - $s = ''; - $this->insertArticleLink( $s, $rc, $unpatrolled, $watched ); - return $s; + return "{$s} {$articlelink}"; } /** diff --git a/includes/changes/EnhancedChangesList.php b/includes/changes/EnhancedChangesList.php index ed374b0e61..1c49545759 100644 --- a/includes/changes/EnhancedChangesList.php +++ b/includes/changes/EnhancedChangesList.php @@ -161,19 +161,22 @@ class EnhancedChangesList extends ChangesList { protected function recentChangesBlockGroup( $block ) { # Add the namespace and title of the block as part of the class - $classes = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' ); + $tableClasses = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' ); if ( $block[0]->mAttribs['rc_log_type'] ) { # Log entry - $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' + $tableClasses[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $block[0]->mAttribs['rc_log_type'] ); } else { - $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' + $tableClasses[] = Sanitizer::escapeClass( 'mw-changeslist-ns' . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] ); } - $classes[] = $block[0]->watched && $block[0]->mAttribs['rc_timestamp'] >= $block[0]->watched - ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched'; - $r = Html::openElement( 'table', array( 'class' => $classes ) ) . - Html::openElement( 'tr' ); + if ( $block[0]->watched + && $block[0]->mAttribs['rc_timestamp'] >= $block[0]->watched + ) { + $tableClasses[] = 'mw-changeslist-line-watched'; + } else { + $tableClasses[] = 'mw-changeslist-line-not-watched'; + } # Collate list of users $userlinks = array(); @@ -243,61 +246,44 @@ class EnhancedChangesList extends ChangesList { array_push( $users, $text ); } - $users = ' ' - . $this->msg( 'brackets' )->rawParams( - implode( $this->message['semicolon-separator'], $users ) - )->escaped() . ''; - - $tl = ''; - $r .= "$tl"; - - # Main line - $r .= '' . $this->recentChangesFlags( - $collectedRcFlags - ); - - # Timestamp - $r .= ' ' . $block[0]->timestamp . ' '; - # Article link + $articleLink = ''; + $revDeletedMsg = false; if ( $namehidden ) { - $r .= ' ' . - $this->msg( 'rev-deleted-event' )->escaped() . ''; + $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped(); } elseif ( $allLogs ) { - $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched ); + $articleLink = $this->maybeWatchedLink( $block[0]->link, $block[0]->watched ); } else { - $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched ); + $articleLink = $this->getArticleLink( $block[0], $block[0]->unpatrolled, $block[0]->watched ); } - $r .= $this->getLanguage()->getDirMark(); - $queryParams['curid'] = $curId; # Sub-entries - $lines = ''; + $lines = array(); foreach ( $block as $i => $rcObj ) { $line = $this->getLineData( $block, $rcObj, $queryParams ); - $lines .= $line; if ( !$line ) { // completely ignore this RC entry if we don't want to render it unset( $block[$i] ); } + $lines[] = $line; } // Further down are some assumptions that $block is a 0-indexed array // with (count-1) as last key. Let's make sure it is. $block = array_values( $block ); - if ( empty( $block ) ) { + + if ( empty( $block ) || !$lines ) { // if we can't show anything, don't display this block altogether return ''; } - $r .= $this->getLogText( $block, $queryParams, $allLogs, - $collectedRcFlags['newpage'], $namehidden ); - - $r .= ' . . '; + $logText = $this->getLogText( $block, $queryParams, $allLogs, + $collectedRcFlags['newpage'], $namehidden + ); # Character difference (does not apply if only log items) + $charDifference = false; if ( $RCShowChangedSize && !$allLogs ) { $last = 0; $first = count( $block ) - 1; @@ -309,37 +295,42 @@ class EnhancedChangesList extends ChangesList { $first--; } # Get net change - $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] ); - - if ( $chardiff == '' ) { - $r .= ' '; - } else { - $r .= ' ' . $chardiff . ' . . '; - } - } - - $r .= $users; - $r .= $this->numberofWatchingusers( $block[0]->numberofWatchingusers ); - $r .= ''; - - if ( !$lines ) { - // if there are no lines to be rendered (all aborted by hook), don't render the block - return ''; - } - - $r .= $lines; - $r .= "\n"; + $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] ); + } + + $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers ); + $usersList = $this->msg( 'brackets' )->rawParams( + implode( $this->message['semicolon-separator'], $users ) + )->escaped(); + + $templateParams = array( + 'articleLink' => $articleLink, + 'charDifference' => $charDifference, + 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ), + 'languageDirMark' => $this->getLanguage()->getDirMark(), + 'lines' => $lines, + 'logText' => $logText, + 'numberofWatchingusers' => $numberofWatchingusers, + 'rev-deleted-event' => $revDeletedMsg, + 'tableClasses' => $tableClasses, + 'timestamp' => $block[0]->timestamp, + 'users' => $usersList, + ); $this->rcCacheIndex++; - return $r; + $templateParser = new TemplateParser(); + return $templateParser->processTemplate( + 'EnhancedChangesListGroup', + $templateParams + ); } /** * @param RCCacheEntry[] $block * @param RCCacheEntry $rcObj * @param array $queryParams - * @return string + * @return array * @throws Exception * @throws FatalError * @throws MWException @@ -351,9 +342,13 @@ class EnhancedChangesList extends ChangesList { $classes = array(); $type = $rcObj->mAttribs['rc_type']; $data = array(); + $lineParams = array(); - $trClass = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched - ? ' class="mw-enhanced-watched"' : ''; + if ( $rcObj->watched + && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched + ) { + $lineParams['classes'] = array( 'mw-enhanced-watched' ); + } $separator = ' . . '; $data['recentChangesFlags'] = array( @@ -430,27 +425,23 @@ class EnhancedChangesList extends ChangesList { array( $this, &$data, $block, $rcObj ) ); if ( !$success ) { // skip entry if hook aborted it - return ''; + return array(); } - $line = ''; if ( isset( $data['recentChangesFlags'] ) ) { - $line .= $this->recentChangesFlags( $data['recentChangesFlags'] ); + $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] ); unset( $data['recentChangesFlags'] ); } - $line .= ' '; if ( isset( $data['timestampLink'] ) ) { - $line .= '' . $data['timestampLink'] . ''; + $lineParams['timestampLink'] = $data['timestampLink']; unset( $data['timestampLink'] ); } // everything else: makes it easier for extensions to add or remove data - $line .= implode( '', $data ); + $lineParams['data'] = array_values( $data ); - $line .= "\n"; - - return $line; + return $lineParams; } /** diff --git a/includes/changes/OldChangesList.php b/includes/changes/OldChangesList.php index 31b355d4cf..58a57a4a69 100644 --- a/includes/changes/OldChangesList.php +++ b/includes/changes/OldChangesList.php @@ -98,7 +98,7 @@ class OldChangesList extends ChangesList { ), '' ); - $this->insertArticleLink( $html, $rc, $unpatrolled, $watched ); + $html .= $this->getArticleLink( $rc, $unpatrolled, $watched ); } # Edit/log timestamp $this->insertTimestamp( $html, $rc ); diff --git a/includes/templates/EnhancedChangesListGroup.mustache b/includes/templates/EnhancedChangesListGroup.mustache new file mode 100644 index 0000000000..352eb17d21 --- /dev/null +++ b/includes/templates/EnhancedChangesListGroup.mustache @@ -0,0 +1,28 @@ + + + + + + + {{# lines }} + + + + + + {{/ lines }} +
+ + {{{ collectedRcFlags }}} {{ timestamp }}  + {{# rev-deleted-event }}{{{ . }}}{{/ rev-deleted-event }} + {{{ articleLink }}}{{{ languageDirMark }}}{{{ logText }}} + . . + {{# charDifference }}{{{ . }}} . .{{/ charDifference }} + {{{ users }}} + {{ numberofWatchingusers }} +
{{{ recentChangesFlags }}}  + {{# timestampLink }} + {{{ . }}} + {{/ timestampLink }} + {{# data }}{{{ . }}}{{/ data }} +