Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.2KB

  1. <?php
  2. /**
  3. * @file src/Model/ItemURI.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\BaseObject;
  7. use Friendica\Database\DBA;
  8. class ItemURI extends BaseObject
  9. {
  10. /**
  11. * @brief Insert an item-uri record and return its id
  12. *
  13. * @param array $fields Item-uri fields
  14. *
  15. * @return integer item-uri id
  16. */
  17. public static function insert($fields)
  18. {
  19. // If the URI gets too long we only take the first parts and hope for best
  20. $uri = substr($fields['uri'], 0, 255);
  21. if (!DBA::exists('item-uri', ['uri' => $uri])) {
  22. DBA::insert('item-uri', $fields, true);
  23. }
  24. $itemuri = DBA::selectFirst('item-uri', ['id'], ['uri' => $uri]);
  25. if (!DBA::isResult($itemuri)) {
  26. // This shouldn't happen
  27. return null;
  28. }
  29. return $itemuri['id'];
  30. }
  31. /**
  32. * @brief Searched for an id of a given uri. Adds it, if not existing yet.
  33. *
  34. * @param string $uri
  35. *
  36. * @return integer item-uri id
  37. */
  38. public static function getIdByURI($uri)
  39. {
  40. // If the URI gets too long we only take the first parts and hope for best
  41. $uri = substr($uri, 0, 255);
  42. $itemuri = DBA::selectFirst('item-uri', ['id'], ['uri' => $uri]);
  43. if (!DBA::isResult($itemuri)) {
  44. return self::insert(['uri' => $uri]);
  45. }
  46. return $itemuri['id'];
  47. }
  48. }