mediawiki-extensions-Echo/includes/formatters/EditUserTalkPresentationModel.php
Stephane Bisson 779b7030d9 Truncate usernames, titles and excerpts in notifications
Bug: T121822
Change-Id: Ia0a52926133ab7e04d7d9c2a095ef8f9d0871a49
2016-01-29 07:04:23 -05:00

141 lines
3.8 KiB
PHP

<?php
class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
public function canRender() {
return (bool)$this->event->getTitle();
}
public function getIconType() {
return 'edit-user-talk';
}
public function getPrimaryLink() {
$title = $this->event->getTitle();
if ( !$this->isBundled() && $this->hasSection() ) {
$title = Title::makeTitle(
$title->getNamespace(),
$title->getDBkey(),
$this->formatSubjectAnchor()
);
}
return array(
'url' => $title->getFullURL(),
'label' => $this->msg( 'notification-link-text-view-message' )->text()
);
}
public function getSecondaryLinks() {
$diffLink = array(
'url' => $this->getDiffLinkUrl(),
'label' => $this->msg( 'notification-link-text-view-changes' )->text(),
'description' => '',
'icon' => 'changes',
'prioritized' => true
);
if ( $this->isBundled() ) {
return array( $diffLink );
} else {
return array( $this->getAgentLink(), $diffLink );
}
}
public function getHeaderMessage() {
if ( $this->isBundled() ) {
$msg = $this->msg( "notification-bundle-header-{$this->type}-v2" );
list( $formattedCount, $countForPlural ) = $this->getNotificationCountForOutput();
$msg->params( $formattedCount );
$msg->params( $countForPlural );
$msg->params( $this->getViewingUserForGender() );
return $msg;
} elseif ( $this->hasSection() ) {
$msg = $this->getMessageWithAgent( "notification-header-{$this->type}-with-section" );
$msg->params( $this->getViewingUserForGender() );
$msg->params( $this->getSectionTitleSnippet() );
return $msg;
} else {
$msg = parent::getHeaderMessage();
$msg->params( $this->getViewingUserForGender() );
return $msg;
}
}
public function getBodyMessage() {
if ( !$this->isBundled() && $this->hasSection() ) {
$msg = $this->msg( 'notification-body-edit-user-talk-with-section' );
$msg->params( wfEscapeWikiText( $this->getRevisionSnippet() ) );
return $msg;
} else {
return false;
}
}
private function hasSection() {
return (bool)$this->event->getExtraParam( 'section-title' );
}
/**
* 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();
}
$sectionTitle = $this->event->getExtraParam( 'section-title' );
if ( $sectionTitle === null ) {
return '';
}
// Strip out #
return substr( $wgParser->guessLegacySectionNameFromWikiText( $sectionTitle ), 1 );
}
private function getDiffLinkUrl() {
$revId = $this->event->getExtraParam( 'revid' );
$oldId = $this->isBundled() ? $this->getRevBeforeFirstNotification() : 'prev';
$query = array(
'oldid' => $oldId,
'diff' => $revId,
);
return $this->event->getTitle()->getFullURL( $query );
}
private function getRevBeforeFirstNotification() {
$events = $this->getBundledEvents();
$firstNotificationRevId = end( $events )->getExtraParam( 'revid' );
return $this->event->getTitle()->getPreviousRevisionID( $firstNotificationRevId );
}
}