2020-11-15 21:51:28 +01:00
|
|
|
<?php
|
2020-11-22 15:09:31 +01:00
|
|
|
|
|
|
|
|
2020-11-15 21:51:28 +01:00
|
|
|
class DataBase
|
|
|
|
{
|
2020-11-16 21:31:59 +01:00
|
|
|
const DEFAULT_DURATION = '+7 day';
|
|
|
|
const OPTIONS_DURATION = array(
|
|
|
|
'day' => '+1 day',
|
|
|
|
'week' => '+7 day',
|
|
|
|
'2week' => '+14 day',
|
|
|
|
'month' => '+1 month'
|
|
|
|
);
|
|
|
|
|
2020-11-15 21:51:28 +01:00
|
|
|
private $db;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function init()
|
|
|
|
{
|
2020-11-22 15:09:31 +01:00
|
|
|
require_once 'dbconfig.php';
|
|
|
|
$dsn = 'pgsql:dbname=' . $dbname
|
|
|
|
. ';host=' . $host
|
|
|
|
. ';port=' . $port;
|
2020-11-15 21:51:28 +01:00
|
|
|
try {
|
2020-11-22 15:09:31 +01:00
|
|
|
$this->db = new PDO($dsn, $username, $password);
|
2020-11-15 21:51:28 +01:00
|
|
|
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
2020-11-22 15:09:31 +01:00
|
|
|
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
2020-11-15 21:51:28 +01:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isInit()
|
|
|
|
{
|
|
|
|
return isset($this->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildTables()
|
|
|
|
{
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
CREATE TABLE IF NOT EXISTS clouds(
|
2020-11-22 15:09:31 +01:00
|
|
|
id_cloud serial PRIMARY KEY,
|
2020-11-16 21:31:59 +01:00
|
|
|
code TEXT NOT NULL UNIQUE,
|
|
|
|
size INTEGER NOT NULL DEFAULT 3,
|
|
|
|
text TEXT,
|
|
|
|
delete_t TIMESTAMP DEFAULT
|
2020-11-22 15:09:31 +01:00
|
|
|
(CURRENT_TIMESTAMP + INTERVAL '" . self::DEFAULT_DURATION . "')
|
2020-11-16 21:31:59 +01:00
|
|
|
);
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->execute();
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
CREATE TABLE IF NOT EXISTS words(
|
2020-11-22 15:09:31 +01:00
|
|
|
id_word serial PRIMARY KEY,
|
2020-11-16 21:31:59 +01:00
|
|
|
word TEXT NOT NULL,
|
|
|
|
count INT DEFAULT 1,
|
|
|
|
cloud_id INT NOT NULL,
|
|
|
|
FOREIGN KEY (cloud_id)
|
|
|
|
REFERENCES clouds(id_cloud) ON UPDATE CASCADE
|
|
|
|
ON DELETE CASCADE
|
|
|
|
);
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addWord(string $cloud, string $word)
|
|
|
|
{
|
|
|
|
if (empty($word)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
SELECT *
|
|
|
|
FROM clouds
|
|
|
|
WHERE code = :id;
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->bindValue(':id', $cloud, PDO::PARAM_STR);
|
|
|
|
$stmt->execute();
|
|
|
|
$cloudId = null;
|
|
|
|
if ($data = $stmt->fetch()) {
|
|
|
|
$cloudId = $data['id_cloud'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
SELECT *
|
|
|
|
FROM words
|
|
|
|
WHERE cloud_id = :cid
|
|
|
|
AND word = :w;
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':w', $word, PDO::PARAM_STR);
|
|
|
|
$stmt->execute();
|
|
|
|
if ($data = $stmt->fetch()) {
|
|
|
|
$wordId = $data['id_word'];
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
UPDATE words
|
|
|
|
SET count = count + 1
|
|
|
|
WHERE id_word = :id;
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->bindValue(':id', $wordId, PDO::PARAM_INT);
|
|
|
|
$stmt->execute();
|
|
|
|
} else {
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
INSERT INTO words(word, cloud_id)
|
|
|
|
VALUES (:w, :cid);
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->bindValue(':w', $word, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
|
|
|
|
$stmt->execute();
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 15:09:31 +01:00
|
|
|
public function getWordsList(string $id)
|
2020-11-15 21:51:28 +01:00
|
|
|
{
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
SELECT *
|
|
|
|
FROM words
|
|
|
|
JOIN clouds
|
|
|
|
ON words.cloud_id = clouds.id_cloud
|
|
|
|
WHERE code = :id;
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->bindValue(':id', $id);
|
|
|
|
$stmt->execute();
|
|
|
|
$words = [];
|
|
|
|
$values = [];
|
|
|
|
while ($data = $stmt->fetch()) {
|
|
|
|
$words[] = array($data['word'], $data['count']);
|
|
|
|
$values[] = $data['count'];
|
|
|
|
$total += $data['count'];
|
|
|
|
}
|
|
|
|
|
|
|
|
array_multisort($values, SORT_DESC, $words);
|
|
|
|
return $words;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getWordsPercentage(string $id)
|
|
|
|
{
|
2020-11-22 15:09:31 +01:00
|
|
|
$words = $this->getWordsList($id);
|
2020-11-15 21:51:28 +01:00
|
|
|
$total = 0;
|
|
|
|
foreach ($words as $word) {
|
|
|
|
$total += $word[1];
|
|
|
|
}
|
|
|
|
foreach ($words as $key => $word) {
|
|
|
|
$words[$key] = array($word[0], $word[1] / $total);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $words;
|
|
|
|
}
|
|
|
|
|
2020-11-22 15:09:31 +01:00
|
|
|
public function getWordsRelative(string $id)
|
2020-11-15 21:51:28 +01:00
|
|
|
{
|
2020-11-22 15:09:31 +01:00
|
|
|
$words = $this->getWordsList($id);
|
2020-11-15 21:51:28 +01:00
|
|
|
$max = 0;
|
|
|
|
foreach ($words as $word) {
|
|
|
|
$max = max($max, $word[1]);
|
|
|
|
}
|
|
|
|
foreach ($words as $key => $word) {
|
|
|
|
$words[$key] = array($word[0], $word[1] / $max);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $words;
|
|
|
|
}
|
|
|
|
|
2020-11-16 21:31:59 +01:00
|
|
|
public function createCloud(
|
|
|
|
string $ref,
|
|
|
|
string $text = '',
|
|
|
|
int $size = null,
|
|
|
|
string $duration = null)
|
2020-11-15 21:51:28 +01:00
|
|
|
{
|
2020-11-16 21:31:59 +01:00
|
|
|
if (!isset($size)) {
|
|
|
|
$size = 3;
|
|
|
|
}
|
|
|
|
if (!isset($duration)) {
|
|
|
|
$duration = self::DEFAULT_DURATION;
|
|
|
|
} elseif (!in_array($duration, self::OPTIONS_DURATION)) {
|
|
|
|
$duration = self::DEFAULT_DURATION;
|
|
|
|
}
|
|
|
|
$duration = date('Y-m-d H:i:s', strtotime($duration));
|
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
INSERT INTO clouds(code, text, size, delete_t)
|
|
|
|
VALUES (:code, :text, :size, :duration);
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->bindValue(':code', $ref);
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt->bindValue(':text', $text);
|
|
|
|
$stmt->bindValue(':size', $size);
|
|
|
|
$stmt->bindValue(':duration', $duration, PDO::PARAM_STR);
|
2020-11-15 21:51:28 +01:00
|
|
|
try {
|
|
|
|
$stmt->execute();
|
|
|
|
} catch (PDOEXception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-16 21:31:59 +01:00
|
|
|
public function getCloudSize(string $ref)
|
|
|
|
{
|
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
SELECT *
|
|
|
|
FROM clouds
|
|
|
|
WHERE code= :code;
|
|
|
|
");
|
|
|
|
$stmt->bindValue(':code', $ref, PDO::PARAM_STR);
|
|
|
|
$stmt->execute();
|
|
|
|
if ($data = $stmt->fetch()) {
|
|
|
|
return $data['size'];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCloudText(string $ref)
|
|
|
|
{
|
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
SELECT *
|
|
|
|
FROM clouds
|
|
|
|
WHERE code= :code;
|
|
|
|
");
|
|
|
|
$stmt->bindValue(':code', $ref, PDO::PARAM_STR);
|
|
|
|
$stmt->execute();
|
|
|
|
if ($data = $stmt->fetch()) {
|
|
|
|
return $data['text'];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:51:28 +01:00
|
|
|
public function cleanCloud()
|
|
|
|
{
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
DELETE
|
|
|
|
FROM clouds
|
|
|
|
WHERE delete_t < CURRENT_TIMESTAMP;
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->execute();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isCloud(string $id)
|
|
|
|
{
|
2020-11-16 21:31:59 +01:00
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
SELECT *
|
|
|
|
FROM clouds
|
|
|
|
WHERE code = :id;
|
|
|
|
");
|
2020-11-15 21:51:28 +01:00
|
|
|
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
|
|
|
|
$stmt->execute();
|
|
|
|
if ($stmt->fetch()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|