From cec8f61ab84122a3f4b0702aaad6f7a141f99f7b Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 28 Mar 2012 17:24:44 +0200 Subject: [PATCH] editing works basically --- data/templates/edit-file.htm | 5 +++-- data/templates/edit.htm | 2 +- src/Phorkie/File.php | 10 +++++++++ src/Phorkie/Repository.php | 18 ++++++++++++++++ www/edit.php | 42 ++++++++++++++++++++++++++++++++++++ www/index.php | 3 ++- 6 files changed, 76 insertions(+), 4 deletions(-) diff --git a/data/templates/edit-file.htm b/data/templates/edit-file.htm index 66647f0..b22314d 100644 --- a/data/templates/edit-file.htm +++ b/data/templates/edit-file.htm @@ -2,7 +2,8 @@
- + +
@@ -14,6 +15,6 @@
- + diff --git a/data/templates/edit.htm b/data/templates/edit.htm index 56e08ef..300b6cb 100644 --- a/data/templates/edit.htm +++ b/data/templates/edit.htm @@ -2,7 +2,7 @@ {% block title %}Edit paste{% endblock %} {% block content %} -
+
diff --git a/src/Phorkie/File.php b/src/Phorkie/File.php index 6cdc833..3c6ea4b 100644 --- a/src/Phorkie/File.php +++ b/src/Phorkie/File.php @@ -58,6 +58,16 @@ class File return basename($this->path); } + /** + * Return the full path to the file + * + * @return string + */ + public function getPath() + { + return $this->path; + } + /** * Get file extension without dot * diff --git a/src/Phorkie/Repository.php b/src/Phorkie/Repository.php index aeccc72..5273b60 100644 --- a/src/Phorkie/Repository.php +++ b/src/Phorkie/Repository.php @@ -82,6 +82,9 @@ class Repository if ($base != $name) { throw new Exception('No directories supported for now'); } + if ($name == '') { + throw new Exception_Input('Empty file name given'); + } $path = $this->repoDir . '/' . $base; if (!is_readable($path)) { throw new Exception_Input('File does not exist'); @@ -89,6 +92,16 @@ class Repository return new File($path, $this); } + public function hasFile($name) + { + try { + $this->getFileByName($name); + } catch (Exception $e) { + return false; + } + return true; + } + public function getDescription() { if (!is_readable($this->repoDir . '/.git/description')) { @@ -97,6 +110,11 @@ class Repository return file_get_contents($this->repoDir . '/.git/description'); } + public function setDescription($description) + { + file_put_contents($this->repoDir . '/.git/description', $description); + } + /** * Get a link to the repository * diff --git a/www/edit.php b/www/edit.php index d86df41..3bcec6e 100644 --- a/www/edit.php +++ b/www/edit.php @@ -8,6 +8,48 @@ require_once 'www-header.php'; $repo = new Repository(); $repo->loadFromRequest(); +if (isset($_POST['files'])) { + $vc = $repo->getVc(); + $repo->setDescription($_POST['description']); + + $bChanged = false; + foreach ($_POST['files'] as $num => $arFile) { + if (!isset($arFile['original_name']) + || !$repo->hasFile($arFile['original_name']) + ) { + //FIXME: Show error message + continue; + } + //FIXME: fix file names from .. and ./ + if ($arFile['original_name'] != $arFile['name']) { + //FIXME: what to do with overwrites? + $vc->getCommand('mv') + ->addArgument($arFile['original_name']) + ->addArgument($arFile['name']) + ->execute(); + $bChanged = true; + } + $file = $repo->getFileByName($arFile['name']); + if ($file->getContent() != $arFile['content']) { + file_put_contents($file->getPath(), $arFile['content']); + $command = $vc->getCommand('add') + ->addArgument($file->getFilename()) + ->execute(); + $bChanged = true; + } + } + + if ($bChanged) { + $vc->getCommand('commit') + ->setOption('message', '') + ->setOption('allow-empty-message') + ->setOption('author', 'Anonymous ') + ->execute(); + } + + redirect($repo->getLink('display')); +} + render( 'edit', array( diff --git a/www/index.php b/www/index.php index f821c84..db3fd7b 100644 --- a/www/index.php +++ b/www/index.php @@ -21,10 +21,11 @@ if (isset($_POST['files'])) { foreach (glob($repo->repoDir . '/.git/hooks/*') as $hookfile) { unlink($hookfile); } - file_put_contents($repo->repoDir . '.git/description', $_POST['description']); + $repo->setDescription($_POST['description']); foreach ($_POST['files'] as $num => $arFile) { if ($arFile['name'] != '') { + //FIXME: fix file name from .. $fname = $arFile['name']; } else { $fname = 'phork' . $num . '.' . $arFile['type']; -- 2.30.2