Successful new wiki git repo and item table record

This commit is contained in:
Andrew Manning 2016-05-21 19:02:23 -04:00
parent 5f2baa59f5
commit 049147a9d7
5 changed files with 103 additions and 82 deletions

View File

@ -62,6 +62,7 @@ class Wiki extends \Zotlabs\Web\Controller {
} }
function post() { function post() {
require_once('include/wiki.php');
// TODO: Implement wiki API // TODO: Implement wiki API
@ -77,37 +78,39 @@ class Wiki extends \Zotlabs\Web\Controller {
// Create a new wiki // Create a new wiki
if ((argc() > 3) && (argv(2) === 'create') && (argv(3) === 'wiki')) { if ((argc() > 3) && (argv(2) === 'create') && (argv(3) === 'wiki')) {
$which = argv(1);
// Determine if observer has permission to create wiki // Determine if observer has permission to create wiki
if (local_channel()) { if (local_channel()) {
$channel = \App::get_channel(); $channel = \App::get_channel();
} else { } else {
$which = argv(1);
$channel = get_channel_by_nick($which); $channel = get_channel_by_nick($which);
$observer_hash = get_observer_hash();
// Figure out who the page owner is. // Figure out who the page owner is.
$perms = get_all_perms(intval($channel['channel_id']), get_observer_hash()); $perms = get_all_perms(intval($channel['channel_id']), $observer_hash);
if (!$perms['write_wiki']) { if (!$perms['write_wiki']) {
notice(t('Permission denied.') . EOL); notice(t('Permission denied.') . EOL);
json_return_and_die(array('success' => false)); json_return_and_die(array('success' => false));
} }
} }
$name = escape_tags(urlencode($_REQUEST['name'])); //Get new wiki name $name = escape_tags(urlencode($_REQUEST['wikiName'])); //Get new wiki name
if($name === '') {
notice('Error creating wiki. Invalid name.');
goaway('/wiki');
}
// Get ACL for permissions // Get ACL for permissions
$acl = new Zotlabs\Access\AccessList($channel); $acl = new \Zotlabs\Access\AccessList($channel);
$acl->set_from_array($_REQUEST); $acl->set_from_array($_REQUEST);
$r = wiki_create_wiki($channel, $observer_hash, $name, $acl);
$r = wiki_create_wiki($channel, $name, $acl);
if ($r['success']) { if ($r['success']) {
json_return_and_die(array('success' => true)); goaway('/wiki/'.$which.'/'.$name);
} else { } else {
json_return_and_die(array('success' => false)); notice('Error creating wiki');
goaway('/wiki');
} }
} }
json_return_and_die(array('success' => false)); json_return_and_die(array('success' => false));
} }
} }

View File

@ -76,6 +76,15 @@ class GitRepo {
} }
} }
public function initRepo() {
if(!$this->path) return false;
try {
return $this->git->init($this->path);
} catch (\PHPGit\Exception\GitException $ex) {
return false;
}
}
public function pull() { public function pull() {
try { try {
$success = $this->git->pull(); $success = $this->git->pull();

View File

@ -513,6 +513,7 @@ define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
define ( 'ACTIVITY_OBJ_EVENT', NAMESPACE_ACTIVITY_SCHEMA . 'event' ); define ( 'ACTIVITY_OBJ_EVENT', NAMESPACE_ACTIVITY_SCHEMA . 'event' );
define ( 'ACTIVITY_OBJ_GROUP', NAMESPACE_ACTIVITY_SCHEMA . 'group' ); define ( 'ACTIVITY_OBJ_GROUP', NAMESPACE_ACTIVITY_SCHEMA . 'group' );
define ( 'ACTIVITY_OBJ_GAME', NAMESPACE_ACTIVITY_SCHEMA . 'game' ); define ( 'ACTIVITY_OBJ_GAME', NAMESPACE_ACTIVITY_SCHEMA . 'game' );
define ( 'ACTIVITY_OBJ_WIKI', NAMESPACE_ACTIVITY_SCHEMA . 'wiki' );
define ( 'ACTIVITY_OBJ_TAGTERM', NAMESPACE_ZOT . '/activity/tagterm' ); define ( 'ACTIVITY_OBJ_TAGTERM', NAMESPACE_ZOT . '/activity/tagterm' );
define ( 'ACTIVITY_OBJ_PROFILE', NAMESPACE_ZOT . '/activity/profile' ); define ( 'ACTIVITY_OBJ_PROFILE', NAMESPACE_ZOT . '/activity/profile' );
define ( 'ACTIVITY_OBJ_THING', NAMESPACE_ZOT . '/activity/thing' ); define ( 'ACTIVITY_OBJ_THING', NAMESPACE_ZOT . '/activity/thing' );

View File

@ -4,6 +4,7 @@
* @brief Wiki related functions. * @brief Wiki related functions.
*/ */
use \Zotlabs\Storage\GitRepo as GitRepo;
define ( 'WIKI_ITEM_RESOURCE_TYPE', 'wiki' ); define ( 'WIKI_ITEM_RESOURCE_TYPE', 'wiki' );
function wiki_create() { function wiki_create() {
@ -24,8 +25,30 @@ function wiki_pages() {
return array('pages' => array('page1.md', 'page2.md')); return array('pages' => array('page1.md', 'page2.md'));
} }
function wiki_create_wiki($channel, $name, $acl) { function wiki_init_wiki($channel, $name) {
// Store the path as a relative path, but pass absolute path to mkdir
$path = 'store/[data]/git/'.$channel['channel_address'].'/wiki/'.$name;
if (!mkdir(__DIR__ . '/../' . $path, 0770, true)) {
logger('Error creating wiki path: ' . $name);
return null;
}
// Create GitRepo object
$git = new GitRepo($channel['channel_address'], null, false, $name, __DIR__ . '/../' . $path);
if(!$git->initRepo()) {
logger('Error creating new git repo in ' . $git->path);
return null;
}
return array('path' => $path);
}
function wiki_create_wiki($channel, $observer_hash, $name, $acl) {
$wikiinit = wiki_init_wiki($channel, $name);
if (!$wikiinit['path']) {
notice('Error creating wiki');
return array('item' => null, 'success' => false);
}
$path = $wikiinit['path'];
// Generate unique resource_id using the same method as item_message_id() // Generate unique resource_id using the same method as item_message_id()
do { do {
$dups = false; $dups = false;
@ -41,12 +64,8 @@ function wiki_create_wiki($channel, $name, $acl) {
$ac = $acl->get(); $ac = $acl->get();
$mid = item_message_id(); $mid = item_message_id();
$arr = array(); // Initialize the array of parameters for the post $arr = array(); // Initialize the array of parameters for the post
$perms = $acl->get();
$allow_cid = expand_acl($perms['allow_cid']);
$players = array($channel['channel_hash'], $allow_cid[0]);
$item_hidden = 0; // TODO: Allow form creator to send post to ACL about new game automatically $item_hidden = 0; // TODO: Allow form creator to send post to ACL about new game automatically
$game_url = z_root() . '/chess/' . $channel['channel_address'] . '/' . $resource_id; $wiki_url = z_root() . '/wiki/' . $channel['channel_address'] . '/' . $name;
$arr['aid'] = $channel['channel_account_id']; $arr['aid'] = $channel['channel_account_id'];
$arr['uid'] = $channel['channel_id']; $arr['uid'] = $channel['channel_id'];
$arr['mid'] = $mid; $arr['mid'] = $mid;
@ -55,28 +74,30 @@ function wiki_create_wiki($channel, $name, $acl) {
$arr['resource_type'] = WIKI_ITEM_RESOURCE_TYPE; $arr['resource_type'] = WIKI_ITEM_RESOURCE_TYPE;
$arr['resource_id'] = $resource_id; $arr['resource_id'] = $resource_id;
$arr['owner_xchan'] = $channel['channel_hash']; $arr['owner_xchan'] = $channel['channel_hash'];
$arr['author_xchan'] = $channel['channel_hash']; $arr['author_xchan'] = $observer_hash;
// Store info about the type of chess item using the "title" field $arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid'];
// Other types include 'move' for children items but may in the future include $arr['llink'] = $arr['plink'];
// additional types that will determine how the "object" field is interpreted $arr['title'] = $name; // name of new wiki;
$arr['title'] = $name;
$arr['allow_cid'] = $ac['allow_cid']; $arr['allow_cid'] = $ac['allow_cid'];
$arr['allow_gid'] = $ac['allow_gid'];
$arr['deny_cid'] = $ac['deny_cid'];
$arr['deny_gid'] = $ac['deny_gid'];
$arr['item_wall'] = 1; $arr['item_wall'] = 1;
$arr['item_origin'] = 1; $arr['item_origin'] = 1;
$arr['item_thread_top'] = 1; $arr['item_thread_top'] = 1;
$arr['item_private'] = intval($acl->is_private()); $arr['item_private'] = intval($acl->is_private());
$arr['verb'] = ACTIVITY_CREATE; $arr['verb'] = ACTIVITY_CREATE;
$arr['obj_type'] = ACTIVITY_OBJ_WIKI; $arr['obj_type'] = ACTIVITY_OBJ_WIKI;
$arr['object'] = $object; $arr['object'] = array('path' => $path);
$arr['body'] = '[table][tr][td][h1]New Chess Game[/h1][/td][/tr][tr][td][zrl='.$game_url.']Click here to play[/zrl][/td][/tr][/table]'; $arr['body'] = '[table][tr][td][h1]New Wiki[/h1][/td][/tr][tr][td][zrl=' . $wiki_url . ']' . $name . '[/zrl][/td][/tr][/table]';
$post = item_store($arr); $post = item_store($arr);
$item_id = $post['item_id']; $item_id = $post['item_id'];
if ($item_id) { if ($item_id) {
proc_run('php', "include/notifier.php", "activity", $item_id); proc_run('php', "include/notifier.php", "activity", $item_id);
return array('item' => $arr, 'status' => true); return array('item' => $arr, 'success' => true);
} else { } else {
return array('item' => null, 'status' => false); return array('item' => null, 'success' => false);
} }
} }

View File

@ -19,20 +19,19 @@
<div class="clear"></div> <div class="clear"></div>
</div> </div>
<div id="new-wiki-form-wrapper" class="section-content-tools-wrapper" style="display:none;"> <div id="new-wiki-form-wrapper" class="section-content-tools-wrapper" style="display:none;">
<form id="new-wiki-form" action="wiki/create/wiki" method="post" > <form id="new-wiki-form" action="wiki/{{$channel}}/create/wiki" method="post" >
<div class="clear"></div> <div class="clear"></div>
{{include file="field_input.tpl" field=$wikiName}} {{include file="field_input.tpl" field=$wikiName}}
<div class="btn-group pull-right"> <div class="btn-group pull-right">
<div id="acl-container">
<div id="profile-jot-submit-right" class="btn-group"> <div id="profile-jot-submit-right" class="btn-group">
<button id="dbtn-acl" class="btn btn-default btn-sm" data-toggle="modal" data-target="#aclModal" title="Permission settings" onclick="return false;"> <button id="dbtn-acl" class="btn btn-default btn-sm" data-toggle="modal" data-target="#aclModal" title="Permission settings" onclick="return false;">
<i id="jot-perms-icon" class="icon-{{$lockstate}} jot-icons">{{$bang}}</i> <i id="jot-perms-icon" class="fa fa-{{$lockstate}} jot-icons">{{$bang}}</i>
</button> </button>
</div> </div>
</div>
<button id="new-wiki-submit" class="btn btn-primary" type="submit" name="submit" >Create Wiki</button> <button id="new-wiki-submit" class="btn btn-primary" type="submit" name="submit" >Create Wiki</button>
</div> </div>
</form> <div class="clear"></div> </form>
<div class="clear"></div>
<hr> <hr>
</div> </div>
@ -99,16 +98,4 @@
ev.preventDefault(); ev.preventDefault();
}); });
$('#new-wiki-submit').click(function (ev) {
var wikiName = $('#id_wikiName').val();
$.post("wiki/{{$channel}}/create/wiki", {name: wikiName}, function (data) {
if (data.success) {
$('#wiki-preview').html(data.html);
} else {
window.console.log('Error previewing page.');
}
}, 'json');
ev.preventDefault();
});
</script> </script>