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