]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Update copyright
[friendica.git] / src / Model / GServer.php
index c56e7701e0bbc225555a75a9c15485c3b8d0cf25..bf0d0d2c2e76eb6790b1b7753ffb5726680c8a2f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -117,6 +117,33 @@ class GServer
                return self::getID($url, true);
        }
 
+       /**
+        * Retrieves all the servers which base domain are matching the provided domain pattern
+        *
+        * The pattern is a simple fnmatch() pattern with ? for single wildcard and * for multiple wildcard
+        *
+        * @param string $pattern
+        * @return array
+        * @throws Exception
+        */
+       public static function listByDomainPattern(string $pattern): array
+       {
+               $likePattern = 'http://' . strtr($pattern, ['_' => '\_', '%' => '\%', '?' => '_', '*' => '%']);
+
+               // The SUBSTRING_INDEX returns everything before the eventual third /, which effectively trims an
+               // eventual server path and keep only the server domain which we're matching against the pattern.
+               $sql = "SELECT `gserver`.*, COUNT(*) AS `contacts`
+                       FROM `gserver`
+                       LEFT JOIN `contact` ON `gserver`.`id` = `contact`.`gsid`
+                       WHERE SUBSTRING_INDEX(`gserver`.`nurl`, '/', 3) LIKE ?
+                       AND NOT `gserver`.`failed`
+                       GROUP BY `gserver`.`id`";
+
+               $stmt = DI::dba()->p($sql, $likePattern);
+
+               return DI::dba()->toArray($stmt);
+       }
+
        /**
         * Checks if the given server is reachable
         *