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