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