2015-08-19 20:22:45 +00:00
|
|
|
<?php
|
|
|
|
|
2017-05-05 18:51:04 +00:00
|
|
|
use Wikimedia\Timestamp\TimestampException;
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* Class that returns structured data based
|
|
|
|
* on the provided event.
|
|
|
|
*/
|
2016-02-12 22:15:23 +00:00
|
|
|
abstract class EchoEventPresentationModel implements JsonSerializable {
|
2015-08-19 20:22:45 +00:00
|
|
|
|
2016-01-20 21:19:54 +00:00
|
|
|
/**
|
2018-06-15 20:01:16 +00:00
|
|
|
* Recommended length of usernames included in messages, in
|
|
|
|
* characters (not bytes).
|
2016-01-20 21:19:54 +00:00
|
|
|
*/
|
2016-02-09 18:46:39 +00:00
|
|
|
const USERNAME_RECOMMENDED_LENGTH = 20;
|
2016-01-20 21:19:54 +00:00
|
|
|
|
2016-02-17 17:27:49 +00:00
|
|
|
/**
|
2018-06-15 20:01:16 +00:00
|
|
|
* Recommended length of usernames used as link label, in
|
|
|
|
* characters (not bytes).
|
2016-02-17 17:27:49 +00:00
|
|
|
*/
|
|
|
|
const USERNAME_AS_LABEL_RECOMMENDED_LENGTH = 15;
|
|
|
|
|
2016-01-20 21:19:54 +00:00
|
|
|
/**
|
2018-06-15 20:01:16 +00:00
|
|
|
* Recommended length of page names included in messages, in
|
|
|
|
* characters (not bytes).
|
2016-01-20 21:19:54 +00:00
|
|
|
*/
|
|
|
|
const PAGE_NAME_RECOMMENDED_LENGTH = 50;
|
|
|
|
|
2016-02-17 17:27:49 +00:00
|
|
|
/**
|
2018-06-15 20:01:16 +00:00
|
|
|
* Recommended length of page names used as link label, in
|
|
|
|
* characters (not bytes).
|
2016-02-17 17:27:49 +00:00
|
|
|
*/
|
|
|
|
const PAGE_NAME_AS_LABEL_RECOMMENDED_LENGTH = 15;
|
|
|
|
|
2016-01-20 21:19:54 +00:00
|
|
|
/**
|
2018-06-15 20:01:16 +00:00
|
|
|
* Recommended length of section titles included in messages, in
|
|
|
|
* characters (not bytes).
|
2016-01-20 21:19:54 +00:00
|
|
|
*/
|
2016-02-15 20:59:00 +00:00
|
|
|
const SECTION_TITLE_RECOMMENDED_LENGTH = 50;
|
2016-01-20 21:19:54 +00:00
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* @var EchoEvent
|
|
|
|
*/
|
|
|
|
protected $event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Language
|
|
|
|
*/
|
|
|
|
protected $language;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User for permissions checking
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
|
|
|
* @var string 'web' or 'email'
|
|
|
|
*/
|
|
|
|
private $distributionType;
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* @param EchoEvent $event
|
2018-05-28 04:54:08 +00:00
|
|
|
* @param Language $language
|
2015-11-12 22:34:24 +00:00
|
|
|
* @param User $user Only used for permissions checking and GENDER
|
2015-10-29 21:14:35 +00:00
|
|
|
* @param string $distributionType
|
2015-08-19 20:22:45 +00:00
|
|
|
*/
|
2018-08-25 10:51:14 +00:00
|
|
|
protected function __construct(
|
|
|
|
EchoEvent $event,
|
|
|
|
Language $language,
|
|
|
|
User $user,
|
|
|
|
$distributionType
|
|
|
|
) {
|
2015-08-19 20:22:45 +00:00
|
|
|
$this->event = $event;
|
|
|
|
$this->type = $event->getType();
|
2018-05-28 04:54:08 +00:00
|
|
|
$this->language = $language;
|
2015-08-19 20:22:45 +00:00
|
|
|
$this->user = $user;
|
2015-10-29 21:14:35 +00:00
|
|
|
$this->distributionType = $distributionType;
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to detect whether the event type
|
2018-07-03 16:49:20 +00:00
|
|
|
* has a presentation model available for rendering
|
2015-08-19 20:22:45 +00:00
|
|
|
*
|
|
|
|
* @param string $type event type
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function supportsPresentationModel( $type ) {
|
|
|
|
global $wgEchoNotifications;
|
2018-07-03 16:49:20 +00:00
|
|
|
return isset( $wgEchoNotifications[$type]['presentation-model'] )
|
|
|
|
&& class_exists( $wgEchoNotifications[$type]['presentation-model'] );
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EchoEvent $event
|
2018-05-28 04:54:08 +00:00
|
|
|
* @param Language $language
|
2015-08-19 20:22:45 +00:00
|
|
|
* @param User $user
|
2015-10-29 21:14:35 +00:00
|
|
|
* @param string $distributionType 'web' or 'email'
|
2015-08-19 20:22:45 +00:00
|
|
|
* @return EchoEventPresentationModel
|
|
|
|
*/
|
2018-08-25 10:51:14 +00:00
|
|
|
public static function factory(
|
|
|
|
EchoEvent $event,
|
|
|
|
Language $language,
|
|
|
|
User $user,
|
|
|
|
$distributionType = 'web'
|
|
|
|
) {
|
2015-08-19 20:22:45 +00:00
|
|
|
global $wgEchoNotifications;
|
|
|
|
// @todo don't depend upon globals
|
|
|
|
|
|
|
|
$class = $wgEchoNotifications[$event->getType()]['presentation-model'];
|
2015-10-29 21:14:35 +00:00
|
|
|
return new $class( $event, $language, $user, $distributionType );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of event
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final public function getType() {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2016-08-05 21:44:55 +00:00
|
|
|
/**
|
|
|
|
* Get the user receiving the notification
|
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
final public function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
|
|
|
* Get the category of event
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final public function getCategory() {
|
|
|
|
return $this->event->getCategory();
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent to IContextSource::msg for the current
|
|
|
|
* language
|
|
|
|
*
|
2019-03-10 01:54:04 +00:00
|
|
|
* @param string ...$args
|
2015-08-19 20:22:45 +00:00
|
|
|
* @return Message
|
|
|
|
*/
|
2019-03-10 01:54:04 +00:00
|
|
|
protected function msg( ...$args ) {
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* @var Message $msg
|
|
|
|
*/
|
2019-03-10 01:54:04 +00:00
|
|
|
$msg = wfMessage( ...$args );
|
2015-08-19 20:22:45 +00:00
|
|
|
$msg->inLanguage( $this->language );
|
|
|
|
|
2016-09-14 20:38:55 +00:00
|
|
|
// Notifications are considered UI (and should be in UI language, not
|
|
|
|
// content), and this flag is set false by inLanguage.
|
|
|
|
$msg->setInterfaceMessageFlag( true );
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2015-11-19 18:06:58 +00:00
|
|
|
/**
|
|
|
|
* @return EchoEvent[]
|
|
|
|
*/
|
|
|
|
final protected function getBundledEvents() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return $this->event->getBundledEvents() ?: [];
|
2015-11-19 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* Get the ids of the bundled notifications or false if it's not bundled
|
|
|
|
*
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return int[]|false
|
2016-06-02 14:00:48 +00:00
|
|
|
*/
|
|
|
|
public function getBundledIds() {
|
|
|
|
if ( $this->isBundled() ) {
|
|
|
|
return array_map( function ( EchoEvent $event ) {
|
|
|
|
return $event->getId();
|
|
|
|
}, $this->getBundledEvents() );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-27 14:37:19 +00:00
|
|
|
/**
|
2015-11-30 22:31:19 +00:00
|
|
|
* This method returns true when there are bundled notifications, even if they are all
|
|
|
|
* in the same group according to getBundleGrouping(). For presentation purposes, you may
|
|
|
|
* want to check if getBundleCount( true, $yourCallback ) > 1 instead.
|
|
|
|
*
|
|
|
|
* @return bool Whether there are other notifications bundled with this one.
|
2015-11-27 14:37:19 +00:00
|
|
|
*/
|
|
|
|
final protected function isBundled() {
|
|
|
|
return $this->getBundleCount() > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-30 22:31:19 +00:00
|
|
|
* Count the number of event groups in this bundle.
|
|
|
|
*
|
|
|
|
* By default, each event is in its own group, and this method returns the number of events.
|
|
|
|
* To group events differently, pass $groupCallback. For example, to group events with the
|
|
|
|
* same title together, use $callback = function ( $event ) { return $event->getTitle()->getPrefixedText(); }
|
|
|
|
*
|
|
|
|
* If $includeCurrent is false, all events in the same group as the current one will be ignored.
|
|
|
|
*
|
|
|
|
* @param bool $includeCurrent Include the current event (and its group)
|
2018-05-26 02:15:41 +00:00
|
|
|
* @param callable|null $groupCallback Callback that takes an EchoEvent and returns a grouping value
|
2015-11-30 22:31:19 +00:00
|
|
|
* @return int Number of bundled events or groups
|
2015-12-14 18:41:54 +00:00
|
|
|
* @throws InvalidArgumentException
|
2015-11-27 14:37:19 +00:00
|
|
|
*/
|
2015-11-30 22:31:19 +00:00
|
|
|
final protected function getBundleCount( $includeCurrent = true, $groupCallback = null ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$events = array_merge( $this->getBundledEvents(), [ $this->event ] );
|
2015-11-30 22:31:19 +00:00
|
|
|
if ( $groupCallback ) {
|
|
|
|
if ( !is_callable( $groupCallback ) ) {
|
|
|
|
// If we pass an invalid callback to array_map(), it'll just throw a warning
|
|
|
|
// and return NULL, so $count ends up being 0 or -1. Instead of doing that,
|
|
|
|
// throw an exception.
|
2015-12-14 18:41:54 +00:00
|
|
|
throw new InvalidArgumentException( 'Invalid callback passed to getBundleCount' );
|
2015-11-30 22:31:19 +00:00
|
|
|
}
|
|
|
|
$events = array_unique( array_map( $groupCallback, $events ) );
|
|
|
|
}
|
|
|
|
$count = count( $events );
|
|
|
|
|
|
|
|
if ( !$includeCurrent ) {
|
|
|
|
$count--;
|
2015-11-27 14:37:19 +00:00
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the count of notifications bundled together.
|
2015-11-30 22:31:19 +00:00
|
|
|
*
|
2018-08-13 07:17:15 +00:00
|
|
|
* For parameters, see {@see EchoEventPresentationModel::getBundleCount}.
|
2015-11-30 22:31:19 +00:00
|
|
|
*
|
2015-11-27 14:37:19 +00:00
|
|
|
* @param bool $includeCurrent
|
2018-05-26 02:15:41 +00:00
|
|
|
* @param callable|null $groupCallback
|
2016-09-11 19:46:08 +00:00
|
|
|
* @return int count
|
2015-11-27 14:37:19 +00:00
|
|
|
*/
|
2015-11-30 22:31:19 +00:00
|
|
|
final protected function getNotificationCountForOutput( $includeCurrent = true, $groupCallback = null ) {
|
|
|
|
$count = $this->getBundleCount( $includeCurrent, $groupCallback );
|
2016-03-09 04:50:31 +00:00
|
|
|
$cappedCount = EchoNotificationController::getCappedNotificationCount( $count );
|
|
|
|
return $cappedCount;
|
2015-11-27 14:37:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* @return string The symbolic icon name as defined in $wgEchoNotificationIcons
|
|
|
|
*/
|
|
|
|
abstract public function getIconType();
|
|
|
|
|
2015-10-29 21:13:10 +00:00
|
|
|
/**
|
|
|
|
* @return string Timestamp the event occurred at
|
|
|
|
*/
|
|
|
|
final public function getTimestamp() {
|
|
|
|
return $this->event->getTimestamp();
|
|
|
|
}
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* Helper for EchoEvent::userCan
|
|
|
|
*
|
|
|
|
* @param int $type Revision::DELETED_* constant
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
final protected function userCan( $type ) {
|
|
|
|
return $this->event->userCan( $type, $this->user );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return string[]|false ['wikitext to display', 'username for GENDER'], false if no agent
|
2015-08-19 20:22:45 +00:00
|
|
|
*
|
|
|
|
* We have to display wikitext so we can add CSS classes for revision deleted user.
|
|
|
|
* The goal of this function is for callers not to worry about whether
|
|
|
|
* the user is visible or not.
|
|
|
|
* @par Example:
|
|
|
|
* @code
|
|
|
|
* list( $formattedName, $genderName ) = $this->getAgentForOutput();
|
|
|
|
* $msg->params( $formattedName, $genderName );
|
|
|
|
* @endcode
|
|
|
|
*/
|
|
|
|
final protected function getAgentForOutput() {
|
|
|
|
$agent = $this->event->getAgent();
|
|
|
|
if ( !$agent ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->userCan( Revision::DELETED_USER ) ) {
|
|
|
|
// Not deleted
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2016-03-10 00:22:52 +00:00
|
|
|
$this->getTruncatedUsername( $agent ),
|
2016-02-24 21:34:53 +00:00
|
|
|
$agent->getName()
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2015-08-19 20:22:45 +00:00
|
|
|
} else {
|
|
|
|
// Deleted/hidden
|
|
|
|
$msg = $this->msg( 'rev-deleted-user' )->plain();
|
|
|
|
// HACK: Pass an invalid username to GENDER to force the default
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ '<span class="history-deleted">' . $msg . '</span>', '[]' ];
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 18:13:29 +00:00
|
|
|
/**
|
|
|
|
* Return a message with the given key and the agent's
|
|
|
|
* formatted name and name for GENDER as 1st and
|
|
|
|
* 2nd parameters.
|
|
|
|
* @param string $key
|
|
|
|
* @return Message
|
|
|
|
*/
|
|
|
|
final protected function getMessageWithAgent( $key ) {
|
|
|
|
$msg = $this->msg( $key );
|
|
|
|
list( $formattedName, $genderName ) = $this->getAgentForOutput();
|
|
|
|
$msg->params( $formattedName, $genderName );
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:34:24 +00:00
|
|
|
/**
|
|
|
|
* Get the viewing user's name for usage in GENDER
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final protected function getViewingUserForGender() {
|
|
|
|
return $this->user->getName();
|
|
|
|
}
|
|
|
|
|
2015-12-22 15:57:13 +00:00
|
|
|
/**
|
|
|
|
* @return array|null Link object to the user's page or Special:Contributions for anon users.
|
|
|
|
* Can be used for primary or secondary links.
|
|
|
|
* Same format as secondary link.
|
|
|
|
* Returns null if the current user cannot see the agent.
|
|
|
|
*/
|
|
|
|
final protected function getAgentLink() {
|
2016-02-17 17:27:49 +00:00
|
|
|
return $this->getUserLink( $this->event->getAgent() );
|
2015-12-22 15:57:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 17:15:25 +00:00
|
|
|
/**
|
|
|
|
* To be overridden by subclasses if they are unable to render the
|
|
|
|
* notification, for example when a page is deleted.
|
|
|
|
* If this function returns false, no other methods will be called
|
|
|
|
* on the object.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canRender() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* @return string Message key that will be used in getHeaderMessage
|
|
|
|
*/
|
|
|
|
protected function getHeaderMessageKey() {
|
|
|
|
return "notification-header-{$this->type}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a message object and add the performer's name as
|
|
|
|
* a parameter. It is expected that subclasses will override
|
|
|
|
* this.
|
|
|
|
*
|
|
|
|
* @return Message
|
|
|
|
*/
|
|
|
|
public function getHeaderMessage() {
|
2015-11-17 18:13:29 +00:00
|
|
|
return $this->getMessageWithAgent( $this->getHeaderMessageKey() );
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 20:08:16 +00:00
|
|
|
/**
|
|
|
|
* @return string Message key that will be used in getCompactHeaderMessage
|
|
|
|
*/
|
|
|
|
public function getCompactHeaderMessageKey() {
|
|
|
|
return "notification-compact-header-{$this->type}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a message object and add the performer's name as
|
|
|
|
* a parameter. It is expected that subclasses will override
|
|
|
|
* this.
|
|
|
|
*
|
|
|
|
* This message should be more compact than the header message
|
2016-07-21 16:25:46 +00:00
|
|
|
* ( getHeaderMessage() ). It is displayed when a
|
2016-06-07 20:08:16 +00:00
|
|
|
* notification is part of an expanded bundle.
|
|
|
|
*
|
|
|
|
* @return Message
|
|
|
|
*/
|
|
|
|
public function getCompactHeaderMessage() {
|
|
|
|
$msg = $this->getMessageWithAgent( $this->getCompactHeaderMessageKey() );
|
|
|
|
if ( $msg->isDisabled() ) {
|
|
|
|
// Back-compat for models that haven't been updated yet
|
|
|
|
$msg = $this->getHeaderMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2015-10-29 21:14:35 +00:00
|
|
|
/**
|
|
|
|
* @return string Message key that will be used in getSubjectMessage
|
|
|
|
*/
|
|
|
|
protected function getSubjectMessageKey() {
|
|
|
|
return "notification-subject-{$this->type}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a message object and add the performer's name as
|
|
|
|
* a parameter. It is expected that subclasses will override
|
|
|
|
* this. The output of the message should be plaintext.
|
|
|
|
*
|
2016-07-21 16:25:46 +00:00
|
|
|
* This message is used as the subject line in
|
|
|
|
* single-notification emails.
|
|
|
|
*
|
|
|
|
* For backward compatibility, if this is not defined,
|
|
|
|
* the header message ( getHeaderMessage() ) is used instead.
|
|
|
|
*
|
2015-10-29 21:14:35 +00:00
|
|
|
* @return Message
|
|
|
|
*/
|
|
|
|
public function getSubjectMessage() {
|
|
|
|
$msg = $this->getMessageWithAgent( $this->getSubjectMessageKey() );
|
2019-04-04 21:37:03 +00:00
|
|
|
$msg->params( $this->getViewingUserForGender() );
|
2015-10-29 21:14:35 +00:00
|
|
|
if ( $msg->isDisabled() ) {
|
|
|
|
// Back-compat for models that haven't been updated yet
|
|
|
|
$msg = $this->getHeaderMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
2015-11-04 20:41:29 +00:00
|
|
|
* Get a message for the notification's body, false if it has no body
|
2015-08-19 20:22:45 +00:00
|
|
|
*
|
|
|
|
* @return bool|Message
|
|
|
|
*/
|
2015-11-04 20:41:29 +00:00
|
|
|
public function getBodyMessage() {
|
2015-08-19 20:22:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-19 15:41:18 +00:00
|
|
|
* Array of primary link details, with possibly-relative URL & label.
|
2015-08-19 20:22:45 +00:00
|
|
|
*
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return array|false Array of link data, or false for no link:
|
2016-01-12 21:50:41 +00:00
|
|
|
* ['url' => (string) url, 'label' => (string) link text (non-escaped)]
|
2015-08-19 20:22:45 +00:00
|
|
|
*/
|
|
|
|
abstract public function getPrimaryLink();
|
|
|
|
|
2016-05-03 01:56:13 +00:00
|
|
|
/**
|
|
|
|
* Like getPrimaryLink(), but with the URL altered to add ?markasread=XYZ. When this link is followed,
|
|
|
|
* the notification is marked as read.
|
2016-06-13 20:41:12 +00:00
|
|
|
*
|
2018-09-03 17:01:06 +00:00
|
|
|
* If the notification is a bundle, the notification IDs are added to the parameter value
|
|
|
|
* separated by a "|". If cross-wiki notifications are enabled, a markasreadwiki parameter is
|
|
|
|
* added.
|
2016-06-13 20:41:12 +00:00
|
|
|
*
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return array|false
|
2016-05-03 01:56:13 +00:00
|
|
|
*/
|
2016-09-11 19:46:08 +00:00
|
|
|
final public function getPrimaryLinkWithMarkAsRead() {
|
2018-09-03 17:01:06 +00:00
|
|
|
global $wgEchoCrossWikiNotifications;
|
2016-05-03 01:56:13 +00:00
|
|
|
$primaryLink = $this->getPrimaryLink();
|
|
|
|
if ( $primaryLink ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$eventIds = [ $this->event->getId() ];
|
2016-06-13 20:41:12 +00:00
|
|
|
if ( $this->getBundledIds() ) {
|
|
|
|
$eventIds = array_merge( $eventIds, $this->getBundledIds() );
|
|
|
|
}
|
2018-09-03 17:01:06 +00:00
|
|
|
|
|
|
|
$queryParams = [ 'markasread' => implode( '|', $eventIds ) ];
|
|
|
|
if ( $wgEchoCrossWikiNotifications ) {
|
|
|
|
$queryParams['markasreadwiki'] = wfWikiID();
|
|
|
|
}
|
|
|
|
|
|
|
|
$primaryLink['url'] = wfAppendQuery( $primaryLink['url'], $queryParams );
|
2016-05-03 01:56:13 +00:00
|
|
|
}
|
|
|
|
return $primaryLink;
|
|
|
|
}
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
2015-11-19 15:41:18 +00:00
|
|
|
* Array of secondary link details, including possibly-relative URLs, label,
|
|
|
|
* description & icon name.
|
2015-08-19 20:22:45 +00:00
|
|
|
*
|
2015-11-19 15:41:18 +00:00
|
|
|
* @return array Array of links in the format of:
|
2016-01-12 21:50:41 +00:00
|
|
|
* [['url' => (string) url,
|
|
|
|
* 'label' => (string) link text (non-escaped),
|
2016-08-24 23:45:02 +00:00
|
|
|
* 'description' => (string) descriptive text (optional, non-escaped),
|
2016-08-05 21:44:55 +00:00
|
|
|
* 'icon' => (bool|string) symbolic ooui icon name (or false if there is none),
|
2018-08-25 10:51:14 +00:00
|
|
|
* 'type' => (string) optional action type. Used to note a dynamic action,
|
|
|
|
* by setting it to 'dynamic-action'
|
2018-08-22 14:13:10 +00:00
|
|
|
* 'data' => (array) optional array containing information about the dynamic
|
|
|
|
* action. It must include 'tokenType' (string), 'messages' (array)
|
|
|
|
* with messages supplied for the item and the confirmation dialog
|
|
|
|
* and 'params' (array) for the API operation needed to complete the
|
|
|
|
* action. For example:
|
2016-08-05 21:44:55 +00:00
|
|
|
* 'data' => [
|
|
|
|
* 'tokenType' => 'watch',
|
|
|
|
* 'params' => [
|
|
|
|
* 'action' => 'watch',
|
|
|
|
* 'titles' => 'Namespace:SomeTitle'
|
|
|
|
* ],
|
|
|
|
* 'messages' => [
|
|
|
|
* 'confirmation' => [
|
|
|
|
* 'title' => 'message (parsed as HTML)',
|
|
|
|
* 'description' => 'optional message (parsed as HTML)'
|
|
|
|
* ]
|
|
|
|
* ]
|
|
|
|
* ]
|
2017-09-09 01:07:20 +00:00
|
|
|
* 'prioritized' => (bool) true to request the link be placed outside the action menu.
|
|
|
|
* false or omitted for the default behavior. By default, a link will
|
|
|
|
* be placed inside the menu, unless there are maxPrioritizedActions
|
|
|
|
* or fewer secondary links. If there are maxPrioritizedActions or
|
|
|
|
* fewer secondary links, they will all appear outside the action menu.
|
|
|
|
* At most maxPrioritizedActions links will be placed outside the action menu.
|
|
|
|
* maxPrioritizedActions is 2 on desktop and 1 on mobile.
|
2015-11-19 15:41:18 +00:00
|
|
|
* ...]
|
2015-12-22 15:57:13 +00:00
|
|
|
*
|
|
|
|
* Note that you should call array_values(array_filter()) on the
|
|
|
|
* result of this function (FIXME).
|
2015-08-19 20:22:45 +00:00
|
|
|
*/
|
|
|
|
public function getSecondaryLinks() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|
2015-11-16 16:05:07 +00:00
|
|
|
|
2016-03-09 20:13:48 +00:00
|
|
|
/**
|
|
|
|
* Get the ID of the associated event
|
|
|
|
* @return int Event id
|
|
|
|
*/
|
|
|
|
public function getEventId() {
|
|
|
|
return $this->event->getId();
|
|
|
|
}
|
|
|
|
|
2015-11-16 16:05:07 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws TimestampException
|
|
|
|
*/
|
|
|
|
public function jsonSerialize() {
|
|
|
|
$body = $this->getBodyMessage();
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2015-11-16 16:05:07 +00:00
|
|
|
'header' => $this->getHeaderMessage()->parse(),
|
2016-07-07 17:16:26 +00:00
|
|
|
'compactHeader' => $this->getCompactHeaderMessage()->parse(),
|
2016-02-25 18:19:19 +00:00
|
|
|
'body' => $body ? $body->escaped() : '',
|
2015-11-16 16:05:07 +00:00
|
|
|
'icon' => $this->getIconType(),
|
2016-12-05 18:51:07 +00:00
|
|
|
'links' => [
|
|
|
|
'primary' => $this->getPrimaryLinkWithMarkAsRead() ?: [],
|
2015-12-22 15:57:13 +00:00
|
|
|
'secondary' => array_values( array_filter( $this->getSecondaryLinks() ) ),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2015-11-16 16:05:07 +00:00
|
|
|
}
|
2016-01-20 21:19:54 +00:00
|
|
|
|
2017-01-30 10:54:55 +00:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-01-20 21:19:54 +00:00
|
|
|
protected function getTruncatedUsername( User $user ) {
|
2018-08-25 10:51:14 +00:00
|
|
|
return $this->language->embedBidi( $this->language->truncateForVisual(
|
|
|
|
$user->getName(), self::USERNAME_RECOMMENDED_LENGTH, '...', false ) );
|
2016-01-20 21:19:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 10:54:55 +00:00
|
|
|
/**
|
|
|
|
* @param Title $title
|
|
|
|
* @param bool $includeNamespace
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-01-20 21:19:54 +00:00
|
|
|
protected function getTruncatedTitleText( Title $title, $includeNamespace = false ) {
|
|
|
|
$text = $includeNamespace ? $title->getPrefixedText() : $title->getText();
|
2018-08-25 10:51:14 +00:00
|
|
|
return $this->language->embedBidi( $this->language->truncateForVisual(
|
|
|
|
$text, self::PAGE_NAME_RECOMMENDED_LENGTH, '...', false ) );
|
2016-01-20 21:19:54 +00:00
|
|
|
}
|
2016-02-17 17:27:49 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-24 16:42:03 +00:00
|
|
|
* @param User|null $user
|
2016-02-17 17:27:49 +00:00
|
|
|
* @return array|null
|
|
|
|
*/
|
2016-02-24 16:42:03 +00:00
|
|
|
final protected function getUserLink( $user ) {
|
|
|
|
if ( !$user ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-02-17 17:27:49 +00:00
|
|
|
if ( !$this->userCan( Revision::DELETED_USER ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = $user->isAnon()
|
|
|
|
? SpecialPage::getTitleFor( 'Contributions', $user->getName() )->getFullURL()
|
|
|
|
: $user->getUserPage()->getFullURL();
|
|
|
|
|
|
|
|
$label = $user->getName();
|
2018-08-25 10:51:14 +00:00
|
|
|
$truncatedLabel = $this->language->truncateForVisual(
|
|
|
|
$label, self::USERNAME_AS_LABEL_RECOMMENDED_LENGTH, '...', false );
|
2016-02-17 17:27:49 +00:00
|
|
|
$isTruncated = $label !== $truncatedLabel;
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2016-02-17 17:27:49 +00:00
|
|
|
'url' => $url,
|
2016-02-29 20:48:11 +00:00
|
|
|
'label' => $this->language->embedBidi( $truncatedLabel ),
|
2016-02-17 17:27:49 +00:00
|
|
|
'tooltip' => $isTruncated ? $label : '',
|
|
|
|
'description' => '',
|
|
|
|
'icon' => 'userAvatar',
|
|
|
|
'prioritized' => true,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-02-17 17:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title
|
|
|
|
* @param string $description
|
|
|
|
* @param bool $prioritized
|
|
|
|
* @param array $query
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
final protected function getPageLink( Title $title, $description, $prioritized, $query = [] ) {
|
2016-02-23 11:53:55 +00:00
|
|
|
if ( $title->getNamespace() === NS_USER_TALK ) {
|
2016-02-17 17:27:49 +00:00
|
|
|
$icon = 'userSpeechBubble';
|
2016-02-23 11:53:55 +00:00
|
|
|
} elseif ( $title->isTalkPage() ) {
|
2016-02-17 17:27:49 +00:00
|
|
|
$icon = 'speechBubbles';
|
|
|
|
} else {
|
|
|
|
$icon = 'article';
|
|
|
|
}
|
2016-02-29 20:48:11 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2016-02-17 17:27:49 +00:00
|
|
|
'url' => $title->getFullURL( $query ),
|
2016-02-29 20:48:11 +00:00
|
|
|
'label' => $this->language->embedBidi(
|
2018-08-25 10:51:14 +00:00
|
|
|
$this->language->truncateForVisual(
|
|
|
|
$title->getText(), self::PAGE_NAME_AS_LABEL_RECOMMENDED_LENGTH, '...', false )
|
2016-02-29 20:48:11 +00:00
|
|
|
),
|
2016-02-17 17:27:49 +00:00
|
|
|
'tooltip' => $title->getPrefixedText(),
|
|
|
|
'description' => $description,
|
|
|
|
'icon' => $icon,
|
|
|
|
'prioritized' => $prioritized,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-02-17 17:27:49 +00:00
|
|
|
}
|
2016-08-05 21:44:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a dynamic action link
|
|
|
|
*
|
|
|
|
* @param Title $title Title relating to this action
|
|
|
|
* @param bool $icon Optional. Symbolic name of the OOUI icon to use
|
2016-08-24 23:45:02 +00:00
|
|
|
* @param string $label link text (non-escaped)
|
2018-05-26 02:15:41 +00:00
|
|
|
* @param string|null $description descriptive text (optional, non-escaped)
|
2016-08-05 21:44:55 +00:00
|
|
|
* @param array $data Action data
|
|
|
|
* @param array $query
|
|
|
|
* @return array Array compatible with the structure of
|
|
|
|
* secondary links
|
|
|
|
*/
|
2018-08-25 10:51:14 +00:00
|
|
|
final protected function getDynamicActionLink(
|
|
|
|
Title $title,
|
|
|
|
$icon,
|
|
|
|
$label,
|
|
|
|
$description = null,
|
|
|
|
$data = [],
|
|
|
|
$query = []
|
|
|
|
) {
|
2016-08-05 21:44:55 +00:00
|
|
|
if ( !$icon && $title->getNamespace() === NS_USER_TALK ) {
|
|
|
|
$icon = 'userSpeechBubble';
|
|
|
|
} elseif ( !$icon && $title->isTalkPage() ) {
|
|
|
|
$icon = 'speechBubbles';
|
2016-12-05 18:51:07 +00:00
|
|
|
} elseif ( !$icon ) {
|
2016-08-05 21:44:55 +00:00
|
|
|
$icon = 'article';
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2016-08-05 21:44:55 +00:00
|
|
|
'type' => 'dynamic-action',
|
2016-08-24 23:45:02 +00:00
|
|
|
'label' => $label,
|
|
|
|
'description' => $description,
|
2016-08-05 21:44:55 +00:00
|
|
|
'data' => $data,
|
|
|
|
'url' => $title->getFullURL( $query ),
|
|
|
|
'icon' => $icon,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-08-05 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an 'watch' or 'unwatch' dynamic action link
|
|
|
|
*
|
|
|
|
* @param Title $title Title to watch or unwatch
|
|
|
|
* @return array Array compatible with dynamic action link
|
|
|
|
*/
|
|
|
|
final protected function getWatchActionLink( Title $title ) {
|
|
|
|
$isTitleWatched = $this->getUser()->isWatched( $title );
|
|
|
|
$availableAction = $isTitleWatched ? 'unwatch' : 'watch';
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$data = [
|
2016-08-05 21:44:55 +00:00
|
|
|
'tokenType' => 'watch',
|
2016-12-05 18:51:07 +00:00
|
|
|
'params' => [
|
2016-08-05 21:44:55 +00:00
|
|
|
'action' => 'watch',
|
|
|
|
'titles' => $title->getPrefixedText(),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
'messages' => [
|
|
|
|
'confirmation' => [
|
2016-08-05 21:44:55 +00:00
|
|
|
// notification-dynamic-actions-watch-confirmation
|
|
|
|
// notification-dynamic-actions-unwatch-confirmation
|
|
|
|
'title' => $this
|
2016-09-12 22:24:19 +00:00
|
|
|
->msg( 'notification-dynamic-actions-' . $availableAction . '-confirmation' )
|
|
|
|
->params(
|
|
|
|
$this->getTruncatedTitleText( $title ),
|
|
|
|
$title->getFullURL(),
|
|
|
|
$this->getUser()->getName()
|
|
|
|
),
|
2016-08-05 21:44:55 +00:00
|
|
|
// notification-dynamic-actions-watch-confirmation-description
|
|
|
|
// notification-dynamic-actions-unwatch-confirmation-description
|
|
|
|
'description' => $this
|
|
|
|
->msg( 'notification-dynamic-actions-' . $availableAction . '-confirmation-description' )
|
|
|
|
->params(
|
2016-08-26 21:49:10 +00:00
|
|
|
$this->getTruncatedTitleText( $title ),
|
2016-09-12 22:24:19 +00:00
|
|
|
$title->getFullURL(),
|
|
|
|
$this->getUser()->getName()
|
2016-08-05 21:44:55 +00:00
|
|
|
),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2016-08-05 21:44:55 +00:00
|
|
|
|
|
|
|
// "Unwatching" action requires another parameter
|
|
|
|
if ( $isTitleWatched ) {
|
|
|
|
$data[ 'params' ][ 'unwatch' ] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getDynamicActionLink(
|
|
|
|
$title,
|
2016-08-26 21:49:10 +00:00
|
|
|
// Design requirements are to flip the star icons
|
|
|
|
// in their meaning; that is, for the 'unwatch' action
|
|
|
|
// we should display an empty star, and for the 'watch'
|
|
|
|
// action a full star. In OOUI icons, their names
|
|
|
|
// are reversed.
|
|
|
|
$isTitleWatched ? 'star' : 'unStar',
|
2016-08-24 23:45:02 +00:00
|
|
|
// notification-dynamic-actions-watch
|
|
|
|
// notification-dynamic-actions-unwatch
|
|
|
|
$this->msg( 'notification-dynamic-actions-' . $availableAction )
|
2016-08-26 21:49:10 +00:00
|
|
|
->params(
|
|
|
|
$this->getTruncatedTitleText( $title ),
|
2016-12-05 18:51:07 +00:00
|
|
|
$title->getFullURL( [ 'action' => $availableAction ] ),
|
2016-09-12 22:24:19 +00:00
|
|
|
$this->getUser()->getName()
|
2016-08-26 21:49:10 +00:00
|
|
|
),
|
2016-08-24 23:45:02 +00:00
|
|
|
null,
|
2016-08-05 21:44:55 +00:00
|
|
|
$data,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'action' => $availableAction ]
|
2016-08-05 21:44:55 +00:00
|
|
|
);
|
|
|
|
}
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|