Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/295591

Change subject: [WIP] Implement subject+talk and null+user page grouping in the 
API
......................................................................

[WIP] Implement subject+talk and null+user page grouping in the API

Also changes the format from an object to an array.

Bug: T137502
Change-Id: I443ca00ff5e5d36fd6910101226358942e6aa8ee
---
M i18n/en.json
M i18n/qqq.json
M includes/api/ApiEchoUnreadNotificationPages.php
3 files changed, 67 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/91/295591/1

diff --git a/i18n/en.json b/i18n/en.json
index b175404..d718b82 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -272,6 +272,7 @@
        "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.",
+       "apihelp-query+unreadnotificationpages-param-grouppages": "Group talk 
pages together with their subject page, and group notifications not associated 
with a page together with the current user's user page.",
        "apihelp-query+unreadnotificationpages-param-limit": "The maximum 
number of pages to return.",
        "apihelp-query+unreadnotificationpages-param-wikis": "List of wikis to 
fetch pages with unread notifications from (defaults to only current wiki).",
        "apihelp-query+unreadnotificationpages-example-1": "List pages with 
(their amount of) unread notifications"
diff --git a/i18n/qqq.json b/i18n/qqq.json
index f6c2648..19da74a 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -263,6 +263,7 @@
        "apihelp-query+notifications-example-1": 
"{{doc-apihelp-example|query+notifications}}",
        "apihelp-query+notifications-example-2": 
"{{doc-apihelp-example|query+notifications}}",
        "apihelp-query+unreadnotificationpages-description": 
"{{doc-apihelp-description|query+unreadnotificationpages}}",
+       "apihelp-query+unreadnotificationpages-param-grouppages": 
"{{doc-apihelp-param|query+unreadnotificationpages|grouppages}}",
        "apihelp-query+unreadnotificationpages-param-limit": 
"{{doc-apihelp-param|query+unreadnotificationpages|limit}}",
        "apihelp-query+unreadnotificationpages-param-wikis": 
"{{doc-apihelp-param|query+unreadnotificationpages|wikis}}",
        "apihelp-query+unreadnotificationpages-example-1": 
"{{doc-apihelp-example|query+unreadnotificationpages}}"
diff --git a/includes/api/ApiEchoUnreadNotificationPages.php 
b/includes/api/ApiEchoUnreadNotificationPages.php
index a0fa918..5a99038 100644
--- a/includes/api/ApiEchoUnreadNotificationPages.php
+++ b/includes/api/ApiEchoUnreadNotificationPages.php
@@ -29,7 +29,7 @@
 
                $result = array();
                if ( in_array( wfWikiId(), $this->getRequestedWikis() ) ) {
-                       $result[wfWikiID()] = $this->getFromLocal( 
$params['limit'] );
+                       $result[wfWikiID()] = $this->getFromLocal( 
$params['limit'], $params['grouppages'] );
                }
 
                if ( $this->getRequestedForeignWikis() ) {
@@ -39,8 +39,7 @@
                $apis = $this->foreignNotifications->getApiEndpoints( 
$this->getRequestedWikis() );
                foreach ( $result as $wiki => $data ) {
                        $result[$wiki]['source'] = $apis[$wiki];
-                       // StdClass to ensure empty data is json_encoded to 
`{}` instead of `[]`
-                       $result[$wiki]['pages'] = $data['pages'] ?: new 
StdClass;
+                       $result[$wiki]['pages'] = $data['pages'] ?: array();
                }
 
                $this->getResult()->addValue( 'query', $this->getModuleName(), 
$result );
@@ -48,10 +47,14 @@
 
        /**
         * @param int $limit
+        * @param bool $groupPages
         * @return array
         */
-       protected function getFromLocal( $limit ) {
+       protected function getFromLocal( $limit, $groupPages ) {
                $dbr = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_SLAVE );
+               // If $groupPages is true, we need to fetch all pages and apply 
the ORDER BY and LIMIT ourselves
+               // after grouping.
+               $extraOptions = $groupPages ? array() : array( 'ORDER BY' => 
'count DESC', 'LIMIT' => $limit );
                $rows = $dbr->select(
                        array( 'echo_event', 'echo_notification' ),
                        array( 'event_page_id', 'count' => 'COUNT(*)' ),
@@ -59,14 +62,11 @@
                                'notification_user' => 
$this->getUser()->getId(),
                                'notification_read_timestamp' => null,
                                'notification_bundle_base' => 1,
-                               'event_page_id IS NOT NULL',
                        ),
                        __METHOD__,
                        array(
                                'GROUP BY' => 'event_page_id',
-                               'ORDER BY' => 'count DESC',
-                               'LIMIT' => $limit,
-                       ),
+                       ) + $extraOptions,
                        array( 'echo_notification' => array( 'INNER JOIN', 
'notification_event = event_id' ) )
                );
 
@@ -74,17 +74,62 @@
                        return array();
                }
 
-               $pages = array();
+               $nullCount = 0;
+               $pageCounts = array();
                foreach ( $rows as $row ) {
-                       $pages[$row->event_page_id] = $row->count;
+                       if ( $row->event_page_id !== null ) {
+                               $pageCounts[$row->event_page_id] = intval( 
$row->count );
+                       } else {
+                               $nullCount = intval( $row->count );
+                       }
+               }
+
+               $titles = Title::newFromIDs( array_keys( $pageCounts ) );
+
+               $groupCounts = array();
+               foreach ( $titles as $title ) {
+                       if ( $groupPages ) {
+                               // If $title is a talk page, add its count to 
its subject page's count
+                               $pageName = 
$title->getSubjectPage()->getPrefixedText();
+                       } else {
+                               $pageName = $title->getPrefixedText();
+                       }
+                       $count = $pageCounts[$title->getArticleId()];
+                       if ( isset( $groupCounts[$pageName] ) ) {
+                               $groupCounts[$pageName] += $count;
+                       } else {
+                               $groupCounts[$pageName] = $count;
+                       }
+               }
+
+               if ( $nullCount > 0 ) {
+                       if ( $groupPages ) {
+                               // Add the count for NULL (not associated with 
any page) to the count for the user page
+                               $userPageName = 
$this->getUser()->getUserPage()->getPrefixedText();
+                               if ( isset( $groupCounts[$userPageName] ) ) {
+                                       $groupCounts[$userPageName] += 
$nullCount;
+                               } else {
+                                       $groupCounts[$userPageName] = 
$nullCount;
+                               }
+                       }
+               }
+
+               arsort( $groupCounts );
+               if ( $groupPages ) {
+                       $groupCounts = array_slice( $groupCounts, 0, $limit );
                }
 
                $result = array();
-               $titles = Title::newFromIDs( array_keys( $pages ) );
-               foreach ( $titles as $title ) {
-                       $result[$title->getArticleID()] = array(
-                               'title' => $title->getPrefixedText(),
-                               'count' => $pages[$title->getArticleID()],
+               foreach ( $groupCounts as $pageName => $count ) {
+                       $result[] = array(
+                               'title' => $pageName === '|null|' ? null : 
$pageName,
+                               'count' => $count,
+                       );
+               }
+               if ( !$groupPages && $nullCount > 0 ) {
+                       $result[] = array(
+                               'title' => null,
+                               'count' => $nullCount,
                        );
                }
 
@@ -113,9 +158,13 @@
                global $wgEchoMaxUpdateCount;
 
                return parent::getAllowedParams() + array(
+                       'grouppages' => array(
+                               ApiBase::PARAM_TYPE => 'boolean',
+                               ApiBase::PARAM_DFLT => false,
+                       ),
                        'limit' => array(
                                ApiBase::PARAM_TYPE => 'limit',
-                               ApiBase::PARAM_DFLT => 20,
+                               ApiBase::PARAM_DFLT => 10,
                                ApiBase::PARAM_MIN => 1,
                                ApiBase::PARAM_MAX => $wgEchoMaxUpdateCount,
                                ApiBase::PARAM_MAX2 => $wgEchoMaxUpdateCount,

-- 
To view, visit https://gerrit.wikimedia.org/r/295591
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I443ca00ff5e5d36fd6910101226358942e6aa8ee
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Catrope <roan.katt...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to