2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ApiEchoNotifications extends ApiQueryBase {
|
|
|
|
public function __construct( $query, $moduleName ) {
|
|
|
|
parent::__construct( $query, $moduleName, 'not' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
2012-07-26 17:23:18 +00:00
|
|
|
$user = $this->getUser();
|
2012-06-01 10:57:09 +00:00
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( count( $params['markread'] ) ) {
|
2012-07-26 17:23:18 +00:00
|
|
|
EchoNotificationController::markRead( $user, $params['markread'] );
|
2012-06-01 10:57:09 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$result = $this->getResult();
|
|
|
|
$prop = $params['prop'];
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'list', $prop ) ) {
|
2012-08-01 19:53:05 +00:00
|
|
|
$r = $this->getNotifications( $user, $params['unread'], $params['format'], $params['limit'] );
|
2012-04-27 15:14:24 +00:00
|
|
|
} else {
|
|
|
|
$r = array();
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'count', $prop ) ) {
|
2012-11-13 23:06:11 +00:00
|
|
|
$r['count'] = EchoNotificationController::getFormattedNotificationCount( $user );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'index', $prop ) ) {
|
2012-06-01 10:57:09 +00:00
|
|
|
$r['index'] = array_keys( $r );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$result->setIndexedTagName( $r, 'notification' );
|
|
|
|
$result->addValue( 'query', $this->getModuleName(), $r );
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* @param $user User to get notifications for
|
|
|
|
* @param $unread Boolean: True to get only unread notifications.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $format bool false to not format any notifications, or an output format name.
|
|
|
|
* @param $limit int The maximum number of notifications to return.
|
2012-05-17 00:29:37 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-01 19:53:05 +00:00
|
|
|
public function getNotifications( $user, $unread = false, $format = false, $limit = 20 ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
|
|
|
$output = array();
|
|
|
|
|
|
|
|
$conds = array(
|
|
|
|
'notification_user' => $user->getID(),
|
2012-11-13 23:06:11 +00:00
|
|
|
'event_type' => EchoEvent::gatherValidEchoEvents(),
|
2012-04-27 15:14:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( $unread ) {
|
|
|
|
$conds['notification_read_timestamp'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $dbr->select(
|
|
|
|
array( 'echo_notification', 'echo_event' ),
|
|
|
|
'*',
|
|
|
|
$conds,
|
|
|
|
__METHOD__,
|
|
|
|
array(
|
|
|
|
'ORDER BY' => 'notification_timestamp DESC',
|
2012-08-01 19:53:05 +00:00
|
|
|
'LIMIT' => $limit,
|
2012-04-27 15:14:24 +00:00
|
|
|
),
|
|
|
|
array(
|
2012-08-30 16:04:39 +00:00
|
|
|
'echo_event' => array( 'LEFT JOIN', 'notification_event=event_id' ),
|
2012-04-27 15:14:24 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $res as $row ) {
|
2012-06-06 07:04:28 +00:00
|
|
|
$event = EchoEvent::newFromRow( $row );
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
if ( MWInit::methodExists( 'Language', 'prettyTimestamp' ) ) {
|
2012-10-25 21:07:21 +00:00
|
|
|
$ts = $this->getLanguage()->prettyTimestamp( $event->getTimestamp(), false, $user );
|
2012-07-31 00:29:49 +00:00
|
|
|
} else {
|
|
|
|
$ts = $this->getLanguage()->timeanddate( $event->getTimestamp(), true );
|
|
|
|
}
|
|
|
|
|
2012-09-02 09:30:38 +00:00
|
|
|
$timestamp = new MWTimestamp( $event->getTimestamp() );
|
|
|
|
$timestampUnix = $timestamp->getTimestamp( TS_UNIX );
|
|
|
|
$timestampMw = $timestamp->getTimestamp( TS_MW );
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$thisEvent = array(
|
|
|
|
'type' => $event->getType(),
|
|
|
|
'timestamp' => array(
|
2012-09-02 09:30:38 +00:00
|
|
|
'unix' => $timestampUnix,
|
|
|
|
'mw' => $timestampMw,
|
2012-07-31 00:29:49 +00:00
|
|
|
'pretty' => $ts,
|
2012-04-27 15:14:24 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $event->getVariant() ) {
|
|
|
|
$thisEvent['variant'] = $event->getVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $event->getTitle() ) {
|
|
|
|
$thisEvent['title'] = array(
|
|
|
|
'full' => $event->getTitle()->getPrefixedText(),
|
|
|
|
'namespace' => $event->getTitle()->getNSText(),
|
|
|
|
'namespace-key' => $event->getTitle()->getNamespace(),
|
|
|
|
'text' => $event->getTitle()->getText(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $event->getAgent() ) {
|
|
|
|
$thisEvent['agent'] = array(
|
|
|
|
'id' => $event->getAgent()->getId(),
|
|
|
|
'name' => $event->getAgent()->getName(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $event->getExtra() ) {
|
|
|
|
$thisEvent['extra'] = $event->getExtra();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $row->notification_read_timestamp ) {
|
|
|
|
$thisEvent['read'] = $row->notification_read_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $format ) {
|
|
|
|
$thisEvent['*'] = EchoNotificationController::formatNotification(
|
2012-08-31 21:50:46 +00:00
|
|
|
$event, $this->getUser(), $format );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$output[$event->getID()] = $thisEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'prop' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
2012-08-31 21:50:46 +00:00
|
|
|
'list',
|
|
|
|
'count',
|
|
|
|
'index',
|
|
|
|
),
|
2012-04-27 15:14:24 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'list',
|
|
|
|
),
|
|
|
|
'markread' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
),
|
|
|
|
'unread' => false,
|
|
|
|
'format' => array(
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
2012-08-31 21:50:46 +00:00
|
|
|
'text',
|
|
|
|
'html',
|
|
|
|
),
|
2012-04-27 15:14:24 +00:00
|
|
|
),
|
2012-08-01 19:53:05 +00:00
|
|
|
'limit' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
ApiBase::PARAM_DFLT => 20,
|
|
|
|
ApiBase::PARAM_MAX => 50,
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
),
|
2012-06-01 10:57:09 +00:00
|
|
|
'index' => false,
|
2012-04-27 15:14:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
|
|
|
return array(
|
2012-06-01 10:57:09 +00:00
|
|
|
'prop' => 'Details to request.',
|
|
|
|
'markread' => 'A list of notification IDs to mark as read',
|
|
|
|
'unread' => 'Request only unread notifications',
|
|
|
|
'format' => 'If specified, notifications will be returned formatted this way.',
|
|
|
|
'index' => 'If specified, a list of notification IDs, in order, will be returned.',
|
2012-08-01 19:53:05 +00:00
|
|
|
'limit' => 'The maximum number of notifications to return.'
|
2012-04-27 15:14:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
|
|
|
return 'Get notifications waiting for the current user';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getExamples() {
|
|
|
|
return array(
|
|
|
|
'api.php?action=query&meta=notifications',
|
|
|
|
'api.php?action=query&meta=notifications¬prop=count',
|
|
|
|
'api.php?action=query&meta=notifications¬markread=8',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHelpUrls() {
|
|
|
|
return 'https://www.mediawiki.org/wiki/Echo_(notifications)/API';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVersion() {
|
|
|
|
return __CLASS__ . '-0.1';
|
|
|
|
}
|
2012-07-26 17:23:18 +00:00
|
|
|
}
|