EntryDAO.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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'] = substr($valuesTmp['title'], 0, 255);
  152. $this->addEntryPrepared->bindParam(':title', $valuesTmp['title']);
  153. $valuesTmp['author'] = substr($valuesTmp['author'], 0, 255);
  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'] = substr($valuesTmp['tags'], 0, 1023);
  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'] = substr($valuesTmp['title'], 0, 255);
  229. $this->updateEntryPrepared->bindParam(':title', $valuesTmp['title']);
  230. $valuesTmp['author'] = substr($valuesTmp['author'], 0, 255);
  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'] = substr($valuesTmp['tags'], 0, 1023);
  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, $filter = 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.', $filter, $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, $filter = 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.', $filter, $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, $filter = 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('', $filter, $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 = '', $filter = 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 === '' && parent::$sharedDbType === 'mysql') {
  604. //MySQL optimization. TODO: check if this is needed again, after the filtering for old articles has been removed in 0.9-dev
  605. $firstId = $order === 'DESC' ? '9000000000'. '000000' : '0';
  606. }*/
  607. if ($firstId !== '') {
  608. $search .= 'AND ' . $alias . 'id ' . ($order === 'DESC' ? '<=' : '>=') . ' ? ';
  609. $values[] = $firstId;
  610. }
  611. if ($date_min > 0) {
  612. $search .= 'AND ' . $alias . 'id >= ? ';
  613. $values[] = $date_min . '000000';
  614. }
  615. if ($filter) {
  616. if ($filter->getMinDate()) {
  617. $search .= 'AND ' . $alias . 'id >= ? ';
  618. $values[] = "{$filter->getMinDate()}000000";
  619. }
  620. if ($filter->getMaxDate()) {
  621. $search .= 'AND ' . $alias . 'id <= ? ';
  622. $values[] = "{$filter->getMaxDate()}000000";
  623. }
  624. if ($filter->getMinPubdate()) {
  625. $search .= 'AND ' . $alias . 'date >= ? ';
  626. $values[] = $filter->getMinPubdate();
  627. }
  628. if ($filter->getMaxPubdate()) {
  629. $search .= 'AND ' . $alias . 'date <= ? ';
  630. $values[] = $filter->getMaxPubdate();
  631. }
  632. if ($filter->getAuthor()) {
  633. foreach ($filter->getAuthor() as $author) {
  634. $search .= 'AND ' . $alias . 'author LIKE ? ';
  635. $values[] = "%{$author}%";
  636. }
  637. }
  638. if ($filter->getIntitle()) {
  639. foreach ($filter->getIntitle() as $title) {
  640. $search .= 'AND ' . $alias . 'title LIKE ? ';
  641. $values[] = "%{$title}%";
  642. }
  643. }
  644. if ($filter->getTags()) {
  645. foreach ($filter->getTags() as $tag) {
  646. $search .= 'AND ' . $alias . 'tags LIKE ? ';
  647. $values[] = "%{$tag}%";
  648. }
  649. }
  650. if ($filter->getInurl()) {
  651. foreach ($filter->getInurl() as $url) {
  652. $search .= 'AND CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ? ';
  653. $values[] = "%{$url}%";
  654. }
  655. }
  656. if ($filter->getNotAuthor()) {
  657. foreach ($filter->getNotAuthor() as $author) {
  658. $search .= 'AND (NOT ' . $alias . 'author LIKE ?) ';
  659. $values[] = "%{$author}%";
  660. }
  661. }
  662. if ($filter->getNotIntitle()) {
  663. foreach ($filter->getNotIntitle() as $title) {
  664. $search .= 'AND (NOT ' . $alias . 'title LIKE ?) ';
  665. $values[] = "%{$title}%";
  666. }
  667. }
  668. if ($filter->getNotTags()) {
  669. foreach ($filter->getNotTags() as $tag) {
  670. $search .= 'AND (NOT ' . $alias . 'tags LIKE ?) ';
  671. $values[] = "%{$tag}%";
  672. }
  673. }
  674. if ($filter->getNotInurl()) {
  675. foreach ($filter->getNotInurl() as $url) {
  676. $search .= 'AND (NOT CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ?) ';
  677. $values[] = "%{$url}%";
  678. }
  679. }
  680. if ($filter->getSearch()) {
  681. foreach ($filter->getSearch() as $search_value) {
  682. $search .= 'AND ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ? ';
  683. $values[] = "%{$search_value}%";
  684. }
  685. }
  686. if ($filter->getNotSearch()) {
  687. foreach ($filter->getNotSearch() as $search_value) {
  688. $search .= 'AND (NOT ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ?) ';
  689. $values[] = "%{$search_value}%";
  690. }
  691. }
  692. }
  693. return array($values, $search);
  694. }
  695. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  696. if (!$state) {
  697. $state = FreshRSS_Entry::STATE_ALL;
  698. }
  699. $where = '';
  700. $joinFeed = false;
  701. $values = array();
  702. switch ($type) {
  703. case 'a':
  704. $where .= 'f.priority > ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  705. break;
  706. case 's': //Deprecated: use $state instead
  707. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  708. $where .= 'AND e.is_favorite=1 ';
  709. break;
  710. case 'c':
  711. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  712. $where .= 'AND f.category=? ';
  713. $values[] = intval($id);
  714. break;
  715. case 'f':
  716. $where .= 'e.id_feed=? ';
  717. $values[] = intval($id);
  718. break;
  719. case 'A':
  720. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  721. break;
  722. default:
  723. throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
  724. }
  725. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filter, $state, $order, $firstId, $date_min);
  726. return array(array_merge($values, $searchValues),
  727. 'SELECT e.id FROM `' . $this->prefix . 'entry` e '
  728. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  729. . 'WHERE ' . $where
  730. . $search
  731. . 'ORDER BY e.id ' . $order
  732. . ($limit > 0 ? ' LIMIT ' . $limit : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  733. }
  734. public function listWhereRaw($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  735. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  736. $sql = 'SELECT e0.id, e0.guid, e0.title, e0.author, '
  737. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  738. . ', e0.link, e0.date, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags '
  739. . 'FROM `' . $this->prefix . 'entry` e0 '
  740. . 'INNER JOIN ('
  741. . $sql
  742. . ') e2 ON e2.id=e0.id '
  743. . 'ORDER BY e0.id ' . $order;
  744. $stm = $this->bd->prepare($sql);
  745. $stm->execute($values);
  746. return $stm;
  747. }
  748. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  749. $stm = $this->listWhereRaw($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  750. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  751. }
  752. public function listByIds($ids, $order = 'DESC') {
  753. if (count($ids) < 1) {
  754. return array();
  755. }
  756. $sql = 'SELECT id, guid, title, author, '
  757. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  758. . ', link, date, is_read, is_favorite, id_feed, tags '
  759. . 'FROM `' . $this->prefix . 'entry` '
  760. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?) '
  761. . 'ORDER BY id ' . $order;
  762. $stm = $this->bd->prepare($sql);
  763. $stm->execute($ids);
  764. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  765. }
  766. public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) { //For API
  767. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  768. $stm = $this->bd->prepare($sql);
  769. $stm->execute($values);
  770. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  771. }
  772. public function listHashForFeedGuids($id_feed, $guids) {
  773. if (count($guids) < 1) {
  774. return array();
  775. }
  776. $guids = array_unique($guids);
  777. $sql = 'SELECT guid, ' . $this->sqlHexEncode('hash') . ' AS hex_hash FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  778. $stm = $this->bd->prepare($sql);
  779. $values = array($id_feed);
  780. $values = array_merge($values, $guids);
  781. if ($stm && $stm->execute($values)) {
  782. $result = array();
  783. $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
  784. foreach ($rows as $row) {
  785. $result[$row['guid']] = $row['hex_hash'];
  786. }
  787. return $result;
  788. } else {
  789. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  790. if ($this->autoUpdateDb($info)) {
  791. return $this->listHashForFeedGuids($id_feed, $guids);
  792. }
  793. Minz_Log::error('SQL error listHashForFeedGuids: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  794. . ' while querying feed ' . $id_feed);
  795. return false;
  796. }
  797. }
  798. public function updateLastSeen($id_feed, $guids, $mtime = 0) {
  799. if (count($guids) < 1) {
  800. return 0;
  801. }
  802. $sql = 'UPDATE `' . $this->prefix . 'entry` SET `lastSeen`=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  803. $stm = $this->bd->prepare($sql);
  804. if ($mtime <= 0) {
  805. $mtime = time();
  806. }
  807. $values = array($mtime, $id_feed);
  808. $values = array_merge($values, $guids);
  809. if ($stm && $stm->execute($values)) {
  810. return $stm->rowCount();
  811. } else {
  812. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  813. if ($this->autoUpdateDb($info)) {
  814. return $this->updateLastSeen($id_feed, $guids);
  815. }
  816. Minz_Log::error('SQL error updateLastSeen: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  817. . ' while updating feed ' . $id_feed);
  818. return false;
  819. }
  820. }
  821. public function countUnreadRead() {
  822. $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 priority > 0'
  823. . ' 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 priority > 0 AND is_read=0';
  824. $stm = $this->bd->prepare($sql);
  825. $stm->execute();
  826. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  827. $all = empty($res[0]) ? 0 : $res[0];
  828. $unread = empty($res[1]) ? 0 : $res[1];
  829. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  830. }
  831. public function count($minPriority = null) {
  832. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
  833. if ($minPriority !== null) {
  834. $sql = ' WHERE priority > ' . intval($minPriority);
  835. }
  836. $stm = $this->bd->prepare($sql);
  837. $stm->execute();
  838. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  839. return $res[0];
  840. }
  841. public function countNotRead($minPriority = null) {
  842. $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 is_read=0';
  843. if ($minPriority !== null) {
  844. $sql = ' AND priority > ' . intval($minPriority);
  845. }
  846. $stm = $this->bd->prepare($sql);
  847. $stm->execute();
  848. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  849. return $res[0];
  850. }
  851. public function countUnreadReadFavorites() {
  852. $sql = <<<SQL
  853. SELECT c
  854. FROM (
  855. SELECT COUNT(e1.id) AS c
  856. , 1 AS o
  857. FROM `{$this->prefix}entry` AS e1
  858. JOIN `{$this->prefix}feed` AS f1 ON e1.id_feed = f1.id
  859. WHERE e1.is_favorite = 1
  860. AND f1.priority >= :priority_normal
  861. UNION
  862. SELECT COUNT(e2.id) AS c
  863. , 2 AS o
  864. FROM `{$this->prefix}entry` AS e2
  865. JOIN `{$this->prefix}feed` AS f2 ON e2.id_feed = f2.id
  866. WHERE e2.is_favorite = 1
  867. AND e2.is_read = 0
  868. AND f2.priority >= :priority_normal
  869. ) u
  870. ORDER BY o
  871. SQL;
  872. $stm = $this->bd->prepare($sql);
  873. $stm->execute(array(':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL));
  874. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  875. $all = empty($res[0]) ? 0 : $res[0];
  876. $unread = empty($res[1]) ? 0 : $res[1];
  877. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  878. }
  879. public static function daoToEntry($dao) {
  880. $entry = new FreshRSS_Entry(
  881. $dao['id_feed'],
  882. $dao['guid'],
  883. $dao['title'],
  884. $dao['author'],
  885. $dao['content'],
  886. $dao['link'],
  887. $dao['date'],
  888. $dao['is_read'],
  889. $dao['is_favorite'],
  890. $dao['tags']
  891. );
  892. if (isset($dao['id'])) {
  893. $entry->_id($dao['id']);
  894. }
  895. return $entry;
  896. }
  897. private static function daoToEntries($listDAO) {
  898. $list = array();
  899. if (!is_array($listDAO)) {
  900. $listDAO = array($listDAO);
  901. }
  902. foreach ($listDAO as $key => $dao) {
  903. $list[] = self::daoToEntry($dao);
  904. }
  905. unset($listDAO);
  906. return $list;
  907. }
  908. }