Clean up section title handling in mention and edit-user-talk presentation models

They were using different code and different approaches.
I've left getSection() and the code for creating a Title
with a section duplicated for now, and I'll move them
into a trait in the next commit. I wanted to do that
separately because we aren't using traits in this
code base yet.

Change-Id: I56cf745d2cc8a6c43caec08024187de9598cecb8
This commit is contained in:
Roan Kattouw 2016-04-21 18:31:12 -07:00
parent bf2accd3ca
commit c5361da55a
3 changed files with 41 additions and 52 deletions

View file

@ -2,6 +2,8 @@
class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
private $sectionTitle = null;
public function canRender() {
return (bool)$this->event->getTitle();
}
@ -16,11 +18,12 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
$title = Title::makeTitle(
$title->getNamespace(),
$title->getDBkey(),
$this->formatSubjectAnchor()
$this->getSection()
);
}
return array(
// Need FullURL so the section is included
'url' => $title->getFullURL(),
'label' => $this->msg( 'notification-link-text-view-message' )->text()
);
@ -54,7 +57,7 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
} elseif ( $this->hasSection() ) {
$msg = $this->getMessageWithAgent( "notification-header-{$this->type}-with-section" );
$msg->params( $this->getViewingUserForGender() );
$msg->plaintextParams( $this->language->embedBidi( $this->getSectionTitleSnippet() ) );
$msg->plaintextParams( $this->getTruncatedSectionTitle( $this->getSection() ) );
return $msg;
} else {
$msg = parent::getHeaderMessage();
@ -66,7 +69,8 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
public function getBodyMessage() {
if ( !$this->isBundled() && $this->hasSection() ) {
$msg = $this->msg( 'notification-body-edit-user-talk-with-section' );
$msg->plaintextParams( $this->getRevisionSnippet() );
// section-text is safe to use here, because hasSection() returns false if the revision is deleted
$msg->plaintextParams( $this->event->getExtraParam( 'section-text' ) );
return $msg;
} else {
return false;
@ -74,53 +78,26 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
}
private function hasSection() {
return (bool)$this->event->getExtraParam( 'section-title' );
return (bool)$this->getSection();
}
/**
* Get the section title for a talk page post
* @return string
*/
private function getSectionTitleSnippet() {
if ( $this->userCan( Revision::DELETED_TEXT ) ) {
return EchoDiscussionParser::getTextSnippet(
$this->event->getExtraParam( 'section-title' ),
$this->language,
self::SECTION_TITLE_RECOMMENDED_LENGTH
);
} else {
return $this->msg( 'echo-rev-deleted-text-view' )->text();
}
}
private function getRevisionSnippet() {
$sectionText = $this->event->getExtraParam( 'section-text' );
if ( $sectionText === null || !$this->userCan( Revision::DELETED_TEXT ) ) {
return '';
}
return trim( $sectionText );
}
/**
* Extract the subject anchor (linkable portion of the edited page) from
* the event.
*
* @return string The anchor on page, or an empty string
*/
private function formatSubjectAnchor() {
global $wgParser;
if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
return $this->msg( 'echo-rev-deleted-text-view' )->text();
private function getSection() {
if ( $this->sectionTitle !== null ) {
return $this->sectionTitle;
}
$sectionTitle = $this->event->getExtraParam( 'section-title' );
if ( $sectionTitle === null ) {
return '';
if ( !$sectionTitle ) {
$this->sectionTitle = false;
return false;
}
// Check permissions
if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
$this->sectionTitle = false;
return false;
}
// Strip out #
return substr( $wgParser->guessLegacySectionNameFromWikiText( $sectionTitle ), 1 );
$this->sectionTitle = $sectionTitle;
return $this->sectionTitle;
}
private function getDiffLinkUrl() {

View file

@ -373,6 +373,15 @@ abstract class EchoEventPresentationModel implements JsonSerializable {
return $this->language->embedBidi( $this->language->truncate( $text, self::PAGE_NAME_RECOMMENDED_LENGTH, '...', false ) );
}
protected function getTruncatedSectionTitle( $section ) {
return $this->language->embedBidi( $this->language->truncate(
EchoDiscussionParser::getTextSnippet( $section, $this->language ),
self::SECTION_TITLE_RECOMMENDED_LENGTH,
'...',
false
) );
}
/**
* @param User|null $user
* @return array|null

View file

@ -73,13 +73,7 @@ class EchoMentionPresentationModel extends EchoEventPresentationModel {
$section = $this->getSection();
if ( $section ) {
$msg->plaintextParams( $this->language->embedBidi(
EchoDiscussionParser::getTextSnippet(
$section,
$this->language,
self::SECTION_TITLE_RECOMMENDED_LENGTH
)
) );
$msg->plaintextParams( $this->getTruncatedSectionTitle( $section ) );
}
return $msg;
@ -119,9 +113,18 @@ 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' => $this->getTitleWithSection()->getFullURL(),
'url' => $title->getFullURL(),
'label' => $this->msg( 'notification-link-text-view-mention' )->text()
);
}