X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FItemURI.php;h=1755552299741b6884060ea8e4452c75b4aac13d;hb=c6a9e8be397c5b3c519470f8e22f23b0fbdd3b68;hp=559babb7620bd66300753cb8aad1a6dfae231195;hpb=71ec84f6dc83f753fe80170cfdfd32d202850d90;p=friendica.git diff --git a/src/Model/ItemURI.php b/src/Model/ItemURI.php index 559babb762..1755552299 100644 --- a/src/Model/ItemURI.php +++ b/src/Model/ItemURI.php @@ -1,56 +1,94 @@ . + * */ namespace Friendica\Model; -use Friendica\BaseObject; +use Friendica\Core\Logger; +use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\DI; -require_once 'boot.php'; - -class ItemURI extends BaseObject +class ItemURI { /** - * @brief Insert an item-uri record and return its id + * Insert an item-uri record and return its id * * @param array $fields Item-uri fields - * - * @return integer item-uri id + * @return int|null item-uri id + * @throws \Exception */ - public static function insert($fields) + public static function insert(array $fields) { + $fields = DI::dbaDefinition()->truncateFieldsForTable('item-uri', $fields); + if (!DBA::exists('item-uri', ['uri' => $fields['uri']])) { - DBA::insert('item-uri', $fields, true); + DBA::insert('item-uri', $fields, Database::INSERT_IGNORE); } - $itemuri = DBA::selectFirst('item-uri', ['id'], ['uri' => $fields['uri']]); - + $itemuri = DBA::selectFirst('item-uri', ['id', 'guid'], ['uri' => $fields['uri']]); if (!DBA::isResult($itemuri)) { // This shouldn't happen + Logger::warning('Item-uri not found', $fields); return null; } + if (empty($itemuri['guid']) && !empty($fields['guid'])) { + DBA::update('item-uri', ['guid' => $fields['guid']], ['id' => $itemuri['id']]); + } + return $itemuri['id']; } /** - * @brief Searched for an id of a given uri. Adds it, if not existing yet. + * Searched for an id of a given uri. Adds it, if not existing yet. * * @param string $uri + * @param bool $insert * * @return integer item-uri id + * + * @throws \Exception */ - public static function getIdByURI($uri) + public static function getIdByURI(string $uri, bool $insert = true): int { + if (empty($uri)) { + return 0; + } + $itemuri = DBA::selectFirst('item-uri', ['id'], ['uri' => $uri]); - if (!DBA::isResult($itemuri)) { + if (!DBA::isResult($itemuri) && $insert) { return self::insert(['uri' => $uri]); } - return $itemuri['id']; + return $itemuri['id'] ?? 0; + } + + /** + * @param int $uriId + * @return bool + * @throws \Exception + */ + public static function exists(int $uriId): bool + { + return DBA::exists('item-uri', ['id' => $uriId]); } }