2017-03-07 09:38:47 +01:00
|
|
|
<?php
|
2018-07-31 15:58:23 +02:00
|
|
|
|
|
|
|
class PileDB
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
private $db;
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
function __construct()
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
$this->db = new SQLite3("pile.db");
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
function prepare($statement)
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
return $this->db->prepare($statement);
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
function query($statement)
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
return $this->db->query($statement);
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function getDocCount()
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
$ret_count = $this->db->query("SELECT count(ID) FROM Documents")->fetchArray(SQLITE3_NUM);
|
|
|
|
return $ret_count[0];
|
|
|
|
}
|
2018-07-31 15:58:23 +02:00
|
|
|
|
|
|
|
public function getUntaggedDocCount()
|
|
|
|
{
|
2017-03-07 21:20:20 +01:00
|
|
|
$ret_count = $this->db->query("SELECT
|
|
|
|
count(ID)
|
|
|
|
FROM
|
|
|
|
Documents d
|
|
|
|
LEFT OUTER JOIN
|
|
|
|
DocumentstoTags dt ON dt.Document = d.ID
|
|
|
|
WHERE dt.Document IS NULL")->fetchArray(SQLITE3_NUM);
|
|
|
|
return $ret_count[0];
|
|
|
|
}
|
2017-03-07 09:38:47 +01:00
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function getTags()
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
$tag_query = "SELECT
|
|
|
|
ID, Name, count(Document)
|
|
|
|
FROM
|
|
|
|
Tags t
|
|
|
|
LEFT OUTER JOIN
|
|
|
|
DocumentstoTags d ON t.ID = d.Tag
|
|
|
|
GROUP BY Name
|
|
|
|
ORDER BY count(Document) DESC, Name";
|
|
|
|
$tags_ret = $this->db->query($tag_query);
|
|
|
|
$tags = [];
|
|
|
|
while ($row = $tags_ret->fetchArray(SQLITE3_NUM)) {
|
|
|
|
array_push($tags, array(
|
2019-04-30 19:13:45 +02:00
|
|
|
'id' => $row[0],
|
2017-03-07 09:38:47 +01:00
|
|
|
'name' => $row[1],
|
|
|
|
'count' => $row[2]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function fetchDoc($id)
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
$stmt_doc = $this->db->prepare("SELECT * FROM Documents WHERE ID = :id");
|
|
|
|
$stmt_doc->bindValue(":id", $id, SQLITE3_INTEGER);
|
|
|
|
$doc = $stmt_doc->execute()->fetchArray(SQLITE3_ASSOC);
|
|
|
|
|
|
|
|
$stmt_tags = $this->db->prepare("SELECT t.ID, t.Name FROM Tags t
|
|
|
|
JOIN DocumentsToTags dt ON t.ID = dt.Tag
|
|
|
|
JOIN Documents d on d.ID = dt.Document
|
|
|
|
WHERE d.ID = :id");
|
|
|
|
$stmt_tags->bindValue(":id", $id, SQLITE3_INTEGER);
|
|
|
|
$ret = $stmt_tags->execute();
|
|
|
|
$doc["tags"] = [];
|
|
|
|
while ($tag = $ret->fetchArray(SQLITE3_ASSOC)) {
|
|
|
|
array_push($doc["tags"], $tag);
|
|
|
|
}
|
|
|
|
return $doc;
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function listDocs()
|
|
|
|
{
|
|
|
|
if (func_num_args() > 0) {
|
2017-03-07 09:38:47 +01:00
|
|
|
$tag = func_get_arg(0);
|
2018-07-31 15:58:23 +02:00
|
|
|
if ($tag > 0) {
|
2017-03-07 09:38:47 +01:00
|
|
|
$stmt = $this->db->prepare("SELECT
|
|
|
|
ID, Title, Author, Published, URL
|
|
|
|
FROM
|
|
|
|
Documents d
|
|
|
|
LEFT OUTER JOIN
|
|
|
|
DocumentsToTags dt ON d.ID = dt.Document
|
|
|
|
WHERE Tag == :tag");
|
|
|
|
$stmt->bindValue(":tag", $tag, SQLITE3_INTEGER);
|
|
|
|
} else {
|
|
|
|
$stmt = $this->db->prepare("SELECT
|
|
|
|
ID, Title, Author, Published, URL
|
|
|
|
FROM
|
|
|
|
Documents d
|
|
|
|
LEFT OUTER JOIN
|
|
|
|
DocumentsToTags dt ON d.ID = dt.Document
|
|
|
|
WHERE dt.Document IS NULL");
|
|
|
|
}
|
|
|
|
$doc_ret = $stmt->execute();
|
|
|
|
} else {
|
|
|
|
$query = "SELECT ID, Title, Author, Published, URL FROM Documents";
|
|
|
|
$doc_ret = $this->db->query($query);
|
|
|
|
}
|
|
|
|
$docs = [];
|
|
|
|
while ($doc = $doc_ret->fetchArray(SQLITE3_ASSOC)) {
|
|
|
|
$doc['date'] = empty($doc["Published"]) ? "" : "(" . $doc["Published"] . ")";
|
|
|
|
array_push($docs, $doc);
|
|
|
|
}
|
|
|
|
return $docs;
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function updateDoc($id, $title, $author, $description, $published, $url, $tag_ids)
|
|
|
|
{
|
|
|
|
if (empty($id)) {
|
2017-03-07 09:38:47 +01:00
|
|
|
$stmt = $this->db->prepare("INSERT INTO Documents
|
|
|
|
(ID, Title, Author, Description, Published, URL)
|
|
|
|
VALUES
|
|
|
|
(NULL, :title, :author, :description, :published, :url)");
|
|
|
|
} else {
|
|
|
|
$stmt = $this->db->prepare("UPDATE Documents SET
|
|
|
|
Title=:title,
|
|
|
|
Author=:author,
|
|
|
|
Description=:description,
|
|
|
|
Published=:published,
|
|
|
|
URL=:url
|
|
|
|
WHERE ID = :id");
|
|
|
|
$stmt->bindValue(":id", $id, SQLITE3_INTEGER);
|
|
|
|
}
|
|
|
|
$stmt->bindValue(":title", $title, SQLITE3_TEXT);
|
|
|
|
$stmt->bindValue(":author", $author, SQLITE3_TEXT);
|
|
|
|
$stmt->bindValue(":description", $description, SQLITE3_TEXT);
|
|
|
|
$stmt->bindValue(":published", $published, SQLITE3_TEXT);
|
|
|
|
$stmt->bindValue(":url", $url, SQLITE3_TEXT);
|
|
|
|
$stmt->execute();
|
2018-07-31 15:58:23 +02:00
|
|
|
if (empty($id)) {
|
2017-03-07 09:38:47 +01:00
|
|
|
$id = $this->db->lastInsertRowid();
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
if (!empty($id)) {
|
2017-03-07 09:38:47 +01:00
|
|
|
$delete_stmt = $this->db->prepare("DELETE FROM DocumentsToTags
|
|
|
|
WHERE Document = :id");
|
|
|
|
$delete_stmt->bindValue(":id", $id, SQLITE3_INTEGER);
|
|
|
|
$delete_stmt->execute();
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
foreach ($tag_ids as $tag) {
|
2017-03-07 09:38:47 +01:00
|
|
|
$tag_stmt = $this->db->prepare("INSERT INTO DocumentsToTags ('Document', 'Tag')
|
|
|
|
VALUES (:doc, :tag)");
|
|
|
|
$tag_stmt->bindValue("doc", $id, SQLITE3_INTEGER);
|
|
|
|
$tag_stmt->bindValue("tag", $tag, SQLITE3_INTEGER);
|
|
|
|
$tag_stmt->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function removeDoc($id)
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
$doc_stmt = $this->db->prepare("DELETE FROM Documents
|
|
|
|
WHERE ID = :id");
|
|
|
|
$doc_stmt->bindValue("id", $id, SQLITE3_INTEGER);
|
|
|
|
$doc_stmt->execute();
|
2018-07-31 15:58:23 +02:00
|
|
|
|
2017-03-07 09:38:47 +01:00
|
|
|
$tag_stmt = $this->db->prepare("DELETE FROM DocumentsToTags
|
|
|
|
WHERE Document = :id");
|
|
|
|
$tag_stmt->bindValue("id", $id, SQLITE3_INTEGER);
|
|
|
|
$tag_stmt->execute();
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function findTag($name)
|
|
|
|
{
|
2017-03-07 21:04:01 +01:00
|
|
|
$stmt = $this->db->prepare("SELECT * FROM Tags WHERE Name == :name COLLATE NOCASE");
|
2017-03-07 09:38:47 +01:00
|
|
|
$stmt->bindValue(":name", $name, SQLITE3_TEXT);
|
|
|
|
return $stmt->execute()->fetchArray(SQLITE3_ASSOC);
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function fetchTag($tag)
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
$stmt = $this->db->prepare("SELECT * FROM Tags WHERE ID == :tag");
|
|
|
|
$stmt->bindValue(":tag", $tag, SQLITE3_INTEGER);
|
|
|
|
return $stmt->execute()->fetchArray(SQLITE3_ASSOC);
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function updateTag($id, $name, $description)
|
|
|
|
{
|
|
|
|
if (empty($id)) {
|
2017-03-07 09:38:47 +01:00
|
|
|
$stmt = $this->db->prepare("INSERT INTO Tags
|
2017-03-07 10:15:20 +01:00
|
|
|
(ID, Name, Description)
|
2017-03-07 09:38:47 +01:00
|
|
|
VALUES
|
2017-03-07 10:15:20 +01:00
|
|
|
(NULL, :name, :description)");
|
2017-03-07 09:38:47 +01:00
|
|
|
} else {
|
|
|
|
$stmt = $this->db->prepare("UPDATE Tags SET
|
|
|
|
Name=:name,
|
|
|
|
Description=:description,
|
|
|
|
Parent=:Parent
|
|
|
|
WHERE ID = :id");
|
|
|
|
$stmt->bindValue(":id", $id, SQLITE3_INTEGER);
|
|
|
|
}
|
|
|
|
$stmt->bindValue(":name", $name, SQLITE3_TEXT);
|
|
|
|
$stmt->bindValue(":description", $description, SQLITE3_TEXT);
|
|
|
|
return $stmt->execute();
|
|
|
|
}
|
|
|
|
|
2018-09-17 11:44:47 +02:00
|
|
|
public function deleteTag($tag)
|
|
|
|
{
|
|
|
|
$stmt = $this->db->prepare("DELETE FROM Tags WHERE ID == :tag");
|
|
|
|
$stmt->bindValue(":tag", $tag, SQLITE3_INTEGER);
|
|
|
|
return $stmt->execute();
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
public function authenticate($username, $password)
|
|
|
|
{
|
2017-03-07 09:38:47 +01:00
|
|
|
$stmt = $this->db->prepare("SELECT
|
|
|
|
*
|
|
|
|
FROM
|
|
|
|
Users
|
|
|
|
WHERE
|
|
|
|
Username = :username");
|
|
|
|
$stmt->bindValue(":username", $username, SQLITE3_TEXT);
|
|
|
|
$auth_ret = $stmt->execute();
|
|
|
|
$auth = $auth_ret->fetchArray(SQLITE3_ASSOC);
|
|
|
|
|
2018-07-31 15:58:23 +02:00
|
|
|
if (password_verify($password, $auth["Password"])) {
|
2017-03-07 09:38:47 +01:00
|
|
|
return $auth["ID"];
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 15:58:23 +02:00
|
|
|
|
2017-03-07 09:38:47 +01:00
|
|
|
?>
|