Add support for querying notifications not associated with any page

Uses the magic value '[]' to mean 'no title'. This is a bit ugly,
but I think introducing an additional &notwithouttitle=1 parameter
is uglier and results in more code.

Change-Id: I83278182aeaf3905eb0f3e24c4c6c247720b1e76
This commit is contained in:
Roan Kattouw 2016-06-24 00:32:18 +02:00
parent eae458801f
commit 7d97881344
4 changed files with 20 additions and 7 deletions

View file

@ -268,7 +268,7 @@
"apihelp-query+notifications-param-alertunreadfirst": "Whether to show unread message notifications first (only used if groupbysection is set).",
"apihelp-query+notifications-param-messagecontinue": "When more message results are available, use this to continue.",
"apihelp-query+notifications-param-messageunreadfirst": "Whether to show unread alert notifications first (only used if groupbysection is set).",
"apihelp-query+notifications-param-titles": "Only return notifications for these pages.",
"apihelp-query+notifications-param-titles": "Only return notifications for these pages. To get notifications not associated with any page, use [] as a title.",
"apihelp-query+notifications-example-1": "List notifications",
"apihelp-query+notifications-example-2": "List notifications, grouped by section, with counts",
"apihelp-query+unreadnotificationpages-description": "Get pages for which there are unread notifications for the current user.",

View file

@ -78,6 +78,9 @@ class ApiEchoNotifications extends ApiCrossWikiBase {
$titles = null;
if ( $params['titles'] ) {
$titles = array_values( array_filter( array_map( 'Title::newFromText', $params['titles'] ) ) );
if ( in_array( '[]', $params['titles'] ) ) {
$titles[] = null;
}
}
$result = array();

View file

@ -98,6 +98,7 @@ class EchoNotificationMapper extends EchoAbstractMapper {
* @param string $continue Used for offset
* @param string[] $eventTypes
* @param Title[] $titles If set, only return notifications for these pages.
* To find notifications not associated with any page, add null as an element to this array.
* @param int $dbSource Use master or slave database
* @return EchoNotification[]
*/
@ -123,6 +124,7 @@ class EchoNotificationMapper extends EchoAbstractMapper {
* @param string $continue Used for offset
* @param string[] $eventTypes
* @param Title[] $titles If set, only return notifications for these pages.
* To find notifications not associated with any page, add null as an element to this array.
* @param int $dbSource Use master or slave database
* @return EchoNotification[]
*/
@ -146,6 +148,7 @@ class EchoNotificationMapper extends EchoAbstractMapper {
* @param array $eventTypes Event types to load
* @param array $excludeEventIds Event id's to exclude.
* @param Title[] $titles If set, only return notifications for these pages.
* To find notifications not associated with any page, add null as an element to this array.
* @return EchoNotification[]
*/
public function fetchByUser( User $user, $limit, $continue, array $eventTypes = array(), array $excludeEventIds = array(), array $titles = null ) {
@ -168,7 +171,9 @@ class EchoNotificationMapper extends EchoAbstractMapper {
protected function getIdsForTitles( array $titles ) {
$ids = array();
foreach ( $titles as $title ) {
if ( $title->exists() ) {
if ( $title === null ) {
$ids[] = null;
} elseif ( $title->exists() ) {
$ids[] = $title->getArticleId();
}
}

View file

@ -96,11 +96,13 @@
* defining the offset to fetch notifications
* @param {string} [filterObject.readState] Notification read
* state, 'all', 'read' or 'unread'
* @param {string|string[]} [filterObject.titles] Requested titles
* @param {string|string[]} [filterObject.titles] Requested titles. To request notifications with no title,
* use null (standalone or as an array element).
* @return {Object} API parameter definitions to override
*/
mw.echo.api.EchoApi.prototype.convertFiltersToAPIParams = function ( filterObject ) {
var overrideParams = {};
var titles,
overrideParams = {};
filterObject = filterObject || {};
@ -115,9 +117,12 @@
}
if ( filterObject.titles ) {
overrideParams.nottitles = Array.isArray( filterObject.titles ) ?
filterObject.titles.join( '|' ) :
filterObject.titles;
titles = Array.isArray( filterObject.titles ) ? filterObject.titles : [ filterObject.titles ];
if ( titles.indexOf( null ) !== -1 ) {
// Map null to '[]'
titles.splice( titles.indexOf( null ), 1, '[]' );
}
overrideParams.nottitles = titles.join( '|' );
}
return overrideParams;