mediawiki-extensions-Echo/includes/formatters/EditUserTalkPresentationModel.php
Stephane Bisson 1ebb8e3daa Don't add CommentStoreComment as plaintext params
Follow-up Icfc8516 and I3d997f9 (migrating from deprecated Revision
to RevisionRecord)

When generating 'edit-user-talk' events, if we don't have the section-text,
we use the edit summary instead.

Revision->getComment() returns a string but RevisionRecord->getComment()
returns a CommentStoreComment. As a result of the migration, an instance
of CommentStoreComment was put into the event 'extra_data' and later
added as a plain text params during notification presentation.

This patch proposes to
1) Make sure to add the comment's TEXT to the event
2) Test that the 'section-text' is a string before using it
   as a message parameter. This will cause faulty notifications to
   render without a body instead of erroring out. We could write a
   maintenance script to fix those notifications but I don't
   think it's worth it.

Bug: T223741
Change-Id: Ie3a1c59a43b0981ec42fc77e7ebb0e8bd132dbe2
2019-05-21 11:44:54 -04:00

116 lines
3.4 KiB
PHP

<?php
class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
/**
* @var EchoPresentationModelSection
*/
private $section;
/**
* @inheritDoc
*/
protected function __construct( EchoEvent $event, Language $language, User $user, $distributionType ) {
parent::__construct( $event, $language, $user, $distributionType );
$this->section = new EchoPresentationModelSection( $event, $user, $language );
}
public function canRender() {
return (bool)$this->event->getTitle();
}
public function getIconType() {
return 'edit-user-talk';
}
public function getPrimaryLink() {
return [
// Need FullURL so the section is included
'url' => $this->section->getTitleWithSection()->getFullURL(),
'label' => $this->msg( 'notification-link-text-view-message' )->text()
];
}
public function getSecondaryLinks() {
$diffLink = [
'url' => $this->getDiffLinkUrl(),
'label' => $this->msg( 'notification-link-text-view-changes', $this->getViewingUserForGender() )->text(),
'description' => '',
'icon' => 'changes',
'prioritized' => true
];
if ( $this->isBundled() ) {
return [ $diffLink ];
} else {
return [ $this->getAgentLink(), $diffLink ];
}
}
public function getHeaderMessage() {
if ( $this->isBundled() ) {
$msg = $this->msg( 'notification-bundle-header-edit-user-talk-v2' );
$count = $this->getNotificationCountForOutput();
// Repeat is B/C until unused parameter is removed from translations
$msg->numParams( $count, $count );
$msg->params( $this->getViewingUserForGender() );
return $msg;
} elseif ( $this->section->exists() ) {
$msg = $this->getMessageWithAgent( 'notification-header-edit-user-talk-with-section' );
$msg->params( $this->getViewingUserForGender() );
$msg->plaintextParams( $this->section->getTruncatedSectionTitle() );
return $msg;
} else {
$msg = parent::getHeaderMessage();
$msg->params( $this->getViewingUserForGender() );
return $msg;
}
}
public function getCompactHeaderMessage() {
$hasSection = $this->section->exists();
$key = $hasSection
? 'notification-compact-header-edit-user-talk-with-section'
: 'notification-compact-header-edit-user-talk';
$msg = $this->getMessageWithAgent( $key );
$msg->params( $this->getViewingUserForGender() );
if ( $hasSection ) {
$msg->params( $this->section->getTruncatedSectionTitle() );
}
return $msg;
}
public function getBodyMessage() {
$sectionText = $this->event->getExtraParam( 'section-text' );
if ( !$this->isBundled() && $this->section->exists() && is_string( $sectionText ) ) {
$msg = $this->msg( 'notification-body-edit-user-talk-with-section' );
// section-text is safe to use here, because section->exists() returns false if the revision is deleted
$msg->plaintextParams( $sectionText );
return $msg;
} else {
return false;
}
}
private function getDiffLinkUrl() {
$revId = $this->event->getExtraParam( 'revid' );
$oldId = $this->isBundled() ? $this->getRevBeforeFirstNotification() : 'prev';
$query = [
'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 );
}
protected function getSubjectMessageKey() {
return 'notification-edit-talk-page-email-subject2';
}
}