EntryDAO.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 searchByGuid($id_feed, $guid) {
  525. // un guid est unique pour un flux donné
  526. $sql = 'SELECT id, guid, title, author, '
  527. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  528. . ', link, date, is_read, is_favorite, id_feed, tags '
  529. . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
  530. $stm = $this->bd->prepare($sql);
  531. $values = array(
  532. $id_feed,
  533. $guid,
  534. );
  535. $stm->execute($values);
  536. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  537. $entries = self::daoToEntries($res);
  538. return isset($entries[0]) ? $entries[0] : null;
  539. }
  540. public function searchById($id) {
  541. $sql = 'SELECT id, guid, title, author, '
  542. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  543. . ', link, date, is_read, is_favorite, id_feed, tags '
  544. . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
  545. $stm = $this->bd->prepare($sql);
  546. $values = array($id);
  547. $stm->execute($values);
  548. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  549. $entries = self::daoToEntries($res);
  550. return isset($entries[0]) ? $entries[0] : null;
  551. }
  552. protected function sqlConcat($s1, $s2) {
  553. return 'CONCAT(' . $s1 . ',' . $s2 . ')'; //MySQL
  554. }
  555. protected function sqlListEntriesWhere($alias = '', $filter = null, $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $firstId = '', $date_min = 0) {
  556. $search = ' ';
  557. $values = array();
  558. if ($state & FreshRSS_Entry::STATE_NOT_READ) {
  559. if (!($state & FreshRSS_Entry::STATE_READ)) {
  560. $search .= 'AND ' . $alias . 'is_read=0 ';
  561. }
  562. } elseif ($state & FreshRSS_Entry::STATE_READ) {
  563. $search .= 'AND ' . $alias . 'is_read=1 ';
  564. }
  565. if ($state & FreshRSS_Entry::STATE_FAVORITE) {
  566. if (!($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  567. $search .= 'AND ' . $alias . 'is_favorite=1 ';
  568. }
  569. } elseif ($state & FreshRSS_Entry::STATE_NOT_FAVORITE) {
  570. $search .= 'AND ' . $alias . 'is_favorite=0 ';
  571. }
  572. switch ($order) {
  573. case 'DESC':
  574. case 'ASC':
  575. break;
  576. default:
  577. throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
  578. }
  579. /*if ($firstId === '' && parent::$sharedDbType === 'mysql') {
  580. //MySQL optimization. TODO: check if this is needed again, after the filtering for old articles has been removed in 0.9-dev
  581. $firstId = $order === 'DESC' ? '9000000000'. '000000' : '0';
  582. }*/
  583. if ($firstId !== '') {
  584. $search .= 'AND ' . $alias . 'id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
  585. }
  586. if ($date_min > 0) {
  587. $search .= 'AND ' . $alias . 'id >= ' . $date_min . '000000 ';
  588. }
  589. if ($filter) {
  590. if ($filter->getMinDate()) {
  591. $search .= 'AND ' . $alias . 'id >= ? ';
  592. $values[] = "{$filter->getMinDate()}000000";
  593. }
  594. if ($filter->getMaxDate()) {
  595. $search .= 'AND ' . $alias . 'id <= ? ';
  596. $values[] = "{$filter->getMaxDate()}000000";
  597. }
  598. if ($filter->getMinPubdate()) {
  599. $search .= 'AND ' . $alias . 'date >= ? ';
  600. $values[] = $filter->getMinPubdate();
  601. }
  602. if ($filter->getMaxPubdate()) {
  603. $search .= 'AND ' . $alias . 'date <= ? ';
  604. $values[] = $filter->getMaxPubdate();
  605. }
  606. if ($filter->getAuthor()) {
  607. foreach ($filter->getAuthor() as $author) {
  608. $search .= 'AND ' . $alias . 'author LIKE ? ';
  609. $values[] = "%{$author}%";
  610. }
  611. }
  612. if ($filter->getIntitle()) {
  613. foreach ($filter->getIntitle() as $title) {
  614. $search .= 'AND ' . $alias . 'title LIKE ? ';
  615. $values[] = "%{$title}%";
  616. }
  617. }
  618. if ($filter->getTags()) {
  619. foreach ($filter->getTags() as $tag) {
  620. $search .= 'AND ' . $alias . 'tags LIKE ? ';
  621. $values[] = "%{$tag}%";
  622. }
  623. }
  624. if ($filter->getInurl()) {
  625. foreach ($filter->getInurl() as $url) {
  626. $search .= 'AND CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ? ';
  627. $values[] = "%{$url}%";
  628. }
  629. }
  630. if ($filter->getNotAuthor()) {
  631. foreach ($filter->getNotAuthor() as $author) {
  632. $search .= 'AND (NOT ' . $alias . 'author LIKE ?) ';
  633. $values[] = "%{$author}%";
  634. }
  635. }
  636. if ($filter->getNotIntitle()) {
  637. foreach ($filter->getNotIntitle() as $title) {
  638. $search .= 'AND (NOT ' . $alias . 'title LIKE ?) ';
  639. $values[] = "%{$title}%";
  640. }
  641. }
  642. if ($filter->getNotTags()) {
  643. foreach ($filter->getNotTags() as $tag) {
  644. $search .= 'AND (NOT ' . $alias . 'tags LIKE ?) ';
  645. $values[] = "%{$tag}%";
  646. }
  647. }
  648. if ($filter->getNotInurl()) {
  649. foreach ($filter->getNotInurl() as $url) {
  650. $search .= 'AND (NOT CONCAT(' . $alias . 'link, ' . $alias . 'guid) LIKE ?) ';
  651. $values[] = "%{$url}%";
  652. }
  653. }
  654. if ($filter->getSearch()) {
  655. foreach ($filter->getSearch() as $search_value) {
  656. $search .= 'AND ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ? ';
  657. $values[] = "%{$search_value}%";
  658. }
  659. }
  660. if ($filter->getNotSearch()) {
  661. foreach ($filter->getNotSearch() as $search_value) {
  662. $search .= 'AND (NOT ' . $this->sqlconcat($alias . 'title', $this->isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ?) ';
  663. $values[] = "%{$search_value}%";
  664. }
  665. }
  666. }
  667. return array($values, $search);
  668. }
  669. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  670. if (!$state) {
  671. $state = FreshRSS_Entry::STATE_ALL;
  672. }
  673. $where = '';
  674. $joinFeed = false;
  675. $values = array();
  676. switch ($type) {
  677. case 'a':
  678. $where .= 'f.priority > ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  679. break;
  680. case 's': //Deprecated: use $state instead
  681. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  682. $where .= 'AND e.is_favorite=1 ';
  683. break;
  684. case 'c':
  685. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  686. $where .= 'AND f.category=? ';
  687. $values[] = intval($id);
  688. break;
  689. case 'f':
  690. $where .= 'e.id_feed=? ';
  691. $values[] = intval($id);
  692. break;
  693. case 'A':
  694. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  695. break;
  696. default:
  697. throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
  698. }
  699. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filter, $state, $order, $firstId, $date_min);
  700. return array(array_merge($values, $searchValues),
  701. 'SELECT e.id FROM `' . $this->prefix . 'entry` e '
  702. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  703. . 'WHERE ' . $where
  704. . $search
  705. . 'ORDER BY e.id ' . $order
  706. . ($limit > 0 ? ' LIMIT ' . $limit : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  707. }
  708. public function listWhereRaw($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  709. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  710. $sql = 'SELECT e0.id, e0.guid, e0.title, e0.author, '
  711. . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  712. . ', e0.link, e0.date, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags '
  713. . 'FROM `' . $this->prefix . 'entry` e0 '
  714. . 'INNER JOIN ('
  715. . $sql
  716. . ') e2 ON e2.id=e0.id '
  717. . 'ORDER BY e0.id ' . $order;
  718. $stm = $this->bd->prepare($sql);
  719. $stm->execute($values);
  720. return $stm;
  721. }
  722. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) {
  723. $stm = $this->listWhereRaw($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  724. return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
  725. }
  726. public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0) { //For API
  727. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filter, $date_min);
  728. $stm = $this->bd->prepare($sql);
  729. $stm->execute($values);
  730. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  731. }
  732. public function listHashForFeedGuids($id_feed, $guids) {
  733. if (count($guids) < 1) {
  734. return array();
  735. }
  736. $guids = array_unique($guids);
  737. $sql = 'SELECT guid, ' . $this->sqlHexEncode('hash') . ' AS hex_hash FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  738. $stm = $this->bd->prepare($sql);
  739. $values = array($id_feed);
  740. $values = array_merge($values, $guids);
  741. if ($stm && $stm->execute($values)) {
  742. $result = array();
  743. $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
  744. foreach ($rows as $row) {
  745. $result[$row['guid']] = $row['hex_hash'];
  746. }
  747. return $result;
  748. } else {
  749. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  750. if ($this->autoUpdateDb($info)) {
  751. return $this->listHashForFeedGuids($id_feed, $guids);
  752. }
  753. Minz_Log::error('SQL error listHashForFeedGuids: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  754. . ' while querying feed ' . $id_feed);
  755. return false;
  756. }
  757. }
  758. public function updateLastSeen($id_feed, $guids, $mtime = 0) {
  759. if (count($guids) < 1) {
  760. return 0;
  761. }
  762. $sql = 'UPDATE `' . $this->prefix . 'entry` SET `lastSeen`=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  763. $stm = $this->bd->prepare($sql);
  764. if ($mtime <= 0) {
  765. $mtime = time();
  766. }
  767. $values = array($mtime, $id_feed);
  768. $values = array_merge($values, $guids);
  769. if ($stm && $stm->execute($values)) {
  770. return $stm->rowCount();
  771. } else {
  772. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  773. if ($this->autoUpdateDb($info)) {
  774. return $this->updateLastSeen($id_feed, $guids);
  775. }
  776. Minz_Log::error('SQL error updateLastSeen: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  777. . ' while updating feed ' . $id_feed);
  778. return false;
  779. }
  780. }
  781. public function countUnreadRead() {
  782. $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'
  783. . ' 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';
  784. $stm = $this->bd->prepare($sql);
  785. $stm->execute();
  786. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  787. $all = empty($res[0]) ? 0 : $res[0];
  788. $unread = empty($res[1]) ? 0 : $res[1];
  789. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  790. }
  791. public function count($minPriority = null) {
  792. $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
  793. if ($minPriority !== null) {
  794. $sql = ' WHERE priority > ' . intval($minPriority);
  795. }
  796. $stm = $this->bd->prepare($sql);
  797. $stm->execute();
  798. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  799. return $res[0];
  800. }
  801. public function countNotRead($minPriority = null) {
  802. $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';
  803. if ($minPriority !== null) {
  804. $sql = ' AND priority > ' . intval($minPriority);
  805. }
  806. $stm = $this->bd->prepare($sql);
  807. $stm->execute();
  808. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  809. return $res[0];
  810. }
  811. public function countUnreadReadFavorites() {
  812. $sql = <<<SQL
  813. SELECT c
  814. FROM (
  815. SELECT COUNT(e1.id) AS c
  816. , 1 AS o
  817. FROM `{$this->prefix}entry` AS e1
  818. JOIN `{$this->prefix}feed` AS f1 ON e1.id_feed = f1.id
  819. WHERE e1.is_favorite = 1
  820. AND f1.priority >= :priority_normal
  821. UNION
  822. SELECT COUNT(e2.id) AS c
  823. , 2 AS o
  824. FROM `{$this->prefix}entry` AS e2
  825. JOIN `{$this->prefix}feed` AS f2 ON e2.id_feed = f2.id
  826. WHERE e2.is_favorite = 1
  827. AND e2.is_read = 0
  828. AND f2.priority >= :priority_normal
  829. ) u
  830. ORDER BY o
  831. SQL;
  832. $stm = $this->bd->prepare($sql);
  833. $stm->execute(array(':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL));
  834. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  835. $all = empty($res[0]) ? 0 : $res[0];
  836. $unread = empty($res[1]) ? 0 : $res[1];
  837. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  838. }
  839. public static function daoToEntry($dao) {
  840. $entry = new FreshRSS_Entry(
  841. $dao['id_feed'],
  842. $dao['guid'],
  843. $dao['title'],
  844. $dao['author'],
  845. $dao['content'],
  846. $dao['link'],
  847. $dao['date'],
  848. $dao['is_read'],
  849. $dao['is_favorite'],
  850. $dao['tags']
  851. );
  852. if (isset($dao['id'])) {
  853. $entry->_id($dao['id']);
  854. }
  855. return $entry;
  856. }
  857. private static function daoToEntries($listDAO) {
  858. $list = array();
  859. if (!is_array($listDAO)) {
  860. $listDAO = array($listDAO);
  861. }
  862. foreach ($listDAO as $key => $dao) {
  863. $list[] = self::daoToEntry($dao);
  864. }
  865. unset($listDAO);
  866. return $list;
  867. }
  868. }