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 { class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
private $sectionTitle = null;
public function canRender() { public function canRender() {
return (bool)$this->event->getTitle(); return (bool)$this->event->getTitle();
} }
@ -16,11 +18,12 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
$title = Title::makeTitle( $title = Title::makeTitle(
$title->getNamespace(), $title->getNamespace(),
$title->getDBkey(), $title->getDBkey(),
$this->formatSubjectAnchor() $this->getSection()
); );
} }
return array( return array(
// Need FullURL so the section is included
'url' => $title->getFullURL(), 'url' => $title->getFullURL(),
'label' => $this->msg( 'notification-link-text-view-message' )->text() 'label' => $this->msg( 'notification-link-text-view-message' )->text()
); );
@ -54,7 +57,7 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
} elseif ( $this->hasSection() ) { } elseif ( $this->hasSection() ) {
$msg = $this->getMessageWithAgent( "notification-header-{$this->type}-with-section" ); $msg = $this->getMessageWithAgent( "notification-header-{$this->type}-with-section" );
$msg->params( $this->getViewingUserForGender() ); $msg->params( $this->getViewingUserForGender() );
$msg->plaintextParams( $this->language->embedBidi( $this->getSectionTitleSnippet() ) ); $msg->plaintextParams( $this->getTruncatedSectionTitle( $this->getSection() ) );
return $msg; return $msg;
} else { } else {
$msg = parent::getHeaderMessage(); $msg = parent::getHeaderMessage();
@ -66,7 +69,8 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
public function getBodyMessage() { public function getBodyMessage() {
if ( !$this->isBundled() && $this->hasSection() ) { if ( !$this->isBundled() && $this->hasSection() ) {
$msg = $this->msg( 'notification-body-edit-user-talk-with-section' ); $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; return $msg;
} else { } else {
return false; return false;
@ -74,53 +78,26 @@ class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
} }
private function hasSection() { private function hasSection() {
return (bool)$this->event->getExtraParam( 'section-title' ); return (bool)$this->getSection();
} }
/** private function getSection() {
* Get the section title for a talk page post if ( $this->sectionTitle !== null ) {
* @return string return $this->sectionTitle;
*/
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();
} }
$sectionTitle = $this->event->getExtraParam( 'section-title' ); $sectionTitle = $this->event->getExtraParam( 'section-title' );
if ( $sectionTitle === null ) { if ( !$sectionTitle ) {
return ''; $this->sectionTitle = false;
return false;
}
// Check permissions
if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
$this->sectionTitle = false;
return false;
} }
// Strip out # $this->sectionTitle = $sectionTitle;
return substr( $wgParser->guessLegacySectionNameFromWikiText( $sectionTitle ), 1 ); return $this->sectionTitle;
} }
private function getDiffLinkUrl() { 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 ) ); 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 * @param User|null $user
* @return array|null * @return array|null

View file

@ -73,13 +73,7 @@ class EchoMentionPresentationModel extends EchoEventPresentationModel {
$section = $this->getSection(); $section = $this->getSection();
if ( $section ) { if ( $section ) {
$msg->plaintextParams( $this->language->embedBidi( $msg->plaintextParams( $this->getTruncatedSectionTitle( $section ) );
EchoDiscussionParser::getTextSnippet(
$section,
$this->language,
self::SECTION_TITLE_RECOMMENDED_LENGTH
)
) );
} }
return $msg; return $msg;
@ -119,9 +113,18 @@ class EchoMentionPresentationModel extends EchoEventPresentationModel {
} }
public function getPrimaryLink() { public function getPrimaryLink() {
$title = $this->event->getTitle();
$section = $this->getSection();
if ( $section ) {
$title = Title::makeTitle(
$title->getNamespace(),
$title->getDBkey(),
$section
);
}
return array( return array(
// Need FullURL so the section is included // Need FullURL so the section is included
'url' => $this->getTitleWithSection()->getFullURL(), 'url' => $title->getFullURL(),
'label' => $this->msg( 'notification-link-text-view-mention' )->text() 'label' => $this->msg( 'notification-link-text-view-mention' )->text()
); );
} }