<?php
/**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
namespace Friendica\Model;
+use Friendica\Core\Logger;
use Friendica\Database\Database;
use Friendica\Database\DBA;
+use Friendica\DI;
class ItemURI
{
*/
public static function insert(array $fields)
{
- // If the URI gets too long we only take the first parts and hope for best
- $uri = substr($fields['uri'], 0, 255);
+ $fields = DI::dbaDefinition()->truncateFieldsForTable('item-uri', $fields);
- if (!DBA::exists('item-uri', ['uri' => $uri])) {
- DBA::insert('item-uri', $fields, Database::INSERT_UPDATE);
+ if (!DBA::exists('item-uri', ['uri' => $fields['uri']])) {
+ DBA::insert('item-uri', $fields, Database::INSERT_IGNORE);
}
- $itemuri = DBA::selectFirst('item-uri', ['id', 'guid'], ['uri' => $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;
}
* 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(string $uri): int
+ public static function getIdByURI(string $uri, bool $insert = true): int
{
- // If the URI gets too long we only take the first parts and hope for best
- $uri = substr($uri, 0, 255);
+ 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;
}
+
/**
- * Searched for an id of a given guid.
- *
- * @param string $guid
- * @return integer item-uri id
+ * @param int $uriId
+ * @return bool
* @throws \Exception
*/
- public static function getIdByGUID(string $guid): int
+ public static function exists(int $uriId): bool
{
- // If the GUID gets too long we only take the first parts and hope for best
- $guid = substr($guid, 0, 255);
-
- $itemuri = DBA::selectFirst('item-uri', ['id'], ['guid' => $guid]);
-
- if (!DBA::isResult($itemuri)) {
- return 0;
- }
-
- return $itemuri['id'];
+ return DBA::exists('item-uri', ['id' => $uriId]);
}
}