aboutsummaryrefslogtreecommitdiff
path: root/models/webpage.php
blob: a42c300dc9f82fa5800e083d601cece2ea88629d (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Database;
use PDO;

class Webpage extends Table {
    public $WID;
    public $Path;
    public $URL;
    public $Date;
    public $Visits;
    public $RequesterUID;
    public $FaviconPath;
    public $Title;

    static function create(string $Path, string $URL, int $RequesterUID, ?string $FaviconPath, ?string $Title) : int {
        return Table::_create(
            'Webpages',
            '(Path, URL, Date, Visits, RequesterUID, FaviconPath, Title)',
            "(\"$Path\", \"$URL\", (NOW() + INTERVAL 2 HOUR), 0, \"$RequesterUID\", \"$FaviconPath\", \"$Title\")"
        );
    }

    static function fromDB(string $URL) : Webpage {
        return Table::_fromDB(
            "SELECT * FROM Webpages WHERE URL = \"$URL\" ORDER BY Date DESC LIMIT 1",
            "Database\Webpage"
        );
    }

    static function fromDBwid(int $WID) : Webpage {
        return Table::_fromDB(
            "SELECT * FROM Webpages WHERE WID = \"$WID\"",
            "Database\Webpage"
        );
    }

    // TODO: remove this, refer to archive.php
    // NOTE: Why remove? Let it be. Leave the thing alone
    static function getPagesCount() : int {
        return Table::_get_entries_count("Webpages");
    }

    static function updateNewArchive(int $WID, ?string $faviconPath, ?string $newTitle) : void {
        Table::_update("Webpages", "FaviconPath = \"$faviconPath\", Title = \"$newTitle\"", "WID = $WID");
    }

    static function fromDBmostVisited(int $count) : array {
        return Table::_get_all(
            'Webpages',
            'Database\Webpage',
            "GROUP BY URL ORDER BY Visits DESC, Date DESC LIMIT $count",
            'WID,Path,URL,Date,MAX(Visits) as Visits,RequesterUID,FaviconPath,Title'
        );
    }

    static function getArchivePathsByPattern(string $URLPattern) : array {
        return Table::_get_all(
            'Webpages',
            'Database\Webpage',
            "WHERE URL LIKE \"$URLPattern\" ORDER BY Date DESC",
            "Path, WID"
        );
    }

    function allArchives() : array {
        return Table::_get_all(
            'Webpages',
            'Database\Webpage',
            "WHERE URL = \"$this->URL\" ORDER BY Date DESC"
        );
    }

    function totalViewCount() : int {
        return Table::_get_all(
            'Webpages',
            'Database\Webpage',
            "WHERE URL = \"$this->URL\"",
            "SUM(Visits) as WID"
        )[0]->WID;
    }

    function previousPageId() : int {
        $foundId = Table::_get_all(
            "Webpages",
            "Database\Webpage",
            "WHERE URL = \"$this->URL\" AND Date < \"$this->Date\"
             ORDER BY Date DESC
             LIMIT 1",
            "WID"
        );
        if (count($foundId) > 0) {
            return $foundId[0]->WID;
        }
        return 0;
    }

    function nextPageId() : int {
        $foundId = Table::_get_all(
            "Webpages",
            "Database\Webpage",
            "WHERE URL = \"$this->URL\" AND Date > \"$this->Date\"
             ORDER BY Date ASC
             LIMIT 1",
            "WID"
        );
        if (count($foundId) > 0) {
            return $foundId[0]->WID;
        }
        return 0;
    }

    function incrementVisits() {
        Table::_update(
            'Webpages',
            "Visits = \"" . ($this->Visits + 1) . "\"",
            "WID = \"{$this->WID}\""
        );
    }

    function delete() {
        Table::_delete(
            'Webpages',
            "WID = \"$this->WID\""
        );
    }
}