Parcourir la source

API: stream itemRefs output of stream/items/ids (#9669)

Follow-up of #6137 and #8041 for #6136

Contributes to #6136

Changes proposed in this pull request:

- `streamContentsItemsIds()` now streams its `itemRefs` JSON output instead of building it in memory, like `streamContents()` and `streamContentsItems()`
- Add the missing `Content-Type: application/json; charset=UTF-8` header, for consistency
- News+ workaround and `continuation` logic unchanged; output identical apart from whitespace

* API: stream itemRefs output of stream/items/ids

Follow-up of #6137 and #8041 for #6136

* Update to PHP 8.2+ Heredoc indent style

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Galaba Mohit il y a 5 heures
Parent
commit
99cc2a95f0
1 fichiers modifiés avec 46 ajouts et 29 suppressions
  1. 46 29
      p/api/greader.php

+ 46 - 29
p/api/greader.php

@@ -742,12 +742,12 @@ final class GReaderAPI {
 
 		// Note: This section must be streamed to avoid memory issues with large responses
 		echo <<<TXT
-{
-	"id": "user/-/state/com.google/reading-list",
-	"updated": $time,
-	"items": [
+			{
+				"id": "user/-/state/com.google/reading-list",
+				"updated": $time,
+				"items": [
 
-TXT;
+			TXT;
 		foreach ($items as $item) {
 			if (!is_array($item) || empty($item)) {
 				continue;
@@ -762,19 +762,19 @@ TXT;
 		}
 		echo <<<'TXT'
 
-	]
-TXT;
+				]
+			TXT;
 		if ($nbItems >= $count && $lastEntryId > 0) {
 			echo <<<TXT
-,
-	"continuation": "$lastEntryId"
-TXT;
+				,
+					"continuation": "$lastEntryId"
+				TXT;
 		}
 		echo <<<'TXT'
 
-}
+			}
 
-TXT;
+			TXT;
 		exit();
 	}
 
@@ -786,6 +786,8 @@ TXT;
 		// https://github.com/mihaip/google-reader-api/blob/master/wiki/ApiStreamItemsIds.wiki
 		// https://code.google.com/archive/p/pyrfeed/wikis/GoogleReaderAPI.wiki
 		// https://web.archive.org/web/20210126115837/https://blog.martindoms.com/2009/10/16/using-the-google-reader-api-part-2#feed
+		header('Content-Type: application/json; charset=UTF-8');
+
 		$type = 'A';
 		if ($streamId === 'user/-/state/com.google/reading-list') {
 			$type = 'A';
@@ -838,24 +840,39 @@ TXT;
 		if (empty($ids) && isset($_GET['client']) && $_GET['client'] === 'newsplus') {
 			$ids = [ 0 ];	//For News+ bug https://github.com/noinnion/newsplus/issues/84#issuecomment-57834632
 		}
-		$itemRefs = [];
+		$nbItems = 0;
+
+		// Note: This section must be streamed to avoid memory issues with large responses
+		echo <<<'TXT'
+			{
+				"itemRefs": [
+
+			TXT;
 		foreach ($ids as $entryId) {
-			$itemRefs[] = [
-				'id' => '' . $entryId,	//64-bit decimal
-			];
+			if ($nbItems > 0) {
+				echo ",\n";
+			}
+			echo json_encode(['id' => '' . $entryId], JSON_OPTIONS);	//64-bit decimal
+			$nbItems++;
 		}
+		echo <<<'TXT'
 
-		$response = [
-			'itemRefs' => $itemRefs,
-		];
+				]
+			TXT;
 		if (count($ids) >= $count) {
 			$entryId = end($ids);
 			if ($entryId != false) {
-				$response['continuation'] = '' . $entryId;
+				echo <<<TXT
+					,
+						"continuation": "$entryId"
+					TXT;
 			}
 		}
+		echo <<<'TXT'
+
+			}
 
-		echo json_encode($response, JSON_OPTIONS), "\n";
+			TXT;
 		exit();
 	}
 
@@ -882,12 +899,12 @@ TXT;
 
 		// Note: This section must be streamed to avoid memory issues with large responses
 		echo <<<TXT
-{
-	"id": "user/-/state/com.google/reading-list",
-	"updated": $time,
-	"items": [
+			{
+				"id": "user/-/state/com.google/reading-list",
+				"updated": $time,
+				"items": [
 
-TXT;
+			TXT;
 		foreach ($items as $item) {
 			if (!is_array($item) || empty($item)) {
 				continue;
@@ -901,10 +918,10 @@ TXT;
 		}
 		echo <<<'TXT'
 
-	]
-}
+				]
+			}
 
-TXT;
+			TXT;
 		exit();
 	}