bookmarks.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * ownCloud - bookmarks plugin
  4. *
  5. * @author Arthur Schiwon
  6. * @copyright 2011 Arthur Schiwon blizzz@arthur-schiwon.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * This class manages bookmarks
  24. */
  25. class OC_Bookmarks_Bookmarks{
  26. /**
  27. * @brief Finds all bookmarks, matching the filter
  28. * @param offset result offset
  29. * @param sqlSortColumn sort result with this column
  30. * @param filter can be: empty -> no filter, a string -> filter this, a string array -> filter for all strings
  31. * @param filterTagOnly if true, filter affacts only tags, else filter affects url, title and tags
  32. * @return void
  33. */
  34. public static function findBookmarks($offset, $sqlSortColumn, $filter, $filterTagOnly){
  35. //OCP\Util::writeLog('bookmarks', 'findBookmarks ' .$offset. ' '.$sqlSortColumn.' '. $filter.' '. $filterTagOnly ,OCP\Util::DEBUG);
  36. $CONFIG_DBTYPE = OCP\Config::getSystemValue( 'dbtype', 'sqlite' );
  37. $params=array(OCP\USER::getUser());
  38. if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
  39. $_gc_separator = ', \' \'';
  40. } else {
  41. $_gc_separator = 'SEPARATOR \' \'';
  42. }
  43. if($filter){
  44. if($CONFIG_DBTYPE == 'pgsql' )
  45. $tagString = 'array_to_string(array_agg(tag), \' \')';
  46. else
  47. $tagString = 'tags';
  48. $sqlFilterTag = 'HAVING ';
  49. if(is_array($filter)){
  50. $first = true;
  51. $filterstring = '';
  52. foreach ($filter as $singleFilter){
  53. $filterstring = $filterstring . ($first?'':' AND ') . $tagString.' LIKE ? ';
  54. $params[] = '%'.$singleFilter.'%';
  55. $first=false;
  56. }
  57. $sqlFilterTag = $sqlFilterTag . $filterstring;
  58. } else{
  59. $sqlFilterTag = $sqlFilterTag .$tagString.' LIKE ? ';
  60. $params[] = '%'.$filter.'%';
  61. }
  62. } else {
  63. $sqlFilterTag = '';
  64. }
  65. if($CONFIG_DBTYPE == 'pgsql' ){
  66. $query = OCP\DB::prepare('
  67. SELECT id, url, title, '.($filterTagOnly?'':'url || title ||').' array_to_string(array_agg(tag), \' \') as tags
  68. FROM *PREFIX*bookmarks
  69. LEFT JOIN *PREFIX*bookmarks_tags ON *PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id
  70. WHERE
  71. *PREFIX*bookmarks.user_id = ?
  72. GROUP BY id, url, title
  73. '.$sqlFilterTag.'
  74. ORDER BY *PREFIX*bookmarks.'.$sqlSortColumn.' DESC
  75. LIMIT 10
  76. OFFSET '. $offset);
  77. } else {
  78. if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' )
  79. $concatFunction = '(url || title || ';
  80. else
  81. $concatFunction = 'Concat(Concat( url, title), ';
  82. $query = OCP\DB::prepare('
  83. SELECT id, url, title, '
  84. .($filterTagOnly?'':$concatFunction).
  85. 'CASE WHEN *PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id
  86. THEN GROUP_CONCAT( tag ' .$_gc_separator. ' )
  87. ELSE \' \'
  88. END '
  89. .($filterTagOnly?'':')').'
  90. AS tags
  91. FROM *PREFIX*bookmarks
  92. LEFT JOIN *PREFIX*bookmarks_tags ON 1=1
  93. WHERE (*PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id
  94. OR *PREFIX*bookmarks.id NOT IN (
  95. SELECT *PREFIX*bookmarks_tags.bookmark_id FROM *PREFIX*bookmarks_tags
  96. )
  97. )
  98. AND *PREFIX*bookmarks.user_id = ?
  99. GROUP BY url
  100. '.$sqlFilterTag.'
  101. ORDER BY *PREFIX*bookmarks.'.$sqlSortColumn.' DESC
  102. LIMIT '.$offset.', 10');
  103. }
  104. $bookmarks = $query->execute($params)->fetchAll();
  105. return $bookmarks;
  106. }
  107. public static function deleteUrl($id)
  108. {
  109. $user = OCP\USER::getUser();
  110. $query = OCP\DB::prepare("
  111. SELECT id FROM *PREFIX*bookmarks
  112. WHERE id = ?
  113. AND user_id = ?
  114. ");
  115. $result = $query->execute(array($id, $user));
  116. $id = $result->fetchOne();
  117. if ($id === false) {
  118. return false;
  119. }
  120. $query = OCP\DB::prepare("
  121. DELETE FROM *PREFIX*bookmarks
  122. WHERE id = $id
  123. ");
  124. $result = $query->execute();
  125. $query = OCP\DB::prepare("
  126. DELETE FROM *PREFIX*bookmarks_tags
  127. WHERE bookmark_id = $id
  128. ");
  129. $result = $query->execute();
  130. return true;
  131. }
  132. }