2015-08-19 20:22:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* Recommended length of usernames included in messages
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Recommended length of usernames used as link label
|
|
|
|
*/
|
|
|
|
const USERNAME_AS_LABEL_RECOMMENDED_LENGTH = 15;
|
|
|
|
|
2016-01-20 21:19:54 +00:00
|
|
|
/**
|
|
|
|
* Recommended length of page names included in messages
|
|
|
|
*/
|
|
|
|
const PAGE_NAME_RECOMMENDED_LENGTH = 50;
|
|
|
|
|
2016-02-17 17:27:49 +00:00
|
|
|
/**
|
|
|
|
* Recommended length of page names used as link label
|
|
|
|
*/
|
|
|
|
const PAGE_NAME_AS_LABEL_RECOMMENDED_LENGTH = 15;
|
|
|
|
|
2016-01-20 21:19:54 +00:00
|
|
|
/**
|
|
|
|
* Recommended length of section titles included in messages
|
|
|
|
*/
|
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-11-24 02:15:30 +00:00
|
|
|
/**
|
|
|
|
* @var EchoEvent[]|null
|
|
|
|
*/
|
|
|
|
private $bundledEvents;
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* @param EchoEvent $event
|
|
|
|
* @param Language|string $language
|
2015-11-12 22:34:24 +00:00
|
|
|
* @param User $user Only used for permissions checking and GENDER
|
2015-08-19 20:22:45 +00:00
|
|
|
*/
|
|
|
|
protected function __construct( EchoEvent $event, $language, User $user ) {
|
|
|
|
$this->event = $event;
|
|
|
|
$this->type = $event->getType();
|
|
|
|
$this->language = wfGetLangObj( $language );
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to detect whether the event type
|
|
|
|
* has been updated to use the presentation model system
|
|
|
|
*
|
|
|
|
* @param string $type event type
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function supportsPresentationModel( $type ) {
|
|
|
|
global $wgEchoNotifications;
|
|
|
|
return isset( $wgEchoNotifications[$type]['presentation-model'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EchoEvent $event
|
|
|
|
* @param Language|string $language
|
|
|
|
* @param User $user
|
|
|
|
* @return EchoEventPresentationModel
|
|
|
|
*/
|
|
|
|
public static function factory( EchoEvent $event, $language, User $user ) {
|
|
|
|
global $wgEchoNotifications;
|
|
|
|
// @todo don't depend upon globals
|
|
|
|
|
|
|
|
$class = $wgEchoNotifications[$event->getType()]['presentation-model'];
|
|
|
|
return new $class( $event, $language, $user );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent to IContextSource::msg for the current
|
|
|
|
* language
|
|
|
|
*
|
|
|
|
* @return Message
|
|
|
|
*/
|
|
|
|
protected function msg( /* ,,, */ ) {
|
|
|
|
/**
|
|
|
|
* @var Message $msg
|
|
|
|
*/
|
|
|
|
$msg = call_user_func_array( 'wfMessage', func_get_args() );
|
|
|
|
$msg->inLanguage( $this->language );
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2015-11-19 18:06:58 +00:00
|
|
|
/**
|
|
|
|
* @return EchoEvent[]
|
|
|
|
*/
|
|
|
|
final protected function getBundledEvents() {
|
|
|
|
if ( !$this->event->getBundleHash() ) {
|
|
|
|
return array();
|
|
|
|
}
|
2015-11-24 02:15:30 +00:00
|
|
|
if ( $this->bundledEvents !== null ) {
|
|
|
|
return $this->bundledEvents;
|
|
|
|
}
|
2015-11-19 18:06:58 +00:00
|
|
|
|
|
|
|
// FIXME: We really shouldn't be making db queries like this
|
|
|
|
// in the presentation model
|
|
|
|
$eventMapper = new EchoEventMapper();
|
2015-11-24 02:15:30 +00:00
|
|
|
$this->bundledEvents = $eventMapper->fetchByUserBundleHash(
|
2015-11-19 18:06:58 +00:00
|
|
|
$this->user,
|
|
|
|
$this->event->getBundleHash()
|
|
|
|
// default params: web, DESC, limit=250
|
|
|
|
);
|
|
|
|
|
2015-11-24 02:15:30 +00:00
|
|
|
return $this->bundledEvents;
|
2015-11-19 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
* @param callable $groupCallback Callback that takes an EchoEvent and returns a grouping value
|
|
|
|
* @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 ) {
|
|
|
|
$events = array_merge( $this->getBundledEvents(), array( $this->event ) );
|
|
|
|
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
|
|
|
*
|
|
|
|
* For parameters, see getBundleCount().
|
|
|
|
*
|
2015-11-27 14:37:19 +00:00
|
|
|
* @param bool $includeCurrent
|
2015-11-30 22:31:19 +00:00
|
|
|
* @param callable $groupCallback
|
2015-11-27 14:37:19 +00:00
|
|
|
* @return array ['number for display', 'number for PLURAL']
|
|
|
|
*/
|
2015-11-30 22:31:19 +00:00
|
|
|
final protected function getNotificationCountForOutput( $includeCurrent = true, $groupCallback = null ) {
|
2015-11-27 14:37:19 +00:00
|
|
|
global $wgEchoMaxNotificationCount;
|
2015-11-30 22:31:19 +00:00
|
|
|
$count = $this->getBundleCount( $includeCurrent, $groupCallback );
|
2015-11-27 14:37:19 +00:00
|
|
|
if ( $count > $wgEchoMaxNotificationCount ) {
|
|
|
|
return array(
|
|
|
|
$this->msg( 'echo-notification-count' )->numParams( $wgEchoMaxNotificationCount )->text(),
|
|
|
|
$wgEchoMaxNotificationCount
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return array(
|
|
|
|
$this->language->formatNum( $count ),
|
|
|
|
$count
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|bool ['wikitext to display', 'username for GENDER'], false if no agent
|
|
|
|
*
|
|
|
|
* 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-02-24 21:34:53 +00:00
|
|
|
return array(
|
|
|
|
$this->language->embedBidi( $this->getTruncatedUsername( $agent ) ),
|
|
|
|
$agent->getName()
|
|
|
|
);
|
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
|
|
|
|
return array( '<span class="history-deleted">' . $msg . '</span>', '[]' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
2015-11-19 15:41:18 +00:00
|
|
|
* @return array|bool 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();
|
|
|
|
|
|
|
|
/**
|
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),
|
|
|
|
* 'description' => (string) descriptive text (non-escaped),
|
|
|
|
* 'icon' => (bool|string) symbolic icon name (or false if there is none),
|
|
|
|
* 'prioritized' => (bool) true if the link should be outside the
|
2015-11-19 15:41:18 +00:00
|
|
|
* action menu, false for inside)],
|
|
|
|
* ...]
|
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() {
|
|
|
|
return array();
|
|
|
|
}
|
2015-11-16 16:05:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws TimestampException
|
|
|
|
*/
|
|
|
|
public function jsonSerialize() {
|
|
|
|
$body = $this->getBodyMessage();
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'header' => $this->getHeaderMessage()->parse(),
|
2016-02-25 18:19:19 +00:00
|
|
|
'body' => $body ? $body->escaped() : '',
|
2015-11-16 16:05:07 +00:00
|
|
|
'icon' => $this->getIconType(),
|
|
|
|
'links' => array(
|
|
|
|
'primary' => $this->getPrimaryLink() ?: array(),
|
2015-12-22 15:57:13 +00:00
|
|
|
'secondary' => array_values( array_filter( $this->getSecondaryLinks() ) ),
|
2015-11-16 16:05:07 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2016-01-20 21:19:54 +00:00
|
|
|
|
|
|
|
protected function getTruncatedUsername( User $user ) {
|
|
|
|
return $this->language->truncate( $user->getName(), self::USERNAME_RECOMMENDED_LENGTH );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTruncatedTitleText( Title $title, $includeNamespace = false ) {
|
|
|
|
$text = $includeNamespace ? $title->getPrefixedText() : $title->getText();
|
|
|
|
return $this->language->truncate( $text, self::PAGE_NAME_RECOMMENDED_LENGTH );
|
|
|
|
}
|
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();
|
|
|
|
$truncatedLabel = $this->language->truncate( $label, self::USERNAME_AS_LABEL_RECOMMENDED_LENGTH );
|
|
|
|
$isTruncated = $label !== $truncatedLabel;
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title
|
|
|
|
* @param string $description
|
|
|
|
* @param bool $prioritized
|
|
|
|
* @param array $query
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
final protected function getPageLink( Title $title, $description, $prioritized, $query = array() ) {
|
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-02-17 17:27:49 +00:00
|
|
|
return array(
|
|
|
|
'url' => $title->getFullURL( $query ),
|
2016-02-29 20:48:11 +00:00
|
|
|
'label' => $this->language->embedBidi(
|
|
|
|
$this->language->truncate( $title->getText(), self::PAGE_NAME_AS_LABEL_RECOMMENDED_LENGTH )
|
|
|
|
),
|
2016-02-17 17:27:49 +00:00
|
|
|
'tooltip' => $title->getPrefixedText(),
|
|
|
|
'description' => $description,
|
|
|
|
'icon' => $icon,
|
|
|
|
'prioritized' => $prioritized,
|
|
|
|
);
|
|
|
|
}
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|