/src/Model/Post/Content.php

https://github.com/friendica/friendica · PHP · 143 lines · 64 code · 20 blank · 59 comment · 7 complexity · f1d515c43ac9c62f099a1211d01e880c MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2022, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Friendica\Model\Post;
  22. use \BadMethodCallException;
  23. use Friendica\Core\Protocol;
  24. use Friendica\Database\Database;
  25. use Friendica\Database\DBA;
  26. use Friendica\Database\DBStructure;
  27. use Friendica\DI;
  28. use Friendica\Model\Post;
  29. class Content
  30. {
  31. /**
  32. * Insert a new post-content entry
  33. *
  34. * @param integer $uri_id
  35. * @param array $fields
  36. * @return bool success
  37. * @throws \Exception
  38. */
  39. public static function insert(int $uri_id, array $data = [])
  40. {
  41. if (empty($uri_id)) {
  42. throw new BadMethodCallException('Empty URI_id');
  43. }
  44. $fields = DI::dbaDefinition()->truncateFieldsForTable('post-content', $data);
  45. // Additionally assign the key fields
  46. $fields['uri-id'] = $uri_id;
  47. return DBA::insert('post-content', $fields, Database::INSERT_IGNORE);
  48. }
  49. /**
  50. * Update a post content entry
  51. *
  52. * @param integer $uri_id
  53. * @param array $data
  54. * @param bool $insert_if_missing
  55. * @return bool
  56. * @throws \Exception
  57. */
  58. public static function update(int $uri_id, array $data = [], bool $insert_if_missing = false)
  59. {
  60. if (empty($uri_id)) {
  61. throw new BadMethodCallException('Empty URI_id');
  62. }
  63. $fields = DI::dbaDefinition()->truncateFieldsForTable('post-content', $data);
  64. // Remove the key fields
  65. unset($fields['uri-id']);
  66. if (empty($fields)) {
  67. return true;
  68. }
  69. return DBA::update('post-content', $fields, ['uri-id' => $uri_id], $insert_if_missing ? true : []);
  70. }
  71. /**
  72. * Delete a row from the post-content table
  73. *
  74. * @param array $conditions Field condition(s)
  75. * @param array $options
  76. * - cascade: If true we delete records in other tables that depend on the one we're deleting through
  77. * relations (default: true)
  78. *
  79. * @return boolean was the delete successful?
  80. * @throws \Exception
  81. */
  82. public static function delete(array $conditions, array $options = [])
  83. {
  84. return DBA::delete('post-content', $conditions, $options);
  85. }
  86. /**
  87. * Search posts for given content
  88. *
  89. * @param string $search
  90. * @param integer $uid
  91. * @param integer $start
  92. * @param integer $limit
  93. * @param integer $last_uriid
  94. * @return array
  95. */
  96. public static function getURIIdListBySearch(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
  97. {
  98. $condition = ["`uri-id` IN (SELECT `uri-id` FROM `post-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
  99. AND (`uid` = ? OR (`uid` = ? AND NOT `global`)) AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
  100. str_replace('@', ' ', $search), 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
  101. if (!empty($last_uriid)) {
  102. $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
  103. }
  104. $params = [
  105. 'order' => ['uri-id' => true],
  106. 'limit' => [$start, $limit]
  107. ];
  108. $tags = Post::select(['uri-id'], $condition, $params);
  109. $uriids = [];
  110. while ($tag = DBA::fetch($tags)) {
  111. $uriids[] = $tag['uri-id'];
  112. }
  113. DBA::close($tags);
  114. return $uriids;
  115. }
  116. public static function countBySearch(string $search, int $uid = 0)
  117. {
  118. $condition = ["`uri-id` IN (SELECT `uri-id` FROM `post-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
  119. AND (`uid` = ? OR (`uid` = ? AND NOT `global`)) AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
  120. str_replace('@', ' ', $search), 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
  121. return Post::count($condition);
  122. }
  123. }