Move section methods into a trait

Change-Id: Ief2005918b92945ff93aea85d17e595c7c38adf2
This commit is contained in:
Roan Kattouw 2016-04-21 19:33:35 -07:00 committed by Catrope
parent 6d6845d9e4
commit 33124f35a8
4 changed files with 78 additions and 100 deletions

View file

@ -75,6 +75,7 @@ $wgAutoloadClasses += array(
'EchoOnWikiList' => __DIR__ . '/includes/ContainmentSet.php',
'EchoPageLinkFormatter' => __DIR__ . '/includes/formatters/PageLinkFormatter.php',
'EchoPageLinkedPresentationModel' => __DIR__ . '/includes/formatters/PageLinkedPresentationModel.php',
'EchoPresentationModelSectionTrait' => __DIR__ . '/includes/formatters/PresentationModelSectionTrait.php',
'EchoRevertedPresentationModel' => __DIR__ . '/includes/formatters/RevertedPresentationModel.php',
'EchoRevisionLocalCache' => __DIR__ . '/includes/cache/RevisionLocalCache.php',
'EchoSeenTime' => __DIR__ . '/includes/SeenTime.php',

View file

@ -1,8 +1,7 @@
<?php
class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
private $sectionTitle = null;
use EchoPresentationModelSectionTrait;
public function canRender() {
return (bool)$this->event->getTitle();
@ -13,18 +12,9 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
}
public function getPrimaryLink() {
$title = $this->event->getTitle();
if ( !$this->isBundled() && $this->hasSection() ) {
$title = Title::makeTitle(
$title->getNamespace(),
$title->getDBkey(),
$this->getSection()
);
}
return array(
// Need FullURL so the section is included
'url' => $title->getFullURL(),
'url' => $this->getTitleWithSection()->getFullURL(),
'label' => $this->msg( 'notification-link-text-view-message' )->text()
);
}
@ -77,29 +67,6 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
}
}
private function hasSection() {
return (bool)$this->getSection();
}
private function getSection() {
if ( $this->sectionTitle !== null ) {
return $this->sectionTitle;
}
$sectionTitle = $this->event->getExtraParam( 'section-title' );
if ( !$sectionTitle ) {
$this->sectionTitle = false;
return false;
}
// Check permissions
if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
$this->sectionTitle = false;
return false;
}
$this->sectionTitle = $sectionTitle;
return $this->sectionTitle;
}
private function getDiffLinkUrl() {
$revId = $this->event->getExtraParam( 'revid' );
$oldId = $this->isBundled() ? $this->getRevBeforeFirstNotification() : 'prev';

View file

@ -1,55 +1,33 @@
<?php
class EchoMentionPresentationModel extends EchoEventPresentationModel {
private $sectionTitle = null;
use EchoPresentationModelSectionTrait;
public function getIconType() {
return 'mention';
}
private function getSection() {
if ( $this->sectionTitle !== null ) {
return $this->sectionTitle;
}
$sectionTitle = $this->event->getExtraParam( 'section-title' );
if ( !$sectionTitle ) {
$this->sectionTitle = false;
return false;
}
// Check permissions
if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
$this->sectionTitle = false;
return false;
}
$this->sectionTitle = $sectionTitle;
return $this->sectionTitle;
}
public function canRender() {
return (bool)$this->event->getTitle();
}
protected function getHeaderMessageKey() {
$noSection = !$this->getSection();
if ( $this->onArticleTalkpage() ) {
return $noSection ?
'notification-header-mention-article-talkpage-nosection' :
'notification-header-mention-article-talkpage';
return $this->hasSection() ?
'notification-header-mention-article-talkpage' :
'notification-header-mention-article-talkpage-nosection';
} elseif ( $this->onAgentTalkpage() ) {
return $noSection ?
'notification-header-mention-agent-talkpage-nosection' :
'notification-header-mention-agent-talkpage';
return $this->hasSection() ?
'notification-header-mention-agent-talkpage' :
'notification-header-mention-agent-talkpage-nosection';
} elseif ( $this->onUserTalkpage() ) {
return $noSection ?
'notification-header-mention-user-talkpage-nosection' :
'notification-header-mention-user-talkpage-v2';
return $this->hasSection() ?
'notification-header-mention-user-talkpage-v2' :
'notification-header-mention-user-talkpage-nosection';
} else {
return $noSection ?
'notification-header-mention-other-nosection' :
'notification-header-mention-other';
return $this->hasSection() ?
'notification-header-mention-other' :
'notification-header-mention-other-nosection';
}
}
@ -71,31 +49,13 @@ class EchoMentionPresentationModel extends EchoEventPresentationModel {
$msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
}
$section = $this->getSection();
if ( $section ) {
$msg->plaintextParams( $this->getTruncatedSectionTitle( $section ) );
if ( $this->hasSection() ) {
$msg->plaintextParams( $this->getTruncatedSectionTitle( $this->getSection() ) );
}
return $msg;
}
/**
* @return Title
*/
private function getTitleWithSection() {
$title = $this->event->getTitle();
$section = $this->getSection();
if ( $section ) {
$title = Title::makeTitle(
$title->getNamespace(),
$title->getDBkey(),
$section
);
}
return $title;
}
public function getBodyMessage() {
$content = $this->event->getExtraParam( 'content' );
if ( $content && $this->userCan( Revision::DELETED_TEXT ) ) {
@ -113,18 +73,9 @@ class EchoMentionPresentationModel extends EchoEventPresentationModel {
}
public function getPrimaryLink() {
$title = $this->event->getTitle();
$section = $this->getSection();
if ( $section ) {
$title = Title::makeTitle(
$title->getNamespace(),
$title->getDBkey(),
$section
);
}
return array(
// Need FullURL so the section is included
'url' => $title->getFullURL(),
'url' => $this->getTitleWithSection()->getFullURL(),
'label' => $this->msg( 'notification-link-text-view-mention' )->text()
);
}

View file

@ -0,0 +1,59 @@
<?php
/**
* Trait that adds section title handling to an EchoEventPresentationModel subclass.
*/
trait EchoPresentationModelSectionTrait {
private $sectionTitle = null;
/**
* Get the section title
* @return string Section title
*/
protected function getSection() {
if ( $this->sectionTitle !== null ) {
return $this->sectionTitle;
}
$sectionTitle = $this->event->getExtraParam( 'section-title' );
if ( !$sectionTitle ) {
$this->sectionTitle = false;
return false;
}
// Check permissions
if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
$this->sectionTitle = false;
return false;
}
$this->sectionTitle = $sectionTitle;
return $this->sectionTitle;
}
/**
* Check if there is a section.
*
* This also returns false if the revision is deleted,
* even if there is a section, because the section can't
* be viewed in that case.
* @return boolean Whether there is a section
*/
protected function hasSection() {
return (bool)$this->getSection();
}
/**
* Get a Title pointing to the section, if available.
* @return Title
*/
protected function getTitleWithSection() {
$title = $this->event->getTitle();
$section = $this->getSection();
if ( $section ) {
$title = Title::makeTitle(
$title->getNamespace(),
$title->getDBkey(),
$section
);
}
return $title;
}
}