From 0a3fbdd128dd3b80868c93cb93901b501edf576c Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 5 Jun 2016 16:32:03 -0400 Subject: Basic page reversion implemented. The revert button on the history view replaces the editor text but does not save the page. --- Zotlabs/Module/Wiki.php | 35 +++++++++++++++++++++++++++++++++++ include/wiki.php | 39 ++++++++++++++++++++++++++++++++++++++- view/tpl/wiki.tpl | 17 +++++++++++++++++ view/tpl/wiki_page_history.tpl | 5 +++-- 4 files changed, 93 insertions(+), 3 deletions(-) diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index e335e8917..5b5bfe87f 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -401,6 +401,41 @@ class Wiki extends \Zotlabs\Web\Controller { } } + // Revert a page + if ((argc() === 4) && (argv(2) === 'revert') && (argv(3) === 'page')) { + $nick = argv(1); + $resource_id = $_POST['resource_id']; + $pageUrlName = $_POST['name']; + $commitHash = $_POST['commitHash']; + // Determine if observer has permission to revert pages + if (local_channel()) { + $channel = \App::get_channel(); + } else { + $channel = get_channel_by_nick($nick); + $observer_hash = get_observer_hash(); + // Figure out who the page owner is. + $perms = get_all_perms(intval($channel['channel_id']), $observer_hash); + // TODO: Create a new permission setting for wiki analogous to webpages. Until + // then, use webpage permissions + if (!$perms['write_pages']) { + logger('Wiki editing permission denied.' . EOL); + json_return_and_die(array('success' => false)); + } + $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash); + if(!$perms['write']) { + logger('Wiki write permission denied. Read only.' . EOL); + json_return_and_die(array('success' => false)); + } + } + $reverted = wiki_revert_page(array('commitHash' => $commitHash, 'observer' => \App::get_observer(), 'resource_id' => $resource_id, 'pageUrlName' => $pageUrlName)); + if($reverted['success']) { + json_return_and_die(array('content' => $reverted['content'], 'message' => '', 'success' => true)); + } else { + json_return_and_die(array('content' => '', 'message' => 'Error reverting page', 'success' => false)); + } + } + + //notice('You must be authenticated.'); json_return_and_die(array('message' => 'You must be authenticated.', 'success' => false)); diff --git a/include/wiki.php b/include/wiki.php index 1f90fae1e..cc948dee2 100644 --- a/include/wiki.php +++ b/include/wiki.php @@ -263,7 +263,6 @@ function wiki_page_history($arr) { $git = new GitRepo('', null, false, $w['wiki']['title'], $w['path']); try { $gitlog = $git->git->log('', $page_path , array('limit' => 500)); - logger('gitlog: ' . json_encode($gitlog)); return array('history' => $gitlog, 'message' => '', 'success' => true); } catch (\PHPGit\Exception\GitException $e) { return array('history' => null, 'message' => 'GitRepo error thrown', 'success' => false); @@ -307,6 +306,44 @@ function wiki_delete_page($arr) { } } +function wiki_revert_page($arr) { + $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : ''); + $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : ''); + $commitHash = ((array_key_exists('commitHash',$arr)) ? $arr['commitHash'] : null); + if (! $commitHash) { + return array('content' => $content, 'message' => 'No commit has provided', 'success' => false); + } + $w = wiki_get_wiki($resource_id); + if (!$w['path']) { + return array('content' => $content, 'message' => 'Error reading wiki', 'success' => false); + } + $page_path = $w['path'].'/'.$pageUrlName.'.md'; + if (is_writable($page_path) === true) { + + $reponame = ((array_key_exists('title', $w['wiki'])) ? urlencode($w['wiki']['title']) : 'repo'); + if($reponame === '') { + $reponame = 'repo'; + } + $git = new GitRepo($observer['xchan_addr'], null, false, $w['wiki']['title'], $w['path']); + $content = null; + try { + $git->setIdentity($observer['xchan_name'], $observer['xchan_addr']); + foreach ($git->git->tree($commitHash) as $object) { + logger('tree object: ' . json_encode($object)); + if ($object['type'] == 'blob' && $object['file'] === $pageUrlName.'.md' ) { + $content = $git->git->cat->blob($object['hash']); + logger('content: ' . json_encode($content)); + } + } + } catch (\PHPGit\Exception\GitException $e) { + json_return_and_die(array('content' => $content, 'message' => 'GitRepo error thrown', 'success' => false)); + } + return array('content' => $content, 'message' => '', 'success' => true); + } else { + return array('content' => $content, 'message' => 'Page file not writable', 'success' => false); + } +} + function wiki_git_commit($arr) { $files = ((array_key_exists('files', $arr)) ? $arr['files'] : null); $commit_msg = ((array_key_exists('commit_msg', $arr)) ? $arr['commit_msg'] : 'Repo updated'); diff --git a/view/tpl/wiki.tpl b/view/tpl/wiki.tpl index 6cdfabdb9..d9a490bf8 100644 --- a/view/tpl/wiki.tpl +++ b/view/tpl/wiki.tpl @@ -232,4 +232,21 @@ function wiki_delete_wiki(wikiHtmlName, resource_id) { }, 'json'); ev.preventDefault(); }); + + function wiki_revert_page(commitHash) { + if (window.wiki_resource_id === '' || window.wiki_page_name === '') { + window.console.log('You must have a wiki page open in order to revert pages.'); + return false; + } + $.post("wiki/{{$channel}}/revert/page", {commitHash: commitHash, name: window.wiki_page_name, resource_id: window.wiki_resource_id}, + function (data) { + if (data.success) { + window.console.log('Reverted content: ' + data.content); + // put contents in editor + editor.getSession().setValue(data.content); + } else { + window.console.log('Error reverting page.'); + } + }, 'json'); + } diff --git a/view/tpl/wiki_page_history.tpl b/view/tpl/wiki_page_history.tpl index 7efc4aa96..b124d4cb5 100644 --- a/view/tpl/wiki_page_history.tpl +++ b/view/tpl/wiki_page_history.tpl @@ -1,8 +1,9 @@ {{foreach $pageHistory as $commit}}
- - +
Date{{$commit.date}}
+
Date{{$commit.date}} +
Name{{$commit.name}}
Message{{$commit.title}}
-- cgit v1.2.3