Hygiene: Refactor Minerva history link generation
* Create several sub functions to manage complexity of the history link * Clean up MinervaTemplate to use data directly and remove isMainPage template variable which is no longer being used. Change-Id: I124aec9637f3635a335c58e559e578b2a56eb4c5
This commit is contained in:
parent
5d75af50c6
commit
ed5e9dd1d5
|
@ -107,18 +107,11 @@ class MinervaTemplate extends BaseTemplate {
|
||||||
protected function getHistoryLinkHtml( $data ) {
|
protected function getHistoryLinkHtml( $data ) {
|
||||||
$action = Action::getActionName( RequestContext::getMain() );
|
$action = Action::getActionName( RequestContext::getMain() );
|
||||||
if ( isset( $data['historyLink'] ) && $action === 'view' ) {
|
if ( isset( $data['historyLink'] ) && $action === 'view' ) {
|
||||||
$historyLink = $data['historyLink'];
|
|
||||||
$args = [
|
$args = [
|
||||||
'clockIconClass' => MinervaUI::iconClass( 'clock-gray', 'before' ),
|
'clockIconClass' => MinervaUI::iconClass( 'clock-gray', 'before' ),
|
||||||
'arrowIconClass' => MinervaUI::iconClass(
|
'arrowIconClass' => MinervaUI::iconClass(
|
||||||
'arrow-gray', 'element', 'mw-ui-icon-small mf-mw-ui-icon-rotate-anti-clockwise indicator' ),
|
'arrow-gray', 'element', 'mw-ui-icon-small mf-mw-ui-icon-rotate-anti-clockwise indicator' ),
|
||||||
'isMainPage' => $this->getSkin()->getTitle()->isMainPage(),
|
] + $data['historyLink'];
|
||||||
'link' => $historyLink['href'],
|
|
||||||
'text' => $historyLink['text'],
|
|
||||||
'username' => $historyLink['data-user-name'],
|
|
||||||
'userGender' => $historyLink['data-user-gender'],
|
|
||||||
'timestamp' => $historyLink['data-timestamp']
|
|
||||||
];
|
|
||||||
$templateParser = new TemplateParser( __DIR__ );
|
$templateParser = new TemplateParser( __DIR__ );
|
||||||
return $templateParser->processTemplate( 'history', $args );
|
return $templateParser->processTemplate( 'history', $args );
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -717,17 +717,61 @@ class SkinMinerva extends SkinTemplate implements ICustomizableSkin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a history link which describes author and relative time of last edit
|
||||||
|
* @param Title $title The Title object of the page being viewed
|
||||||
|
* @param int $timestamp
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getRelativeHistoryLink( Title $title, $timestamp ) {
|
||||||
|
$user = $this->getUser();
|
||||||
|
$text = $this->msg(
|
||||||
|
'minerva-last-modified-date',
|
||||||
|
$this->getLanguage()->userDate( $timestamp, $user ),
|
||||||
|
$this->getLanguage()->userTime( $timestamp, $user )
|
||||||
|
)->parse();
|
||||||
|
return [
|
||||||
|
// Use $edit['timestamp'] (Unix format) instead of $timestamp (MW format)
|
||||||
|
'data-timestamp' => wfTimestamp( TS_UNIX, $timestamp ),
|
||||||
|
'href' => $this->getHistoryUrl( $title ),
|
||||||
|
'text' => $text,
|
||||||
|
] + $this->getRevisionEditorData( $title );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a history link which makes no reference to user or last edited time
|
||||||
|
* @param Title $title The Title object of the page being viewed
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getGenericHistoryLink( Title $title ) {
|
||||||
|
$text = $this->msg( 'mobile-frontend-history' )->plain();
|
||||||
|
return [
|
||||||
|
'href' => $this->getHistoryUrl( $title ),
|
||||||
|
'text' => $text,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the URL for the history page for the given title using Special:History
|
||||||
|
* when available.
|
||||||
|
* @param Title $title The Title object of the page being viewed
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getHistoryUrl( Title $title ) {
|
||||||
|
return SpecialPageFactory::exists( 'History' ) ?
|
||||||
|
SpecialPage::getTitleFor( 'History', $title )->getLocalURL() :
|
||||||
|
$title->getLocalURL( [ 'action' => 'history' ] );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare the content for the 'last edited' message, e.g. 'Last edited on 30 August
|
* Prepare the content for the 'last edited' message, e.g. 'Last edited on 30 August
|
||||||
* 2013, at 23:31'. This message is different for the main page since main page
|
* 2013, at 23:31'. This message is different for the main page since main page
|
||||||
* content is typically transcuded rather than edited directly.
|
* content is typically transcuded rather than edited directly.
|
||||||
* @param Title $title The Title object of the page being viewed
|
* @param Title $title The Title object of the page being viewed
|
||||||
* @param Boolean $doNotShowRelativeTime (optional) when passed will use a generic
|
|
||||||
* label which makes no reference to the time the page was edited.
|
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getHistoryLink( Title $title, $doNotShowRelativeTime ) {
|
protected function getHistoryLink( Title $title ) {
|
||||||
$user = $this->getUser();
|
$isLatestRevision = $this->getRevisionId() === $title->getLatestRevID();
|
||||||
// Get rev_timestamp of current revision (preloaded by MediaWiki core)
|
// Get rev_timestamp of current revision (preloaded by MediaWiki core)
|
||||||
$timestamp = $this->getOutput()->getRevisionTimestamp();
|
$timestamp = $this->getOutput()->getRevisionTimestamp();
|
||||||
# No cached timestamp, load it from the database
|
# No cached timestamp, load it from the database
|
||||||
|
@ -735,61 +779,34 @@ class SkinMinerva extends SkinTemplate implements ICustomizableSkin {
|
||||||
$timestamp = Revision::getTimestampFromId( $this->getTitle(), $this->getRevisionId() );
|
$timestamp = Revision::getTimestampFromId( $this->getTitle(), $this->getRevisionId() );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main pages tend to include transclusions (see bug 51924)
|
return !$isLatestRevision || $title->isMainPage() ?
|
||||||
if ( $doNotShowRelativeTime ) {
|
$this->getGenericHistoryLink( $title ) :
|
||||||
$lastModified = $this->msg( 'mobile-frontend-history' )->plain();
|
$this->getRelativeHistoryLink( $title, $timestamp );
|
||||||
} else {
|
|
||||||
$lastModified = $this->msg(
|
|
||||||
'minerva-last-modified-date',
|
|
||||||
$this->getLanguage()->userDate( $timestamp, $user ),
|
|
||||||
$this->getLanguage()->userTime( $timestamp, $user )
|
|
||||||
)->parse();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( SpecialPageFactory::exists( 'History' ) ) {
|
|
||||||
$historyUrl = SpecialPage::getTitleFor( 'History', $title )->getLocalURL();
|
|
||||||
} else {
|
|
||||||
$historyUrl = $title->getLocalURL( [ 'action' => 'history' ] );
|
|
||||||
}
|
|
||||||
|
|
||||||
$rev = Revision::newFromTitle( $title );
|
|
||||||
if ( $rev ) {
|
|
||||||
$editor = $this->getRevisionEditor( $rev );
|
|
||||||
} else {
|
|
||||||
$editor = false;
|
|
||||||
}
|
|
||||||
return [
|
|
||||||
// Use $edit['timestamp'] (Unix format) instead of $timestamp (MW format)
|
|
||||||
'data-timestamp' => $doNotShowRelativeTime ? '' : wfTimestamp( TS_UNIX, $timestamp ),
|
|
||||||
'href' => $historyUrl,
|
|
||||||
'text' => $lastModified,
|
|
||||||
'data-user-name' => $editor ? $editor['name'] : '',
|
|
||||||
'data-user-gender' => $editor ? $editor['gender'] : ''
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the editor of a current revision.
|
* Returns data attributes representing the editor for the current revision.
|
||||||
* There appears to be no shorthand method for this in core.
|
* @param Title $title The Title object of the page being viewed
|
||||||
* @return array|false representing user with name and gender fields. False if the editor no longer
|
* @return array representing user with name and gender fields. Empty if the editor no longer
|
||||||
* exists in the database or is hidden from public view
|
* exists in the database or is hidden from public view.
|
||||||
*/
|
*/
|
||||||
private function getRevisionEditor( Revision $rev ) {
|
private function getRevisionEditorData( Title $title ) {
|
||||||
$editorName = '';
|
$rev = Revision::newFromTitle( $title );
|
||||||
$editorGender = '';
|
$result = [];
|
||||||
$revUserId = $rev->getUser();
|
if ( $rev ) {
|
||||||
// Note the user will only be returned if that information is public
|
$revUserId = $rev->getUser();
|
||||||
if ( $revUserId ) {
|
// Note the user will only be returned if that information is public
|
||||||
$revUser = User::newFromId( $revUserId );
|
if ( $revUserId ) {
|
||||||
$editorName = $revUser->getName();
|
$revUser = User::newFromId( $revUserId );
|
||||||
$editorGender = $revUser->getOption( 'gender' );
|
$editorName = $revUser->getName();
|
||||||
} else {
|
$editorGender = $revUser->getOption( 'gender' );
|
||||||
return false;
|
$result += [
|
||||||
|
'data-user-name' => $editorName,
|
||||||
|
'data-user-gender' => $editorGender,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return [
|
return $result;
|
||||||
'name' => $editorName,
|
|
||||||
'gender' => $editorGender,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -888,15 +905,13 @@ class SkinMinerva extends SkinTemplate implements ICustomizableSkin {
|
||||||
$tpl->set( 'postheadinghtml', $postHeadingHtml );
|
$tpl->set( 'postheadinghtml', $postHeadingHtml );
|
||||||
|
|
||||||
if ( $this->canUseWikiPage() ) {
|
if ( $this->canUseWikiPage() ) {
|
||||||
$isLatestRevision = $this->getRevisionId() === $title->getLatestRevID();
|
|
||||||
// If it's a page that exists, add last edited timestamp
|
// If it's a page that exists, add last edited timestamp
|
||||||
// The relative time is only rendered on the latest revision.
|
// The relative time is only rendered on the latest revision.
|
||||||
// For older revisions the last modified
|
// For older revisions the last modified
|
||||||
// information will not render with a relative time
|
// information will not render with a relative time
|
||||||
// nor will it show the name of the editor.
|
// nor will it show the name of the editor.
|
||||||
if ( $this->getWikiPage()->exists() ) {
|
if ( $this->getWikiPage()->exists() ) {
|
||||||
$tpl->set( 'historyLink', $this->getHistoryLink( $title,
|
$tpl->set( 'historyLink', $this->getHistoryLink( $title ) );
|
||||||
!$isLatestRevision || $title->isMainPage() ) );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$tpl->set( 'headinghtml', $this->getHeadingHtml() );
|
$tpl->set( 'headinghtml', $this->getHeadingHtml() );
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
<div class="last-modified-bar view-border-box footer-element">
|
<div class="last-modified-bar view-border-box footer-element">
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<div class="last-modifier-tagline truncated-text {{clockIconClass}}">
|
<div class="last-modifier-tagline truncated-text {{clockIconClass}}">
|
||||||
<div class="{{arrowIconClass}}"></div><a href="{{link}}"
|
<div class="{{arrowIconClass}}"></div><a href="{{href}}"
|
||||||
data-user-name="{{username}}"
|
data-user-name="{{data-user-name}}"
|
||||||
data-user-gender="{{userGender}}"
|
data-user-gender="{{data-user-gender}}"
|
||||||
data-timestamp="{{timestamp}}">
|
data-timestamp="{{data-timestamp}}">
|
||||||
{{text}}
|
{{text}}
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -32,4 +32,4 @@
|
||||||
{{>footer}}
|
{{>footer}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!-- v:8.1 -->
|
<!-- v:8.1.1 -->
|
||||||
|
|
Loading…
Reference in New Issue