2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ApiEchoNotifications extends ApiQueryBase {
|
2013-12-02 23:11:59 +00:00
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
public function __construct( $query, $moduleName ) {
|
|
|
|
parent::__construct( $query, $moduleName, 'not' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
2013-07-12 22:46:37 +00:00
|
|
|
// To avoid API warning, register the parameter used to bust browser cache
|
|
|
|
$this->getMain()->getVal( '_' );
|
|
|
|
|
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();
|
2013-05-04 00:46:30 +00:00
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$prop = $params['prop'];
|
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
2012-12-07 01:08:33 +00:00
|
|
|
$result = array();
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'list', $prop ) ) {
|
2014-07-30 22:09:22 +00:00
|
|
|
$result = $this->getPropList(
|
|
|
|
$user,
|
2014-07-22 21:33:22 +00:00
|
|
|
$attributeManager->getUserEnabledEvents( $user, 'web' ),
|
2014-07-30 22:09:22 +00:00
|
|
|
$params['limit'], $params['continue'], $params['format']
|
|
|
|
);
|
2013-05-02 16:59:48 +00:00
|
|
|
$this->getResult()->setIndexedTagName( $result['list'], 'notification' );
|
2014-07-30 22:09:22 +00:00
|
|
|
// 'index' is built on top of 'list'
|
|
|
|
if ( in_array( 'index', $prop ) ) {
|
|
|
|
$result['index'] = $this->getPropIndex( $result['list'] );
|
|
|
|
$this->getResult()->setIndexedTagName( $result['index'], 'id' );
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'count', $prop ) ) {
|
2014-07-30 22:09:22 +00:00
|
|
|
$result += $this->getPropCount( $user );
|
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
|
|
|
}
|
|
|
|
|
2014-07-30 22:09:22 +00:00
|
|
|
/**
|
|
|
|
* Internal helper method for getting property 'count' data
|
|
|
|
*/
|
|
|
|
protected function getPropCount( User $user ) {
|
|
|
|
$result = array();
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $user );
|
|
|
|
// Always get total count
|
|
|
|
$rawCount = $notifUser->getNotificationCount();
|
|
|
|
$result['rawcount'] = $rawCount;
|
|
|
|
$result['count'] = EchoNotificationController::formatNotificationCount( $rawCount );
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal helper method for getting property 'list' data
|
|
|
|
*/
|
|
|
|
protected function getPropList( User $user, array $eventTypesToLoad, $limit, $continue, $format ) {
|
|
|
|
$result = array(
|
|
|
|
'list' => array(),
|
|
|
|
'continue' => null
|
|
|
|
);
|
|
|
|
|
|
|
|
// Fetch the result for the event types above
|
|
|
|
$notifMapper = new EchoNotificationMapper( MWEchoDbFactory::newFromDefault() );
|
2014-07-22 21:33:22 +00:00
|
|
|
$notifs = $notifMapper->fetchByUser( $user, $limit + 1, $continue, $eventTypesToLoad );
|
2014-07-30 22:09:22 +00:00
|
|
|
foreach ( $notifs as $notif ) {
|
|
|
|
$result['list'][$notif->getEvent()->getID()] = EchoDataOutputFormatter::formatOutput( $notif, $format, $user );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( count( $result['list'] ) > $limit ) {
|
|
|
|
$lastItem = array_pop( $result['list'] );
|
|
|
|
$result['continue'] = $lastItem['timestamp']['utcunix'] . '|' . $lastItem['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal helper method for getting property 'index' data
|
|
|
|
*/
|
|
|
|
protected function getPropIndex( $list ) {
|
|
|
|
$result = array();
|
|
|
|
foreach ( array_keys( $list ) as $key ) {
|
|
|
|
// Don't include the XML tag name ('_element' key)
|
|
|
|
if ( $key != '_element' ) {
|
|
|
|
$result[] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
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',
|
|
|
|
),
|
|
|
|
'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,
|
2013-05-03 23:58:56 +00:00
|
|
|
'continue' => null,
|
2013-08-27 00:55:45 +00:00
|
|
|
'uselang' => null
|
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.',
|
|
|
|
'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.',
|
2013-05-03 23:58:56 +00:00
|
|
|
'continue' => 'When more results are available, use this to continue',
|
2013-08-27 00:55:45 +00:00
|
|
|
'uselang' => 'the desired language to format the output'
|
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',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|