TagDAO.php 10 KB

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