podcrawler.php 1.6 KB
Newer Older
David Morley's avatar
David Morley committed
1 2
<?php

3 4 5 6 7 8
/**
 * Crawl and add all pods from the-federation.info list.
 */

declare(strict_types=1);

9
if (PHP_SAPI !== 'cli') {
10 11
    header('HTTP/1.0 403 Forbidden');
    exit;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
}

use RedBeanPHP\R;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../config.php';

define('PODUPTIME', microtime(true));

// Set up global DB connection.
R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true);
R::testConnection() || die('Error in DB connection');
R::usePartialBeans(true);

try {
27 28 29 30 31
    $sql  = '
        SELECT domain, status
        FROM pods
    ';
    $pods = R::getAll($sql);
32 33 34 35 36 37 38 39 40 41 42
} catch (\RedBeanPHP\RedException $e) {
    die('Error in SQL query: ' . $e->getMessage());
}

//get all existing pod domains
$existingpods = array_column($pods, 'domain');

$foundpods = [];

//pulling all nodes for now
if ($pods = json_decode(file_get_contents('https://the-federation.info/graphql?query=%7Bnodes%7Bhost%20platform%7Bname%7Dprotocols%7Bname%7D%7D%7D&raw'), true)) {
43 44
    foreach ($pods['data']['nodes'] ?? [] as $poddata) {
        $protocols = array_column($poddata['protocols'] ?? [], 'name');
45

46 47 48 49
        //limiting to diaspora compatible for now
        if (in_array('diaspora', $protocols, true)) {
            $foundpods[] = $poddata['host'];
        }
dmorley's avatar
dmorley committed
50
    }
51 52 53
}

if ($pods = json_decode(file_get_contents('https://diasp.org/pods.json'), true)) {
54 55 56
    foreach ($pods ?? [] as $poddata) {
        $foundpods[] = $poddata['host'];
    }
57 58 59 60
}

$results = array_diff($foundpods, $existingpods);
foreach ($results as $result) {
dmorley's avatar
dmorley committed
61
    echo $result . ' ';
62
    echo exec("php-cgi add.php domain={$result}") . "\r\n";
David Morley's avatar
David Morley committed
63
}