EntryDAO.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. <?php
  2. class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. public function isCompressed() {
  4. return parent::$sharedDbType === 'mysql';
  5. }
  6. public function hasNativeHex() {
  7. return parent::$sharedDbType !== 'sqlite';
  8. }
  9. public function sqlHexDecode($x) {
  10. return 'unhex(' . $x . ')';
  11. }
  12. public function sqlHexEncode($x) {
  13. return 'hex(' . $x . ')';
  14. }
  15. protected function addColumn($name) {
  16. Minz_Log::warning('FreshRSS_EntryDAO::addColumn: ' . $name);
  17. $hasTransaction = false;
  18. try {
  19. $stm = null;
  20. if ($name === 'lastSeen') { //v1.1.1
  21. if (!$this->bd->inTransaction()) {
  22. $this->bd->beginTransaction();
  23. $hasTransaction = true;
  24. }
  25. $stm = $this->bd->prepare('ALTER TABLE `' . $this->prefix . 'entry` ADD COLUMN `lastSeen` INT(11) DEFAULT 0');
  26. if ($stm && $stm->execute()) {
  27. $stm = $this->bd->prepare('CREATE INDEX entry_lastSeen_index ON `' . $this->prefix . 'entry`(`lastSeen`);'); //"IF NOT EXISTS" does not exist in MySQL 5.7
  28. if ($stm && $stm->execute()) {
  29. if ($hasTransaction) {
  30. $this->bd->commit();
  31. }
  32. return true;
  33. }
  34. }
  35. if ($hasTransaction) {
  36. $this->bd->rollBack();
  37. }
  38. } elseif ($name === 'hash') { //v1.1.1
  39. $stm = $this->bd->prepare('ALTER TABLE `' . $this->prefix . 'entry` ADD COLUMN hash BINARY(16)');
  40. return $stm && $stm->execute();
  41. }
  42. } catch (Exception $e) {
  43. Minz_Log::error('FreshRSS_EntryDAO::addColumn error: ' . $e->getMessage());
  44. if ($hasTransaction) {
  45. $this->bd->rollBack();
  46. }
  47. }
  48. return false;
  49. }
  50. private $triedUpdateToUtf8mb4 = false;
  51. protected function updateToUtf8mb4() {
  52. if ($this->triedUpdateToUtf8mb4) {
  53. return false;
  54. }
  55. $this->triedUpdateToUtf8mb4 = true;
  56. $db = FreshRSS_Context::$system_conf->db;
  57. if ($db['type'] === 'mysql') {
  58. include_once(APP_PATH . '/SQL/install.sql.mysql.php');
  59. if (defined('SQL_UPDATE_UTF8MB4')) {
  60. Minz_Log::warning('Updating MySQL to UTF8MB4...');
  61. $hadTransaction = $this->bd->inTransaction();
  62. if ($hadTransaction) {
  63. $this->bd->commit();
  64. }
  65. $ok = false;
  66. try {
  67. $sql = sprintf(SQL_UPDATE_UTF8MB4, $this->prefix, $db['base']);
  68. $stm = $this->bd->prepare($sql);
  69. $ok = $stm->execute();
  70. } catch (Exception $e) {
  71. Minz_Log::error('FreshRSS_EntryDAO::updateToUtf8mb4 error: ' . $e->getMessage());
  72. }
  73. if ($hadTransaction) {
  74. $this->bd->beginTransaction();
  75. //NB: Transaction not starting. Why? (tested on PHP 7.0.8-0ubuntu and MySQL 5.7.13-0ubuntu)
  76. }
  77. return $ok;
  78. }
  79. }
  80. return false;
  81. }
  82. protected function createEntryTempTable() {
  83. $ok = false;
  84. $hadTransaction = $this->bd->inTransaction();
  85. if ($hadTransaction) {
  86. $this->bd->commit();
  87. }
  88. try {
  89. $db = FreshRSS_Context::$system_conf->db;
  90. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  91. Minz_Log::warning('SQL CREATE TABLE entrytmp...');
  92. if (defined('SQL_CREATE_TABLE_ENTRYTMP')) {
  93. $sql = sprintf(SQL_CREATE_TABLE_ENTRYTMP, $this->prefix);
  94. $stm = $this->bd->prepare($sql);
  95. $ok = $stm && $stm->execute();
  96. } else {
  97. global $SQL_CREATE_TABLE_ENTRYTMP;
  98. $ok = !empty($SQL_CREATE_TABLE_ENTRYTMP);
  99. foreach ($SQL_CREATE_TABLE_ENTRYTMP as $instruction) {
  100. $sql = sprintf($instruction, $this->prefix);
  101. $stm = $this->bd->prepare($sql);
  102. $ok &= $stm && $stm->execute();
  103. }
  104. }
  105. } catch (Exception $e) {
  106. Minz_Log::error('FreshRSS_EntryDAO::createEntryTempTable error: ' . $e->getMessage());
  107. }
  108. if ($hadTransaction) {
  109. $this->bd->beginTransaction();
  110. }
  111. return $ok;
  112. }
  113. protected function autoUpdateDb($errorInfo) {
  114. if (isset($errorInfo[0])) {
  115. if ($errorInfo[0] === '42S22') { //ER_BAD_FIELD_ERROR
  116. //autoAddColumn
  117. foreach (array('lastSeen', 'hash') as $column) {
  118. if (stripos($errorInfo[2], $column) !== false) {
  119. return $this->addColumn($column);
  120. }
  121. }
  122. } elseif ($errorInfo[0] === '42S02' && stripos($errorInfo[2], 'entrytmp') !== false) { //ER_BAD_TABLE_ERROR
  123. return $this->createEntryTempTable(); //v1.7
  124. }
  125. }
  126. if (isset($errorInfo[1])) {
  127. if ($errorInfo[1] == '1366') { //ER_TRUNCATED_WRONG_VALUE_FOR_FIELD
  128. return $this->updateToUtf8mb4();
  129. }
  130. }
  131. return false;
  132. }
  133. private $addEntryPrepared = null;
  134. public function addEntry($valuesTmp) {
  135. if ($this->addEntryPrepared == null) {
  136. $sql = 'INSERT INTO `' . $this->prefix . 'entrytmp` (id, guid, title, author, '
  137. . ($this->isCompressed() ? 'content_bin' : 'content')
  138. . ', link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags) '
  139. . 'VALUES(:id, :guid, :title, :author, '
  140. . ($this->isCompressed() ? 'COMPRESS(:content)' : ':content')
  141. . ', :link, :date, :last_seen, '
  142. . $this->sqlHexDecode(':hash')
  143. . ', :is_read, :is_favorite, :id_feed, :tags)';
  144. $this->addEntryPrepared = $this->bd->prepare($sql);
  145. }
  146. if ($this->addEntryPrepared) {
  147. $this->addEntryPrepared->bindParam(':id', $valuesTmp['id']);
  148. $valuesTmp['guid'] = substr($valuesTmp['guid'], 0, 760);
  149. $valuesTmp['guid'] = safe_ascii($valuesTmp['guid']);
  150. $this->addEntryPrepared->bindParam(':guid', $valuesTmp['guid']);
  151. $valuesTmp['title'] = mb_strcut($valuesTmp['title'], 0, 255, 'UTF-8');
  152. $this->addEntryPrepared->bindParam(':title', $valuesTmp['title']);
  153. $valuesTmp['author'] = mb_strcut($valuesTmp['author'], 0, 255, 'UTF-8');
  154. $this->addEntryPrepared->bindParam(':author', $valuesTmp['author']);
  155. $this->addEntryPrepared->bindParam(':content', $valuesTmp['content']);
  156. $valuesTmp['link'] = substr($valuesTmp['link'], 0, 1023);
  157. $valuesTmp['link'] = safe_ascii($valuesTmp['link']);
  158. $this->addEntryPrepared->bindParam(':link', $valuesTmp['link']);
  159. $this->addEntryPrepared->bindParam(':date', $valuesTmp['date'], PDO::PARAM_INT);
  160. $valuesTmp['lastSeen'] = time();
  161. $this->addEntryPrepared->bindParam(':last_seen', $valuesTmp['lastSeen'], PDO::PARAM_INT);
  162. $valuesTmp['is_read'] = $valuesTmp['is_read'] ? 1 : 0;
  163. $this->addEntryPrepared->bindParam(':is_read', $valuesTmp['is_read'], PDO::PARAM_INT);
  164. $valuesTmp['is_favorite'] = $valuesTmp['is_favorite'] ? 1 : 0;
  165. $this->addEntryPrepared->bindParam(':is_favorite', $valuesTmp['is_favorite'], PDO::PARAM_INT);
  166. $this->addEntryPrepared->bindParam(':id_feed', $valuesTmp['id_feed'], PDO::PARAM_INT);
  167. $valuesTmp['tags'] = mb_strcut($valuesTmp['tags'], 0, 1023, 'UTF-8');
  168. $this->addEntryPrepared->bindParam(':tags', $valuesTmp['tags']);
  169. if ($this->hasNativeHex()) {
  170. $this->addEntryPrepared->bindParam(':hash', $valuesTmp['hash']);
  171. } else {
  172. $valuesTmp['hashBin'] = pack('H*', $valuesTmp['hash']); //hex2bin() is PHP5.4+
  173. $this->addEntryPrepared->bindParam(':hash', $valuesTmp['hashBin']);
  174. }
  175. }
  176. if ($this->addEntryPrepared && $this->addEntryPrepared->execute()) {
  177. return true;
  178. } else {
  179. $info = $this->addEntryPrepared == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $this->addEntryPrepared->errorInfo();
  180. if ($this->autoUpdateDb($info)) {
  181. $this->addEntryPrepared = null;
  182. return $this->addEntry($valuesTmp);
  183. } elseif ((int)((int)$info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  184. Minz_Log::error('SQL error addEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  185. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title']);
  186. }
  187. return false;
  188. }
  189. }
  190. public function commitNewEntries() {
  191. $sql = 'SET @rank=(SELECT MAX(id) - COUNT(*) FROM `' . $this->prefix . 'entrytmp`); ' . //MySQL-specific
  192. 'INSERT IGNORE INTO `' . $this->prefix . 'entry`
  193. (
  194. id, guid, title, author, content_bin, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
  195. ) ' .
  196. 'SELECT @rank:=@rank+1 AS id, guid, title, author, content_bin, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
  197. FROM `' . $this->prefix . 'entrytmp`
  198. ORDER BY date; ' .
  199. 'DELETE FROM `' . $this->prefix . 'entrytmp` WHERE id <= @rank;';
  200. $hadTransaction = $this->bd->inTransaction();
  201. if (!$hadTransaction) {
  202. $this->bd->beginTransaction();
  203. }
  204. $result = $this->bd->exec($sql) !== false;
  205. if (!$hadTransaction) {
  206. $this->bd->commit();
  207. }
  208. return $result;
  209. }
  210. private $updateEntryPrepared = null;
  211. public function updateEntry($valuesTmp) {
  212. if (!isset($valuesTmp['is_read'])) {
  213. $valuesTmp['is_read'] = null;
  214. }
  215. if ($this->updateEntryPrepared === null) {
  216. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  217. . 'SET title=:title, author=:author, '
  218. . ($this->isCompressed() ? 'content_bin=COMPRESS(:content)' : 'content=:content')
  219. . ', link=:link, date=:date, `lastSeen`=:last_seen, '
  220. . 'hash=' . $this->sqlHexDecode(':hash')
  221. . ', ' . ($valuesTmp['is_read'] === null ? '' : 'is_read=:is_read, ')
  222. . 'tags=:tags '
  223. . 'WHERE id_feed=:id_feed AND guid=:guid';
  224. $this->updateEntryPrepared = $this->bd->prepare($sql);
  225. }
  226. $valuesTmp['guid'] = substr($valuesTmp['guid'], 0, 760);
  227. $this->updateEntryPrepared->bindParam(':guid', $valuesTmp['guid']);
  228. $valuesTmp['title'] = mb_strcut($valuesTmp['title'], 0, 255, 'UTF-8');
  229. $this->updateEntryPrepared->bindParam(':title', $valuesTmp['title']);
  230. $valuesTmp['author'] = mb_strcut($valuesTmp['author'], 0, 255, 'UTF-8');
  231. $this->updateEntryPrepared->bindParam(':author', $valuesTmp['author']);
  232. $this->updateEntryPrepared->bindParam(':content', $valuesTmp['content']);
  233. $valuesTmp['link'] = substr($valuesTmp['link'], 0, 1023);
  234. $valuesTmp['link'] = safe_ascii($valuesTmp['link']);
  235. $this->updateEntryPrepared->bindParam(':link', $valuesTmp['link']);
  236. $this->updateEntryPrepared->bindParam(':date', $valuesTmp['date'], PDO::PARAM_INT);
  237. $valuesTmp['lastSeen'] = time();
  238. $this->updateEntryPrepared->bindParam(':last_seen', $valuesTmp['lastSeen'], PDO::PARAM_INT);
  239. if ($valuesTmp['is_read'] !== null) {
  240. $this->updateEntryPrepared->bindValue(':is_read', $valuesTmp['is_read'] ? 1 : 0, PDO::PARAM_INT);
  241. }
  242. $this->updateEntryPrepared->bindParam(':id_feed', $valuesTmp['id_feed'], PDO::PARAM_INT);
  243. $valuesTmp['tags'] = mb_strcut($valuesTmp['tags'], 0, 1023, 'UTF-8');
  244. $this->updateEntryPrepared->bindParam(':tags', $valuesTmp['tags']);
  245. if ($this->hasNativeHex()) {
  246. $this->updateEntryPrepared->bindParam(':hash', $valuesTmp['hash']);
  247. } else {
  248. $valuesTmp['hashBin'] = pack('H*', $valuesTmp['hash']); //hex2bin() is PHP5.4+
  249. $this->updateEntryPrepared->bindParam(':hash', $valuesTmp['hashBin']);
  250. }
  251. if ($this->updateEntryPrepared && $this->updateEntryPrepared->execute()) {
  252. return true;
  253. } else {
  254. $info = $this->updateEntryPrepared == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $this->updateEntryPrepared->errorInfo();
  255. if ($this->autoUpdateDb($info)) {
  256. return $this->updateEntry($valuesTmp);
  257. }
  258. Minz_Log::error('SQL error updateEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  259. . ' while updating entry with GUID ' . $valuesTmp['guid'] . ' in feed ' . $valuesTmp['id_feed']);
  260. return false;
  261. }
  262. }
  263. /**
  264. * Toggle favorite marker on one or more article
  265. *
  266. * @todo simplify the query by removing the str_repeat. I am pretty sure
  267. * there is an other way to do that.
  268. *
  269. * @param integer|array $ids
  270. * @param boolean $is_favorite
  271. * @return false|integer
  272. */
  273. public function markFavorite($ids, $is_favorite = true) {
  274. if (!is_array($ids)) {
  275. $ids = array($ids);
  276. }
  277. if (count($ids) < 1) {
  278. return 0;
  279. }
  280. FreshRSS_UserDAO::touch();
  281. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  282. . 'SET is_favorite=? '
  283. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  284. $values = array($is_favorite ? 1 : 0);
  285. $values = array_merge($values, $ids);
  286. $stm = $this->bd->prepare($sql);
  287. if ($stm && $stm->execute($values)) {
  288. return $stm->rowCount();
  289. } else {
  290. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  291. Minz_Log::error('SQL error markFavorite: ' . $info[2]);
  292. return false;
  293. }
  294. }
  295. /**
  296. * Update the unread article cache held on every feed details.
  297. * Depending on the parameters, it updates the cache on one feed, on all
  298. * feeds from one category or on all feeds.
  299. *
  300. * @todo It can use the query builder refactoring to build that query
  301. *
  302. * @param false|integer $catId category ID
  303. * @param false|integer $feedId feed ID
  304. * @return boolean
  305. */
  306. protected function updateCacheUnreads($catId = false, $feedId = false) {
  307. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  308. . 'LEFT OUTER JOIN ('
  309. . 'SELECT e.id_feed, '
  310. . 'COUNT(*) AS nbUnreads '
  311. . 'FROM `' . $this->prefix . 'entry` e '
  312. . 'WHERE e.is_read=0 '
  313. . 'GROUP BY e.id_feed'
  314. . ') x ON x.id_feed=f.id '
  315. . 'SET f.`cache_nbUnreads`=COALESCE(x.nbUnreads, 0)';
  316. $hasWhere = false;
  317. $values = array();
  318. if ($feedId !== false) {
  319. $sql .= $hasWhere ? ' AND' : ' WHERE';
  320. $hasWhere = true;
  321. $sql .= ' f.id=?';
  322. $values[] = $id;
  323. }
  324. if ($catId !== false) {
  325. $sql .= $hasWhere ? ' AND' : ' WHERE';
  326. $hasWhere = true;
  327. $sql .= ' f.category=?';
  328. $values[] = $catId;
  329. }
  330. $stm = $this->bd->prepare($sql);
  331. if ($stm && $stm->execute($values)) {
  332. return true;
  333. } else {
  334. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  335. Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
  336. return false;
  337. }
  338. }
  339. /**
  340. * Toggle the read marker on one or more article.
  341. * Then the cache is updated.
  342. *
  343. * @todo change the way the query is build because it seems there is
  344. * unnecessary code in here. For instance, the part with the str_repeat.
  345. * @todo remove code duplication. It seems the code is basically the
  346. * same if it is an array or not.
  347. *
  348. * @param integer|array $ids
  349. * @param boolean $is_read
  350. * @return integer affected rows
  351. */
  352. public function markRead($ids, $is_read = true) {
  353. FreshRSS_UserDAO::touch();
  354. if (is_array($ids)) { //Many IDs at once (used by API)
  355. if (count($ids) < 6) { //Speed heuristics
  356. $affected = 0;
  357. foreach ($ids as $id) {
  358. $affected += $this->markRead($id, $is_read);
  359. }
  360. return $affected;
  361. }
  362. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  363. . 'SET is_read=? '
  364. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  365. $values = array($is_read ? 1 : 0);
  366. $values = array_merge($values, $ids);
  367. $stm = $this->bd->prepare($sql);
  368. if (!($stm && $stm->execute($values))) {
  369. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  370. Minz_Log::error('SQL error markRead: ' . $info[2]);
  371. return false;
  372. }
  373. $affected = $stm->rowCount();
  374. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  375. return false;
  376. }
  377. return $affected;
  378. } else {
  379. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  380. . 'SET e.is_read=?,'
  381. . 'f.`cache_nbUnreads`=f.`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
  382. . 'WHERE e.id=? AND e.is_read=?';
  383. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  384. $stm = $this->bd->prepare($sql);
  385. if ($stm && $stm->execute($values)) {
  386. return $stm->rowCount();
  387. } else {
  388. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  389. Minz_Log::error('SQL error markRead: ' . $info[2]);
  390. return false;
  391. }
  392. }
  393. }
  394. /**
  395. * Mark all entries as read depending on parameters.
  396. * If $onlyFavorites is true, it is used when the user mark as read in
  397. * the favorite pseudo-category.
  398. * If $priorityMin is greater than 0, it is used when the user mark as
  399. * read in the main feed pseudo-category.
  400. * Then the cache is updated.
  401. *
  402. * If $idMax equals 0, a deprecated debug message is logged
  403. *
  404. * @todo refactor this method along with markReadCat and markReadFeed
  405. * since they are all doing the same thing. I think we need to build a
  406. * tool to generate the query instead of having queries all over the
  407. * place. It will be reused also for the filtering making every thing
  408. * separated.
  409. *
  410. * @param integer $idMax fail safe article ID
  411. * @param boolean $onlyFavorites
  412. * @param integer $priorityMin
  413. * @return integer affected rows
  414. */
  415. public function markReadEntries($idMax = 0, $onlyFavorites = false, $priorityMin = 0, $filters = null, $state = 0) {
  416. FreshRSS_UserDAO::touch();
  417. if ($idMax == 0) {
  418. $idMax = time() . '000000';
  419. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  420. }
  421. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  422. . 'SET e.is_read=1 '
  423. . 'WHERE e.is_read=0 AND e.id <= ?';
  424. if ($onlyFavorites) {
  425. $sql .= ' AND e.is_favorite=1';
  426. } elseif ($priorityMin >= 0) {
  427. $sql .= ' AND f.priority > ' . intval($priorityMin);
  428. }
  429. $values = array($idMax);
  430. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  431. $stm = $this->bd->prepare($sql . $search);
  432. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  433. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  434. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  435. return false;
  436. }
  437. $affected = $stm->rowCount();
  438. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  439. return false;
  440. }
  441. return $affected;
  442. }
  443. /**
  444. * Mark all the articles in a category as read.
  445. * There is a fail safe to prevent to mark as read articles that are
  446. * loaded during the mark as read action. Then the cache is updated.
  447. *
  448. * If $idMax equals 0, a deprecated debug message is logged
  449. *
  450. * @param integer $id category ID
  451. * @param integer $idMax fail safe article ID
  452. * @return integer affected rows
  453. */
  454. public function markReadCat($id, $idMax = 0, $filters = null, $state = 0) {
  455. FreshRSS_UserDAO::touch();
  456. if ($idMax == 0) {
  457. $idMax = time() . '000000';
  458. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  459. }
  460. $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
  461. . 'SET e.is_read=1 '
  462. . 'WHERE f.category=? AND e.is_read=0 AND e.id <= ?';
  463. $values = array($id, $idMax);
  464. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  465. $stm = $this->bd->prepare($sql . $search);
  466. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  467. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  468. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  469. return false;
  470. }
  471. $affected = $stm->rowCount();
  472. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  473. return false;
  474. }
  475. return $affected;
  476. }
  477. /**
  478. * Mark all the articles in a feed as read.
  479. * There is a fail safe to prevent to mark as read articles that are
  480. * loaded during the mark as read action. Then the cache is updated.
  481. *
  482. * If $idMax equals 0, a deprecated debug message is logged
  483. *
  484. * @param integer $id_feed feed ID
  485. * @param integer $idMax fail safe article ID
  486. * @return integer affected rows
  487. */
  488. public function markReadFeed($id_feed, $idMax = 0, $filters = null, $state = 0) {
  489. FreshRSS_UserDAO::touch();
  490. if ($idMax == 0) {
  491. $idMax = time() . '000000';
  492. Minz_Log::debug('Calling markReadFeed(0) is deprecated!');
  493. }
  494. $this->bd->beginTransaction();
  495. $sql = 'UPDATE `' . $this->prefix . 'entry` '
  496. . 'SET is_read=1 '
  497. . 'WHERE id_feed=? AND is_read=0 AND id <= ?';
  498. $values = array($id_feed, $idMax);
  499. list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
  500. $stm = $this->bd->prepare($sql . $search);
  501. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  502. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  503. Minz_Log::error('SQL error markReadFeed: ' . $info[2] . ' with SQL: ' . $sql . $search);
  504. $this->bd->rollBack();
  505. return false;
  506. }
  507. $affected = $stm->rowCount();
  508. if ($affected > 0) {
  509. $sql = 'UPDATE `' . $this->prefix . 'feed` '
  510. . 'SET `cache_nbUnreads`=`cache_nbUnreads`-' . $affected
  511. . ' WHERE id=?';
  512. $values = array($id_feed);
  513. $stm = $this->bd->prepare($sql);
  514. if (!($stm && $stm->execute($values))) {
  515. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  516. Minz_Log::error('SQL error markReadFeed cache: ' . $info[2]);
  517. $this->bd->rollBack();
  518. return false;
  519. }
  520. }
  521. $this->bd->commit();
  522. return $affected;
  523. }
  524. public function cleanOldEntries($id_feed, $date_min, $keep = 15) { //Remember to call updateCachedValue($id_feed) or updateCachedValues() just after
  525. $sql = 'DELETE FROM `' . $this->prefix . 'entry` '
  526. . 'WHERE id_feed=:id_feed AND id<=:id_max '
  527. . 'AND is_favorite=0 ' //Do not remove favourites
  528. . 'AND `lastSeen` < (SELECT maxLastSeen FROM (SELECT (MAX(e3.`lastSeen`)-99) AS maxLastSeen FROM `' . $this->prefix . 'entry` e3 WHERE e3.id_feed=:id_feed) recent) ' //Do not remove the most newly seen articles, plus a few seconds of tolerance
  529. . 'AND id NOT IN (SELECT id FROM (SELECT e2.id FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=:id_feed ORDER BY id DESC LIMIT :keep) keep)'; //Double select: MySQL doesn't support 'LIMIT & IN/ALL/ANY/SOME subquery'
  530. $stm = $this->bd->prepare($sql);
  531. if ($stm) {
  532. $id_max = intval($date_min) . '000000';
  533. $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
  534. $stm->bindParam(':id_max', $id_max, PDO::PARAM_STR);
  535. $stm->bindParam(':keep', $keep, PDO::PARAM_INT);
  536. }
  537. if ($stm && $stm->execute()) {
  538. return $stm->rowCount();
  539. } else {
  540. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  541. if ($this->autoUpdateDb($info)) {
  542. return $this->cleanOldEntries($id_feed, $date_min, $keep);
  543. }
  544. Minz_Log::error('SQL error cleanOldEntries: ' . $info[2]);
  545. return false;
  546. }
  547. }
  548. public function searchByGuid($id_feed, $guid) {
  549. // un guid est unique pour un flux donné
  550. $sql = 'SELECT id, guid, title, author, '
  551. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  552. . ', link, date, is_read, is_favorite, id_feed, tags '
  553. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  554. $stm = $this->bd->prepare($sql);
  555. $values = array(
  556. $id_feed,
  557. $guid,
  558. );
  559. $stm->execute($values);
  560. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  561. $entries = self::daoToEntries($res);
  562. return isset($entries[0]) ? $entries[0] : null;
  563. }
  564. public function searchById($id) {
  565. $sql = 'SELECT id, guid, title, author, '
  566. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  567. . ', link, date, is_read, is_favorite, id_feed, tags '
  568. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  569. $stm = $this->bd->prepare($sql);
  570. $values = array($id);
  571. $stm->execute($values);
  572. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  573. $entries = self::daoToEntries($res);
  574. return isset($entries[0]) ? $entries[0] : null;
  575. }
  576. protected function sqlConcat($s1, $s2) {
  577. return 'CONCAT(' . $s1 . ',' . $s2 . ')'; //MySQL
  578. }
  579. protected function sqlListEntriesWhere($alias = '', $filters = null, $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $firstId = '', $date_min = 0) {
  580. $search = ' ';
  581. $values = array();
  582. if ($state & FreshRSS_Entry::STATE_NOT_READ) {
  583. if (!($state & FreshRSS_Entry::STATE_READ)) {
  584. $search .= 'AND ' . $alias . 'is_read=0 ';
  585. }
  586. } elseif ($state & FreshRSS_Entry::STATE_READ) {
  587. $search .= 'AND ' . $alias . 'is_read=1 ';
  588. }
  589. if ($state & FreshRSS_Entry::STATE_FAVORITE) {
  590. if (!($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  591. $search .= 'AND ' . $alias . 'is_favorite=1 ';
  592. }
  593. } elseif ($state & FreshRSS_Entry::STATE_NOT_FAVORITE) {
  594. $search .= 'AND ' . $alias . 'is_favorite=0 ';
  595. }
  596. switch ($order) {
  597. case 'DESC':
  598. case 'ASC':
  599. break;
  600. default:
  601. throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
  602. }
  603. if ($firstId !== '') {
  604. $search .= 'AND ' . $alias . 'id ' . ($order === 'DESC' ? '<=' : '>=') . ' ? ';
  605. $values[] = $firstId;
  606. }
  607. if ($date_min > 0) {
  608. $search .= 'AND ' . $alias . 'id >= ? ';
  609. $values[] = $date_min . '000000';
  610. }
  611. if ($filters && count($filters->searches()) > 0) {
  612. $isOpen = false;
  613. foreach ($filters->searches() as $filter) {
  614. if ($filter == null) {
  615. continue;
  616. }
  617. $sub_search = '';
  618. if ($filter->getMinDate()) {
  619. $sub_search .= 'AND ' . $alias . 'id >= ? ';
  620. $values[] = "{$filter->getMinDate()}000000";
  621. }
  622. if ($filter->getMaxDate()) {
  623. $sub_search .= 'AND ' . $alias . 'id <= ? ';
  624. $values[] = "{$filter->getMaxDate()}000000";
  625. }
  626. if ($filter->getMinPubdate()) {
  627. $sub_search .= 'AND ' . $alias . 'date >= ? ';
  628. $values[] = $filter->getMinPubdate();
  629. }
  630. if ($filter->getMaxPubdate()) {
  631. $sub_search .= 'AND ' . $alias . 'date <= ? ';
  632. $values[] = $filter->getMaxPubdate();
  633. }
  634. if ($filter->getAuthor()) {
  635. foreach ($filter->getAuthor() as $author) {
  636. $sub_search .= 'AND ' . $alias . 'author LIKE ? ';
  637. $values[] = "%{$author}%";
  638. }
  639. }
  640. if ($filter->getIntitle()) {
  641. foreach ($filter->getIntitle() as $title) {
  642. $sub_search .= 'AND ' . $alias . 'title LIKE ? ';
  643. $values[] = "%{$title}%";
  644. }
  645. }
  646. if ($filter->getTags()) {
  647. foreach ($filter->getTags() as $tag) {
  648. $sub_search .= 'AND ' . $alias . 'tags LIKE ? ';
  649. $values[] = "%{$tag}%";
  650. }
  651. }
  652. if ($filter->getInurl()) {
  653. foreach ($filter->getInurl() as $url) {
  654. $sub_search .= 'AND CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ? ';
  655. $values[] = "%{$url}%";
  656. }
  657. }
  658. if ($filter->getNotAuthor()) {
  659. foreach ($filter->getNotAuthor() as $author) {
  660. $sub_search .= 'AND (NOT ' . $alias . 'author LIKE ?) ';
  661. $values[] = "%{$author}%";
  662. }
  663. }
  664. if ($filter->getNotIntitle()) {
  665. foreach ($filter->getNotIntitle() as $title) {
  666. $sub_search .= 'AND (NOT ' . $alias . 'title LIKE ?) ';
  667. $values[] = "%{$title}%";
  668. }
  669. }
  670. if ($filter->getNotTags()) {
  671. foreach ($filter->getNotTags() as $tag) {
  672. $sub_search .= 'AND (NOT ' . $alias . 'tags LIKE ?) ';
  673. $values[] = "%{$tag}%";
  674. }
  675. }
  676. if ($filter->getNotInurl()) {
  677. foreach ($filter->getNotInurl() as $url) {
  678. $sub_search .= 'AND (NOT CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ?) ';
  679. $values[] = "%{$url}%";
  680. }
  681. }
  682. if ($filter->getSearch()) {
  683. foreach ($filter->getSearch() as $search_value) {
  684. $sub_search .= 'AND ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ? ';
  685. $values[] = "%{$search_value}%";
  686. }
  687. }
  688. if ($filter->getNotSearch()) {
  689. foreach ($filter->getNotSearch() as $search_value) {
  690. $sub_search .= 'AND (NOT ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ?) ';
  691. $values[] = "%{$search_value}%";
  692. }
  693. }
  694. if ($sub_search != '') {
  695. if ($isOpen) {
  696. $search .= 'OR ';
  697. } else {
  698. $search .= 'AND (';
  699. $isOpen = true;
  700. }
  701. $search .= '(' . substr($sub_search, 4) . ') ';
  702. }
  703. }
  704. if ($isOpen) {
  705. $search .= ') ';
  706. }
  707. }
  708. return array($values, $search);
  709. }
  710. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  711. if (!$state) {
  712. $state = FreshRSS_Entry::STATE_ALL;
  713. }
  714. $where = '';
  715. $joinFeed = false;
  716. $values = array();
  717. switch ($type) {
  718. case 'a':
  719. $where .= 'f.priority > ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  720. break;
  721. case 's': //Deprecated: use $state instead
  722. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  723. $where .= 'AND e.is_favorite=1 ';
  724. break;
  725. case 'c':
  726. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  727. $where .= 'AND f.category=? ';
  728. $values[] = intval($id);
  729. break;
  730. case 'f':
  731. $where .= 'e.id_feed=? ';
  732. $values[] = intval($id);
  733. break;
  734. case 'A':
  735. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  736. break;
  737. default:
  738. throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
  739. }
  740. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state, $order, $firstId, $date_min);
  741. return array(array_merge($values, $searchValues),
  742. 'SELECT e.id FROM `' . $this->prefix . 'entry` e '
  743. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  744. . 'WHERE ' . $where
  745. . $search
  746. . 'ORDER BY e.id ' . $order
  747. . ($limit > 0 ? ' LIMIT ' . intval($limit) : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  748. }
  749. public function listWhereRaw($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  750. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  751. $sql = 'SELECT e0.id, e0.guid, e0.title, e0.author, '
  752. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  753. . ', e0.link, e0.date, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags '
  754. . 'FROM `' . $this->prefix . 'entry` e0 '
  755. . 'INNER JOIN ('
  756. . $sql
  757. . ') e2 ON e2.id=e0.id '
  758. . 'ORDER BY e0.id ' . $order;
  759. $stm = $this->bd->prepare($sql);
  760. $stm->execute($values);
  761. return $stm;
  762. }
  763. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  764. $stm = $this->listWhereRaw($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  765. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  766. }
  767. public function listByIds($ids, $order = 'DESC') {
  768. if (count($ids) < 1) {
  769. return array();
  770. }
  771. $sql = 'SELECT id, guid, title, author, '
  772. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  773. . ', link, date, is_read, is_favorite, id_feed, tags '
  774. . 'FROM `' . $this->prefix . 'entry` '
  775. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?) '
  776. . 'ORDER BY id ' . $order;
  777. $stm = $this->bd->prepare($sql);
  778. $stm->execute($ids);
  779. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  780. }
  781. public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) { //For API
  782. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  783. $stm = $this->bd->prepare($sql);
  784. $stm->execute($values);
  785. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  786. }
  787. public function listHashForFeedGuids($id_feed, $guids) {
  788. if (count($guids) < 1) {
  789. return array();
  790. }
  791. $guids = array_unique($guids);
  792. $sql = 'SELECT guid, ' . $this->sqlHexEncode('hash') . ' AS hex_hash FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  793. $stm = $this->bd->prepare($sql);
  794. $values = array($id_feed);
  795. $values = array_merge($values, $guids);
  796. if ($stm && $stm->execute($values)) {
  797. $result = array();
  798. $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
  799. foreach ($rows as $row) {
  800. $result[$row['guid']] = $row['hex_hash'];
  801. }
  802. return $result;
  803. } else {
  804. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  805. if ($this->autoUpdateDb($info)) {
  806. return $this->listHashForFeedGuids($id_feed, $guids);
  807. }
  808. Minz_Log::error('SQL error listHashForFeedGuids: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  809. . ' while querying feed ' . $id_feed);
  810. return false;
  811. }
  812. }
  813. public function updateLastSeen($id_feed, $guids, $mtime = 0) {
  814. if (count($guids) < 1) {
  815. return 0;
  816. }
  817. $sql = 'UPDATE `' . $this->prefix . 'entry` SET `lastSeen`=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  818. $stm = $this->bd->prepare($sql);
  819. if ($mtime <= 0) {
  820. $mtime = time();
  821. }
  822. $values = array($mtime, $id_feed);
  823. $values = array_merge($values, $guids);
  824. if ($stm && $stm->execute($values)) {
  825. return $stm->rowCount();
  826. } else {
  827. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  828. if ($this->autoUpdateDb($info)) {
  829. return $this->updateLastSeen($id_feed, $guids);
  830. }
  831. Minz_Log::error('SQL error updateLastSeen: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  832. . ' while updating feed ' . $id_feed);
  833. return false;
  834. }
  835. }
  836. public function countUnreadRead() {
  837. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE f.priority > 0'
  838. . ' UNION SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE f.priority > 0 AND e.is_read=0';
  839. $stm = $this->bd->prepare($sql);
  840. $stm->execute();
  841. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  842. $all = empty($res[0]) ? 0 : $res[0];
  843. $unread = empty($res[1]) ? 0 : $res[1];
  844. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  845. }
  846. public function count($minPriority = null) {
  847. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e';
  848. if ($minPriority !== null) {
  849. $sql .= ' INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
  850. $sql .= ' WHERE f.priority > ' . intval($minPriority);
  851. }
  852. $stm = $this->bd->prepare($sql);
  853. $stm->execute();
  854. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  855. return $res[0];
  856. }
  857. public function countNotRead($minPriority = null) {
  858. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e';
  859. if ($minPriority !== null) {
  860. $sql .= ' INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
  861. }
  862. $sql .= ' WHERE e.is_read=0';
  863. if ($minPriority !== null) {
  864. $sql .= ' AND f.priority > ' . intval($minPriority);
  865. }
  866. $stm = $this->bd->prepare($sql);
  867. $stm->execute();
  868. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  869. return $res[0];
  870. }
  871. public function countUnreadReadFavorites() {
  872. $sql = <<<SQL
  873. SELECT c
  874. FROM (
  875. SELECT COUNT(e1.id) AS c
  876. , 1 AS o
  877. FROM `{$this->prefix}entry` AS e1
  878. JOIN `{$this->prefix}feed` AS f1 ON e1.id_feed = f1.id
  879. WHERE e1.is_favorite = 1
  880. AND f1.priority >= :priority_normal
  881. UNION
  882. SELECT COUNT(e2.id) AS c
  883. , 2 AS o
  884. FROM `{$this->prefix}entry` AS e2
  885. JOIN `{$this->prefix}feed` AS f2 ON e2.id_feed = f2.id
  886. WHERE e2.is_favorite = 1
  887. AND e2.is_read = 0
  888. AND f2.priority >= :priority_normal
  889. ) u
  890. ORDER BY o
  891. SQL;
  892. $stm = $this->bd->prepare($sql);
  893. $stm->execute(array(':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL));
  894. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  895. $all = empty($res[0]) ? 0 : $res[0];
  896. $unread = empty($res[1]) ? 0 : $res[1];
  897. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  898. }
  899. public static function daoToEntry($dao) {
  900. $entry = new FreshRSS_Entry(
  901. $dao['id_feed'],
  902. $dao['guid'],
  903. $dao['title'],
  904. $dao['author'],
  905. $dao['content'],
  906. $dao['link'],
  907. $dao['date'],
  908. $dao['is_read'],
  909. $dao['is_favorite'],
  910. $dao['tags']
  911. );
  912. if (isset($dao['id'])) {
  913. $entry->_id($dao['id']);
  914. }
  915. return $entry;
  916. }
  917. private static function daoToEntries($listDAO) {
  918. $list = array();
  919. if (!is_array($listDAO)) {
  920. $listDAO = array($listDAO);
  921. }
  922. foreach ($listDAO as $key => $dao) {
  923. $list[] = self::daoToEntry($dao);
  924. }
  925. unset($listDAO);
  926. return $list;
  927. }
  928. }