simpleWordsCloud/db.php

169 lines
3.9 KiB
PHP

<?php
class DataBase
{
private $db;
public function __construct()
{
$this->init();
}
public function init()
{
try {
$this->db = new PDO('sqlite:test.db');
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
return null;
}
$stmt = $this->db->prepare('PRAGMA foreign_keys = 1;');
$stmt->execute();
}
public function isInit()
{
return isset($this->db);
}
public function buildTables()
{
$stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS clouds('
. 'id_cloud INTEGER PRIMARY KEY, '
. 'code TEXT NOT NULL UNIQUE, '
. 'create_t TIMESTAMP DEFAULT CURRENT_TIMESTAMP);'
);
$stmt->execute();
$stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS words('
. 'id_word INTEGER PRIMARY KEY, '
. '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);'
);
$stmt->execute();
}
public function addWord(string $cloud, string $word)
{
if (empty($word)) {
return false;
}
$stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;');
$stmt->bindValue(':id', $cloud, PDO::PARAM_STR);
$stmt->execute();
$cloudId = null;
if ($data = $stmt->fetch()) {
$cloudId = $data['id_cloud'];
} else {
return false;
}
$stmt = $this->db->prepare('SELECT * FROM words '
. 'WHERE cloud_id = :cid AND word = :w;'
);
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
$stmt->bindValue(':w', $word, PDO::PARAM_STR);
$stmt->execute();
if ($data = $stmt->fetch()) {
$wordId = $data['id_word'];
$stmt = $this->db->prepare(
'UPDATE words SET count = count + 1 WHERE id_word = :id;'
);
$stmt->bindValue(':id', $wordId, PDO::PARAM_INT);
$stmt->execute();
} else {
$stmt = $this->db->prepare(
'INSERT INTO words(word, cloud_id) VALUES(:w, :cid);'
);
$stmt->bindValue(':w', $word, PDO::PARAM_STR);
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
$stmt->execute();
}
}
public function getWords(string $id)
{
$stmt = $this->db->prepare('SELECT * FROM words '
. 'JOIN clouds ON words.cloud_id = clouds.id_cloud '
. 'WHERE code = :id;'
);
$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)
{
$words = $this->getWords($id);
//echo 'test';
//var_dump($words);
$total = 0;
foreach ($words as $word) {
$total += $word[1];
}
foreach ($words as $key => $word) {
$words[$key] = array($word[0], $word[1] / $total);
}
return $words;
}
public function getWordsMax(string $id)
{
$words = $this->getWords($id);
//echo 'test';
//var_dump($words);
$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;
}
public function createCloud(string $ref)
{
$stmt = $this->db->prepare('INSERT INTO clouds(code) VALUES(:code);');
$stmt->bindValue(':code', $ref);
try {
$stmt->execute();
} catch (PDOEXception $e) {
return false;
}
return true;
}
public function cleanCloud()
{
$stmt = $this->db->prepare('DELETE FROM clouds '
. "WHERE create_t < (SELECT datetime('now', '-1 week'));"
);
$stmt->execute();
return true;
}
public function isCloud(string $id)
{
$stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;');
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute();
if ($stmt->fetch()) {
return true;
}
return false;
}
}