TagDAO.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 searchById($id) {
  132. $sql = 'SELECT * FROM `_tag` WHERE id=?';
  133. $stm = $this->pdo->prepare($sql);
  134. $values = array($id);
  135. $stm->execute($values);
  136. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  137. $tag = self::daoToTag($res);
  138. return isset($tag[0]) ? $tag[0] : null;
  139. }
  140. public function searchByName($name) {
  141. $sql = 'SELECT * FROM `_tag` WHERE name=?';
  142. $stm = $this->pdo->prepare($sql);
  143. $values = array($name);
  144. $stm->execute($values);
  145. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  146. $tag = self::daoToTag($res);
  147. return isset($tag[0]) ? $tag[0] : null;
  148. }
  149. public function listTags($precounts = false) {
  150. if ($precounts) {
  151. $sql = 'SELECT t.id, t.name, count(e.id) AS unreads '
  152. . 'FROM `_tag` t '
  153. . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id '
  154. . 'LEFT OUTER JOIN `_entry` e ON et.id_entry = e.id AND e.is_read = 0 '
  155. . 'GROUP BY t.id '
  156. . 'ORDER BY t.name';
  157. } else {
  158. $sql = 'SELECT * FROM `_tag` ORDER BY name';
  159. }
  160. $stm = $this->pdo->query($sql);
  161. if ($stm !== false) {
  162. return self::daoToTag($stm->fetchAll(PDO::FETCH_ASSOC));
  163. } else {
  164. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  165. if ($this->autoUpdateDb($info)) {
  166. return $this->listTags($precounts);
  167. }
  168. Minz_Log::error('SQL error listTags: ' . $info[2]);
  169. return false;
  170. }
  171. }
  172. public function count() {
  173. $sql = 'SELECT COUNT(*) AS count FROM `_tag`';
  174. $stm = $this->pdo->query($sql);
  175. if ($stm !== false) {
  176. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  177. return $res[0]['count'];
  178. } else {
  179. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  180. if ($this->autoUpdateDb($info)) {
  181. return $this->count();
  182. }
  183. Minz_Log::error('SQL error TagDAO::count: ' . $info[2]);
  184. return false;
  185. }
  186. }
  187. public function countEntries($id) {
  188. $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` WHERE id_tag=?';
  189. $stm = $this->pdo->prepare($sql);
  190. $values = array($id);
  191. $stm->execute($values);
  192. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  193. return $res[0]['count'];
  194. }
  195. public function countNotRead($id) {
  196. $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` et '
  197. . 'INNER JOIN `_entry` e ON et.id_entry=e.id '
  198. . 'WHERE et.id_tag=? AND e.is_read=0';
  199. $stm = $this->pdo->prepare($sql);
  200. $values = array($id);
  201. $stm->execute($values);
  202. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  203. return $res[0]['count'];
  204. }
  205. public function tagEntry($id_tag, $id_entry, $checked = true) {
  206. if ($checked) {
  207. $sql = 'INSERT ' . $this->sqlIgnore() . ' INTO `_entrytag`(id_tag, id_entry) VALUES(?, ?)';
  208. } else {
  209. $sql = 'DELETE FROM `_entrytag` WHERE id_tag=? AND id_entry=?';
  210. }
  211. $stm = $this->pdo->prepare($sql);
  212. $values = array($id_tag, $id_entry);
  213. if ($stm && $stm->execute($values)) {
  214. return true;
  215. } else {
  216. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  217. Minz_Log::error('SQL error tagEntry: ' . $info[2]);
  218. return false;
  219. }
  220. }
  221. public function getTagsForEntry($id_entry) {
  222. $sql = 'SELECT t.id, t.name, et.id_entry IS NOT NULL as checked '
  223. . 'FROM `_tag` t '
  224. . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id AND et.id_entry=? '
  225. . 'ORDER BY t.name';
  226. $stm = $this->pdo->prepare($sql);
  227. $values = array($id_entry);
  228. if ($stm && $stm->execute($values)) {
  229. $lines = $stm->fetchAll(PDO::FETCH_ASSOC);
  230. for ($i = count($lines) - 1; $i >= 0; $i--) {
  231. $lines[$i]['id'] = intval($lines[$i]['id']);
  232. $lines[$i]['checked'] = !empty($lines[$i]['checked']);
  233. }
  234. return $lines;
  235. } else {
  236. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  237. if ($this->autoUpdateDb($info)) {
  238. return $this->getTagsForEntry($id_entry);
  239. }
  240. Minz_Log::error('SQL error getTagsForEntry: ' . $info[2]);
  241. return false;
  242. }
  243. }
  244. public function getTagsForEntries($entries) {
  245. $sql = 'SELECT et.id_entry, et.id_tag, t.name '
  246. . 'FROM `_tag` t '
  247. . 'INNER JOIN `_entrytag` et ON et.id_tag = t.id';
  248. $values = array();
  249. if (is_array($entries) && count($entries) > 0) {
  250. $sql .= ' AND et.id_entry IN (' . str_repeat('?,', count($entries) - 1). '?)';
  251. if (is_array($entries[0])) {
  252. foreach ($entries as $entry) {
  253. $values[] = $entry['id'];
  254. }
  255. } elseif (is_object($entries[0])) {
  256. foreach ($entries as $entry) {
  257. $values[] = $entry->id();
  258. }
  259. } else {
  260. foreach ($entries as $entry) {
  261. $values[] = $entry;
  262. }
  263. }
  264. }
  265. $stm = $this->pdo->prepare($sql);
  266. if ($stm && $stm->execute($values)) {
  267. return $stm->fetchAll(PDO::FETCH_ASSOC);
  268. } else {
  269. $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
  270. if ($this->autoUpdateDb($info)) {
  271. return $this->getTagsForEntries($entries);
  272. }
  273. Minz_Log::error('SQL error getTagsForEntries: ' . $info[2]);
  274. return false;
  275. }
  276. }
  277. //For API
  278. public function getEntryIdsTagNames($entries) {
  279. $result = array();
  280. foreach ($this->getTagsForEntries($entries) as $line) {
  281. $entryId = 'e_' . $line['id_entry'];
  282. $tagName = $line['name'];
  283. if (empty($result[$entryId])) {
  284. $result[$entryId] = array();
  285. }
  286. $result[$entryId][] = $tagName;
  287. }
  288. return $result;
  289. }
  290. public static function daoToTag($listDAO) {
  291. $list = array();
  292. if (!is_array($listDAO)) {
  293. $listDAO = array($listDAO);
  294. }
  295. foreach ($listDAO as $key => $dao) {
  296. $tag = new FreshRSS_Tag(
  297. $dao['name']
  298. );
  299. $tag->_id($dao['id']);
  300. if (!empty($dao['attributes'])) {
  301. $tag->_attributes('', $dao['attributes']);
  302. }
  303. if (isset($dao['unreads'])) {
  304. $tag->_nbUnread($dao['unreads']);
  305. }
  306. $list[$key] = $tag;
  307. }
  308. return $list;
  309. }
  310. }