diff options
author | Andrew Manning <tamanning@zoho.com> | 2016-05-27 22:19:05 -0400 |
---|---|---|
committer | Andrew Manning <tamanning@zoho.com> | 2016-05-27 22:19:05 -0400 |
commit | 4691c3ec01eda972e7b4cae4ec940c8c24d51b5a (patch) | |
tree | b088f7c8464f02a01d52078c9a8c390b83c75c21 | |
parent | ca78ebce6d254f244b4c1fbe25eb3aca40b26952 (diff) | |
download | volse-hubzilla-4691c3ec01eda972e7b4cae4ec940c8c24d51b5a.tar.gz volse-hubzilla-4691c3ec01eda972e7b4cae4ec940c8c24d51b5a.tar.bz2 volse-hubzilla-4691c3ec01eda972e7b4cae4ec940c8c24d51b5a.zip |
Add new page to wiki and redirect to editor page.
-rw-r--r-- | Zotlabs/Module/Wiki.php | 59 | ||||
-rw-r--r-- | include/wiki.php | 55 | ||||
-rw-r--r-- | view/tpl/wiki.tpl | 19 |
3 files changed, 120 insertions, 13 deletions
diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index 290c2e005..909b2c84d 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -49,18 +49,25 @@ class Wiki extends \Zotlabs\Web\Controller { 'acl' => populate_acl($channel_acl), 'bang' => '' ); -// $wikiheader = t('Wiki Sandbox'); -// $hide_editor = false; + + $resource_id = ''; + if(argc()>2) { + // Check if wiki exists andr redirect if it does not + $channel = get_channel_by_nick(argv(1)); + $w = wiki_exists_by_name($channel['channel_id'], argv(2)); + logger('wiki_Exists: ' . json_encode($w)); + if(!$w['id']) { + goaway('/'.argv(0).'/'.argv(1)); + } else { + $resource_id = $w['resource_id']; + } + } if(argc()<3) { $wikiheader = t('Wiki Sandbox'); $hide_editor = false; } elseif (argc()<4) { $wikiheader = 'Empty wiki: ' . rawurldecode(argv(2)); // show wiki name - $hide_editor = true; - // Check if wiki exists andr redirect if it does not - if(!wiki_exists_by_name(argv(2))['id']) { - goaway('/'.argv(0).'/'.argv(1)); - } + $hide_editor = true; } elseif (argc()<5) { $wikiheader = rawurldecode(argv(2)) . ': ' . rawurldecode(argv(3)); // show wiki name and page $hide_editor = false; @@ -70,6 +77,7 @@ class Wiki extends \Zotlabs\Web\Controller { '$wikiheader' => $wikiheader, '$hideEditor' => $hide_editor, '$channel' => $channel['channel_address'], + '$resource_id' => $resource_id, '$lockstate' => $x['lockstate'], '$acl' => $x['acl'], '$bang' => $x['bang'], @@ -166,6 +174,43 @@ class Wiki extends \Zotlabs\Web\Controller { json_return_and_die(array('success' => false)); } } + + // Create a page + if ((argc() === 4) && (argv(2) === 'create') && (argv(3) === 'page')) { + $which = argv(1); + $resource_id = $_POST['resource_id']; + // Determine if observer has permission to create wiki + if (local_channel()) { + $channel = \App::get_channel(); + } else { + $channel = get_channel_by_nick($which); + $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)); + } + } + $name = escape_tags(urlencode($_POST['name'])); //Get new wiki name + if($name === '') { + json_return_and_die(array('message' => 'Error creating page. Invalid name.', 'success' => false)); + } + $page = wiki_create_page($name . '.md', $resource_id); + if ($page['success']) { + json_return_and_die(array('url' => '/'.argv(0).'/'.argv(1).'/'.$page['wiki'].'/'.$name, 'success' => true)); + } else { + logger('Error creating page'); + json_return_and_die(array('message' => 'Error creating page.', 'success' => false)); + } + } notice('You must be authenticated.'); goaway('/wiki'); diff --git a/include/wiki.php b/include/wiki.php index 90b30d492..317db1239 100644 --- a/include/wiki.php +++ b/include/wiki.php @@ -125,15 +125,58 @@ function wiki_delete_wiki($resource_id) { } } -function wiki_exists_by_name($name) { - $item = q("SELECT id FROM item WHERE resource_type = '%s' AND title = '%s' AND item_deleted = 0 limit 1", +function wiki_exists_by_name($uid, $name) { + $item = q("SELECT id,resource_id FROM item WHERE resource_type = '%s' AND title = '%s' AND uid = '%s' AND item_deleted = 0 limit 1", dbesc(WIKI_ITEM_RESOURCE_TYPE), - dbesc($name) + dbesc($name), + dbesc($uid) ); if (!$item) { - return array('id' => null); + return array('id' => null, 'resource_id' => null); } else { - return array('id' => $item[0]['id']); + return array('id' => $item[0]['id'], 'resource_id' => $item[0]['resource_id']); } -}
\ No newline at end of file +} + +function wiki_get_permissions($resource_id, $owner_id, $observer_hash) { + // TODO: For now, only the owner can edit + $sql_extra = item_permissions_sql($owner_id, $observer_hash); + $r = q("SELECT * FROM item WHERE resource_type = '%s' AND resource_id = '%s' $sql_extra LIMIT 1", + dbesc(WIKI_ITEM_RESOURCE_TYPE), + dbesc($resource_id) + ); + if(!$r) { + return array('read' => false, 'write' => false, 'success' => false); + } else { + return array('read' => true, 'write' => false, 'success' => true); + } +} + +function wiki_create_page($name, $resource_id) { + $item = q("SELECT id,title,object FROM item WHERE resource_type = '%s' AND resource_id = '%s' AND item_deleted = 0 limit 1", + dbesc(WIKI_ITEM_RESOURCE_TYPE), + dbesc($resource_id) + ); + if (!$item) { + return array('page' => null, 'message' => 'Wiki item not found.', 'success' => false); + } + $object = json_decode($item[0]['object'], true); + $wikiname = $item[0]['title']; + if (!realpath(__DIR__ . '/../' . $object['path'])) { + return array('page' => null, 'message' => 'Wiki directory does not exist.', 'success' => false); + } + // Path to wiki exists + $abs_path = realpath(__DIR__ . '/../' . $object['path']); + $page_path = $abs_path . '/' . $name; + if (is_file($page_path)) { + return array('page' => null, 'message' => 'Page already exists.', 'success' => false); + } + // Create file called $name in the path + if(!touch($page_path)) { + return array('page' => null, 'message' => 'Page file cannot be created.', 'success' => false); + } else { + return array('wiki' => $wikiname, 'message' => '', 'success' => true); + } + +} diff --git a/view/tpl/wiki.tpl b/view/tpl/wiki.tpl index 652434c3e..b0824650c 100644 --- a/view/tpl/wiki.tpl +++ b/view/tpl/wiki.tpl @@ -76,6 +76,7 @@ </div> <script> + window.wiki_resource_id = '{{$resource_id}}'; $(document).ready(function () { // Show Edit tab first. Otherwise the Ace editor does not load. $("#wiki-nav-tabs li:eq(0) a").tab('show'); @@ -112,4 +113,22 @@ function wiki_delete_wiki(wikiName, resource_id) { } }, 'json'); } + + + $('#new-page-submit').click(function (ev) { + if (window.wiki_resource_id === '') { + window.console.log('You must have a wiki open in order to create pages.'); + ev.preventDefault(); + return false; + } + $.post("wiki/{{$channel}}/create/page", {name: $('#id_pageName').val(), resource_id: window.wiki_resource_id}, + function (data) { + if (data.success) { + window.location = data.url; + } else { + window.console.log('Error creating page.'); + } + }, 'json'); + ev.preventDefault(); + }); </script> |