EntryDAO.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. <?php
  2. class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. public static function isCompressed(): bool {
  4. return true;
  5. }
  6. public static function hasNativeHex(): bool {
  7. return true;
  8. }
  9. protected static function sqlConcat($s1, $s2) {
  10. return 'CONCAT(' . $s1 . ',' . $s2 . ')'; //MySQL
  11. }
  12. public static function sqlHexDecode(string $x): string {
  13. return 'unhex(' . $x . ')';
  14. }
  15. public static function sqlHexEncode(string $x): string {
  16. return 'hex(' . $x . ')';
  17. }
  18. public static function sqlIgnoreConflict(string $sql): string {
  19. return str_replace('INSERT INTO ', 'INSERT IGNORE INTO ', $sql);
  20. }
  21. //TODO: Move the database auto-updates to DatabaseDAO
  22. protected function createEntryTempTable() {
  23. $ok = false;
  24. $hadTransaction = $this->pdo->inTransaction();
  25. if ($hadTransaction) {
  26. $this->pdo->commit();
  27. }
  28. try {
  29. require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
  30. Minz_Log::warning('SQL CREATE TABLE entrytmp...');
  31. $ok = $this->pdo->exec($GLOBALS['SQL_CREATE_TABLE_ENTRYTMP'] . $GLOBALS['SQL_CREATE_INDEX_ENTRY_1']) !== false;
  32. } catch (Exception $ex) {
  33. Minz_Log::error(__method__ . ' error: ' . $ex->getMessage());
  34. }
  35. if ($hadTransaction) {
  36. $this->pdo->beginTransaction();
  37. }
  38. return $ok;
  39. }
  40. private function updateToMediumBlob() {
  41. if ($this->pdo->dbType() !== 'mysql') {
  42. return false;
  43. }
  44. Minz_Log::warning('Update MySQL table to use MEDIUMBLOB...');
  45. $sql = <<<'SQL'
  46. ALTER TABLE `_entry` MODIFY `content_bin` MEDIUMBLOB;
  47. ALTER TABLE `_entrytmp` MODIFY `content_bin` MEDIUMBLOB;
  48. SQL;
  49. try {
  50. $ok = $this->pdo->exec($sql) !== false;
  51. } catch (Exception $e) {
  52. $ok = false;
  53. Minz_Log::error(__method__ . ' error: ' . $e->getMessage());
  54. }
  55. return $ok;
  56. }
  57. //TODO: Move the database auto-updates to DatabaseDAO
  58. protected function autoUpdateDb(array $errorInfo) {
  59. if (isset($errorInfo[0])) {
  60. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_TABLE_ERROR) {
  61. if (stripos($errorInfo[2], 'tag') !== false) {
  62. $tagDAO = FreshRSS_Factory::createTagDao();
  63. return $tagDAO->createTagTable(); //v1.12.0
  64. } elseif (stripos($errorInfo[2], 'entrytmp') !== false) {
  65. return $this->createEntryTempTable(); //v1.7.0
  66. }
  67. }
  68. }
  69. if (isset($errorInfo[1])) {
  70. if ($errorInfo[1] == FreshRSS_DatabaseDAO::ER_DATA_TOO_LONG) {
  71. if (stripos($errorInfo[2], 'content_bin') !== false) {
  72. return $this->updateToMediumBlob(); //v1.15.0
  73. }
  74. }
  75. }
  76. return false;
  77. }
  78. /**
  79. * @var PDOStatement|null|false
  80. */
  81. private $addEntryPrepared = false;
  82. public function addEntry(array $valuesTmp, bool $useTmpTable = true) {
  83. if ($this->addEntryPrepared == null) {
  84. $sql = static::sqlIgnoreConflict(
  85. 'INSERT INTO `_' . ($useTmpTable ? 'entrytmp' : 'entry') . '` (id, guid, title, author, '
  86. . (static::isCompressed() ? 'content_bin' : 'content')
  87. . ', link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags) '
  88. . 'VALUES(:id, :guid, :title, :author, '
  89. . (static::isCompressed() ? 'COMPRESS(:content)' : ':content')
  90. . ', :link, :date, :last_seen, '
  91. . static::sqlHexDecode(':hash')
  92. . ', :is_read, :is_favorite, :id_feed, :tags)');
  93. $this->addEntryPrepared = $this->pdo->prepare($sql);
  94. }
  95. if ($this->addEntryPrepared) {
  96. $this->addEntryPrepared->bindParam(':id', $valuesTmp['id']);
  97. $valuesTmp['guid'] = substr($valuesTmp['guid'], 0, 760);
  98. $valuesTmp['guid'] = safe_ascii($valuesTmp['guid']);
  99. $this->addEntryPrepared->bindParam(':guid', $valuesTmp['guid']);
  100. $valuesTmp['title'] = mb_strcut($valuesTmp['title'], 0, 255, 'UTF-8');
  101. $valuesTmp['title'] = safe_utf8($valuesTmp['title']);
  102. $this->addEntryPrepared->bindParam(':title', $valuesTmp['title']);
  103. $valuesTmp['author'] = mb_strcut($valuesTmp['author'], 0, 255, 'UTF-8');
  104. $valuesTmp['author'] = safe_utf8($valuesTmp['author']);
  105. $this->addEntryPrepared->bindParam(':author', $valuesTmp['author']);
  106. $valuesTmp['content'] = safe_utf8($valuesTmp['content']);
  107. $this->addEntryPrepared->bindParam(':content', $valuesTmp['content']);
  108. $valuesTmp['link'] = substr($valuesTmp['link'], 0, 1023);
  109. $valuesTmp['link'] = safe_ascii($valuesTmp['link']);
  110. $this->addEntryPrepared->bindParam(':link', $valuesTmp['link']);
  111. $valuesTmp['date'] = min($valuesTmp['date'], 2147483647);
  112. $this->addEntryPrepared->bindParam(':date', $valuesTmp['date'], PDO::PARAM_INT);
  113. if (empty($valuesTmp['lastSeen'])) {
  114. $valuesTmp['lastSeen'] = time();
  115. }
  116. $this->addEntryPrepared->bindParam(':last_seen', $valuesTmp['lastSeen'], PDO::PARAM_INT);
  117. $valuesTmp['is_read'] = $valuesTmp['is_read'] ? 1 : 0;
  118. $this->addEntryPrepared->bindParam(':is_read', $valuesTmp['is_read'], PDO::PARAM_INT);
  119. $valuesTmp['is_favorite'] = $valuesTmp['is_favorite'] ? 1 : 0;
  120. $this->addEntryPrepared->bindParam(':is_favorite', $valuesTmp['is_favorite'], PDO::PARAM_INT);
  121. $this->addEntryPrepared->bindParam(':id_feed', $valuesTmp['id_feed'], PDO::PARAM_INT);
  122. $valuesTmp['tags'] = mb_strcut($valuesTmp['tags'], 0, 1023, 'UTF-8');
  123. $valuesTmp['tags'] = safe_utf8($valuesTmp['tags']);
  124. $this->addEntryPrepared->bindParam(':tags', $valuesTmp['tags']);
  125. if (static::hasNativeHex()) {
  126. $this->addEntryPrepared->bindParam(':hash', $valuesTmp['hash']);
  127. } else {
  128. $valuesTmp['hashBin'] = hex2bin($valuesTmp['hash']);
  129. $this->addEntryPrepared->bindParam(':hash', $valuesTmp['hashBin']);
  130. }
  131. }
  132. if ($this->addEntryPrepared && $this->addEntryPrepared->execute()) {
  133. return true;
  134. } else {
  135. $info = $this->addEntryPrepared == null ? $this->pdo->errorInfo() : $this->addEntryPrepared->errorInfo();
  136. if ($this->autoUpdateDb($info)) {
  137. $this->addEntryPrepared = null;
  138. return $this->addEntry($valuesTmp);
  139. } elseif ((int)((int)$info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
  140. Minz_Log::error('SQL error addEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  141. . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title']);
  142. }
  143. return false;
  144. }
  145. }
  146. public function commitNewEntries() {
  147. $sql = <<<'SQL'
  148. SET @rank=(SELECT MAX(id) - COUNT(*) FROM `_entrytmp`);
  149. INSERT IGNORE INTO `_entry` (
  150. id, guid, title, author, content_bin, link, date, `lastSeen`,
  151. hash, is_read, is_favorite, id_feed, tags
  152. )
  153. SELECT @rank:=@rank+1 AS id, guid, title, author, content_bin, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
  154. FROM `_entrytmp`
  155. ORDER BY date, id;
  156. DELETE FROM `_entrytmp` WHERE id <= @rank;
  157. SQL;
  158. $hadTransaction = $this->pdo->inTransaction();
  159. if (!$hadTransaction) {
  160. $this->pdo->beginTransaction();
  161. }
  162. $result = $this->pdo->exec($sql) !== false;
  163. if (!$hadTransaction) {
  164. $this->pdo->commit();
  165. }
  166. return $result;
  167. }
  168. private $updateEntryPrepared = null;
  169. public function updateEntry(array $valuesTmp) {
  170. if (!isset($valuesTmp['is_read'])) {
  171. $valuesTmp['is_read'] = null;
  172. }
  173. if ($this->updateEntryPrepared === null) {
  174. $sql = 'UPDATE `_entry` '
  175. . 'SET title=:title, author=:author, '
  176. . (static::isCompressed() ? 'content_bin=COMPRESS(:content)' : 'content=:content')
  177. . ', link=:link, date=:date, `lastSeen`=:last_seen'
  178. . ', hash=' . static::sqlHexDecode(':hash')
  179. . ', is_read=COALESCE(:is_read, is_read)'
  180. . ', tags=:tags '
  181. . 'WHERE id_feed=:id_feed AND guid=:guid';
  182. $this->updateEntryPrepared = $this->pdo->prepare($sql);
  183. }
  184. if ($this->updateEntryPrepared) {
  185. $valuesTmp['guid'] = substr($valuesTmp['guid'], 0, 760);
  186. $valuesTmp['guid'] = safe_ascii($valuesTmp['guid']);
  187. $this->updateEntryPrepared->bindParam(':guid', $valuesTmp['guid']);
  188. $valuesTmp['title'] = mb_strcut($valuesTmp['title'], 0, 255, 'UTF-8');
  189. $valuesTmp['title'] = safe_utf8($valuesTmp['title']);
  190. $this->updateEntryPrepared->bindParam(':title', $valuesTmp['title']);
  191. $valuesTmp['author'] = mb_strcut($valuesTmp['author'], 0, 255, 'UTF-8');
  192. $valuesTmp['author'] = safe_utf8($valuesTmp['author']);
  193. $this->updateEntryPrepared->bindParam(':author', $valuesTmp['author']);
  194. $valuesTmp['content'] = safe_utf8($valuesTmp['content']);
  195. $this->updateEntryPrepared->bindParam(':content', $valuesTmp['content']);
  196. $valuesTmp['link'] = substr($valuesTmp['link'], 0, 1023);
  197. $valuesTmp['link'] = safe_ascii($valuesTmp['link']);
  198. $this->updateEntryPrepared->bindParam(':link', $valuesTmp['link']);
  199. $valuesTmp['date'] = min($valuesTmp['date'], 2147483647);
  200. $this->updateEntryPrepared->bindParam(':date', $valuesTmp['date'], PDO::PARAM_INT);
  201. $valuesTmp['lastSeen'] = time();
  202. $this->updateEntryPrepared->bindParam(':last_seen', $valuesTmp['lastSeen'], PDO::PARAM_INT);
  203. if ($valuesTmp['is_read'] === null) {
  204. $this->updateEntryPrepared->bindValue(':is_read', null, PDO::PARAM_NULL);
  205. } else {
  206. $this->updateEntryPrepared->bindValue(':is_read', $valuesTmp['is_read'] ? 1 : 0, PDO::PARAM_INT);
  207. }
  208. $this->updateEntryPrepared->bindParam(':id_feed', $valuesTmp['id_feed'], PDO::PARAM_INT);
  209. $valuesTmp['tags'] = mb_strcut($valuesTmp['tags'], 0, 1023, 'UTF-8');
  210. $valuesTmp['tags'] = safe_utf8($valuesTmp['tags']);
  211. $this->updateEntryPrepared->bindParam(':tags', $valuesTmp['tags']);
  212. if (static::hasNativeHex()) {
  213. $this->updateEntryPrepared->bindParam(':hash', $valuesTmp['hash']);
  214. } else {
  215. $valuesTmp['hashBin'] = hex2bin($valuesTmp['hash']);
  216. $this->updateEntryPrepared->bindParam(':hash', $valuesTmp['hashBin']);
  217. }
  218. }
  219. if ($this->updateEntryPrepared && $this->updateEntryPrepared->execute()) {
  220. return true;
  221. } else {
  222. $info = $this->updateEntryPrepared == null ? $this->pdo->errorInfo() : $this->updateEntryPrepared->errorInfo();
  223. if ($this->autoUpdateDb($info)) {
  224. return $this->updateEntry($valuesTmp);
  225. }
  226. Minz_Log::error('SQL error updateEntry: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  227. . ' while updating entry with GUID ' . $valuesTmp['guid'] . ' in feed ' . $valuesTmp['id_feed']);
  228. return false;
  229. }
  230. }
  231. /**
  232. * Toggle favorite marker on one or more article
  233. *
  234. * @todo simplify the query by removing the str_repeat. I am pretty sure
  235. * there is an other way to do that.
  236. *
  237. * @param integer|array $ids
  238. * @return false|integer
  239. */
  240. public function markFavorite($ids, bool $is_favorite = true) {
  241. if (!is_array($ids)) {
  242. $ids = array($ids);
  243. }
  244. if (count($ids) < 1) {
  245. return 0;
  246. }
  247. FreshRSS_UserDAO::touch();
  248. if (count($ids) > FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER) {
  249. // Split a query with too many variables parameters
  250. $affected = 0;
  251. $idsChunks = array_chunk($ids, FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER);
  252. foreach ($idsChunks as $idsChunk) {
  253. $affected += $this->markFavorite($idsChunk, $is_favorite);
  254. }
  255. return $affected;
  256. }
  257. $sql = 'UPDATE `_entry` '
  258. . 'SET is_favorite=? '
  259. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  260. $values = array($is_favorite ? 1 : 0);
  261. $values = array_merge($values, $ids);
  262. $stm = $this->pdo->prepare($sql);
  263. if ($stm && $stm->execute($values)) {
  264. return $stm->rowCount();
  265. } else {
  266. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  267. Minz_Log::error('SQL error markFavorite: ' . $info[2]);
  268. return false;
  269. }
  270. }
  271. /**
  272. * Update the unread article cache held on every feed details.
  273. * Depending on the parameters, it updates the cache on one feed, on all
  274. * feeds from one category or on all feeds.
  275. *
  276. * @todo It can use the query builder refactoring to build that query
  277. *
  278. * @param false|integer $catId category ID
  279. * @param false|integer $feedId feed ID
  280. * @return boolean
  281. */
  282. protected function updateCacheUnreads($catId = false, $feedId = false) {
  283. $sql = 'UPDATE `_feed` f '
  284. . 'LEFT OUTER JOIN ('
  285. . 'SELECT e.id_feed, '
  286. . 'COUNT(*) AS nbUnreads '
  287. . 'FROM `_entry` e '
  288. . 'WHERE e.is_read=0 '
  289. . 'GROUP BY e.id_feed'
  290. . ') x ON x.id_feed=f.id '
  291. . 'SET f.`cache_nbUnreads`=COALESCE(x.nbUnreads, 0)';
  292. $hasWhere = false;
  293. $values = array();
  294. if ($feedId !== false) {
  295. $sql .= ' WHERE';
  296. $hasWhere = true;
  297. $sql .= ' f.id=?';
  298. $values[] = $feedId;
  299. }
  300. if ($catId !== false) {
  301. $sql .= $hasWhere ? ' AND' : ' WHERE';
  302. $hasWhere = true;
  303. $sql .= ' f.category=?';
  304. $values[] = $catId;
  305. }
  306. $stm = $this->pdo->prepare($sql);
  307. if ($stm && $stm->execute($values)) {
  308. return true;
  309. } else {
  310. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  311. Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
  312. return false;
  313. }
  314. }
  315. /**
  316. * Toggle the read marker on one or more article.
  317. * Then the cache is updated.
  318. *
  319. * @todo change the way the query is build because it seems there is
  320. * unnecessary code in here. For instance, the part with the str_repeat.
  321. * @todo remove code duplication. It seems the code is basically the
  322. * same if it is an array or not.
  323. *
  324. * @param integer|array $ids
  325. * @param boolean $is_read
  326. * @return integer|false affected rows
  327. */
  328. public function markRead($ids, bool $is_read = true) {
  329. FreshRSS_UserDAO::touch();
  330. if (is_array($ids)) { //Many IDs at once
  331. if (count($ids) < 6) { //Speed heuristics
  332. $affected = 0;
  333. foreach ($ids as $id) {
  334. $affected += $this->markRead($id, $is_read);
  335. }
  336. return $affected;
  337. } elseif (count($ids) > FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER) {
  338. // Split a query with too many variables parameters
  339. $affected = 0;
  340. $idsChunks = array_chunk($ids, FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER);
  341. foreach ($idsChunks as $idsChunk) {
  342. $affected += $this->markRead($idsChunk, $is_read);
  343. }
  344. return $affected;
  345. }
  346. $sql = 'UPDATE `_entry` '
  347. . 'SET is_read=? '
  348. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
  349. $values = array($is_read ? 1 : 0);
  350. $values = array_merge($values, $ids);
  351. $stm = $this->pdo->prepare($sql);
  352. if (!($stm && $stm->execute($values))) {
  353. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  354. Minz_Log::error('SQL error markRead: ' . $info[2]);
  355. return false;
  356. }
  357. $affected = $stm->rowCount();
  358. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  359. return false;
  360. }
  361. return $affected;
  362. } else {
  363. $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
  364. . 'SET e.is_read=?,'
  365. . 'f.`cache_nbUnreads`=f.`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
  366. . 'WHERE e.id=? AND e.is_read=?';
  367. $values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
  368. $stm = $this->pdo->prepare($sql);
  369. if ($stm && $stm->execute($values)) {
  370. return $stm->rowCount();
  371. } else {
  372. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  373. Minz_Log::error('SQL error markRead: ' . $info[2]);
  374. return false;
  375. }
  376. }
  377. }
  378. /**
  379. * Mark all entries as read depending on parameters.
  380. * If $onlyFavorites is true, it is used when the user mark as read in
  381. * the favorite pseudo-category.
  382. * If $priorityMin is greater than 0, it is used when the user mark as
  383. * read in the main feed pseudo-category.
  384. * Then the cache is updated.
  385. *
  386. * If $idMax equals 0, a deprecated debug message is logged
  387. *
  388. * @todo refactor this method along with markReadCat and markReadFeed
  389. * since they are all doing the same thing. I think we need to build a
  390. * tool to generate the query instead of having queries all over the
  391. * place. It will be reused also for the filtering making every thing
  392. * separated.
  393. *
  394. * @param string $idMax fail safe article ID
  395. * @param boolean $onlyFavorites
  396. * @param integer $priorityMin
  397. * @param FreshRSS_BooleanSearch|null $filters
  398. * @return integer|false affected rows
  399. */
  400. public function markReadEntries(string $idMax = '0', bool $onlyFavorites = false, int $priorityMin = 0, $filters = null, int $state = 0, bool $is_read = true) {
  401. FreshRSS_UserDAO::touch();
  402. if ($idMax == 0) {
  403. $idMax = time() . '000000';
  404. Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
  405. }
  406. $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
  407. . 'SET e.is_read=? '
  408. . 'WHERE e.is_read <> ? AND e.id <= ?';
  409. if ($onlyFavorites) {
  410. $sql .= ' AND e.is_favorite=1';
  411. } elseif ($priorityMin >= 0) {
  412. $sql .= ' AND f.priority > ' . intval($priorityMin);
  413. }
  414. $values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax);
  415. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  416. $stm = $this->pdo->prepare($sql . $search);
  417. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  418. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  419. Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
  420. return false;
  421. }
  422. $affected = $stm->rowCount();
  423. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  424. return false;
  425. }
  426. return $affected;
  427. }
  428. /**
  429. * Mark all the articles in a category as read.
  430. * There is a fail safe to prevent to mark as read articles that are
  431. * loaded during the mark as read action. Then the cache is updated.
  432. *
  433. * If $idMax equals 0, a deprecated debug message is logged
  434. *
  435. * @param integer $id category ID
  436. * @param string $idMax fail safe article ID
  437. * @param FreshRSS_BooleanSearch|null $filters
  438. * @return integer|false affected rows
  439. */
  440. public function markReadCat(int $id, string $idMax = '0', $filters = null, int $state = 0, bool $is_read = true) {
  441. FreshRSS_UserDAO::touch();
  442. if ($idMax == '0') {
  443. $idMax = time() . '000000';
  444. Minz_Log::debug('Calling markReadCat(0) is deprecated!');
  445. }
  446. $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
  447. . 'SET e.is_read=? '
  448. . 'WHERE f.category=? AND e.is_read <> ? AND e.id <= ?';
  449. $values = array($is_read ? 1 : 0, $id, $is_read ? 1 : 0, $idMax);
  450. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  451. $stm = $this->pdo->prepare($sql . $search);
  452. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  453. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  454. Minz_Log::error('SQL error markReadCat: ' . $info[2]);
  455. return false;
  456. }
  457. $affected = $stm->rowCount();
  458. if (($affected > 0) && (!$this->updateCacheUnreads($id, false))) {
  459. return false;
  460. }
  461. return $affected;
  462. }
  463. /**
  464. * Mark all the articles in a feed as read.
  465. * There is a fail safe to prevent to mark as read articles that are
  466. * loaded during the mark as read action. Then the cache is updated.
  467. *
  468. * If $idMax equals 0, a deprecated debug message is logged
  469. *
  470. * @param integer $id_feed feed ID
  471. * @param string $idMax fail safe article ID
  472. * @param FreshRSS_BooleanSearch|null $filters
  473. * @return integer|false affected rows
  474. */
  475. public function markReadFeed(int $id_feed, string $idMax = '0', $filters = null, int $state = 0, bool $is_read = true) {
  476. FreshRSS_UserDAO::touch();
  477. if ($idMax == '0') {
  478. $idMax = time() . '000000';
  479. Minz_Log::debug('Calling markReadFeed(0) is deprecated!');
  480. }
  481. $this->pdo->beginTransaction();
  482. $sql = 'UPDATE `_entry` '
  483. . 'SET is_read=? '
  484. . 'WHERE id_feed=? AND is_read <> ? AND id <= ?';
  485. $values = array($is_read ? 1 : 0, $id_feed, $is_read ? 1 : 0, $idMax);
  486. list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
  487. $stm = $this->pdo->prepare($sql . $search);
  488. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  489. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  490. Minz_Log::error('SQL error markReadFeed: ' . $info[2] . ' with SQL: ' . $sql . $search);
  491. $this->pdo->rollBack();
  492. return false;
  493. }
  494. $affected = $stm->rowCount();
  495. if ($affected > 0) {
  496. $sql = 'UPDATE `_feed` '
  497. . 'SET `cache_nbUnreads`=`cache_nbUnreads`-' . $affected
  498. . ' WHERE id=:id';
  499. $stm = $this->pdo->prepare($sql);
  500. $stm->bindParam(':id', $id_feed, PDO::PARAM_INT);
  501. if (!($stm && $stm->execute())) {
  502. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  503. Minz_Log::error('SQL error markReadFeed cache: ' . $info[2]);
  504. $this->pdo->rollBack();
  505. return false;
  506. }
  507. }
  508. $this->pdo->commit();
  509. return $affected;
  510. }
  511. /**
  512. * Mark all the articles in a tag as read.
  513. * @param integer $id tag ID, or empty for targeting any tag
  514. * @param string $idMax max article ID
  515. * @return integer|false affected rows
  516. */
  517. public function markReadTag($id = 0, string $idMax = '0', $filters = null, int $state = 0, bool $is_read = true) {
  518. FreshRSS_UserDAO::touch();
  519. if ($idMax == '0') {
  520. $idMax = time() . '000000';
  521. Minz_Log::debug('Calling markReadTag(0) is deprecated!');
  522. }
  523. $sql = 'UPDATE `_entry` e INNER JOIN `_entrytag` et ON et.id_entry = e.id '
  524. . 'SET e.is_read = ? '
  525. . 'WHERE '
  526. . ($id == 0 ? '' : 'et.id_tag = ? AND ')
  527. . 'e.is_read <> ? AND e.id <= ?';
  528. $values = array($is_read ? 1 : 0);
  529. if ($id != 0) {
  530. $values[] = $id;
  531. }
  532. $values[] = $is_read ? 1 : 0;
  533. $values[] = $idMax;
  534. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
  535. $stm = $this->pdo->prepare($sql . $search);
  536. if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
  537. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  538. Minz_Log::error('SQL error markReadTag: ' . $info[2]);
  539. return false;
  540. }
  541. $affected = $stm->rowCount();
  542. if (($affected > 0) && (!$this->updateCacheUnreads(false, false))) {
  543. return false;
  544. }
  545. return $affected;
  546. }
  547. /**
  548. * Remember to call updateCachedValue($id_feed) or updateCachedValues() just after.
  549. */
  550. public function cleanOldEntries($id_feed, $options = []) {
  551. $sql = 'DELETE FROM `_entry` WHERE id_feed = :id_feed1'; //No alias for MySQL / MariaDB
  552. $params = [];
  553. $params[':id_feed1'] = $id_feed;
  554. //==Exclusions==
  555. if (!empty($options['keep_favourites'])) {
  556. $sql .= ' AND is_favorite = 0';
  557. }
  558. if (!empty($options['keep_unreads'])) {
  559. $sql .= ' AND is_read = 1';
  560. }
  561. if (!empty($options['keep_labels'])) {
  562. $sql .= ' AND NOT EXISTS (SELECT 1 FROM `_entrytag` WHERE id_entry = id)';
  563. }
  564. if (!empty($options['keep_min']) && $options['keep_min'] > 0) {
  565. //Double SELECT for MySQL workaround ERROR 1093 (HY000)
  566. $sql .= ' AND `lastSeen` < (SELECT `lastSeen`'
  567. . ' FROM (SELECT e2.`lastSeen` FROM `_entry` e2 WHERE e2.id_feed = :id_feed2'
  568. . ' ORDER BY e2.`lastSeen` DESC LIMIT 1 OFFSET :keep_min) last_seen2)';
  569. $params[':id_feed2'] = $id_feed;
  570. $params[':keep_min'] = (int)$options['keep_min'];
  571. }
  572. //Keep at least the articles seen at the last refresh
  573. $sql .= ' AND `lastSeen` < (SELECT maxlastseen'
  574. . ' FROM (SELECT MAX(e3.`lastSeen`) AS maxlastseen FROM `_entry` e3 WHERE e3.id_feed = :id_feed3) last_seen3)';
  575. $params[':id_feed3'] = $id_feed;
  576. //==Inclusions==
  577. $sql .= ' AND (1=0';
  578. if (!empty($options['keep_period'])) {
  579. $sql .= ' OR `lastSeen` < :max_last_seen';
  580. $now = new DateTime('now');
  581. $now->sub(new DateInterval($options['keep_period']));
  582. $params[':max_last_seen'] = $now->format('U');
  583. }
  584. if (!empty($options['keep_max']) && $options['keep_max'] > 0) {
  585. $sql .= ' OR `lastSeen` <= (SELECT `lastSeen`'
  586. . ' FROM (SELECT e4.`lastSeen` FROM `_entry` e4 WHERE e4.id_feed = :id_feed4'
  587. . ' ORDER BY e4.`lastSeen` DESC LIMIT 1 OFFSET :keep_max) last_seen4)';
  588. $params[':id_feed4'] = $id_feed;
  589. $params[':keep_max'] = (int)$options['keep_max'];
  590. }
  591. $sql .= ')';
  592. $stm = $this->pdo->prepare($sql);
  593. if ($stm && $stm->execute($params)) {
  594. return $stm->rowCount();
  595. } else {
  596. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  597. if ($this->autoUpdateDb($info)) {
  598. return $this->cleanOldEntries($id_feed, $options);
  599. }
  600. Minz_Log::error(__method__ . ' error:' . json_encode($info));
  601. return false;
  602. }
  603. }
  604. public function selectAll() {
  605. $sql = 'SELECT id, guid, title, author, '
  606. . (static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  607. . ', link, date, `lastSeen`, ' . static::sqlHexEncode('hash') . ' AS hash, is_read, is_favorite, id_feed, tags '
  608. . 'FROM `_entry`';
  609. $stm = $this->pdo->query($sql);
  610. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  611. yield $row;
  612. }
  613. }
  614. /** @return FreshRSS_Entry|null */
  615. public function searchByGuid($id_feed, $guid) {
  616. // un guid est unique pour un flux donné
  617. $sql = 'SELECT id, guid, title, author, '
  618. . (static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  619. . ', link, date, is_read, is_favorite, id_feed, tags '
  620. . 'FROM `_entry` WHERE id_feed=:id_feed AND guid=:guid';
  621. $stm = $this->pdo->prepare($sql);
  622. $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
  623. $stm->bindParam(':guid', $guid);
  624. $stm->execute();
  625. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  626. return isset($res[0]) ? FreshRSS_Entry::fromArray($res[0]) : null;
  627. }
  628. /** @return FreshRSS_Entry|null */
  629. public function searchById($id) {
  630. $sql = 'SELECT id, guid, title, author, '
  631. . (static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  632. . ', link, date, is_read, is_favorite, id_feed, tags '
  633. . 'FROM `_entry` WHERE id=:id';
  634. $stm = $this->pdo->prepare($sql);
  635. $stm->bindParam(':id', $id, PDO::PARAM_INT);
  636. $stm->execute();
  637. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  638. return isset($res[0]) ? FreshRSS_Entry::fromArray($res[0]) : null;
  639. }
  640. public function searchIdByGuid($id_feed, $guid) {
  641. $sql = 'SELECT id FROM `_entry` WHERE id_feed=:id_feed AND guid=:guid';
  642. $stm = $this->pdo->prepare($sql);
  643. $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
  644. $stm->bindParam(':guid', $guid);
  645. $stm->execute();
  646. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  647. return isset($res[0]) ? $res[0] : null;
  648. }
  649. /** @param FreshRSS_BooleanSearch $filters */
  650. public static function sqlBooleanSearch(string $alias, $filters, int $level = 0) {
  651. $search = '';
  652. $values = [];
  653. $isOpen = false;
  654. foreach ($filters->searches() as $filter) {
  655. if ($filter == null) {
  656. continue;
  657. }
  658. if ($filter instanceof FreshRSS_BooleanSearch) {
  659. // BooleanSearches are combined by AND (default) or OR (special case) operator and are recursive
  660. list($filterValues, $filterSearch) = self::sqlBooleanSearch($alias, $filter, $level + 1);
  661. $filterSearch = trim($filterSearch);
  662. if ($filterSearch !== '') {
  663. if ($search !== '') {
  664. $search .= $filter->operator();
  665. }
  666. $search .= ' (' . $filterSearch . ') ';
  667. $values = array_merge($values, $filterValues);
  668. }
  669. continue;
  670. }
  671. // Searches are combined by OR and are not recursive
  672. $sub_search = '';
  673. if ($filter->getEntryIds()) {
  674. foreach ($filter->getEntryIds() as $entry_ids) {
  675. $sub_search .= 'AND ' . $alias . 'id IN (';
  676. foreach ($entry_ids as $entry_id) {
  677. $sub_search .= '?,';
  678. $values[] = $entry_id;
  679. }
  680. $sub_search = rtrim($sub_search, ',');
  681. $sub_search .= ') ';
  682. }
  683. }
  684. if ($filter->getNotEntryIds()) {
  685. foreach ($filter->getNotEntryIds() as $entry_ids) {
  686. $sub_search .= 'AND ' . $alias . 'id NOT IN (';
  687. foreach ($entry_ids as $entry_id) {
  688. $sub_search .= '?,';
  689. $values[] = $entry_id;
  690. }
  691. $sub_search = rtrim($sub_search, ',');
  692. $sub_search .= ') ';
  693. }
  694. }
  695. if ($filter->getMinDate()) {
  696. $sub_search .= 'AND ' . $alias . 'id >= ? ';
  697. $values[] = "{$filter->getMinDate()}000000";
  698. }
  699. if ($filter->getMaxDate()) {
  700. $sub_search .= 'AND ' . $alias . 'id <= ? ';
  701. $values[] = "{$filter->getMaxDate()}000000";
  702. }
  703. if ($filter->getMinPubdate()) {
  704. $sub_search .= 'AND ' . $alias . 'date >= ? ';
  705. $values[] = $filter->getMinPubdate();
  706. }
  707. if ($filter->getMaxPubdate()) {
  708. $sub_search .= 'AND ' . $alias . 'date <= ? ';
  709. $values[] = $filter->getMaxPubdate();
  710. }
  711. //Negation of date intervals must be combined by OR
  712. if ($filter->getNotMinDate() || $filter->getNotMaxDate()) {
  713. $sub_search .= 'AND (';
  714. if ($filter->getNotMinDate()) {
  715. $sub_search .= $alias . 'id < ?';
  716. $values[] = "{$filter->getNotMinDate()}000000";
  717. if ($filter->getNotMaxDate()) {
  718. $sub_search .= ' OR ';
  719. }
  720. }
  721. if ($filter->getNotMaxDate()) {
  722. $sub_search .= $alias . 'id > ?';
  723. $values[] = "{$filter->getNotMaxDate()}000000";
  724. }
  725. $sub_search .= ') ';
  726. }
  727. if ($filter->getNotMinPubdate() || $filter->getNotMaxPubdate()) {
  728. $sub_search .= 'AND (';
  729. if ($filter->getNotMinPubdate()) {
  730. $sub_search .= $alias . 'date < ?';
  731. $values[] = $filter->getNotMinPubdate();
  732. if ($filter->getNotMaxPubdate()) {
  733. $sub_search .= ' OR ';
  734. }
  735. }
  736. if ($filter->getNotMaxPubdate()) {
  737. $sub_search .= $alias . 'date > ?';
  738. $values[] = $filter->getNotMaxPubdate();
  739. }
  740. $sub_search .= ') ';
  741. }
  742. if ($filter->getFeedIds()) {
  743. foreach ($filter->getFeedIds() as $feed_ids) {
  744. $sub_search .= 'AND ' . $alias . 'id_feed IN (';
  745. foreach ($feed_ids as $feed_id) {
  746. $sub_search .= '?,';
  747. $values[] = $feed_id;
  748. }
  749. $sub_search = rtrim($sub_search, ',');
  750. $sub_search .= ') ';
  751. }
  752. }
  753. if ($filter->getNotFeedIds()) {
  754. foreach ($filter->getNotFeedIds() as $feed_ids) {
  755. $sub_search .= 'AND ' . $alias . 'id_feed NOT IN (';
  756. foreach ($feed_ids as $feed_id) {
  757. $sub_search .= '?,';
  758. $values[] = $feed_id;
  759. }
  760. $sub_search = rtrim($sub_search, ',');
  761. $sub_search .= ') ';
  762. }
  763. }
  764. if ($filter->getLabelIds()) {
  765. foreach ($filter->getLabelIds() as $label_ids) {
  766. if ($label_ids === '*') {
  767. $sub_search .= 'AND EXISTS (SELECT et.id_tag FROM `_entrytag` et WHERE et.id_entry = ' . $alias . 'id) ';
  768. } else {
  769. $sub_search .= 'AND ' . $alias . 'id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (';
  770. foreach ($label_ids as $label_id) {
  771. $sub_search .= '?,';
  772. $values[] = $label_id;
  773. }
  774. $sub_search = rtrim($sub_search, ',');
  775. $sub_search .= ')) ';
  776. }
  777. }
  778. }
  779. if ($filter->getNotLabelIds()) {
  780. foreach ($filter->getNotLabelIds() as $label_ids) {
  781. if ($label_ids === '*') {
  782. $sub_search .= 'AND NOT EXISTS (SELECT et.id_tag FROM `_entrytag` et WHERE et.id_entry = ' . $alias . 'id) ';
  783. } else {
  784. $sub_search .= 'AND ' . $alias . 'id NOT IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (';
  785. foreach ($label_ids as $label_id) {
  786. $sub_search .= '?,';
  787. $values[] = $label_id;
  788. }
  789. $sub_search = rtrim($sub_search, ',');
  790. $sub_search .= ')) ';
  791. }
  792. }
  793. }
  794. if ($filter->getLabelNames()) {
  795. foreach ($filter->getLabelNames() as $label_names) {
  796. $sub_search .= 'AND ' . $alias . 'id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (';
  797. foreach ($label_names as $label_name) {
  798. $sub_search .= '?,';
  799. $values[] = $label_name;
  800. }
  801. $sub_search = rtrim($sub_search, ',');
  802. $sub_search .= ')) ';
  803. }
  804. }
  805. if ($filter->getNotLabelNames()) {
  806. foreach ($filter->getNotLabelNames() as $label_names) {
  807. $sub_search .= 'AND ' . $alias . 'id NOT IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (';
  808. foreach ($label_names as $label_name) {
  809. $sub_search .= '?,';
  810. $values[] = $label_name;
  811. }
  812. $sub_search = rtrim($sub_search, ',');
  813. $sub_search .= ')) ';
  814. }
  815. }
  816. if ($filter->getAuthor()) {
  817. foreach ($filter->getAuthor() as $author) {
  818. $sub_search .= 'AND ' . $alias . 'author LIKE ? ';
  819. $values[] = "%{$author}%";
  820. }
  821. }
  822. if ($filter->getIntitle()) {
  823. foreach ($filter->getIntitle() as $title) {
  824. $sub_search .= 'AND ' . $alias . 'title LIKE ? ';
  825. $values[] = "%{$title}%";
  826. }
  827. }
  828. if ($filter->getTags()) {
  829. foreach ($filter->getTags() as $tag) {
  830. $sub_search .= 'AND ' . $alias . 'tags LIKE ? ';
  831. $values[] = "%{$tag}%";
  832. }
  833. }
  834. if ($filter->getInurl()) {
  835. foreach ($filter->getInurl() as $url) {
  836. $sub_search .= 'AND ' . static::sqlConcat($alias . 'link', $alias . 'guid') . ' LIKE ? ';
  837. $values[] = "%{$url}%";
  838. }
  839. }
  840. if ($filter->getNotAuthor()) {
  841. foreach ($filter->getNotAuthor() as $author) {
  842. $sub_search .= 'AND (NOT ' . $alias . 'author LIKE ?) ';
  843. $values[] = "%{$author}%";
  844. }
  845. }
  846. if ($filter->getNotIntitle()) {
  847. foreach ($filter->getNotIntitle() as $title) {
  848. $sub_search .= 'AND (NOT ' . $alias . 'title LIKE ?) ';
  849. $values[] = "%{$title}%";
  850. }
  851. }
  852. if ($filter->getNotTags()) {
  853. foreach ($filter->getNotTags() as $tag) {
  854. $sub_search .= 'AND (NOT ' . $alias . 'tags LIKE ?) ';
  855. $values[] = "%{$tag}%";
  856. }
  857. }
  858. if ($filter->getNotInurl()) {
  859. foreach ($filter->getNotInurl() as $url) {
  860. $sub_search .= 'AND (NOT ' . static::sqlConcat($alias . 'link', $alias . 'guid') . ' LIKE ?) ';
  861. $values[] = "%{$url}%";
  862. }
  863. }
  864. if ($filter->getSearch()) {
  865. foreach ($filter->getSearch() as $search_value) {
  866. $sub_search .= 'AND ' . static::sqlConcat($alias . 'title',
  867. static::isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ? ';
  868. $values[] = "%{$search_value}%";
  869. }
  870. }
  871. if ($filter->getNotSearch()) {
  872. foreach ($filter->getNotSearch() as $search_value) {
  873. $sub_search .= 'AND (NOT ' . static::sqlConcat($alias . 'title',
  874. static::isCompressed() ? 'UNCOMPRESS(' . $alias . 'content_bin)' : '' . $alias . 'content') . ' LIKE ?) ';
  875. $values[] = "%{$search_value}%";
  876. }
  877. }
  878. if ($sub_search != '') {
  879. if ($isOpen) {
  880. $search .= ' OR ';
  881. } else {
  882. $isOpen = true;
  883. }
  884. // Remove superfluous leading 'AND '
  885. $search .= '(' . substr($sub_search, 4) . ')';
  886. }
  887. }
  888. return [ $values, $search ];
  889. }
  890. /** @param FreshRSS_BooleanSearch|null $filters */
  891. protected function sqlListEntriesWhere(string $alias = '', $filters = null, int $state = FreshRSS_Entry::STATE_ALL,
  892. string $order = 'DESC', string $firstId = '', int $date_min = 0) {
  893. $search = ' ';
  894. $values = array();
  895. if ($state & FreshRSS_Entry::STATE_NOT_READ) {
  896. if (!($state & FreshRSS_Entry::STATE_READ)) {
  897. $search .= 'AND ' . $alias . 'is_read=0 ';
  898. }
  899. } elseif ($state & FreshRSS_Entry::STATE_READ) {
  900. $search .= 'AND ' . $alias . 'is_read=1 ';
  901. }
  902. if ($state & FreshRSS_Entry::STATE_FAVORITE) {
  903. if (!($state & FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  904. $search .= 'AND ' . $alias . 'is_favorite=1 ';
  905. }
  906. } elseif ($state & FreshRSS_Entry::STATE_NOT_FAVORITE) {
  907. $search .= 'AND ' . $alias . 'is_favorite=0 ';
  908. }
  909. switch ($order) {
  910. case 'DESC':
  911. case 'ASC':
  912. break;
  913. default:
  914. throw new FreshRSS_EntriesGetter_Exception('Bad order in Entry->listByType: [' . $order . ']!');
  915. }
  916. if ($firstId !== '') {
  917. $search .= 'AND ' . $alias . 'id ' . ($order === 'DESC' ? '<=' : '>=') . ' ? ';
  918. $values[] = $firstId;
  919. }
  920. if ($date_min > 0) {
  921. $search .= 'AND ' . $alias . 'id >= ? ';
  922. $values[] = $date_min . '000000';
  923. }
  924. if ($filters && count($filters->searches()) > 0) {
  925. list($filterValues, $filterSearch) = self::sqlBooleanSearch($alias, $filters);
  926. $filterSearch = trim($filterSearch);
  927. if ($filterSearch !== '') {
  928. $search .= 'AND (' . $filterSearch . ') ';
  929. $values = array_merge($values, $filterValues);
  930. }
  931. }
  932. return array($values, $search);
  933. }
  934. private function sqlListWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL,
  935. $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  936. if (!$state) {
  937. $state = FreshRSS_Entry::STATE_ALL;
  938. }
  939. $where = '';
  940. $joinFeed = false;
  941. $values = array();
  942. switch ($type) {
  943. case 'a': //All PRIORITY_MAIN_STREAM
  944. $where .= 'f.priority > ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  945. break;
  946. case 'A': //All except PRIORITY_ARCHIVED
  947. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  948. break;
  949. case 's': //Starred. Deprecated: use $state instead
  950. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  951. $where .= 'AND e.is_favorite=1 ';
  952. break;
  953. case 'S': //Starred
  954. $where .= 'e.is_favorite=1 ';
  955. break;
  956. case 'c': //Category
  957. $where .= 'f.priority >= ' . FreshRSS_Feed::PRIORITY_NORMAL . ' ';
  958. $where .= 'AND f.category=? ';
  959. $values[] = intval($id);
  960. break;
  961. case 'f': //Feed
  962. $where .= 'e.id_feed=? ';
  963. $values[] = intval($id);
  964. break;
  965. case 't': //Tag (label)
  966. $where .= 'et.id_tag=? ';
  967. $values[] = intval($id);
  968. break;
  969. case 'T': //Any tag (label)
  970. $where .= '1=1 ';
  971. break;
  972. case 'ST': //Starred or tagged (label)
  973. $where .= 'e.is_favorite=1 OR EXISTS (SELECT et2.id_tag FROM `_entrytag` et2 WHERE et2.id_entry = e.id) ';
  974. break;
  975. default:
  976. throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
  977. }
  978. list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state, $order, $firstId, $date_min);
  979. return array(array_merge($values, $searchValues),
  980. 'SELECT '
  981. . ($type === 'T' ? 'DISTINCT ' : '')
  982. . 'e.id FROM `_entry` e '
  983. . 'INNER JOIN `_feed` f ON e.id_feed = f.id '
  984. . ($type === 't' || $type === 'T' ? 'INNER JOIN `_entrytag` et ON et.id_entry = e.id ' : '')
  985. . 'WHERE ' . $where
  986. . $search
  987. . 'ORDER BY e.id ' . $order
  988. . ($limit > 0 ? ' LIMIT ' . intval($limit) : '')); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
  989. }
  990. public function listWhereRaw($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL,
  991. $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  992. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  993. $sql = 'SELECT e0.id, e0.guid, e0.title, e0.author, '
  994. . (static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  995. . ', e0.link, e0.date, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags '
  996. . 'FROM `_entry` e0 '
  997. . 'INNER JOIN ('
  998. . $sql
  999. . ') e2 ON e2.id=e0.id '
  1000. . 'ORDER BY e0.id ' . $order;
  1001. $stm = $this->pdo->prepare($sql);
  1002. if ($stm && $stm->execute($values)) {
  1003. return $stm;
  1004. } else {
  1005. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  1006. Minz_Log::error('SQL error listWhereRaw: ' . $info[2]);
  1007. return false;
  1008. }
  1009. }
  1010. public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL,
  1011. $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) {
  1012. $stm = $this->listWhereRaw($type, $id, $state, $order, $limit, $firstId, $filters, $date_min);
  1013. if ($stm) {
  1014. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  1015. yield FreshRSS_Entry::fromArray($row);
  1016. }
  1017. } else {
  1018. yield false;
  1019. }
  1020. }
  1021. public function listByIds($ids, $order = 'DESC') {
  1022. if (count($ids) < 1) {
  1023. yield false;
  1024. } elseif (count($ids) > FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER) {
  1025. // Split a query with too many variables parameters
  1026. $idsChunks = array_chunk($ids, FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER);
  1027. foreach ($idsChunks as $idsChunk) {
  1028. foreach ($this->listByIds($idsChunk, $order) as $entry) {
  1029. yield $entry;
  1030. }
  1031. }
  1032. return;
  1033. }
  1034. $sql = 'SELECT id, guid, title, author, '
  1035. . (static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
  1036. . ', link, date, is_read, is_favorite, id_feed, tags '
  1037. . 'FROM `_entry` '
  1038. . 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?) '
  1039. . 'ORDER BY id ' . $order;
  1040. $stm = $this->pdo->prepare($sql);
  1041. $stm->execute($ids);
  1042. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  1043. yield FreshRSS_Entry::fromArray($row);
  1044. }
  1045. }
  1046. /**
  1047. * For API
  1048. */
  1049. public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL,
  1050. $order = 'DESC', $limit = 1, $firstId = '', $filters = null) {
  1051. list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters);
  1052. $stm = $this->pdo->prepare($sql);
  1053. $stm->execute($values);
  1054. return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  1055. }
  1056. public function listHashForFeedGuids($id_feed, $guids) {
  1057. $result = [];
  1058. if (count($guids) < 1) {
  1059. return $result;
  1060. } elseif (count($guids) > FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER) {
  1061. // Split a query with too many variables parameters
  1062. $guidsChunks = array_chunk($guids, FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER);
  1063. foreach ($guidsChunks as $guidsChunk) {
  1064. $result += $this->listHashForFeedGuids($id_feed, $guidsChunk);
  1065. }
  1066. return $result;
  1067. }
  1068. $guids = array_unique($guids);
  1069. $sql = 'SELECT guid, ' . static::sqlHexEncode('hash') .
  1070. ' AS hex_hash FROM `_entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  1071. $stm = $this->pdo->prepare($sql);
  1072. $values = array($id_feed);
  1073. $values = array_merge($values, $guids);
  1074. if ($stm && $stm->execute($values)) {
  1075. $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
  1076. foreach ($rows as $row) {
  1077. $result[$row['guid']] = $row['hex_hash'];
  1078. }
  1079. return $result;
  1080. } else {
  1081. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  1082. if ($this->autoUpdateDb($info)) {
  1083. return $this->listHashForFeedGuids($id_feed, $guids);
  1084. }
  1085. Minz_Log::error('SQL error listHashForFeedGuids: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  1086. . ' while querying feed ' . $id_feed);
  1087. return false;
  1088. }
  1089. }
  1090. /**
  1091. * @param int $id_feed
  1092. * @param array<string> $guids
  1093. * @param int $mtime
  1094. * @return int|false The number of affected feeds, or false if error
  1095. */
  1096. public function updateLastSeen($id_feed, $guids, $mtime = 0) {
  1097. if (count($guids) < 1) {
  1098. return 0;
  1099. } elseif (count($guids) > FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER) {
  1100. // Split a query with too many variables parameters
  1101. $affected = 0;
  1102. $guidsChunks = array_chunk($guids, FreshRSS_DatabaseDAO::MAX_VARIABLE_NUMBER);
  1103. foreach ($guidsChunks as $guidsChunk) {
  1104. $affected += $this->updateLastSeen($id_feed, $guidsChunk, $mtime);
  1105. }
  1106. return $affected;
  1107. }
  1108. $sql = 'UPDATE `_entry` SET `lastSeen`=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
  1109. $stm = $this->pdo->prepare($sql);
  1110. if ($mtime <= 0) {
  1111. $mtime = time();
  1112. }
  1113. $values = array($mtime, $id_feed);
  1114. $values = array_merge($values, $guids);
  1115. if ($stm && $stm->execute($values)) {
  1116. return $stm->rowCount();
  1117. } else {
  1118. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  1119. if ($this->autoUpdateDb($info)) {
  1120. return $this->updateLastSeen($id_feed, $guids);
  1121. }
  1122. Minz_Log::error('SQL error updateLastSeen: ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
  1123. . ' while updating feed ' . $id_feed);
  1124. return false;
  1125. }
  1126. }
  1127. public function countUnreadRead() {
  1128. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE f.priority > 0'
  1129. . ' UNION SELECT COUNT(e.id) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE f.priority > 0 AND e.is_read=0';
  1130. $stm = $this->pdo->query($sql);
  1131. if ($stm === false) {
  1132. return false;
  1133. }
  1134. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  1135. rsort($res);
  1136. $all = empty($res[0]) ? 0 : intval($res[0]);
  1137. $unread = empty($res[1]) ? 0 : intval($res[1]);
  1138. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  1139. }
  1140. public function count($minPriority = null) {
  1141. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
  1142. if ($minPriority !== null) {
  1143. $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
  1144. $sql .= ' WHERE f.priority > ' . intval($minPriority);
  1145. }
  1146. $stm = $this->pdo->query($sql);
  1147. if ($stm == false) {
  1148. return false;
  1149. }
  1150. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  1151. return isset($res[0]) ? intval($res[0]) : 0;
  1152. }
  1153. public function countNotRead($minPriority = null) {
  1154. $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
  1155. if ($minPriority !== null) {
  1156. $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
  1157. }
  1158. $sql .= ' WHERE e.is_read=0';
  1159. if ($minPriority !== null) {
  1160. $sql .= ' AND f.priority > ' . intval($minPriority);
  1161. }
  1162. $stm = $this->pdo->query($sql);
  1163. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  1164. return isset($res[0]) ? intval($res[0]) : 0;
  1165. }
  1166. public function countUnreadReadFavorites() {
  1167. $sql = <<<'SQL'
  1168. SELECT c FROM (
  1169. SELECT COUNT(e1.id) AS c, 1 AS o
  1170. FROM `_entry` AS e1
  1171. JOIN `_feed` AS f1 ON e1.id_feed = f1.id
  1172. WHERE e1.is_favorite = 1
  1173. AND f1.priority >= :priority_normal1
  1174. UNION
  1175. SELECT COUNT(e2.id) AS c, 2 AS o
  1176. FROM `_entry` AS e2
  1177. JOIN `_feed` AS f2 ON e2.id_feed = f2.id
  1178. WHERE e2.is_favorite = 1
  1179. AND e2.is_read = 0
  1180. AND f2.priority >= :priority_normal2
  1181. ) u
  1182. ORDER BY o
  1183. SQL;
  1184. $stm = $this->pdo->prepare($sql);
  1185. if (!$stm) {
  1186. Minz_Log::error('SQL error in ' . __method__ . ' ' . json_encode($this->pdo->errorInfo()));
  1187. return false;
  1188. }
  1189. //Binding a value more than once is not standard and does not work with native prepared statements (e.g. MySQL) https://bugs.php.net/bug.php?id=40417
  1190. $stm->bindValue(':priority_normal1', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
  1191. $stm->bindValue(':priority_normal2', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
  1192. $stm->execute();
  1193. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  1194. rsort($res);
  1195. $all = empty($res[0]) ? 0 : intval($res[0]);
  1196. $unread = empty($res[1]) ? 0 : intval($res[1]);
  1197. return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
  1198. }
  1199. }