TagDAO.php 9.4 KB

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