TagDAO.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
  3. public function sqlIgnore() {
  4. return 'IGNORE';
  5. }
  6. public function createTagTable() {
  7. $ok = false;
  8. $hadTransaction = $this->pdo->inTransaction();
  9. if ($hadTransaction) {
  10. $this->pdo->commit();
  11. }
  12. try {
  13. require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
  14. Minz_Log::warning('SQL ALTER GUID case sensitivity...');
  15. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  16. $databaseDAO->ensureCaseInsensitiveGuids();
  17. Minz_Log::warning('SQL CREATE TABLE tag...');
  18. $ok = $this->pdo->exec($SQL_CREATE_TABLE_TAGS) !== false;
  19. } catch (Exception $e) {
  20. Minz_Log::error('FreshRSS_EntryDAO::createTagTable error: ' . $e->getMessage());
  21. }
  22. if ($hadTransaction) {
  23. $this->pdo->beginTransaction();
  24. }
  25. return $ok;
  26. }
  27. protected function autoUpdateDb($errorInfo) {
  28. if (isset($errorInfo[0])) {
  29. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_TABLE_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_TABLE) {
  30. if (stripos($errorInfo[2], 'tag') !== false) {
  31. return $this->createTagTable(); //v1.12.0
  32. }
  33. }
  34. }
  35. return false;
  36. }
  37. public function addTag($valuesTmp) {
  38. $sql = 'INSERT INTO `_tag`(name, attributes) '
  39. . 'SELECT * FROM (SELECT TRIM(?) as name, TRIM(?) as attributes) t2 ' //TRIM() gives a text type hint to PostgreSQL
  40. . 'WHERE NOT EXISTS (SELECT 1 FROM `_category` WHERE name = TRIM(?))'; //No category of the same name
  41. $stm = $this->pdo->prepare($sql);
  42. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, 63, 'UTF-8');
  43. if (!isset($valuesTmp['attributes'])) {
  44. $valuesTmp['attributes'] = [];
  45. }
  46. $values = array(
  47. $valuesTmp['name'],
  48. is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES),
  49. $valuesTmp['name'],
  50. );
  51. if ($stm && $stm->execute($values)) {
  52. return $this->pdo->lastInsertId('`_tag_id_seq`');
  53. } else {
  54. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  55. Minz_Log::error('SQL error addTag: ' . $info[2]);
  56. return false;
  57. }
  58. }
  59. public function addTagObject($tag) {
  60. $tag = $this->searchByName($tag->name());
  61. if (!$tag) {
  62. $values = array(
  63. 'name' => $tag->name(),
  64. 'attributes' => $tag->attributes(),
  65. );
  66. return $this->addTag($values);
  67. }
  68. return $tag->id();
  69. }
  70. public function updateTag($id, $valuesTmp) {
  71. $sql = 'UPDATE `_tag` SET name=?, attributes=? WHERE id=? '
  72. . 'AND NOT EXISTS (SELECT 1 FROM `_category` WHERE name = ?)'; //No category of the same name
  73. $stm = $this->pdo->prepare($sql);
  74. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, 63, 'UTF-8');
  75. if (!isset($valuesTmp['attributes'])) {
  76. $valuesTmp['attributes'] = [];
  77. }
  78. $values = array(
  79. $valuesTmp['name'],
  80. is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES),
  81. $id,
  82. $valuesTmp['name'],
  83. );
  84. if ($stm && $stm->execute($values)) {
  85. return $stm->rowCount();
  86. } else {
  87. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  88. Minz_Log::error('SQL error updateTag: ' . $info[2]);
  89. return false;
  90. }
  91. }
  92. public function updateTagAttribute($tag, $key, $value) {
  93. if ($tag instanceof FreshRSS_Tag) {
  94. $tag->_attributes($key, $value);
  95. return $this->updateTag(
  96. $tag->id(),
  97. [ 'attributes' => $tag->attributes() ]
  98. );
  99. }
  100. return false;
  101. }
  102. public function deleteTag($id) {
  103. if ($id <= 0) {
  104. return false;
  105. }
  106. $sql = 'DELETE FROM `_tag` WHERE id=?';
  107. $stm = $this->pdo->prepare($sql);
  108. $values = array($id);
  109. if ($stm && $stm->execute($values)) {
  110. return $stm->rowCount();
  111. } else {
  112. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  113. Minz_Log::error('SQL error deleteTag: ' . $info[2]);
  114. return false;
  115. }
  116. }
  117. public function selectAll() {
  118. $sql = 'SELECT id, name, attributes FROM `_tag`';
  119. $stm = $this->pdo->query($sql);
  120. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  121. yield $row;
  122. }
  123. }
  124. public function selectEntryTag() {
  125. $sql = 'SELECT id_tag, id_entry FROM `_entrytag`';
  126. $stm = $this->pdo->query($sql);
  127. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  128. yield $row;
  129. }
  130. }
  131. public function updateEntryTag($oldTagId, $newTagId) {
  132. $sql = 'DELETE FROM `_entrytag` WHERE EXISTS (SELECT 1 FROM `_entrytag` AS e WHERE e.id_entry = `_entrytag`.id_entry AND e.id_tag = ? AND `_entrytag`.id_tag = ?)';
  133. $stm = $this->pdo->prepare($sql);
  134. if (!($stm && $stm->execute([$newTagId, $oldTagId]))) {
  135. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  136. Minz_Log::error('SQL error moveTag: ' . $info[2]);
  137. return false;
  138. }
  139. $sql = 'UPDATE `_entrytag` SET id_tag = ? WHERE id_tag = ?';
  140. $stm = $this->pdo->prepare($sql);
  141. if ($stm && $stm->execute([$newTagId, $oldTagId])) {
  142. return $stm->rowCount();
  143. } else {
  144. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  145. Minz_Log::error('SQL error moveTag: ' . $info[2]);
  146. return false;
  147. }
  148. }
  149. public function searchById($id) {
  150. $sql = 'SELECT * FROM `_tag` WHERE id=?';
  151. $stm = $this->pdo->prepare($sql);
  152. $values = array($id);
  153. $stm->execute($values);
  154. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  155. $tag = self::daoToTag($res);
  156. return isset($tag[0]) ? $tag[0] : null;
  157. }
  158. public function searchByName($name) {
  159. $sql = 'SELECT * FROM `_tag` WHERE name=?';
  160. $stm = $this->pdo->prepare($sql);
  161. $values = array($name);
  162. $stm->execute($values);
  163. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  164. $tag = self::daoToTag($res);
  165. return isset($tag[0]) ? $tag[0] : null;
  166. }
  167. public function listTags($precounts = false) {
  168. if ($precounts) {
  169. $sql = 'SELECT t.id, t.name, count(e.id) AS unreads '
  170. . 'FROM `_tag` t '
  171. . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id '
  172. . 'LEFT OUTER JOIN `_entry` e ON et.id_entry = e.id AND e.is_read = 0 '
  173. . 'GROUP BY t.id '
  174. . 'ORDER BY t.name';
  175. } else {
  176. $sql = 'SELECT * FROM `_tag` ORDER BY name';
  177. }
  178. $stm = $this->pdo->query($sql);
  179. if ($stm !== false) {
  180. return self::daoToTag($stm->fetchAll(PDO::FETCH_ASSOC));
  181. } else {
  182. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  183. if ($this->autoUpdateDb($info)) {
  184. return $this->listTags($precounts);
  185. }
  186. Minz_Log::error('SQL error listTags: ' . $info[2]);
  187. return false;
  188. }
  189. }
  190. public function listTagsNewestItemUsec($id_tag = null) {
  191. $sql = 'SELECT t.id AS id_tag, MAX(e.id) AS newest_item_us '
  192. . 'FROM `_tag` t '
  193. . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id '
  194. . 'LEFT OUTER JOIN `_entry` e ON et.id_entry = e.id ';
  195. if ($id_tag === null) {
  196. $sql .= 'GROUP BY t.id';
  197. } else {
  198. $sql .= 'WHERE t.id=' . intval($id_tag);
  199. }
  200. $stm = $this->pdo->query($sql);
  201. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  202. $newestItemUsec = [];
  203. foreach ($res as $line) {
  204. $newestItemUsec['t_' . $line['id_tag']] = $line['newest_item_us'];
  205. }
  206. return $newestItemUsec;
  207. }
  208. public function count() {
  209. $sql = 'SELECT COUNT(*) AS count FROM `_tag`';
  210. $stm = $this->pdo->query($sql);
  211. if ($stm !== false) {
  212. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  213. return $res[0]['count'];
  214. } else {
  215. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  216. if ($this->autoUpdateDb($info)) {
  217. return $this->count();
  218. }
  219. Minz_Log::error('SQL error TagDAO::count: ' . $info[2]);
  220. return false;
  221. }
  222. }
  223. public function countEntries($id) {
  224. $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` WHERE id_tag=?';
  225. $stm = $this->pdo->prepare($sql);
  226. $values = array($id);
  227. $stm->execute($values);
  228. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  229. return $res[0]['count'];
  230. }
  231. public function countNotRead($id) {
  232. $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` et '
  233. . 'INNER JOIN `_entry` e ON et.id_entry=e.id '
  234. . 'WHERE et.id_tag=? AND e.is_read=0';
  235. $stm = $this->pdo->prepare($sql);
  236. $values = array($id);
  237. $stm->execute($values);
  238. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  239. return $res[0]['count'];
  240. }
  241. public function tagEntry($id_tag, $id_entry, $checked = true) {
  242. if ($checked) {
  243. $sql = 'INSERT ' . $this->sqlIgnore() . ' INTO `_entrytag`(id_tag, id_entry) VALUES(?, ?)';
  244. } else {
  245. $sql = 'DELETE FROM `_entrytag` WHERE id_tag=? AND id_entry=?';
  246. }
  247. $stm = $this->pdo->prepare($sql);
  248. $values = array($id_tag, $id_entry);
  249. if ($stm && $stm->execute($values)) {
  250. return true;
  251. } else {
  252. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  253. Minz_Log::error('SQL error tagEntry: ' . $info[2]);
  254. return false;
  255. }
  256. }
  257. public function getTagsForEntry($id_entry) {
  258. $sql = 'SELECT t.id, t.name, et.id_entry IS NOT NULL as checked '
  259. . 'FROM `_tag` t '
  260. . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id AND et.id_entry=? '
  261. . 'ORDER BY t.name';
  262. $stm = $this->pdo->prepare($sql);
  263. $values = array($id_entry);
  264. if ($stm && $stm->execute($values)) {
  265. $lines = $stm->fetchAll(PDO::FETCH_ASSOC);
  266. for ($i = count($lines) - 1; $i >= 0; $i--) {
  267. $lines[$i]['id'] = intval($lines[$i]['id']);
  268. $lines[$i]['checked'] = !empty($lines[$i]['checked']);
  269. }
  270. return $lines;
  271. } else {
  272. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  273. if ($this->autoUpdateDb($info)) {
  274. return $this->getTagsForEntry($id_entry);
  275. }
  276. Minz_Log::error('SQL error getTagsForEntry: ' . $info[2]);
  277. return false;
  278. }
  279. }
  280. public function getTagsForEntries($entries) {
  281. $sql = 'SELECT et.id_entry, et.id_tag, t.name '
  282. . 'FROM `_tag` t '
  283. . 'INNER JOIN `_entrytag` et ON et.id_tag = t.id';
  284. $values = array();
  285. if (is_array($entries) && count($entries) > 0) {
  286. $sql .= ' AND et.id_entry IN (' . str_repeat('?,', count($entries) - 1). '?)';
  287. if (is_array($entries[0])) {
  288. foreach ($entries as $entry) {
  289. $values[] = $entry['id'];
  290. }
  291. } elseif (is_object($entries[0])) {
  292. foreach ($entries as $entry) {
  293. $values[] = $entry->id();
  294. }
  295. } else {
  296. foreach ($entries as $entry) {
  297. $values[] = $entry;
  298. }
  299. }
  300. }
  301. $stm = $this->pdo->prepare($sql);
  302. if ($stm && $stm->execute($values)) {
  303. return $stm->fetchAll(PDO::FETCH_ASSOC);
  304. } else {
  305. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  306. if ($this->autoUpdateDb($info)) {
  307. return $this->getTagsForEntries($entries);
  308. }
  309. Minz_Log::error('SQL error getTagsForEntries: ' . $info[2]);
  310. return false;
  311. }
  312. }
  313. //For API
  314. public function getEntryIdsTagNames($entries) {
  315. $result = array();
  316. foreach ($this->getTagsForEntries($entries) as $line) {
  317. $entryId = 'e_' . $line['id_entry'];
  318. $tagName = $line['name'];
  319. if (empty($result[$entryId])) {
  320. $result[$entryId] = array();
  321. }
  322. $result[$entryId][] = $tagName;
  323. }
  324. return $result;
  325. }
  326. public static function daoToTag($listDAO) {
  327. $list = array();
  328. if (!is_array($listDAO)) {
  329. $listDAO = array($listDAO);
  330. }
  331. foreach ($listDAO as $key => $dao) {
  332. $tag = new FreshRSS_Tag(
  333. $dao['name']
  334. );
  335. $tag->_id($dao['id']);
  336. if (!empty($dao['attributes'])) {
  337. $tag->_attributes('', $dao['attributes']);
  338. }
  339. if (isset($dao['unreads'])) {
  340. $tag->_nbUnread($dao['unreads']);
  341. }
  342. $list[$key] = $tag;
  343. }
  344. return $list;
  345. }
  346. }