]> git.mxchange.org Git - friendica.git/commitdiff
New model class "host"
authorMichael <heluecht@pirati.ca>
Wed, 2 Sep 2020 19:19:00 +0000 (19:19 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 2 Sep 2020 19:19:00 +0000 (19:19 +0000)
database.sql
src/Model/Host.php [new file with mode: 0644]
static/dbstructure.config.php

index 46a2981e77244e5fc1129cd321ec39d2eba82e62..addd396ffd2d10b3924229eadbfae16f9a030069 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2020.09-dev (Red Hot Poker)
--- DB_UPDATE_VERSION 1366
+-- DB_UPDATE_VERSION 1367
 -- ------------------------------------------
 
 
@@ -585,6 +585,16 @@ CREATE TABLE IF NOT EXISTS `hook` (
         UNIQUE INDEX `hook_file_function` (`hook`,`file`,`function`)
 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='addon hook registry';
 
+--
+-- TABLE host
+--
+CREATE TABLE IF NOT EXISTS `host` (
+       `id` tinyint unsigned NOT NULL auto_increment COMMENT 'sequential ID',
+       `name` varchar(128) NOT NULL DEFAULT '' COMMENT 'The hostname',
+        PRIMARY KEY(`id`),
+        UNIQUE INDEX `name` (`name`)
+) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Hostname';
+
 --
 -- TABLE inbox-status
 --
diff --git a/src/Model/Host.php b/src/Model/Host.php
new file mode 100644 (file)
index 0000000..15f75bd
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Model;
+
+use Friendica\Core\Logger;
+use Friendica\Database\DBA;
+
+class Host
+{
+       /**
+        * Get the id for a given hostname
+        * When empty, the current hostname is used
+        *
+        * @param string $hostname
+        *
+        * @return integer host name id
+        * @throws \Exception
+        */
+       public static function getId(string $hostname = '')
+       {
+               if (empty($hostname)) {
+                       $hostname = php_uname('n');
+               }
+
+               $hostname = strtolower($hostname);
+
+               $host = DBA::selectFirst('host', ['id'], ['name' => $hostname]);
+               if (!empty($host['id'])) {
+                       return $host['id'];
+               }
+
+               DBA::replace('host', ['name' => $hostname]);
+
+               $host = DBA::selectFirst('host', ['id'], ['name' => $hostname]);
+               if (empty($host['id'])) {
+                       Logger::warning('Host name could not be inserted', ['name' => $hostname]);
+                       return 0;
+               }
+
+               return $host['id'];
+       }
+
+       /**
+        * Get the hostname for a given id
+        *
+        * @param int $id
+        *
+        * @return string host name
+        * @throws \Exception
+        */
+       public static function getName(int $id)
+       {
+               $host = DBA::selectFirst('host', ['name'], ['id' => $id]);
+               if (!empty($host['name'])) {
+                       return $host['name'];
+               }
+
+               return '';
+       }
+}
index b1cbb6b27b4ae1b283ebd600235c81428a9be4fb..23a299e65208cdf053765ea062b3844b798e9bbd 100755 (executable)
@@ -54,7 +54,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1366);
+       define('DB_UPDATE_VERSION', 1367);
 }
 
 return [
@@ -653,6 +653,17 @@ return [
                        "hook_file_function" => ["UNIQUE", "hook", "file", "function"],
                ]
        ],
+       "host" => [
+               "comment" => "Hostname",
+               "fields" => [
+                       "id" => ["type" => "tinyint unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"],
+                       "name" => ["type" => "varchar(128)", "not null" => "1", "default" => "", "comment" => "The hostname"],
+               ],
+               "indexes" => [
+                       "PRIMARY" => ["id"],
+                       "name" => ["UNIQUE", "name"],
+               ]
+       ],
        "inbox-status" => [
                "comment" => "Status of ActivityPub inboxes",
                "fields" => [