From a6db8df92d9b6c27db4ad4a728966226a283d05d Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 Nov 2021 19:40:39 +0000 Subject: [PATCH] Using the "extension" parameter --- src/Module/Api/Friendica/Events/Index.php | 2 +- .../Api/Friendica/GNUSocial/Version.php | 2 +- src/Module/Api/Friendica/Help/Test.php | 4 +-- src/Module/Api/Friendica/Profile/Show.php | 4 +-- src/Module/BaseApi.php | 27 +++++-------------- 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/Module/Api/Friendica/Events/Index.php b/src/Module/Api/Friendica/Events/Index.php index ec8cc077ae..b0ca78ac9e 100644 --- a/src/Module/Api/Friendica/Events/Index.php +++ b/src/Module/Api/Friendica/Events/Index.php @@ -70,6 +70,6 @@ class Index extends BaseApi ]; } - self::exit('events', ['events' => $items]); + self::exit('events', ['events' => $items], $parameters['extension'] ?? null); } } diff --git a/src/Module/Api/Friendica/GNUSocial/Version.php b/src/Module/Api/Friendica/GNUSocial/Version.php index 83949de7b7..121b571797 100644 --- a/src/Module/Api/Friendica/GNUSocial/Version.php +++ b/src/Module/Api/Friendica/GNUSocial/Version.php @@ -30,6 +30,6 @@ class Version extends BaseApi { public static function rawContent(array $parameters = []) { - self::exit('version', ['version' => '0.9.7']); + self::exit('version', ['version' => '0.9.7'], $parameters['extension'] ?? null); } } diff --git a/src/Module/Api/Friendica/Help/Test.php b/src/Module/Api/Friendica/Help/Test.php index 394c5e4830..244e9f2e57 100644 --- a/src/Module/Api/Friendica/Help/Test.php +++ b/src/Module/Api/Friendica/Help/Test.php @@ -30,12 +30,12 @@ class Test extends BaseApi { public static function rawContent(array $parameters = []) { - if (self::$format == 'xml') { + if (!empty($parameters['extension']) && ($parameters['extension'] == 'xml')) { $ok = 'true'; } else { $ok = 'ok'; } - self::exit('ok', ['ok' => $ok]); + self::exit('ok', ['ok' => $ok], $parameters['extension'] ?? null); } } diff --git a/src/Module/Api/Friendica/Profile/Show.php b/src/Module/Api/Friendica/Profile/Show.php index 67dcfdb6e7..18671e0dd5 100644 --- a/src/Module/Api/Friendica/Profile/Show.php +++ b/src/Module/Api/Friendica/Profile/Show.php @@ -50,7 +50,7 @@ class Show extends BaseApi $profile = self::formatProfile($profile, $profileFields); $profiles = []; - if (self::$format == 'xml') { + if (!empty($parameters['extension']) && ($parameters['extension'] == 'xml')) { $profiles['0:profile'] = $profile; } else { $profiles[] = $profile; @@ -66,7 +66,7 @@ class Show extends BaseApi 'profiles' => $profiles ]; - self::exit('friendica_profiles', ['$result' => $result]); + self::exit('friendica_profiles', ['$result' => $result], $parameters['extension'] ?? null); } /** diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index 75ee9ca29a..a7a0688ef4 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -43,11 +43,6 @@ class BaseApi extends BaseModule const SCOPE_FOLLOW = 'follow'; const SCOPE_PUSH = 'push'; - /** - * @var string json|xml|rss|atom - */ - protected static $format = 'json'; - /** * @var array */ @@ -60,17 +55,6 @@ class BaseApi extends BaseModule public static function init(array $parameters = []) { - $arguments = DI::args(); - - if (substr($arguments->getCommand(), -4) === '.xml') { - self::$format = 'xml'; - } - if (substr($arguments->getCommand(), -4) === '.rss') { - self::$format = 'rss'; - } - if (substr($arguments->getCommand(), -4) === '.atom') { - self::$format = 'atom'; - } } public static function delete(array $parameters = []) @@ -351,14 +335,17 @@ class BaseApi extends BaseModule * Outputs formatted data according to the data type and then exits the execution. * * @param string $root_element - * @param array $data An array with a single element containing the returned result + * @param array $data An array with a single element containing the returned result + * @param string $format Output format (xml, json, rss, atom) * @return false|string */ - protected static function exit(string $root_element, array $data) + protected static function exit(string $root_element, array $data, string $format = null) { - $return = self::formatData($root_element, self::$format, $data); + $format = $format ?? 'json'; - switch (self::$format) { + $return = self::formatData($root_element, $format, $data); + + switch ($format) { case 'xml': header('Content-Type: text/xml'); break;