summaryrefslogtreecommitdiff
path: root/crawler/Database.php
blob: 6d0400954f70153ce8de653ae40606f22633d4f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
 * User: Marvin Borner
 * Date: 16/09/2018
 * Time: 21:34
 */

class Database
{
    public static function getFromQueue($sort): string
    {
        print "\t\e[96mStarting at " . ($sort === 'DESC' ? 'bottom' : 'top') . " of queue\n";
        $conn = self::initDbConnection();
        $checkStmt = $conn->query('SELECT url FROM queue ORDER BY id ' . $sort . ' LIMIT 1');

        return $checkStmt->fetchAll(PDO::FETCH_ASSOC)[0]['url'];
    }

    private static function initDbConnection(): PDO
    {
        global $servername, $dbname, $username, $password;
        $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $conn;
    }

    public static function insertIntoQueue($url): bool
    {
        if (!self::alreadyCrawled($url)) {
            $conn = self::initDbConnection();
            $hash = md5($url);
            $stmt = $conn->prepare('INSERT IGNORE INTO queue (url, hash) VALUES (:url, :hash)');
            $stmt->execute([':url' => $url, 'hash' => $hash]);
            return $stmt->rowCount() > 0;
        }
    }

    public static function alreadyCrawled($url): bool
    {
        $hash = md5($url);
        $conn = self::initDbConnection();
        $checkStmt = $conn->prepare('(SELECT null FROM url_data WHERE hash = :hash) UNION (SELECT null FROM error_url WHERE hash = :hash)');
        $checkStmt->execute([':hash' => $hash]);
        return $checkStmt->rowCount() !== 0; // return true if already crawled
    }

    public static function removeFromQueue($url): void
    {
        $hash = md5($url);
        $conn = self::initDbConnection();
        $checkStmt = $conn->prepare('DELETE FROM queue WHERE hash = :hash');
        $checkStmt->execute([':hash' => $hash]);
    }

    public static function urlHasError($url): void
    {
        $hash = md5($url);
        $conn = self::initDbConnection();
        $checkStmt = $conn->prepare('INSERT IGNORE INTO error_url (url, hash) VALUES (:url, :hash)');
        $checkStmt->execute([':url' => $url, 'hash' => $hash]);
    }

    public static function saveUrlData($data): void
    {
        $conn = self::initDbConnection();
        $stmt = $conn->prepare('INSERT IGNORE INTO url_data (url, title, description, lang, hash) VALUES (:url, :title, :description, :lang, :hash)');
        $stmt->execute([':url' => $data[0], ':title' => $data[1], ':description' => $data[2], ':lang' => $data[3], ':hash' => $data[4]]);
    }
}