TagDAO.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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->bd->inTransaction();
  9. if ($hadTransaction) {
  10. $this->bd->commit();
  11. }
  12. try {
  13. $db = FreshRSS_Context::$system_conf->db;
  14. require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
  15. Minz_Log::warning('SQL CREATE TABLE tag...');
  16. if (defined('SQL_CREATE_TABLE_TAGS')) {
  17. $sql = sprintf(SQL_CREATE_TABLE_TAGS, $this->prefix);
  18. $stm = $this->bd->prepare($sql);
  19. $ok = $stm && $stm->execute();
  20. } else {
  21. global $SQL_CREATE_TABLE_TAGS;
  22. $ok = !empty($SQL_CREATE_TABLE_TAGS);
  23. foreach ($SQL_CREATE_TABLE_TAGS as $instruction) {
  24. $sql = sprintf($instruction, $this->prefix);
  25. $stm = $this->bd->prepare($sql);
  26. $ok &= $stm && $stm->execute();
  27. }
  28. }
  29. } catch (Exception $e) {
  30. Minz_Log::error('FreshRSS_EntryDAO::createTagTable error: ' . $e->getMessage());
  31. }
  32. if ($hadTransaction) {
  33. $this->bd->beginTransaction();
  34. }
  35. return $ok;
  36. }
  37. protected function autoUpdateDb($errorInfo) {
  38. if (isset($errorInfo[0])) {
  39. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_TABLE_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_TABLE) {
  40. if (stripos($errorInfo[2], 'tag') !== false) {
  41. return $this->createTagTable(); //v1.12.0
  42. }
  43. }
  44. }
  45. return false;
  46. }
  47. public function addTag($valuesTmp) {
  48. $sql = 'INSERT INTO `' . $this->prefix . 'tag`(name, attributes) '
  49. . 'SELECT * FROM (SELECT TRIM(?), TRIM(?)) t2 ' //TRIM() to provide a type hint as text for PostgreSQL
  50. . 'WHERE NOT EXISTS (SELECT 1 FROM `' . $this->prefix . 'category` WHERE name = TRIM(?))'; //No category of the same name
  51. $stm = $this->bd->prepare($sql);
  52. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, 63, 'UTF-8');
  53. $values = array(
  54. $valuesTmp['name'],
  55. isset($valuesTmp['attributes']) ? json_encode($valuesTmp['attributes']) : '',
  56. $valuesTmp['name'],
  57. );
  58. if ($stm && $stm->execute($values)) {
  59. return $this->bd->lastInsertId('"' . $this->prefix . 'tag_id_seq"');
  60. } else {
  61. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  62. Minz_Log::error('SQL error addTag: ' . $info[2]);
  63. return false;
  64. }
  65. }
  66. public function addTagObject($tag) {
  67. $tag = $this->searchByName($tag->name());
  68. if (!$tag) {
  69. $values = array(
  70. 'name' => $tag->name(),
  71. 'attributes' => $tag->attributes(),
  72. );
  73. return $this->addTag($values);
  74. }
  75. return $tag->id();
  76. }
  77. public function updateTag($id, $valuesTmp) {
  78. $sql = 'UPDATE `' . $this->prefix . 'tag` SET name=?, attributes=? WHERE id=? '
  79. . 'AND NOT EXISTS (SELECT 1 FROM `' . $this->prefix . 'category` WHERE name = ?)'; //No category of the same name
  80. $stm = $this->bd->prepare($sql);
  81. $valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, 63, 'UTF-8');
  82. $values = array(
  83. $valuesTmp['name'],
  84. isset($valuesTmp['attributes']) ? json_encode($valuesTmp['attributes']) : '',
  85. $id,
  86. $valuesTmp['name'],
  87. );
  88. if ($stm && $stm->execute($values)) {
  89. return $stm->rowCount();
  90. } else {
  91. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  92. Minz_Log::error('SQL error updateTag: ' . $info[2]);
  93. return false;
  94. }
  95. }
  96. public function updateTagAttribute($tag, $key, $value) {
  97. if ($tag instanceof FreshRSS_Tag) {
  98. $tag->_attributes($key, $value);
  99. return $this->updateFeed(
  100. $tag->id(),
  101. array('attributes' => $feed->attributes())
  102. );
  103. }
  104. return false;
  105. }
  106. public function deleteTag($id) {
  107. if ($id <= 0) {
  108. return false;
  109. }
  110. $sql = 'DELETE FROM `' . $this->prefix . 'tag` WHERE id=?';
  111. $stm = $this->bd->prepare($sql);
  112. $values = array($id);
  113. if ($stm && $stm->execute($values)) {
  114. return $stm->rowCount();
  115. } else {
  116. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  117. Minz_Log::error('SQL error deleteTag: ' . $info[2]);
  118. return false;
  119. }
  120. }
  121. public function searchById($id) {
  122. $sql = 'SELECT * FROM `' . $this->prefix . 'tag` WHERE id=?';
  123. $stm = $this->bd->prepare($sql);
  124. $values = array($id);
  125. $stm->execute($values);
  126. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  127. $tag = self::daoToTag($res);
  128. return isset($tag[0]) ? $tag[0] : null;
  129. }
  130. public function searchByName($name) {
  131. $sql = 'SELECT * FROM `' . $this->prefix . 'tag` WHERE name=?';
  132. $stm = $this->bd->prepare($sql);
  133. $values = array($name);
  134. $stm->execute($values);
  135. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  136. $tag = self::daoToTag($res);
  137. return isset($tag[0]) ? $tag[0] : null;
  138. }
  139. public function listTags($precounts = false) {
  140. if ($precounts) {
  141. $sql = 'SELECT t.id, t.name, count(e.id) AS unreads '
  142. . 'FROM `' . $this->prefix . 'tag` t '
  143. . 'LEFT OUTER JOIN `' . $this->prefix . 'entrytag` et ON et.id_tag = t.id '
  144. . 'LEFT OUTER JOIN `' . $this->prefix . 'entry` e ON et.id_entry = e.id AND e.is_read = 0 '
  145. . 'GROUP BY t.id '
  146. . 'ORDER BY t.name';
  147. } else {
  148. $sql = 'SELECT * FROM `' . $this->prefix . 'tag` ORDER BY name';
  149. }
  150. $stm = $this->bd->prepare($sql);
  151. if ($stm && $stm->execute()) {
  152. return self::daoToTag($stm->fetchAll(PDO::FETCH_ASSOC));
  153. } else {
  154. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  155. if ($this->autoUpdateDb($info)) {
  156. return $this->listTags($precounts);
  157. }
  158. Minz_Log::error('SQL error listTags: ' . $info[2]);
  159. return false;
  160. }
  161. }
  162. public function count() {
  163. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'tag`';
  164. $stm = $this->bd->prepare($sql);
  165. $stm->execute();
  166. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  167. return $res[0]['count'];
  168. }
  169. public function countEntries($id) {
  170. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entrytag` WHERE id_tag=?';
  171. $stm = $this->bd->prepare($sql);
  172. $values = array($id);
  173. $stm->execute($values);
  174. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  175. return $res[0]['count'];
  176. }
  177. public function countNotRead($id) {
  178. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entrytag` et '
  179. . 'INNER JOIN `' . $this->prefix . 'entry` e ON et.id_entry=e.id '
  180. . 'WHERE et.id_tag=? AND e.is_read=0';
  181. $stm = $this->bd->prepare($sql);
  182. $values = array($id);
  183. $stm->execute($values);
  184. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  185. return $res[0]['count'];
  186. }
  187. public function tagEntry($id_tag, $id_entry, $checked = true) {
  188. if ($checked) {
  189. $sql = 'INSERT ' . $this->sqlIgnore() . ' INTO `' . $this->prefix . 'entrytag`(id_tag, id_entry) VALUES(?, ?)';
  190. } else {
  191. $sql = 'DELETE FROM `' . $this->prefix . 'entrytag` WHERE id_tag=? AND id_entry=?';
  192. }
  193. $stm = $this->bd->prepare($sql);
  194. $values = array($id_tag, $id_entry);
  195. if ($stm && $stm->execute($values)) {
  196. return true;
  197. } else {
  198. $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
  199. Minz_Log::error('SQL error tagEntry: ' . $info[2]);
  200. return false;
  201. }
  202. }
  203. public function getTagsForEntry($id_entry) {
  204. $sql = 'SELECT t.id, t.name, et.id_entry IS NOT NULL as checked '
  205. . 'FROM `' . $this->prefix . 'tag` t '
  206. . 'LEFT OUTER JOIN `' . $this->prefix . 'entrytag` et ON et.id_tag = t.id AND et.id_entry=? '
  207. . 'ORDER BY t.name';
  208. $stm = $this->bd->prepare($sql);
  209. $values = array($id_entry);
  210. if ($stm && $stm->execute($values)) {
  211. $lines = $stm->fetchAll(PDO::FETCH_ASSOC);
  212. for ($i = count($lines) - 1; $i >= 0; $i--) {
  213. $lines[$i]['id'] = intval($lines[$i]['id']);
  214. $lines[$i]['checked'] = !empty($lines[$i]['checked']);
  215. }
  216. return $lines;
  217. } else {
  218. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  219. if ($this->autoUpdateDb($info)) {
  220. return $this->getTagsForEntry($id_entry);
  221. }
  222. Minz_Log::error('SQL error getTagsForEntry: ' . $info[2]);
  223. return false;
  224. }
  225. }
  226. //For API
  227. public function getEntryIdsTagNames($entries) {
  228. $sql = 'SELECT et.id_entry, t.name '
  229. . 'FROM `' . $this->prefix . 'tag` t '
  230. . 'INNER JOIN `' . $this->prefix . 'entrytag` et ON et.id_tag = t.id';
  231. $values = array();
  232. if (is_array($entries) && count($entries) > 0) {
  233. $sql .= ' AND et.id_entry IN (' . str_repeat('?,', count($entries) - 1). '?)';
  234. foreach ($entries as $entry) {
  235. $values[] = $entry->id();
  236. }
  237. }
  238. $stm = $this->bd->prepare($sql);
  239. if ($stm && $stm->execute($values)) {
  240. $result = array();
  241. foreach ($stm->fetchAll(PDO::FETCH_ASSOC) as $line) {
  242. $entryId = 'e_' . $line['id_entry'];
  243. $tagName = $line['name'];
  244. if (empty($result[$entryId])) {
  245. $result[$entryId] = array();
  246. }
  247. $result[$entryId][] = $tagName;
  248. }
  249. return $result;
  250. } else {
  251. $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
  252. if ($this->autoUpdateDb($info)) {
  253. return $this->getTagNamesEntryIds($id_entry);
  254. }
  255. Minz_Log::error('SQL error getTagNamesEntryIds: ' . $info[2]);
  256. return false;
  257. }
  258. }
  259. public static function daoToTag($listDAO) {
  260. $list = array();
  261. if (!is_array($listDAO)) {
  262. $listDAO = array($listDAO);
  263. }
  264. foreach ($listDAO as $key => $dao) {
  265. $tag = new FreshRSS_Tag(
  266. $dao['name']
  267. );
  268. $tag->_id($dao['id']);
  269. if (!empty($dao['attributes'])) {
  270. $tag->_attributes('', $dao['attributes']);
  271. }
  272. if (isset($dao['unreads'])) {
  273. $tag->_nbUnread($dao['unreads']);
  274. }
  275. $list[$key] = $tag;
  276. }
  277. return $list;
  278. }
  279. }