From 26566732fa99e02d44ebe6de82a1136ef2b3bf16 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 3 Jun 2014 15:53:34 -0400 Subject: [PATCH] API: Add prop=unreadcount to ApiQueryUserInfo People want to be able to fetch the count of unread pages on the watchlist. Bug: 65246 Change-Id: I890f21631ba074a7b3d660534c40e38773d8ea77 --- RELEASE-NOTES-1.24 | 1 + includes/api/ApiQueryUserInfo.php | 32 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES-1.24 b/RELEASE-NOTES-1.24 index 359237c95c..f0370ca417 100644 --- a/RELEASE-NOTES-1.24 +++ b/RELEASE-NOTES-1.24 @@ -99,6 +99,7 @@ production. namespace. * action=expandtemplates has a new parameter, prop, and a new output format. The old format is still used if prop isn't provided, but this is deprecated. +* meta=userinfo can now return the count of unread pages on the watchlist. === Languages updated in 1.24 === diff --git a/includes/api/ApiQueryUserInfo.php b/includes/api/ApiQueryUserInfo.php index ee5e458076..e46baa6d6a 100644 --- a/includes/api/ApiQueryUserInfo.php +++ b/includes/api/ApiQueryUserInfo.php @@ -31,6 +31,8 @@ */ class ApiQueryUserInfo extends ApiQueryBase { + const WL_UNREAD_LIMIT = 1000; + private $prop = array(); public function __construct( ApiQuery $query, $moduleName ) { @@ -50,7 +52,7 @@ class ApiQueryUserInfo extends ApiQueryBase { } protected function getCurrentUserInfo() { - global $wgHiddenPrefs; + global $wgHiddenPrefs, $wgRCMaxAge; $user = $this->getUser(); $result = $this->getResult(); $vals = array(); @@ -153,6 +155,28 @@ class ApiQueryUserInfo extends ApiQueryBase { $vals['acceptlang'] = $acceptLang; } + if ( isset( $this->prop['unreadcount'] ) ) { + $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' ); + + $sql = $dbr->selectSQLText( + 'watchlist', + array( 'dummy' => 1 ), + array( + 'wl_user' => $user->getId(), + 'wl_notificationtimestamp IS NOT NULL', + ), + __METHOD__, + array( 'LIMIT' => self::WL_UNREAD_LIMIT ) + ); + $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' ); + + if ( $count >= self::WL_UNREAD_LIMIT ) { + $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+'; + } else { + $vals['unreadcount'] = (int)$count; + } + } + return $vals; } @@ -212,7 +236,8 @@ class ApiQueryUserInfo extends ApiQueryBase { 'email', 'realname', 'acceptlang', - 'registrationdate' + 'registrationdate', + 'unreadcount', ) ) ); @@ -237,6 +262,9 @@ class ApiQueryUserInfo extends ApiQueryBase { ' acceptlang - Echoes the Accept-Language header sent by ' . 'the client in a structured format', ' registrationdate - Adds the user\'s registration date', + ' unreadcount - Adds the count of unread pages on the user\'s watchlist ' . + '(maximum ' . ( self::WL_UNREAD_LIMIT - 1 ) . '; returns "' . + self::WL_UNREAD_LIMIT . '+" if more)', ) ); } -- 2.20.1