improve: use index in session to avoid parsing data dir each time

This commit is contained in:
quenousimporte 2024-03-07 10:02:11 +01:00
parent 6083f8ee26
commit c13d688e90
1 changed files with 37 additions and 14 deletions

View File

@ -18,6 +18,21 @@
exit;
}
session_start();
function savenote($title, $content)
{
global $dir;
file_put_contents($dir . '/' . $title, $content);
$key = array_search($dir . '/' . $title, $_SESSION['index']);
if ($key)
{
// handle the rename case
array_splice($_SESSION['index'], $key, 1);
array_unshift($_SESSION['index'], $dir . '/' . $title);
}
}
$action = '';
if (isset($_GET['open']))
@ -61,14 +76,14 @@
if ($action == 'clip' && $_GET['param'])
{
$content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/todo');
file_put_contents($dir . '/todo', $content);
savenote('todo', $content);
$_GET['param'] = '';
}
if ($action == 'save')
{
$title = $_POST['title'];
$content = $_POST['content'];
file_put_contents($dir . '/' . $title, $content);
savenote($title, $content);
$previoustitle = $_POST['previoustitle'];
if ($title != $previoustitle)
@ -76,18 +91,21 @@
rename($dir . '/' . $previoustitle, $dir . '/' . $previoustitle . '.del');
}
$action = 'open';
$_GET['param'] = $title;
if (isset($_POST['home']))
{
header('Location: index.php');
$action = '';
}
else
{
$action = 'open';
$_GET['param'] = $title;
}
}
else if ($action == 'delete')
{
$title = $_POST['title'];
rename($dir . '/' . $title, $dir . '/' . $title . '.del');
// reindex!
}
if ($action == 'preview')
@ -116,7 +134,7 @@
if (!file_exists($dir . '/' . $title))
{
$content = "---\r\ndate: " . substr($now, 0, 10) . "\r\ntags: \r\n---\r\n";
file_put_contents($dir . '/' . $title, $content);
savenote($title, $content);
}
else if (!$content)
{
@ -165,14 +183,19 @@
}
require('home.php');
echo '<div class="editor">';
$files = glob($dir . '/*');
usort($files, function($a, $b)
{
return filemtime($b) - filemtime($a);
});
echo '<div>';
foreach($files as $path)
if (!isset($_SESSION['index']) || isset($_GET['reindex']))
{
$files = glob($dir . '/*');
usort($files, function($a, $b)
{
return filemtime($b) - filemtime($a);
});
$_SESSION['index'] = $files;
}
foreach($_SESSION['index'] as $path)
{
$tags = '';
$name = basename($path);