diff options
author | hubzilla <git@macgirvin.com> | 2016-06-16 13:01:49 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-16 13:01:49 +1000 |
commit | e0b705fd54cb68901f0dda9835ccbaf2dae80d7f (patch) | |
tree | 186bf04cf0a1d8f2ef43597a62d0c94a6e0da9b2 | |
parent | 854c23a751bc8a24e4c43e2d16175bcef6021663 (diff) | |
parent | fc7c4e64ff71bfccdbad763ee60096e3d262d277 (diff) | |
download | volse-hubzilla-e0b705fd54cb68901f0dda9835ccbaf2dae80d7f.tar.gz volse-hubzilla-e0b705fd54cb68901f0dda9835ccbaf2dae80d7f.tar.bz2 volse-hubzilla-e0b705fd54cb68901f0dda9835ccbaf2dae80d7f.zip |
Merge pull request #422 from anaqreon/wiki
Wiki rename page feature added. Bug fixes related to git commits.
-rw-r--r-- | Zotlabs/Module/Wiki.php | 54 | ||||
-rw-r--r-- | include/wiki.php | 55 | ||||
-rw-r--r-- | view/tpl/wiki.tpl | 41 |
3 files changed, 136 insertions, 14 deletions
diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index f1f7b87b2..9e7d151b5 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -72,7 +72,8 @@ class Wiki extends \Zotlabs\Web\Controller { switch (argc()) { case 2: // Configure page template - $wikiheader = t('Wiki Sandbox'); + $wikiheaderName = t('Wiki'); + $wikiheaderPage = t('Sandbox'); $content = '"# Wiki Sandbox\n\nContent you **edit** and **preview** here *will not be saved*."'; $hide_editor = false; $showPageControls = false; @@ -113,7 +114,8 @@ class Wiki extends \Zotlabs\Web\Controller { } else { $wiki_editor = true; } - $wikiheader = urldecode($wikiUrlName) . ': ' . urldecode($pageUrlName); // show wiki name and page + $wikiheaderName = urldecode($wikiUrlName); + $wikiheaderPage = urldecode($pageUrlName); $p = wiki_get_page_content(array('resource_id' => $resource_id, 'pageUrlName' => $pageUrlName)); if(!$p['success']) { notice('Error retrieving page content' . EOL); @@ -135,7 +137,8 @@ class Wiki extends \Zotlabs\Web\Controller { require_once('library/markdown.php'); $o .= replace_macros(get_markup_template('wiki.tpl'),array( - '$wikiheader' => $wikiheader, + '$wikiheaderName' => $wikiheaderName, + '$wikiheaderPage' => $wikiheaderPage, '$hideEditor' => $hide_editor, '$showPageControls' => $showPageControls, '$showNewWikiButton'=> $showNewWikiButton, @@ -152,6 +155,7 @@ class Wiki extends \Zotlabs\Web\Controller { '$renderedContent' => Markdown(json_decode($content)), '$wikiName' => array('wikiName', t('Enter the name of your new wiki:'), '', ''), '$pageName' => array('pageName', t('Enter the name of the new page:'), '', ''), + '$pageRename' => array('pageRename', t('Enter the new name:'), '', ''), '$commitMsg' => array('commitMsg', '', '', '', '', 'placeholder="(optional) Enter a custom message when saving the page..."'), '$pageHistory' => $pageHistory['history'] )); @@ -377,7 +381,7 @@ class Wiki extends \Zotlabs\Web\Controller { if($deleted['success']) { $ob = \App::get_observer(); $commit = wiki_git_commit(array( - 'commit_msg' => 'Deleted ' . $pageHtmlName, + 'commit_msg' => 'Deleted ' . $pageUrlName, 'resource_id' => $resource_id, 'observer' => $ob, 'files' => null @@ -416,6 +420,48 @@ class Wiki extends \Zotlabs\Web\Controller { } } + // Rename a page + if ((argc() === 4) && (argv(2) === 'rename') && (argv(3) === 'page')) { + $resource_id = $_POST['resource_id']; + $pageUrlName = $_POST['oldName']; + $pageNewName = $_POST['newName']; + if ($pageUrlName === 'Home') { + json_return_and_die(array('message' => 'Cannot rename Home','success' => false)); + } + if(urlencode(escape_tags($pageNewName)) === '') { + json_return_and_die(array('message' => 'Error renaming page. Invalid name.', 'success' => false)); + } + // Determine if observer has permission to rename pages + $nick = argv(1); + $channel = get_channel_by_nick($nick); + if (local_channel() !== intval($channel['channel_id'])) { + $observer_hash = get_observer_hash(); + $perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash); + if(!$perms['write']) { + logger('Wiki write permission denied. ' . EOL); + json_return_and_die(array('success' => false)); + } + } + $renamed = wiki_rename_page(array('resource_id' => $resource_id, 'pageUrlName' => $pageUrlName, 'pageNewName' => $pageNewName)); + logger('$renamed: ' . json_encode($renamed)); + if($renamed['success']) { + $ob = \App::get_observer(); + $commit = wiki_git_commit(array( + 'commit_msg' => 'Renamed ' . urldecode($pageUrlName) . ' to ' . $renamed['page']['htmlName'], + 'resource_id' => $resource_id, + 'observer' => $ob, + 'files' => array($pageUrlName . '.md', $renamed['page']['fileName']), + 'all' => true + )); + if($commit['success']) { + json_return_and_die(array('name' => $renamed['page'], 'message' => 'Wiki git repo commit made', 'success' => true)); + } else { + json_return_and_die(array('message' => 'Error making git commit','success' => false)); + } + } else { + json_return_and_die(array('message' => 'Error renaming 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 4aa3fc1b4..d60f4a3a7 100644 --- a/include/wiki.php +++ b/include/wiki.php @@ -231,6 +231,34 @@ function wiki_create_page($name, $resource_id) { } +function wiki_rename_page($arr) { + $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : ''); + $pageNewName = ((array_key_exists('pageNewName',$arr)) ? $arr['pageNewName'] : ''); + $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : ''); + $w = wiki_get_wiki($resource_id); + if (!$w['path']) { + return array('message' => 'Wiki not found.', 'success' => false); + } + $page_path_old = $w['path'].'/'.$pageUrlName.'.md'; + logger('$page_path_old: ' . $page_path_old); + if (!is_readable($page_path_old) === true) { + return array('message' => 'Cannot read wiki page: ' . $page_path_old, 'success' => false); + } + $page = array('rawName' => $pageNewName, 'htmlName' => escape_tags($pageNewName), 'urlName' => urlencode(escape_tags($pageNewName)), 'fileName' => urlencode(escape_tags($pageNewName)).'.md'); + $page_path_new = $w['path'] . '/' . $page['fileName'] ; + logger('$page_path_new: ' . $page_path_new); + if (is_file($page_path_new)) { + return array('message' => 'Page already exists.', 'success' => false); + } + // Rename the page file in the wiki repo + if(!rename($page_path_old, $page_path_new)) { + return array('message' => 'Error renaming page file.', 'success' => false); + } else { + return array('page' => $page, 'message' => '', 'success' => true); + } + +} + function wiki_get_page_content($arr) { $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : ''); $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : ''); @@ -342,7 +370,7 @@ function wiki_revert_page($arr) { } } } catch (\PHPGit\Exception\GitException $e) { - json_return_and_die(array('content' => $content, 'message' => 'GitRepo error thrown', 'success' => false)); + return array('content' => $content, 'message' => 'GitRepo error thrown', 'success' => false); } return array('content' => $content, 'message' => '', 'success' => true); } else { @@ -352,9 +380,18 @@ function wiki_revert_page($arr) { function wiki_git_commit($arr) { $files = ((array_key_exists('files', $arr)) ? $arr['files'] : null); + $all = ((array_key_exists('all', $arr)) ? $arr['all'] : false); $commit_msg = ((array_key_exists('commit_msg', $arr)) ? $arr['commit_msg'] : 'Repo updated'); - $resource_id = ((array_key_exists('resource_id', $arr)) ? $arr['resource_id'] : json_return_and_die(array('message' => 'Wiki resource_id required for git commit', 'success' => false))); - $observer = ((array_key_exists('observer', $arr)) ? $arr['observer'] : json_return_and_die(array('message' => 'Observer required for git commit', 'success' => false))); + if(array_key_exists('resource_id', $arr)) { + $resource_id = $arr['resource_id']; + } else { + return array('message' => 'Wiki resource_id required for git commit', 'success' => false); + } + if(array_key_exists('observer', $arr)) { + $observer = $arr['observer']; + } else { + return array('message' => 'Observer required for git commit', 'success' => false); + } $w = wiki_get_wiki($resource_id); if (!$w['path']) { return array('message' => 'Error reading wiki', 'success' => false); @@ -369,23 +406,23 @@ function wiki_git_commit($arr) { if ($files === null) { $options = array('all' => true); // git commit option to include all changes } else { - $options = array(); // git commit options + $options = array('all' => $all); // git commit options\ foreach ($files as $file) { if (!$git->git->add($file)) { // add specified files to the git repo stage if (!$git->git->reset->hard()) { - json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file . '. Error resetting git repo.', 'success' => false)); + return array('message' => 'Error adding file to git stage: ' . $file . '. Error resetting git repo.', 'success' => false); } - json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file, 'success' => false)); + return array('message' => 'Error adding file to git stage: ' . $file, 'success' => false); } } } if ($git->commit($commit_msg, $options)) { - json_return_and_die(array('message' => 'Wiki repo commit succeeded', 'success' => true)); + return array('message' => 'Wiki repo commit succeeded', 'success' => true); } else { - json_return_and_die(array('message' => 'Wiki repo commit failed', 'success' => false)); + return array('message' => 'Wiki repo commit failed', 'success' => false); } } catch (\PHPGit\Exception\GitException $e) { - json_return_and_die(array('message' => 'GitRepo error thrown', 'success' => false)); + return array('message' => 'GitRepo error thrown', 'success' => false); } } diff --git a/view/tpl/wiki.tpl b/view/tpl/wiki.tpl index 1312f8518..dc78aad9f 100644 --- a/view/tpl/wiki.tpl +++ b/view/tpl/wiki.tpl @@ -25,7 +25,7 @@ <button id="inline-btn" type="button" class="btn btn-default btn-xs" onclick="makeFullScreen(false); adjustInlineTopBarHeight();"><i class="fa fa-compress"></i></button> </div> - <h2>{{$wikiheader}}</h2> + <h2><span id="wiki-header-name">{{$wikiheaderName}}</span>: <span id="wiki-header-page">{{$wikiheaderPage}}</span></h2> <div class="clear"></div> </div> <div id="new-wiki-form-wrapper" class="section-content-tools-wrapper" style="display:none;"> @@ -57,6 +57,17 @@ <hr> </div> + <div id="rename-page-form-wrapper" class="section-content-tools-wrapper" style="display:none;"> + <form id="rename-page-form" action="wiki/rename/page" method="post" > + <div class="clear"></div> + {{include file="field_input.tpl" field=$pageRename}} + <div class="btn-group pull-right"> + <button id="rename-page-submit" class="btn btn-warning" type="submit" name="submit" >Rename Page</button> + </div> + </form> <div class="clear"></div> + <hr> + </div> + <div id="wiki-content-container" class="section-content-wrapper" {{if $hideEditor}}style="display: none;"{{/if}}> <ul class="nav nav-tabs" id="wiki-nav-tabs"> <li><a data-toggle="tab" href="#edit-pane">Edit</a></li> @@ -67,6 +78,7 @@ <a data-toggle="dropdown" class="dropdown-toggle" href="#">Page <b class="caret"></b></a> <ul class="dropdown-menu"> <li><a id="save-page" data-toggle="tab" href="#">Save</a></li> + <li><a id="rename-page" data-toggle="tab" href="#">Rename</a></li> <li><a id="delete-page" data-toggle="tab" href="#">Delete</a></li> </ul> </li> @@ -115,7 +127,34 @@ if (window.wiki_page_name === 'Home') { $('#delete-page').hide(); + $('#rename-page').hide(); } + + $('#rename-page').click(function (ev) { + $('#rename-page-form-wrapper').show(); + }); + + $( "#rename-page-form" ).submit(function( event ) { + $.post("wiki/{{$channel}}/rename/page", + { + oldName: window.wiki_page_name, + newName: $('#id_pageRename').val(), + resource_id: window.wiki_resource_id + }, + function (data) { + if (data.success) { + $('#rename-page-form-wrapper').hide(); + window.console.log('data: ' + JSON.stringify(data)); + window.wiki_page_name = data.name.urlName; + $('#wiki-header-page').html(data.name.htmlName); + wiki_refresh_page_list(); + } else { + window.console.log('Error renaming page.'); + } + }, 'json'); + event.preventDefault(); + }); + $(document).ready(function () { wiki_refresh_page_list(); // Show Edit tab first. Otherwise the Ace editor does not load. |