TagDAO.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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_once(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. $values = array(
  44. $valuesTmp['name'],
  45. isset($valuesTmp['attributes']) ? json_encode($valuesTmp['attributes']) : '',
  46. $valuesTmp['name'],
  47. );
  48. if ($stm && $stm->execute($values)) {
  49. return $this->pdo->lastInsertId('`_tag_id_seq`');
  50. } else {
  51. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  52. Minz_Log::error('SQL error addTag: ' . $info[2]);
  53. return false;
  54. }
  55. }
  56. public function addTagObject($tag) {
  57. $tag = $this->searchByName($tag->name());
  58. if (!$tag) {
  59. $values = array(
  60. 'name' => $tag->name(),
  61. 'attributes' => $tag->attributes(),
  62. );
  63. return $this->addTag($values);
  64. }
  65. return $tag->id();
  66. }
  67. public function updateTag($id, $valuesTmp) {
  68. $sql = 'UPDATE `_tag` SET name=?, attributes=? WHERE id=? '
  69. . 'AND NOT EXISTS (SELECT 1 FROM `_category` WHERE name = ?)'; //No category of the same name
  70. $stm = $this->pdo->prepare($sql);
  71. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, 63, 'UTF-8');
  72. $values = array(
  73. $valuesTmp['name'],
  74. isset($valuesTmp['attributes']) ? json_encode($valuesTmp['attributes']) : '',
  75. $id,
  76. $valuesTmp['name'],
  77. );
  78. if ($stm && $stm->execute($values)) {
  79. return $stm->rowCount();
  80. } else {
  81. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  82. Minz_Log::error('SQL error updateTag: ' . $info[2]);
  83. return false;
  84. }
  85. }
  86. public function updateTagAttribute($tag, $key, $value) {
  87. if ($tag instanceof FreshRSS_Tag) {
  88. $tag->_attributes($key, $value);
  89. return $this->updateFeed(
  90. $tag->id(),
  91. array('attributes' => $feed->attributes())
  92. );
  93. }
  94. return false;
  95. }
  96. public function deleteTag($id) {
  97. if ($id <= 0) {
  98. return false;
  99. }
  100. $sql = 'DELETE FROM `_tag` WHERE id=?';
  101. $stm = $this->pdo->prepare($sql);
  102. $values = array($id);
  103. if ($stm && $stm->execute($values)) {
  104. return $stm->rowCount();
  105. } else {
  106. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  107. Minz_Log::error('SQL error deleteTag: ' . $info[2]);
  108. return false;
  109. }
  110. }
  111. public function selectAll() {
  112. $sql = 'SELECT id, name, attributes FROM `_tag`';
  113. $stm = $this->pdo->query($sql);
  114. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  115. yield $row;
  116. }
  117. }
  118. public function selectEntryTag() {
  119. $sql = 'SELECT id_tag, id_entry FROM `_entrytag`';
  120. $stm = $this->pdo->query($sql);
  121. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  122. yield $row;
  123. }
  124. }
  125. public function searchById($id) {
  126. $sql = 'SELECT * FROM `_tag` WHERE id=?';
  127. $stm = $this->pdo->prepare($sql);
  128. $values = array($id);
  129. $stm->execute($values);
  130. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  131. $tag = self::daoToTag($res);
  132. return isset($tag[0]) ? $tag[0] : null;
  133. }
  134. public function searchByName($name) {
  135. $sql = 'SELECT * FROM `_tag` WHERE name=?';
  136. $stm = $this->pdo->prepare($sql);
  137. $values = array($name);
  138. $stm->execute($values);
  139. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  140. $tag = self::daoToTag($res);
  141. return isset($tag[0]) ? $tag[0] : null;
  142. }
  143. public function listTags($precounts = false) {
  144. if ($precounts) {
  145. $sql = 'SELECT t.id, t.name, count(e.id) AS unreads '
  146. . 'FROM `_tag` t '
  147. . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id '
  148. . 'LEFT OUTER JOIN `_entry` e ON et.id_entry = e.id AND e.is_read = 0 '
  149. . 'GROUP BY t.id '
  150. . 'ORDER BY t.name';
  151. } else {
  152. $sql = 'SELECT * FROM `_tag` ORDER BY name';
  153. }
  154. $stm = $this->pdo->query($sql);
  155. if ($stm !== false) {
  156. return self::daoToTag($stm->fetchAll(PDO::FETCH_ASSOC));
  157. } else {
  158. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  159. if ($this->autoUpdateDb($info)) {
  160. return $this->listTags($precounts);
  161. }
  162. Minz_Log::error('SQL error listTags: ' . $info[2]);
  163. return false;
  164. }
  165. }
  166. public function count() {
  167. $sql = 'SELECT COUNT(*) AS count FROM `_tag`';
  168. $stm = $this->pdo->query($sql);
  169. if ($stm !== false) {
  170. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  171. return $res[0]['count'];
  172. } else {
  173. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  174. if ($this->autoUpdateDb($info)) {
  175. return $this->count();
  176. }
  177. Minz_Log::error('SQL error TagDAO::count: ' . $info[2]);
  178. return false;
  179. }
  180. }
  181. public function countEntries($id) {
  182. $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` WHERE id_tag=?';
  183. $stm = $this->pdo->prepare($sql);
  184. $values = array($id);
  185. $stm->execute($values);
  186. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  187. return $res[0]['count'];
  188. }
  189. public function countNotRead($id) {
  190. $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` et '
  191. . 'INNER JOIN `_entry` e ON et.id_entry=e.id '
  192. . 'WHERE et.id_tag=? AND e.is_read=0';
  193. $stm = $this->pdo->prepare($sql);
  194. $values = array($id);
  195. $stm->execute($values);
  196. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  197. return $res[0]['count'];
  198. }
  199. public function tagEntry($id_tag, $id_entry, $checked = true) {
  200. if ($checked) {
  201. $sql = 'INSERT ' . $this->sqlIgnore() . ' INTO `_entrytag`(id_tag, id_entry) VALUES(?, ?)';
  202. } else {
  203. $sql = 'DELETE FROM `_entrytag` WHERE id_tag=? AND id_entry=?';
  204. }
  205. $stm = $this->pdo->prepare($sql);
  206. $values = array($id_tag, $id_entry);
  207. if ($stm && $stm->execute($values)) {
  208. return true;
  209. } else {
  210. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  211. Minz_Log::error('SQL error tagEntry: ' . $info[2]);
  212. return false;
  213. }
  214. }
  215. public function getTagsForEntry($id_entry) {
  216. $sql = 'SELECT t.id, t.name, et.id_entry IS NOT NULL as checked '
  217. . 'FROM `_tag` t '
  218. . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id AND et.id_entry=? '
  219. . 'ORDER BY t.name';
  220. $stm = $this->pdo->prepare($sql);
  221. $values = array($id_entry);
  222. if ($stm && $stm->execute($values)) {
  223. $lines = $stm->fetchAll(PDO::FETCH_ASSOC);
  224. for ($i = count($lines) - 1; $i >= 0; $i--) {
  225. $lines[$i]['id'] = intval($lines[$i]['id']);
  226. $lines[$i]['checked'] = !empty($lines[$i]['checked']);
  227. }
  228. return $lines;
  229. } else {
  230. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  231. if ($this->autoUpdateDb($info)) {
  232. return $this->getTagsForEntry($id_entry);
  233. }
  234. Minz_Log::error('SQL error getTagsForEntry: ' . $info[2]);
  235. return false;
  236. }
  237. }
  238. public function getTagsForEntries($entries) {
  239. $sql = 'SELECT et.id_entry, et.id_tag, t.name '
  240. . 'FROM `_tag` t '
  241. . 'INNER JOIN `_entrytag` et ON et.id_tag = t.id';
  242. $values = array();
  243. if (is_array($entries) && count($entries) > 0) {
  244. $sql .= ' AND et.id_entry IN (' . str_repeat('?,', count($entries) - 1). '?)';
  245. if (is_array($entries[0])) {
  246. foreach ($entries as $entry) {
  247. $values[] = $entry['id'];
  248. }
  249. } elseif (is_object($entries[0])) {
  250. foreach ($entries as $entry) {
  251. $values[] = $entry->id();
  252. }
  253. } else {
  254. foreach ($entries as $entry) {
  255. $values[] = $entry;
  256. }
  257. }
  258. }
  259. $stm = $this->pdo->prepare($sql);
  260. if ($stm && $stm->execute($values)) {
  261. return $stm->fetchAll(PDO::FETCH_ASSOC);
  262. } else {
  263. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  264. if ($this->autoUpdateDb($info)) {
  265. return $this->getTagsForEntries($entries);
  266. }
  267. Minz_Log::error('SQL error getTagsForEntries: ' . $info[2]);
  268. return false;
  269. }
  270. }
  271. //For API
  272. public function getEntryIdsTagNames($entries) {
  273. $result = array();
  274. foreach ($this->getTagsForEntries($entries) as $line) {
  275. $entryId = 'e_' . $line['id_entry'];
  276. $tagName = $line['name'];
  277. if (empty($result[$entryId])) {
  278. $result[$entryId] = array();
  279. }
  280. $result[$entryId][] = $tagName;
  281. }
  282. return $result;
  283. }
  284. public static function daoToTag($listDAO) {
  285. $list = array();
  286. if (!is_array($listDAO)) {
  287. $listDAO = array($listDAO);
  288. }
  289. foreach ($listDAO as $key => $dao) {
  290. $tag = new FreshRSS_Tag(
  291. $dao['name']
  292. );
  293. $tag->_id($dao['id']);
  294. if (!empty($dao['attributes'])) {
  295. $tag->_attributes('', $dao['attributes']);
  296. }
  297. if (isset($dao['unreads'])) {
  298. $tag->_nbUnread($dao['unreads']);
  299. }
  300. $list[$key] = $tag;
  301. }
  302. return $list;
  303. }
  304. }