From c05133283af0486e08c9a97a468bc075e238f2d2 Mon Sep 17 00:00:00 2001 From: Matthew Flaschen Date: Tue, 19 Jul 2016 23:02:33 -0400 Subject: [PATCH] Add seen time to output of API, in ISO 8601 format Like count, it is available both grouped by section and ungrouped (top-level). Unlike count, the top-level still has both sections (but with the string 'seenTime' at the root), and if groupbysection is used, it will not also have top-level (since it would be redundant). Example output at T139993 It will be false or omitted if there is no seen time, depending on JSON format version (2+ is false). Bug: T139993 Change-Id: I9f4f9df69203204b56002afa1be6ed2336c33898 --- includes/SeenTime.php | 14 +++++++++--- includes/api/ApiEchoNotifications.php | 33 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/includes/SeenTime.php b/includes/SeenTime.php index 26e3c5601..21e11f966 100644 --- a/includes/SeenTime.php +++ b/includes/SeenTime.php @@ -39,10 +39,12 @@ class EchoSeenTime { } /** + * @param string $type Type of seen time to get * @param int $flags BagOStuff::READ_LATEST to use the master - * @return string|bool false if no stored time + * @param int $format Format to return time in, defaults to TS_MW + * @return string|bool Timestamp in specified format, or false if no stored time */ - public function getTime( $type = 'all', $flags = 0 ) { + public function getTime( $type = 'all', $flags = 0, $format = TS_MW ) { $vals = array(); if ( $type === 'all' ) { foreach ( self::$allowedTypes as $allowed ) { @@ -62,7 +64,13 @@ class EchoSeenTime { } } - return $data; + if ( $data !== false ) { + $formattedData = wfTimestamp( $format, $data ); + } else { + $formattedData = $data; + } + + return $formattedData; } public function setTime( $time, $type = 'all' ) { diff --git a/includes/api/ApiEchoNotifications.php b/includes/api/ApiEchoNotifications.php index 36652afb6..acb8d94ca 100644 --- a/includes/api/ApiEchoNotifications.php +++ b/includes/api/ApiEchoNotifications.php @@ -131,6 +131,13 @@ class ApiEchoNotifications extends ApiCrossWikiBase { ); } + if ( in_array( 'seenTime', $prop ) ) { + $result = array_merge_recursive( + $result, + $this->getPropSeenTime( $user, $params['sections'], $params['groupbysection'] ) + ); + } + return $result; } @@ -301,6 +308,31 @@ class ApiEchoNotifications extends ApiCrossWikiBase { return $result; } + /** + * Internal helper method for getting property 'seenTime' data + * @param User $user + * @param string[] $sections + * @param boolean $groupBySection + * @return array + */ + protected function getPropSeenTime( User $user, array $sections, $groupBySection ) { + $result = array(); + $seenTimeHelper = EchoSeenTime::newFromUser( $user ); + + if ( $groupBySection ) { + foreach ( $sections as $section ) { + $result[$section]['seenTime'] = $seenTimeHelper->getTime( $section, /*flags*/ 0, TS_ISO_8601 ); + } + } else { + $result['seenTime'] = array(); + foreach ( $sections as $section ) { + $result['seenTime'][$section] = $seenTimeHelper->getTime( $section, /*flags*/ 0, TS_ISO_8601 ); + } + } + + return $result; + } + /** * Build and format a "fake" notification to represent foreign notifications. * @param User $user @@ -529,6 +561,7 @@ class ApiEchoNotifications extends ApiCrossWikiBase { ApiBase::PARAM_TYPE => array( 'list', 'count', + 'seenTime', ), ApiBase::PARAM_DFLT => 'list', ),