1
0
mirror of https://github.com/chylex/Nextcloud-News.git synced 2025-09-15 22:32:09 +02:00

Compare commits

1 Commits

Author SHA1 Message Date
b9c807ff63 Add article word count to JSON
Signed-off-by: chylex <contact@chylex.com>
2022-05-28 08:53:40 +02:00

View File

@@ -349,6 +349,10 @@ class Item extends Entity implements IAPI, \JsonSerializable
return json_decode($this->getCategoriesJson()); return json_decode($this->getCategoriesJson());
} }
public function getWordCount(): int {
return self::countWords(strip_tags($this->getBody()));
}
/** /**
* Turns entity attributes into an array * Turns entity attributes into an array
*/ */
@@ -377,7 +381,8 @@ class Item extends Entity implements IAPI, \JsonSerializable
'fingerprint' => $this->getFingerprint(), 'fingerprint' => $this->getFingerprint(),
'categories' => $this->getCategories(), 'categories' => $this->getCategories(),
'sharedBy' => $this->getSharedBy(), 'sharedBy' => $this->getSharedBy(),
'sharedByDisplayName' => $this->getSharedByDisplayName() 'sharedByDisplayName' => $this->getSharedByDisplayName(),
'wordCount' => $this->getWordCount(),
]; ];
} }
@@ -740,4 +745,11 @@ class Item extends Entity implements IAPI, \JsonSerializable
stripos($mime, 'image/') !== false || stripos($mime, 'image/') !== false ||
stripos($mime, 'video/') !== false)); stripos($mime, 'video/') !== false));
} }
private static function countWords(string $text): int
{
$text = preg_replace('/[\pP\pM]/u', '', $text); // strip punctuation (P) and combination marks (M)
$text = preg_replace('/[^\pL\pN]+/u', ' ', $text); // convert every sequence of non-letters (L) and non-numbers (N) to a space
return 1 + substr_count($text, ' ');
}
} }