Entry.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 (special case) operator and are recursive
  343. if ($filter->operator() === 'OR') {
  344. $ok |= $this->matches($filter);
  345. } else {
  346. $ok &= $this->matches($filter);
  347. }
  348. } elseif ($filter instanceof FreshRSS_Search) {
  349. // Searches are combined by OR and are not recursive
  350. $ok = true;
  351. if ($filter->getMinDate()) {
  352. $ok &= strnatcmp($this->id, $filter->getMinDate() . '000000') >= 0;
  353. }
  354. if ($ok && $filter->getNotMinDate()) {
  355. $ok &= strnatcmp($this->id, $filter->getNotMinDate() . '000000') < 0;
  356. }
  357. if ($ok && $filter->getMaxDate()) {
  358. $ok &= strnatcmp($this->id, $filter->getMaxDate() . '000000') <= 0;
  359. }
  360. if ($ok && $filter->getNotMaxDate()) {
  361. $ok &= strnatcmp($this->id, $filter->getNotMaxDate() . '000000') > 0;
  362. }
  363. if ($ok && $filter->getMinPubdate()) {
  364. $ok &= $this->date >= $filter->getMinPubdate();
  365. }
  366. if ($ok && $filter->getNotMinPubdate()) {
  367. $ok &= $this->date < $filter->getNotMinPubdate();
  368. }
  369. if ($ok && $filter->getMaxPubdate()) {
  370. $ok &= $this->date <= $filter->getMaxPubdate();
  371. }
  372. if ($ok && $filter->getNotMaxPubdate()) {
  373. $ok &= $this->date > $filter->getNotMaxPubdate();
  374. }
  375. if ($ok && $filter->getFeedIds()) {
  376. $ok &= in_array($this->feedId, $filter->getFeedIds());
  377. }
  378. if ($ok && $filter->getNotFeedIds()) {
  379. $ok &= !in_array($this->feedId, $filter->getFeedIds());
  380. }
  381. if ($ok && $filter->getAuthor()) {
  382. foreach ($filter->getAuthor() as $author) {
  383. $ok &= stripos(implode(';', $this->authors), $author) !== false;
  384. }
  385. }
  386. if ($ok && $filter->getNotAuthor()) {
  387. foreach ($filter->getNotAuthor() as $author) {
  388. $ok &= stripos(implode(';', $this->authors), $author) === false;
  389. }
  390. }
  391. if ($ok && $filter->getIntitle()) {
  392. foreach ($filter->getIntitle() as $title) {
  393. $ok &= stripos($this->title, $title) !== false;
  394. }
  395. }
  396. if ($ok && $filter->getNotIntitle()) {
  397. foreach ($filter->getNotIntitle() as $title) {
  398. $ok &= stripos($this->title, $title) === false;
  399. }
  400. }
  401. if ($ok && $filter->getTags()) {
  402. foreach ($filter->getTags() as $tag2) {
  403. $found = false;
  404. foreach ($this->tags as $tag1) {
  405. if (strcasecmp($tag1, $tag2) === 0) {
  406. $found = true;
  407. }
  408. }
  409. $ok &= $found;
  410. }
  411. }
  412. if ($ok && $filter->getNotTags()) {
  413. foreach ($filter->getNotTags() as $tag2) {
  414. $found = false;
  415. foreach ($this->tags as $tag1) {
  416. if (strcasecmp($tag1, $tag2) === 0) {
  417. $found = true;
  418. }
  419. }
  420. $ok &= !$found;
  421. }
  422. }
  423. if ($ok && $filter->getInurl()) {
  424. foreach ($filter->getInurl() as $url) {
  425. $ok &= stripos($this->link, $url) !== false;
  426. }
  427. }
  428. if ($ok && $filter->getNotInurl()) {
  429. foreach ($filter->getNotInurl() as $url) {
  430. $ok &= stripos($this->link, $url) === false;
  431. }
  432. }
  433. if ($ok && $filter->getSearch()) {
  434. foreach ($filter->getSearch() as $needle) {
  435. $ok &= (stripos($this->title, $needle) !== false || stripos($this->content, $needle) !== false);
  436. }
  437. }
  438. if ($ok && $filter->getNotSearch()) {
  439. foreach ($filter->getNotSearch() as $needle) {
  440. $ok &= (stripos($this->title, $needle) === false && stripos($this->content, $needle) === false);
  441. }
  442. }
  443. if ($ok) {
  444. return true;
  445. }
  446. }
  447. }
  448. return $ok;
  449. }
  450. public function applyFilterActions(array $titlesAsRead = []) {
  451. if ($this->feed != null) {
  452. if ($this->feed->attributes('read_upon_reception') ||
  453. ($this->feed->attributes('read_upon_reception') === null && FreshRSS_Context::$user_conf->mark_when['reception'])) {
  454. $this->_isRead(true);
  455. }
  456. if (isset($titlesAsRead[$this->title()])) {
  457. Minz_Log::debug('Mark title as read: ' . $this->title());
  458. $this->_isRead(true);
  459. }
  460. foreach ($this->feed->filterActions() as $filterAction) {
  461. if ($this->matches($filterAction->booleanSearch())) {
  462. foreach ($filterAction->actions() as $action) {
  463. switch ($action) {
  464. case 'read':
  465. $this->_isRead(true);
  466. break;
  467. case 'star':
  468. $this->_isFavorite(true);
  469. break;
  470. case 'label':
  471. //TODO: Implement more actions
  472. break;
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479. public function isDay(int $day, int $today): bool {
  480. $date = $this->dateAdded(true);
  481. switch ($day) {
  482. case FreshRSS_Days::TODAY:
  483. $tomorrow = $today + 86400;
  484. return $date >= $today && $date < $tomorrow;
  485. case FreshRSS_Days::YESTERDAY:
  486. $yesterday = $today - 86400;
  487. return $date >= $yesterday && $date < $today;
  488. case FreshRSS_Days::BEFORE_YESTERDAY:
  489. $yesterday = $today - 86400;
  490. return $date < $yesterday;
  491. default:
  492. return false;
  493. }
  494. }
  495. /**
  496. * @param array<string,mixed> $attributes
  497. */
  498. public static function getContentByParsing(string $url, string $path, array $attributes = [], int $maxRedirs = 3): string {
  499. $cachePath = FreshRSS_Feed::cacheFilename($url, $attributes, FreshRSS_Feed::KIND_HTML_XPATH);
  500. $html = httpGet($url, $cachePath, 'html', $attributes);
  501. if (strlen($html) > 0) {
  502. $doc = new DOMDocument();
  503. $doc->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING);
  504. $xpath = new DOMXPath($doc);
  505. if ($maxRedirs > 0) {
  506. //Follow any HTML redirection
  507. $metas = $xpath->query('//meta[@content]');
  508. /** @var array<DOMElement> $metas */
  509. foreach ($metas as $meta) {
  510. if (strtolower(trim($meta->getAttribute('http-equiv'))) === 'refresh') {
  511. $refresh = preg_replace('/^[0-9.; ]*\s*(url\s*=)?\s*/i', '', trim($meta->getAttribute('content')));
  512. $refresh = SimplePie_Misc::absolutize_url($refresh, $url);
  513. if ($refresh != false && $refresh !== $url) {
  514. return self::getContentByParsing($refresh, $path, $attributes, $maxRedirs - 1);
  515. }
  516. }
  517. }
  518. }
  519. $base = $xpath->evaluate('normalize-space(//base/@href)');
  520. if ($base == false || !is_string($base)) {
  521. $base = $url;
  522. } elseif (substr($base, 0, 2) === '//') {
  523. //Protocol-relative URLs "//www.example.net"
  524. $base = (parse_url($url, PHP_URL_SCHEME) ?? 'https') . ':' . $base;
  525. }
  526. $content = '';
  527. $nodes = $xpath->query(new Gt\CssXPath\Translator($path));
  528. if ($nodes != false) {
  529. foreach ($nodes as $node) {
  530. if (!empty($attributes['path_entries_filter'])) {
  531. $filterednodes = $xpath->query(new Gt\CssXPath\Translator($attributes['path_entries_filter']), $node);
  532. foreach ($filterednodes as $filterednode) {
  533. $filterednode->parentNode->removeChild($filterednode);
  534. }
  535. }
  536. $content .= $doc->saveHtml($node) . "\n";
  537. }
  538. }
  539. $html = trim(sanitizeHTML($content, $base));
  540. return $html;
  541. } else {
  542. throw new Exception();
  543. }
  544. }
  545. public function loadCompleteContent(bool $force = false): bool {
  546. // Gestion du contenu
  547. // Trying to fetch full article content even when feeds do not propose it
  548. $feed = $this->feed();
  549. if ($feed != null && trim($feed->pathEntries()) != '') {
  550. $entryDAO = FreshRSS_Factory::createEntryDao();
  551. $entry = $force ? null : $entryDAO->searchByGuid($this->feedId, $this->guid);
  552. if ($entry) {
  553. // l’article existe déjà en BDD, en se contente de recharger ce contenu
  554. $this->content = $entry->content();
  555. } else {
  556. try {
  557. // l’article n’est pas en BDD, on va le chercher sur le site
  558. $fullContent = self::getContentByParsing(
  559. htmlspecialchars_decode($this->link(), ENT_QUOTES),
  560. $feed->pathEntries(),
  561. $feed->attributes()
  562. );
  563. if ('' !== $fullContent) {
  564. $fullContent = "<!-- FULLCONTENT start //-->{$fullContent}<!-- FULLCONTENT end //-->";
  565. $originalContent = preg_replace('#<!-- FULLCONTENT start //-->.*<!-- FULLCONTENT end //-->#s', '', $this->content());
  566. switch ($feed->attributes('content_action')) {
  567. case 'prepend':
  568. $this->content = $fullContent . $originalContent;
  569. break;
  570. case 'append':
  571. $this->content = $originalContent . $fullContent;
  572. break;
  573. case 'replace':
  574. default:
  575. $this->content = $fullContent;
  576. break;
  577. }
  578. return true;
  579. }
  580. } catch (Exception $e) {
  581. // rien à faire, on garde l’ancien contenu(requête a échoué)
  582. Minz_Log::warning($e->getMessage());
  583. }
  584. }
  585. }
  586. return false;
  587. }
  588. public function toArray(): array {
  589. return array(
  590. 'id' => $this->id(),
  591. 'guid' => $this->guid(),
  592. 'title' => $this->title(),
  593. 'author' => $this->authors(true),
  594. 'content' => $this->content(),
  595. 'link' => $this->link(),
  596. 'date' => $this->date(true),
  597. 'hash' => $this->hash(),
  598. 'is_read' => $this->isRead(),
  599. 'is_favorite' => $this->isFavorite(),
  600. 'id_feed' => $this->feedId(),
  601. 'tags' => $this->tags(true),
  602. 'attributes' => $this->attributes(),
  603. );
  604. }
  605. /**
  606. * Integer format conversion for Google Reader API format
  607. * @param string|int $dec Decimal number
  608. * @return string 64-bit hexa http://code.google.com/p/google-reader-api/wiki/ItemId
  609. */
  610. private static function dec2hex($dec): string {
  611. return PHP_INT_SIZE < 8 ? // 32-bit ?
  612. str_pad(gmp_strval(gmp_init($dec, 10), 16), 16, '0', STR_PAD_LEFT) :
  613. str_pad(dechex($dec), 16, '0', STR_PAD_LEFT);
  614. }
  615. /**
  616. * N.B.: To avoid expensive lookups, ensure to set `$entry->_feed($feed)` before calling this function.
  617. * N.B.: You might have to populate `$entry->_tags()` prior to calling this function.
  618. * @param string $mode Set to `'compat'` to use an alternative Unicode representation for problematic HTML special characters not decoded by some clients;
  619. * set to `'freshrss'` for using FreshRSS additions for internal use (e.g. export/import).
  620. * @return array<string,mixed> A representation of this entry in a format compatible with Google Reader API
  621. */
  622. public function toGReader(string $mode = ''): array {
  623. $feed = $this->feed();
  624. $category = $feed == null ? null : $feed->category();
  625. $item = [
  626. 'id' => 'tag:google.com,2005:reader/item/' . self::dec2hex($this->id()),
  627. 'crawlTimeMsec' => substr($this->dateAdded(true, true), 0, -3),
  628. 'timestampUsec' => '' . $this->dateAdded(true, true), //EasyRSS & Reeder
  629. 'published' => $this->date(true),
  630. // 'updated' => $this->date(true),
  631. 'title' => $this->title(),
  632. 'summary' => ['content' => $this->content()],
  633. 'canonical' => [
  634. ['href' => htmlspecialchars_decode($this->link(), ENT_QUOTES)],
  635. ],
  636. 'alternate' => [
  637. [
  638. 'href' => htmlspecialchars_decode($this->link(), ENT_QUOTES),
  639. 'type' => 'text/html',
  640. ],
  641. ],
  642. 'categories' => [
  643. 'user/-/state/com.google/reading-list',
  644. ],
  645. 'origin' => [
  646. 'streamId' => 'feed/' . $this->feedId,
  647. ],
  648. ];
  649. if ($mode === 'compat') {
  650. $item['title'] = escapeToUnicodeAlternative($this->title(), false);
  651. } elseif ($mode === 'freshrss') {
  652. $item['guid'] = $this->guid();
  653. unset($item['summary']);
  654. $item['content'] = ['content' => $this->content()];
  655. }
  656. if ($category != null && $mode !== 'freshrss') {
  657. $item['categories'][] = 'user/-/label/' . htmlspecialchars_decode($category->name(), ENT_QUOTES);
  658. }
  659. if ($feed != null) {
  660. $item['origin']['htmlUrl'] = htmlspecialchars_decode($feed->website());
  661. $item['origin']['title'] = $feed->name(); //EasyRSS
  662. if ($mode === 'compat') {
  663. $item['origin']['title'] = escapeToUnicodeAlternative($feed->name(), true);
  664. } elseif ($mode === 'freshrss') {
  665. $item['origin']['feedUrl'] = htmlspecialchars_decode($feed->url());
  666. }
  667. }
  668. foreach ($this->enclosures() as $enclosure) {
  669. if (!empty($enclosure['url']) && !empty($enclosure['type'])) {
  670. $media = [
  671. 'href' => $enclosure['url'],
  672. 'type' => $enclosure['type'],
  673. ];
  674. if (!empty($enclosure['length'])) {
  675. $media['length'] = intval($enclosure['length']);
  676. }
  677. $item['enclosure'][] = $media;
  678. }
  679. }
  680. $author = $this->authors(true);
  681. $author = trim($author, '; ');
  682. if ($author != '') {
  683. if ($mode === 'compat') {
  684. $item['author'] = escapeToUnicodeAlternative($author, false);
  685. } else {
  686. $item['author'] = $author;
  687. }
  688. }
  689. if ($this->isRead()) {
  690. $item['categories'][] = 'user/-/state/com.google/read';
  691. } elseif ($mode === 'freshrss') {
  692. $item['categories'][] = 'user/-/state/com.google/unread';
  693. }
  694. if ($this->isFavorite()) {
  695. $item['categories'][] = 'user/-/state/com.google/starred';
  696. }
  697. foreach ($this->tags() as $tagName) {
  698. $item['categories'][] = 'user/-/label/' . htmlspecialchars_decode($tagName, ENT_QUOTES);
  699. }
  700. return $item;
  701. }
  702. }