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() {
|
2012-07-26 17:23:18 +00:00
|
|
|
$user = $this->getUser();
|
2012-12-07 01:08:33 +00:00
|
|
|
if ( $user->isAnon() ) {
|
|
|
|
$this->dieUsage( 'Login is required', 'login-required' );
|
|
|
|
}
|
2012-06-01 10:57:09 +00:00
|
|
|
|
2012-12-07 01:08:33 +00:00
|
|
|
$params = $this->extractRequestParams();
|
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'] );
|
2013-04-17 01:00:21 +00:00
|
|
|
} elseif ( $params['markallread'] ) {
|
|
|
|
EchoNotificationController::markAllRead( $user );
|
2012-06-01 10:57:09 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$prop = $params['prop'];
|
|
|
|
|
2012-12-07 01:08:33 +00:00
|
|
|
$result = array();
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'list', $prop ) ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
$result['list'] = self::getNotifications( $user, $params['format'], $params['limit'] + 1, $params['timestamp'], $params['offset'] );
|
2012-12-07 01:08:33 +00:00
|
|
|
|
|
|
|
// check if there is more elements than we request
|
|
|
|
if ( count( $result['list'] ) > $params['limit'] ) {
|
|
|
|
array_pop( $result['list'] );
|
|
|
|
$result['more'] = '1';
|
|
|
|
} else {
|
|
|
|
$result['more'] = '0';
|
|
|
|
}
|
2013-05-02 16:59:48 +00:00
|
|
|
$this->getResult()->setIndexedTagName( $result['list'], 'notification' );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'count', $prop ) ) {
|
2012-12-07 01:08:33 +00:00
|
|
|
$result['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 ) ) {
|
2013-05-02 16:59:48 +00:00
|
|
|
$result['index'] = array();
|
|
|
|
foreach ( array_keys( $result['list'] ) as $key ) {
|
|
|
|
// Don't include the XML tag name ('_element' key)
|
|
|
|
if ( $key != '_element' ) {
|
|
|
|
$result['index'][] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->getResult()->setIndexedTagName( $result['index'], 'id' );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 01:08:33 +00:00
|
|
|
$this->getResult()->setIndexedTagName( $result, 'notification' );
|
|
|
|
$this->getResult()->addValue( 'query', $this->getModuleName(), $result );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
2012-12-07 01:08:33 +00:00
|
|
|
* Get a list of notifications based on the passed parameters
|
|
|
|
*
|
|
|
|
* @param $user User the user to get notifications for
|
2013-01-15 23:21:39 +00:00
|
|
|
* @param $format string/bool false to not format any notifications, string to a specific output format
|
2012-12-07 01:08:33 +00:00
|
|
|
* @param $limit int The maximum number of notifications to return
|
|
|
|
* @param $timestamp int The timestamp to start from
|
|
|
|
* @param $offset int The notification event id to start from
|
2012-05-17 00:29:37 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-01-15 23:21:39 +00:00
|
|
|
public static function getNotifications( $user, $format = false, $limit = 20, $timestamp = 0, $offset = 0 ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoBackend;
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$output = array();
|
|
|
|
|
2013-01-14 23:52:46 +00:00
|
|
|
// TODO: Make 'web' based on a new API param?
|
2013-01-15 23:21:39 +00:00
|
|
|
$res = $wgEchoBackend->loadNotifications( $user, $limit, $timestamp, $offset, 'web' );
|
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 );
|
2013-01-15 23:21:39 +00:00
|
|
|
if ( $row->notification_bundle_base && $row->notification_bundle_display_hash ) {
|
|
|
|
$event->setBundleHash( $row->notification_bundle_display_hash );
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2012-12-07 01:08:33 +00:00
|
|
|
$timestamp = new MWTimestamp( $row->notification_timestamp );
|
2012-09-02 09:30:38 +00:00
|
|
|
$timestampUnix = $timestamp->getTimestamp( TS_UNIX );
|
|
|
|
$timestampMw = $timestamp->getTimestamp( TS_MW );
|
|
|
|
|
2012-12-07 01:08:33 +00:00
|
|
|
// start creating date section header
|
|
|
|
$today = wfTimestamp( TS_MW );
|
|
|
|
$yesterday = wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $today ) - 24 * 3600 );
|
|
|
|
|
|
|
|
if ( substr( $today, 4, 4 ) === substr( $timestampMw, 4, 4 ) ) {
|
|
|
|
$date = wfMessage( 'echo-date-today' )->escaped();
|
|
|
|
} elseif ( substr( $yesterday, 4, 4 ) === substr( $timestampMw, 4, 4 ) ) {
|
|
|
|
$date = wfMessage( 'echo-date-yesterday' )->escaped();
|
|
|
|
} else {
|
|
|
|
$month = array(
|
|
|
|
'01' => 'january-gen',
|
|
|
|
'02' => 'february-gen',
|
|
|
|
'03' => 'march-gen',
|
|
|
|
'04' => 'april-gen',
|
|
|
|
'05' => 'may-gen',
|
|
|
|
'06' => 'june-gen',
|
|
|
|
'07' => 'july-gen',
|
|
|
|
'08' => 'august-gen',
|
|
|
|
'09' => 'september-gen',
|
|
|
|
'10' => 'october-gen',
|
|
|
|
'11' => 'november-gen',
|
|
|
|
'12' => 'december-gen'
|
|
|
|
);
|
|
|
|
|
|
|
|
$headerMonth = wfMessage( $month[substr( $timestampMw, 4, 2 )] )->text();
|
|
|
|
$headerDate = substr( $timestampMw, 6, 2 );
|
|
|
|
$date = wfMessage( 'echo-date-header' )->params( $headerMonth )->numParams( $headerDate )->escaped();
|
|
|
|
}
|
|
|
|
// end creating date section header
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$thisEvent = array(
|
2012-12-07 01:08:33 +00:00
|
|
|
'id' => $event->getId(),
|
2012-04-27 15:14:24 +00:00
|
|
|
'type' => $event->getType(),
|
2013-03-09 00:32:28 +00:00
|
|
|
'category' => $event->getCategory(),
|
2012-04-27 15:14:24 +00:00
|
|
|
'timestamp' => array(
|
2012-09-02 09:30:38 +00:00
|
|
|
'unix' => $timestampUnix,
|
|
|
|
'mw' => $timestampMw,
|
2012-12-07 01:08:33 +00:00
|
|
|
'date' => $date
|
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 ( $row->notification_read_timestamp ) {
|
|
|
|
$thisEvent['read'] = $row->notification_read_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $format ) {
|
|
|
|
$thisEvent['*'] = EchoNotificationController::formatNotification(
|
2012-12-07 01:08:33 +00:00
|
|
|
$event, $user, $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,
|
|
|
|
),
|
2013-04-17 01:00:21 +00:00
|
|
|
'markallread' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean'
|
|
|
|
),
|
2012-04-27 15:14:24 +00:00
|
|
|
'format' => array(
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
2012-08-31 21:50:46 +00:00
|
|
|
'text',
|
2013-01-07 22:44:58 +00:00
|
|
|
'flyout',
|
2012-08-31 21:50:46 +00:00
|
|
|
'html',
|
|
|
|
),
|
2012-04-27 15:14:24 +00:00
|
|
|
),
|
2012-08-01 19:53:05 +00:00
|
|
|
'limit' => array(
|
2013-05-02 17:56:46 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
2012-08-01 19:53:05 +00:00
|
|
|
ApiBase::PARAM_DFLT => 20,
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
2013-05-02 17:56:46 +00:00
|
|
|
ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
|
|
|
|
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2,
|
2012-08-01 19:53:05 +00:00
|
|
|
),
|
2012-06-01 10:57:09 +00:00
|
|
|
'index' => false,
|
2012-12-07 01:08:33 +00:00
|
|
|
'offset' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
),
|
|
|
|
'timestamp' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
),
|
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',
|
2013-04-17 01:00:21 +00:00
|
|
|
'markallread' => "If set to true, marks all of a user's notifications as read",
|
2012-06-01 10:57:09 +00:00
|
|
|
'format' => 'If specified, notifications will be returned formatted this way.',
|
|
|
|
'index' => 'If specified, a list of notification IDs, in order, will be returned.',
|
2012-12-07 01:08:33 +00:00
|
|
|
'limit' => 'The maximum number of notifications to return.',
|
|
|
|
'offset' => 'Notification event id to start from (requires timestamp param to be passed as well)',
|
|
|
|
'timestamp' => 'Timestamp to start from',
|
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
|
|
|
}
|