mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-03-31 16:45:27 +00:00
implement page history recorder
This commit is contained in:
parent
dac79339e1
commit
374df89a4f
3 changed files with 89 additions and 12 deletions
|
|
@ -78,6 +78,11 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"history":
|
||||
{
|
||||
"enabled":true,
|
||||
"timeout":null
|
||||
},
|
||||
"header":
|
||||
{
|
||||
"margin":8,
|
||||
|
|
@ -129,10 +134,6 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"history":
|
||||
{
|
||||
"enabled":true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,6 +45,14 @@ class Page
|
|||
// Init history
|
||||
$this->history = new \Yggverse\Yoda\Model\History;
|
||||
|
||||
// Run database cleaner
|
||||
if ($this->config->history->timeout)
|
||||
{
|
||||
$this->app->database->cleanHistory(
|
||||
$this->config->history->timeout
|
||||
);
|
||||
}
|
||||
|
||||
// Compose header
|
||||
$this->header = new \GtkBox(
|
||||
\GtkOrientation::HORIZONTAL
|
||||
|
|
@ -364,17 +372,34 @@ class Page
|
|||
$history = false;
|
||||
}
|
||||
|
||||
// Ignore history record on same URL stored in DB
|
||||
if ($result = $this->app->database->getHistory(0, 1))
|
||||
{
|
||||
if ($url == $result[0]->url)
|
||||
{
|
||||
$history = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Update address field by requested
|
||||
$this->address->set_text(
|
||||
$url
|
||||
);
|
||||
|
||||
// Update history pool
|
||||
if ($history)
|
||||
{
|
||||
// Update history in memory pool
|
||||
$this->history->add(
|
||||
$url
|
||||
);
|
||||
|
||||
// Update history in the database
|
||||
if ($this->config->history->enabled)
|
||||
{
|
||||
$this->app->database->addHistory(
|
||||
$url
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Update home button sensibility on match requested
|
||||
|
|
|
|||
|
|
@ -6,16 +6,16 @@ namespace Yggverse\Yoda\Model;
|
|||
|
||||
class Database
|
||||
{
|
||||
public \PDO $database;
|
||||
public \PDO $_database;
|
||||
|
||||
public function __construct(
|
||||
string $database,
|
||||
string $database,
|
||||
?string $username = null,
|
||||
?string $password = null
|
||||
) {
|
||||
try
|
||||
{
|
||||
$this->database = new \PDO(
|
||||
$this->_database = new \PDO(
|
||||
sprintf(
|
||||
'sqlite:%s',
|
||||
$database
|
||||
|
|
@ -24,22 +24,22 @@ class Database
|
|||
$password
|
||||
);
|
||||
|
||||
$this->database->setAttribute(
|
||||
$this->_database->setAttribute(
|
||||
\PDO::ATTR_ERRMODE,
|
||||
\PDO::ERRMODE_EXCEPTION
|
||||
);
|
||||
|
||||
$this->database->setAttribute(
|
||||
$this->_database->setAttribute(
|
||||
\PDO::ATTR_DEFAULT_FETCH_MODE,
|
||||
\PDO::FETCH_OBJ
|
||||
);
|
||||
|
||||
$this->database->query('
|
||||
$this->_database->query('
|
||||
CREATE TABLE IF NOT EXISTS "history"
|
||||
(
|
||||
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
"time" INTEGER NOT NULL,
|
||||
"address" VARCHAR(1024) NOT NULL
|
||||
"url" VARCHAR(1024) NOT NULL
|
||||
)
|
||||
');
|
||||
}
|
||||
|
|
@ -54,4 +54,55 @@ class Database
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function addHistory(
|
||||
string $url
|
||||
): int
|
||||
{
|
||||
$query = $this->_database->prepare(
|
||||
'INSERT INTO `history` (`time`, `url`) VALUES (:time, :url)'
|
||||
);
|
||||
|
||||
$query->execute(
|
||||
[
|
||||
':time' => time(),
|
||||
':url' => $url
|
||||
]
|
||||
);
|
||||
|
||||
return (int) $this->_database->lastInsertId();
|
||||
}
|
||||
|
||||
public function getHistory(
|
||||
int $start = 0,
|
||||
int $limit = 1000
|
||||
): array
|
||||
{
|
||||
$query = $this->_database->query(
|
||||
sprintf(
|
||||
'SELECT * FROM `history` ORDER BY `id` DESC LIMIT %d,%d',
|
||||
$start,
|
||||
$limit
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
return $query->fetchAll();
|
||||
}
|
||||
|
||||
public function cleanHistory(
|
||||
int $timeout = 0
|
||||
): int
|
||||
{
|
||||
$query = $this->_database->query(
|
||||
sprintf(
|
||||
'DELETE FROM `history` WHERE `time` + %d < %d',
|
||||
$timeout,
|
||||
time()
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
return $query->rowCount();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue