Entry.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. class FreshRSS_Entry extends Minz_Model {
  3. const STATE_READ = 1;
  4. const STATE_NOT_READ = 2;
  5. const STATE_ALL = 3;
  6. const STATE_FAVORITE = 4;
  7. const STATE_NOT_FAVORITE = 8;
  8. /**
  9. * @var string
  10. */
  11. private $id = '0';
  12. /**
  13. * @var string
  14. */
  15. private $guid;
  16. private $title;
  17. private $authors;
  18. private $content;
  19. private $link;
  20. private $date;
  21. private $date_added = 0; //In microseconds
  22. /**
  23. * @var string
  24. */
  25. private $hash = '';
  26. /**
  27. * @var bool|null
  28. */
  29. private $is_read;
  30. /** @var bool|null */
  31. private $is_favorite;
  32. /**
  33. * @var int
  34. */
  35. private $feedId;
  36. /**
  37. * @var FreshRSS_Feed|null
  38. */
  39. private $feed;
  40. private $tags;
  41. private $attributes = [];
  42. public function __construct(int $feedId = 0, string $guid = '', string $title = '', string $authors = '', string $content = '',
  43. string $link = '', $pubdate = 0, bool $is_read = false, bool $is_favorite = false, string $tags = '') {
  44. $this->_title($title);
  45. $this->_authors($authors);
  46. $this->_content($content);
  47. $this->_link($link);
  48. $this->_date($pubdate);
  49. $this->_isRead($is_read);
  50. $this->_isFavorite($is_favorite);
  51. $this->_feedId($feedId);
  52. $this->_tags($tags);
  53. $this->_guid($guid);
  54. }
  55. /** @param array<string,mixed> $dao */
  56. public static function fromArray(array $dao): FreshRSS_Entry {
  57. if (empty($dao['content'])) {
  58. $dao['content'] = '';
  59. }
  60. if (!empty($dao['thumbnail'])) {
  61. $dao['content'] .= '<p class="enclosure-content"><img src="' . $dao['thumbnail'] . '" alt="" /></p>';
  62. }
  63. $entry = new FreshRSS_Entry(
  64. $dao['id_feed'] ?? 0,
  65. $dao['guid'] ?? '',
  66. $dao['title'] ?? '',
  67. $dao['author'] ?? '',
  68. $dao['content'] ?? '',
  69. $dao['link'] ?? '',
  70. $dao['date'] ?? 0,
  71. $dao['is_read'] ?? false,
  72. $dao['is_favorite'] ?? false,
  73. $dao['tags'] ?? ''
  74. );
  75. if (!empty($dao['id'])) {
  76. $entry->_id($dao['id']);
  77. }
  78. if (!empty($dao['timestamp'])) {
  79. $entry->_date(strtotime($dao['timestamp']));
  80. }
  81. if (!empty($dao['categories'])) {
  82. $entry->_tags($dao['categories']);
  83. }
  84. if (!empty($dao['attributes'])) {
  85. $entry->_attributes('', $dao['attributes']);
  86. }
  87. return $entry;
  88. }
  89. public function id(): string {
  90. return $this->id;
  91. }
  92. public function guid(): string {
  93. return $this->guid;
  94. }
  95. public function title(): string {
  96. return $this->title == '' ? $this->guid() : $this->title;
  97. }
  98. public function author(): string {
  99. //Deprecated
  100. return $this->authors(true);
  101. }
  102. public function authors(bool $asString = false) {
  103. if ($asString) {
  104. return $this->authors == null ? '' : ';' . implode('; ', $this->authors);
  105. } else {
  106. return $this->authors;
  107. }
  108. }
  109. public function content(): string {
  110. return $this->content;
  111. }
  112. /** @return array<array<string,string>> */
  113. public function enclosures(bool $searchBodyImages = false): array {
  114. $results = [];
  115. try {
  116. $searchEnclosures = strpos($this->content, '<p class="enclosure-content') !== false;
  117. $searchBodyImages &= (stripos($this->content, '<img') !== false);
  118. $xpath = null;
  119. if ($searchEnclosures || $searchBodyImages) {
  120. $dom = new DOMDocument();
  121. $dom->loadHTML('<?xml version="1.0" encoding="UTF-8" ?>' . $this->content, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING);
  122. $xpath = new DOMXpath($dom);
  123. }
  124. if ($searchEnclosures) {
  125. $enclosures = $xpath->query('//div[@class="enclosure"]/p[@class="enclosure-content"]/*[@src]');
  126. foreach ($enclosures as $enclosure) {
  127. $result = [
  128. 'url' => $enclosure->getAttribute('src'),
  129. 'type' => $enclosure->getAttribute('data-type'),
  130. 'medium' => $enclosure->getAttribute('data-medium'),
  131. 'length' => $enclosure->getAttribute('data-length'),
  132. ];
  133. if (empty($result['medium'])) {
  134. switch (strtolower($enclosure->nodeName)) {
  135. case 'img': $result['medium'] = 'image'; break;
  136. case 'video': $result['medium'] = 'video'; break;
  137. case 'audio': $result['medium'] = 'audio'; break;
  138. }
  139. }
  140. $results[] = $result;
  141. }
  142. }
  143. if ($searchBodyImages) {
  144. $images = $xpath->query('//img');
  145. foreach ($images as $img) {
  146. $src = $img->getAttribute('src');
  147. if ($src == null) {
  148. $src = $img->getAttribute('data-src');
  149. }
  150. if ($src != null) {
  151. $results[] = [
  152. 'url' => $src,
  153. 'alt' => $img->getAttribute('alt'),
  154. ];
  155. }
  156. }
  157. }
  158. return $results;
  159. } catch (Exception $ex) {
  160. return $results;
  161. }
  162. }
  163. /**
  164. * @return array<string,string>|null
  165. */
  166. public function thumbnail() {
  167. foreach ($this->enclosures(true) as $enclosure) {
  168. if (!empty($enclosure['url']) && empty($enclosure['type'])) {
  169. return $enclosure;
  170. }
  171. }
  172. return null;
  173. }
  174. public function link(): string {
  175. return $this->link;
  176. }
  177. public function date(bool $raw = false) {
  178. if ($raw) {
  179. return $this->date;
  180. }
  181. return timestamptodate($this->date);
  182. }
  183. public function machineReadableDate(): string {
  184. return @date (DATE_ATOM, $this->date);
  185. }
  186. public function dateAdded(bool $raw = false, bool $microsecond = false) {
  187. if ($raw) {
  188. if ($microsecond) {
  189. return $this->date_added;
  190. } else {
  191. return intval(substr($this->date_added, 0, -6));
  192. }
  193. } else {
  194. $date = intval(substr($this->date_added, 0, -6));
  195. return timestamptodate($date);
  196. }
  197. }
  198. public function isRead() {
  199. return $this->is_read;
  200. }
  201. public function isFavorite() {
  202. return $this->is_favorite;
  203. }
  204. /**
  205. * @return FreshRSS_Feed|null|false
  206. */
  207. public function feed() {
  208. if ($this->feed === null) {
  209. $feedDAO = FreshRSS_Factory::createFeedDao();
  210. $this->feed = $feedDAO->searchById($this->feedId);
  211. }
  212. return $this->feed;
  213. }
  214. public function feedId(): int {
  215. return $this->feedId;
  216. }
  217. public function tags($asString = false) {
  218. if ($asString) {
  219. return $this->tags == null ? '' : '#' . implode(' #', $this->tags);
  220. } else {
  221. return $this->tags;
  222. }
  223. }
  224. public function attributes($key = '') {
  225. if ($key == '') {
  226. return $this->attributes;
  227. } else {
  228. return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
  229. }
  230. }
  231. public function _attributes(string $key, $value) {
  232. if ($key == '') {
  233. if (is_string($value)) {
  234. $value = json_decode($value, true);
  235. }
  236. if (is_array($value)) {
  237. $this->attributes = $value;
  238. }
  239. } elseif ($value === null) {
  240. unset($this->attributes[$key]);
  241. } else {
  242. $this->attributes[$key] = $value;
  243. }
  244. }
  245. public function hash(): string {
  246. if ($this->hash == '') {
  247. //Do not include $this->date because it may be automatically generated when lacking
  248. $this->hash = md5($this->link . $this->title . $this->authors(true) . $this->content . $this->tags(true));
  249. }
  250. return $this->hash;
  251. }
  252. public function _hash(string $value) {
  253. $value = trim($value);
  254. if (ctype_xdigit($value)) {
  255. $this->hash = substr($value, 0, 32);
  256. }
  257. return $this->hash;
  258. }
  259. public function _id($value) {
  260. $this->id = $value;
  261. if ($this->date_added == 0) {
  262. $this->date_added = $value;
  263. }
  264. }
  265. public function _guid(string $value) {
  266. if ($value == '') {
  267. $value = $this->link;
  268. if ($value == '') {
  269. $value = $this->hash();
  270. }
  271. }
  272. $this->guid = $value;
  273. }
  274. public function _title(string $value) {
  275. $this->hash = '';
  276. $this->title = trim($value);
  277. }
  278. public function _author(string $value) {
  279. //Deprecated
  280. $this->_authors($value);
  281. }
  282. public function _authors($value) {
  283. $this->hash = '';
  284. if (!is_array($value)) {
  285. if (strpos($value, ';') !== false) {
  286. $value = htmlspecialchars_decode($value, ENT_QUOTES);
  287. $value = preg_split('/\s*[;]\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
  288. $value = Minz_Helper::htmlspecialchars_utf8($value);
  289. } else {
  290. $value = preg_split('/\s*[,]\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
  291. }
  292. }
  293. $this->authors = $value;
  294. }
  295. public function _content(string $value) {
  296. $this->hash = '';
  297. $this->content = $value;
  298. }
  299. public function _link(string $value) {
  300. $this->hash = '';
  301. $this->link = $value;
  302. }
  303. public function _date($value) {
  304. $this->hash = '';
  305. $value = intval($value);
  306. $this->date = $value > 1 ? $value : time();
  307. }
  308. public function _dateAdded($value, bool $microsecond = false) {
  309. if ($microsecond) {
  310. $this->date_added = $value;
  311. } else {
  312. $this->date_added = $value * 1000000;
  313. }
  314. }
  315. public function _isRead($value) {
  316. $this->is_read = $value === null ? null : (bool)$value;
  317. }
  318. public function _isFavorite($value) {
  319. $this->is_favorite = $value === null ? null : (bool)$value;
  320. }
  321. /** @param FreshRSS_Feed|null $feed */
  322. public function _feed($feed) {
  323. $this->feed = $feed;
  324. $this->feedId = $this->feed == null ? 0 : $this->feed->id();
  325. }
  326. /** @param int|string $id */
  327. private function _feedId($id) {
  328. $this->feed = null;
  329. $this->feedId = intval($id);
  330. }
  331. public function _tags($value) {
  332. $this->hash = '';
  333. if (!is_array($value)) {
  334. $value = preg_split('/\s*[#,]\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
  335. }
  336. $this->tags = $value;
  337. }
  338. public function matches(FreshRSS_BooleanSearch $booleanSearch): bool {
  339. $ok = true;
  340. foreach ($booleanSearch->searches() as $filter) {
  341. if ($filter instanceof FreshRSS_BooleanSearch) {
  342. // BooleanSearches are combined by AND (default) or OR or AND NOT (special cases) operators and are recursive
  343. if ($filter->operator() === 'OR') {
  344. $ok |= $this->matches($filter);
  345. } elseif ($filter->operator() === 'AND NOT') {
  346. $ok &= !$this->matches($filter);
  347. } else { // AND
  348. $ok &= $this->matches($filter);
  349. }
  350. } elseif ($filter instanceof FreshRSS_Search) {
  351. // Searches are combined by OR and are not recursive
  352. $ok = true;
  353. if ($filter->getMinDate()) {
  354. $ok &= strnatcmp($this->id, $filter->getMinDate() . '000000') >= 0;
  355. }
  356. if ($ok && $filter->getNotMinDate()) {
  357. $ok &= strnatcmp($this->id, $filter->getNotMinDate() . '000000') < 0;
  358. }
  359. if ($ok && $filter->getMaxDate()) {
  360. $ok &= strnatcmp($this->id, $filter->getMaxDate() . '000000') <= 0;
  361. }
  362. if ($ok && $filter->getNotMaxDate()) {
  363. $ok &= strnatcmp($this->id, $filter->getNotMaxDate() . '000000') > 0;
  364. }
  365. if ($ok && $filter->getMinPubdate()) {
  366. $ok &= $this->date >= $filter->getMinPubdate();
  367. }
  368. if ($ok && $filter->getNotMinPubdate()) {
  369. $ok &= $this->date < $filter->getNotMinPubdate();
  370. }
  371. if ($ok && $filter->getMaxPubdate()) {
  372. $ok &= $this->date <= $filter->getMaxPubdate();
  373. }
  374. if ($ok && $filter->getNotMaxPubdate()) {
  375. $ok &= $this->date > $filter->getNotMaxPubdate();
  376. }
  377. if ($ok && $filter->getFeedIds()) {
  378. $ok &= in_array($this->feedId, $filter->getFeedIds());
  379. }
  380. if ($ok && $filter->getNotFeedIds()) {
  381. $ok &= !in_array($this->feedId, $filter->getFeedIds());
  382. }
  383. if ($ok && $filter->getAuthor()) {
  384. foreach ($filter->getAuthor() as $author) {
  385. $ok &= stripos(implode(';', $this->authors), $author) !== false;
  386. }
  387. }
  388. if ($ok && $filter->getNotAuthor()) {
  389. foreach ($filter->getNotAuthor() as $author) {
  390. $ok &= stripos(implode(';', $this->authors), $author) === false;
  391. }
  392. }
  393. if ($ok && $filter->getIntitle()) {
  394. foreach ($filter->getIntitle() as $title) {
  395. $ok &= stripos($this->title, $title) !== false;
  396. }
  397. }
  398. if ($ok && $filter->getNotIntitle()) {
  399. foreach ($filter->getNotIntitle() as $title) {
  400. $ok &= stripos($this->title, $title) === false;
  401. }
  402. }
  403. if ($ok && $filter->getTags()) {
  404. foreach ($filter->getTags() as $tag2) {
  405. $found = false;
  406. foreach ($this->tags as $tag1) {
  407. if (strcasecmp($tag1, $tag2) === 0) {
  408. $found = true;
  409. }
  410. }
  411. $ok &= $found;
  412. }
  413. }
  414. if ($ok && $filter->getNotTags()) {
  415. foreach ($filter->getNotTags() as $tag2) {
  416. $found = false;
  417. foreach ($this->tags as $tag1) {
  418. if (strcasecmp($tag1, $tag2) === 0) {
  419. $found = true;
  420. }
  421. }
  422. $ok &= !$found;
  423. }
  424. }
  425. if ($ok && $filter->getInurl()) {
  426. foreach ($filter->getInurl() as $url) {
  427. $ok &= stripos($this->link, $url) !== false;
  428. }
  429. }
  430. if ($ok && $filter->getNotInurl()) {
  431. foreach ($filter->getNotInurl() as $url) {
  432. $ok &= stripos($this->link, $url) === false;
  433. }
  434. }
  435. if ($ok && $filter->getSearch()) {
  436. foreach ($filter->getSearch() as $needle) {
  437. $ok &= (stripos($this->title, $needle) !== false || stripos($this->content, $needle) !== false);
  438. }
  439. }
  440. if ($ok && $filter->getNotSearch()) {
  441. foreach ($filter->getNotSearch() as $needle) {
  442. $ok &= (stripos($this->title, $needle) === false && stripos($this->content, $needle) === false);
  443. }
  444. }
  445. if ($ok) {
  446. return true;
  447. }
  448. }
  449. }
  450. return $ok;
  451. }
  452. public function applyFilterActions(array $titlesAsRead = []) {
  453. if ($this->feed != null) {
  454. if ($this->feed->attributes('read_upon_reception') ||
  455. ($this->feed->attributes('read_upon_reception') === null && FreshRSS_Context::$user_conf->mark_when['reception'])) {
  456. $this->_isRead(true);
  457. }
  458. if (isset($titlesAsRead[$this->title()])) {
  459. Minz_Log::debug('Mark title as read: ' . $this->title());
  460. $this->_isRead(true);
  461. }
  462. foreach ($this->feed->filterActions() as $filterAction) {
  463. if ($this->matches($filterAction->booleanSearch())) {
  464. foreach ($filterAction->actions() as $action) {
  465. switch ($action) {
  466. case 'read':
  467. $this->_isRead(true);
  468. break;
  469. case 'star':
  470. $this->_isFavorite(true);
  471. break;
  472. case 'label':
  473. //TODO: Implement more actions
  474. break;
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
  481. public function isDay(int $day, int $today): bool {
  482. $date = $this->dateAdded(true);
  483. switch ($day) {
  484. case FreshRSS_Days::TODAY:
  485. $tomorrow = $today + 86400;
  486. return $date >= $today && $date < $tomorrow;
  487. case FreshRSS_Days::YESTERDAY:
  488. $yesterday = $today - 86400;
  489. return $date >= $yesterday && $date < $today;
  490. case FreshRSS_Days::BEFORE_YESTERDAY:
  491. $yesterday = $today - 86400;
  492. return $date < $yesterday;
  493. default:
  494. return false;
  495. }
  496. }
  497. /**
  498. * @param array<string,mixed> $attributes
  499. */
  500. public static function getContentByParsing(string $url, string $path, array $attributes = [], int $maxRedirs = 3): string {
  501. $cachePath = FreshRSS_Feed::cacheFilename($url, $attributes, FreshRSS_Feed::KIND_HTML_XPATH);
  502. $html = httpGet($url, $cachePath, 'html', $attributes);
  503. if (strlen($html) > 0) {
  504. $doc = new DOMDocument();
  505. $doc->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING);
  506. $xpath = new DOMXPath($doc);
  507. if ($maxRedirs > 0) {
  508. //Follow any HTML redirection
  509. $metas = $xpath->query('//meta[@content]');
  510. /** @var array<DOMElement> $metas */
  511. foreach ($metas as $meta) {
  512. if (strtolower(trim($meta->getAttribute('http-equiv'))) === 'refresh') {
  513. $refresh = preg_replace('/^[0-9.; ]*\s*(url\s*=)?\s*/i', '', trim($meta->getAttribute('content')));
  514. $refresh = SimplePie_Misc::absolutize_url($refresh, $url);
  515. if ($refresh != false && $refresh !== $url) {
  516. return self::getContentByParsing($refresh, $path, $attributes, $maxRedirs - 1);
  517. }
  518. }
  519. }
  520. }
  521. $base = $xpath->evaluate('normalize-space(//base/@href)');
  522. if ($base == false || !is_string($base)) {
  523. $base = $url;
  524. } elseif (substr($base, 0, 2) === '//') {
  525. //Protocol-relative URLs "//www.example.net"
  526. $base = (parse_url($url, PHP_URL_SCHEME) ?? 'https') . ':' . $base;
  527. }
  528. $content = '';
  529. $nodes = $xpath->query(new Gt\CssXPath\Translator($path));
  530. if ($nodes != false) {
  531. foreach ($nodes as $node) {
  532. if (!empty($attributes['path_entries_filter'])) {
  533. $filterednodes = $xpath->query(new Gt\CssXPath\Translator($attributes['path_entries_filter']), $node);
  534. foreach ($filterednodes as $filterednode) {
  535. $filterednode->parentNode->removeChild($filterednode);
  536. }
  537. }
  538. $content .= $doc->saveHtml($node) . "\n";
  539. }
  540. }
  541. $html = trim(sanitizeHTML($content, $base));
  542. return $html;
  543. } else {
  544. throw new Exception();
  545. }
  546. }
  547. public function loadCompleteContent(bool $force = false): bool {
  548. // Gestion du contenu
  549. // Trying to fetch full article content even when feeds do not propose it
  550. $feed = $this->feed();
  551. if ($feed != null && trim($feed->pathEntries()) != '') {
  552. $entryDAO = FreshRSS_Factory::createEntryDao();
  553. $entry = $force ? null : $entryDAO->searchByGuid($this->feedId, $this->guid);
  554. if ($entry) {
  555. // l’article existe déjà en BDD, en se contente de recharger ce contenu
  556. $this->content = $entry->content();
  557. } else {
  558. try {
  559. // l’article n’est pas en BDD, on va le chercher sur le site
  560. $fullContent = self::getContentByParsing(
  561. htmlspecialchars_decode($this->link(), ENT_QUOTES),
  562. $feed->pathEntries(),
  563. $feed->attributes()
  564. );
  565. if ('' !== $fullContent) {
  566. $fullContent = "<!-- FULLCONTENT start //-->{$fullContent}<!-- FULLCONTENT end //-->";
  567. $originalContent = preg_replace('#<!-- FULLCONTENT start //-->.*<!-- FULLCONTENT end //-->#s', '', $this->content());
  568. switch ($feed->attributes('content_action')) {
  569. case 'prepend':
  570. $this->content = $fullContent . $originalContent;
  571. break;
  572. case 'append':
  573. $this->content = $originalContent . $fullContent;
  574. break;
  575. case 'replace':
  576. default:
  577. $this->content = $fullContent;
  578. break;
  579. }
  580. return true;
  581. }
  582. } catch (Exception $e) {
  583. // rien à faire, on garde l’ancien contenu(requête a échoué)
  584. Minz_Log::warning($e->getMessage());
  585. }
  586. }
  587. }
  588. return false;
  589. }
  590. public function toArray(): array {
  591. return array(
  592. 'id' => $this->id(),
  593. 'guid' => $this->guid(),
  594. 'title' => $this->title(),
  595. 'author' => $this->authors(true),
  596. 'content' => $this->content(),
  597. 'link' => $this->link(),
  598. 'date' => $this->date(true),
  599. 'hash' => $this->hash(),
  600. 'is_read' => $this->isRead(),
  601. 'is_favorite' => $this->isFavorite(),
  602. 'id_feed' => $this->feedId(),
  603. 'tags' => $this->tags(true),
  604. 'attributes' => $this->attributes(),
  605. );
  606. }
  607. /**
  608. * Integer format conversion for Google Reader API format
  609. * @param string|int $dec Decimal number
  610. * @return string 64-bit hexa http://code.google.com/p/google-reader-api/wiki/ItemId
  611. */
  612. private static function dec2hex($dec): string {
  613. return PHP_INT_SIZE < 8 ? // 32-bit ?
  614. str_pad(gmp_strval(gmp_init($dec, 10), 16), 16, '0', STR_PAD_LEFT) :
  615. str_pad(dechex($dec), 16, '0', STR_PAD_LEFT);
  616. }
  617. /**
  618. * N.B.: To avoid expensive lookups, ensure to set `$entry->_feed($feed)` before calling this function.
  619. * N.B.: You might have to populate `$entry->_tags()` prior to calling this function.
  620. * @param string $mode Set to `'compat'` to use an alternative Unicode representation for problematic HTML special characters not decoded by some clients;
  621. * set to `'freshrss'` for using FreshRSS additions for internal use (e.g. export/import).
  622. * @return array<string,mixed> A representation of this entry in a format compatible with Google Reader API
  623. */
  624. public function toGReader(string $mode = ''): array {
  625. $feed = $this->feed();
  626. $category = $feed == null ? null : $feed->category();
  627. $item = [
  628. 'id' => 'tag:google.com,2005:reader/item/' . self::dec2hex($this->id()),
  629. 'crawlTimeMsec' => substr($this->dateAdded(true, true), 0, -3),
  630. 'timestampUsec' => '' . $this->dateAdded(true, true), //EasyRSS & Reeder
  631. 'published' => $this->date(true),
  632. // 'updated' => $this->date(true),
  633. 'title' => $this->title(),
  634. 'summary' => ['content' => $this->content()],
  635. 'canonical' => [
  636. ['href' => htmlspecialchars_decode($this->link(), ENT_QUOTES)],
  637. ],
  638. 'alternate' => [
  639. [
  640. 'href' => htmlspecialchars_decode($this->link(), ENT_QUOTES),
  641. 'type' => 'text/html',
  642. ],
  643. ],
  644. 'categories' => [
  645. 'user/-/state/com.google/reading-list',
  646. ],
  647. 'origin' => [
  648. 'streamId' => 'feed/' . $this->feedId,
  649. ],
  650. ];
  651. if ($mode === 'compat') {
  652. $item['title'] = escapeToUnicodeAlternative($this->title(), false);
  653. } elseif ($mode === 'freshrss') {
  654. $item['guid'] = $this->guid();
  655. unset($item['summary']);
  656. $item['content'] = ['content' => $this->content()];
  657. }
  658. if ($category != null && $mode !== 'freshrss') {
  659. $item['categories'][] = 'user/-/label/' . htmlspecialchars_decode($category->name(), ENT_QUOTES);
  660. }
  661. if ($feed != null) {
  662. $item['origin']['htmlUrl'] = htmlspecialchars_decode($feed->website());
  663. $item['origin']['title'] = $feed->name(); //EasyRSS
  664. if ($mode === 'compat') {
  665. $item['origin']['title'] = escapeToUnicodeAlternative($feed->name(), true);
  666. } elseif ($mode === 'freshrss') {
  667. $item['origin']['feedUrl'] = htmlspecialchars_decode($feed->url());
  668. }
  669. }
  670. foreach ($this->enclosures() as $enclosure) {
  671. if (!empty($enclosure['url']) && !empty($enclosure['type'])) {
  672. $media = [
  673. 'href' => $enclosure['url'],
  674. 'type' => $enclosure['type'],
  675. ];
  676. if (!empty($enclosure['length'])) {
  677. $media['length'] = intval($enclosure['length']);
  678. }
  679. $item['enclosure'][] = $media;
  680. }
  681. }
  682. $author = $this->authors(true);
  683. $author = trim($author, '; ');
  684. if ($author != '') {
  685. if ($mode === 'compat') {
  686. $item['author'] = escapeToUnicodeAlternative($author, false);
  687. } else {
  688. $item['author'] = $author;
  689. }
  690. }
  691. if ($this->isRead()) {
  692. $item['categories'][] = 'user/-/state/com.google/read';
  693. } elseif ($mode === 'freshrss') {
  694. $item['categories'][] = 'user/-/state/com.google/unread';
  695. }
  696. if ($this->isFavorite()) {
  697. $item['categories'][] = 'user/-/state/com.google/starred';
  698. }
  699. foreach ($this->tags() as $tagName) {
  700. $item['categories'][] = 'user/-/label/' . htmlspecialchars_decode($tagName, ENT_QUOTES);
  701. }
  702. return $item;
  703. }
  704. }