Created page history widget to dynamically fetch and display the git commit history for wiki pages.
This commit is contained in:
		| @@ -26,8 +26,11 @@ class Wiki extends \Zotlabs\Web\Controller { | ||||
| 	function get() { | ||||
| 		require_once('include/wiki.php'); | ||||
| 		require_once('include/acl_selectors.php'); | ||||
| 		// TODO: Combine the interface configuration into a unified object | ||||
| 		// Something like $interface = array('new_page_button' => false, 'new_wiki_button' => false, ...) | ||||
| 		$wiki_owner = false; | ||||
| 		$showNewWikiButton = false; | ||||
| 		$pageHistory = array(); | ||||
| 		if(local_channel()) { | ||||
| 			$channel = \App::get_channel(); | ||||
| 		} | ||||
| @@ -107,6 +110,7 @@ class Wiki extends \Zotlabs\Web\Controller { | ||||
| 			$hide_editor = false; | ||||
| 			$showPageControls = $wiki_owner; | ||||
| 			$showNewWikiButton = $wiki_owner; | ||||
| 			$pageHistory = wiki_page_history(array('resource_id' => $resource_id, 'page' => $pagename)); | ||||
| 		} | ||||
| 		require_once('library/markdown.php'); | ||||
| 		$renderedContent = Markdown(json_decode($content)); | ||||
| @@ -125,7 +129,8 @@ class Wiki extends \Zotlabs\Web\Controller { | ||||
| 			'$content' => $content, | ||||
| 			'$renderedContent' => $renderedContent, | ||||
| 			'$wikiName' => array('wikiName', t('Enter the name of your new wiki:'), '', ''), | ||||
| 			'$pageName' => array('pageName', t('Enter the name of the new page:'), '', '') | ||||
| 			'$pageName' => array('pageName', t('Enter the name of the new page:'), '', ''), | ||||
| 			'$pageHistory' => $pageHistory['history'] | ||||
| 		)); | ||||
| 		head_add_js('library/ace/ace.js'); | ||||
| 		return $o; | ||||
| @@ -236,7 +241,7 @@ class Wiki extends \Zotlabs\Web\Controller { | ||||
| 					json_return_and_die(array('success' => false));					 | ||||
| 				} | ||||
| 			} | ||||
| 			$name = escape_tags(urlencode($_POST['name'])); //Get new wiki name | ||||
| 			$name = escape_tags(urlencode($_POST['name'])); //Get new page name | ||||
| 			if($name === '') {				 | ||||
| 				json_return_and_die(array('message' => 'Error creating page. Invalid name.', 'success' => false)); | ||||
| 			} | ||||
| @@ -272,7 +277,7 @@ class Wiki extends \Zotlabs\Web\Controller { | ||||
| 			$resource_id = $_POST['resource_id'];  | ||||
| 			$pagename = escape_tags(urlencode($_POST['name']));  | ||||
| 			$content = escape_tags($_POST['content']); //Get new content | ||||
| 			// Determine if observer has permission to create wiki | ||||
| 			// Determine if observer has permission to save content | ||||
| 			if (local_channel()) { | ||||
| 				$channel = \App::get_channel(); | ||||
| 			} else { | ||||
| @@ -311,6 +316,31 @@ class Wiki extends \Zotlabs\Web\Controller { | ||||
| 			} | ||||
| 		} | ||||
| 		 | ||||
| 		// Update page history | ||||
| 		// /wiki/channel/history/page | ||||
| 		if ((argc() === 4) && (argv(2) === 'history') && (argv(3) === 'page')) { | ||||
| 			$which = argv(1); | ||||
| 			$resource_id = $_POST['resource_id']; | ||||
| 			$pagename = escape_tags(urlencode($_POST['name'])); | ||||
| 			// Determine if observer has permission to view content | ||||
| 			if (local_channel()) { | ||||
| 				$channel = \App::get_channel(); | ||||
| 			} else { | ||||
| 				$channel = get_channel_by_nick($which); | ||||
| 				$observer_hash = get_observer_hash(); | ||||
| 				$perms = wiki_get_permissions($resource_id, intval($channel['channel_id']), $observer_hash); | ||||
| 				if (!$perms['read']) { | ||||
| 					logger('Wiki read permission denied.' . EOL); | ||||
| 					json_return_and_die(array('historyHTML' => '', 'message' => 'Permission denied.', 'success' => false)); | ||||
| 				} | ||||
| 			} | ||||
| 			$historyHTML = widget_wiki_page_history(array( | ||||
| 					'resource_id' => $resource_id, | ||||
| 					'page' => $pagename | ||||
| 			)); | ||||
| 			json_return_and_die(array('historyHTML' => $historyHTML, 'message' => '', 'success' => true)); | ||||
| 		} | ||||
|  | ||||
| 		//notice('You must be authenticated.'); | ||||
| 		json_return_and_die(array('message' => 'You must be authenticated.', 'success' => false)); | ||||
| 		 | ||||
|   | ||||
| @@ -914,6 +914,17 @@ function widget_wiki_pages($arr) { | ||||
| 	)); | ||||
| } | ||||
|  | ||||
| function widget_wiki_page_history($arr) { | ||||
| 	require_once("include/wiki.php"); | ||||
| 	$pagename = ((array_key_exists('page', $arr)) ? $arr['page'] : ''); | ||||
| 	$resource_id = ((array_key_exists('resource_id', $arr)) ? $arr['resource_id'] : ''); | ||||
| 	$pageHistory = wiki_page_history(array('resource_id' => $resource_id, 'page' => $pagename)); | ||||
|  | ||||
| 	return replace_macros(get_markup_template('wiki_page_history.tpl'), array( | ||||
| 			'$pageHistory' => $pageHistory['history'] | ||||
| 	)); | ||||
| } | ||||
|  | ||||
| function widget_bookmarkedchats($arr) { | ||||
|  | ||||
| 	if(! feature_enabled(App::$profile['profile_uid'],'ajaxchat')) | ||||
|   | ||||
| @@ -95,12 +95,6 @@ function wiki_create_wiki($channel, $observer_hash, $name, $acl) { | ||||
| 	$arr['item_private'] = intval($acl->is_private()); | ||||
| 	$arr['verb'] = ACTIVITY_CREATE; | ||||
| 	$arr['obj_type'] = ACTIVITY_OBJ_WIKI; | ||||
| 	$arr['object'] = json_encode(array( | ||||
|         'type' => $arr['obj_type'],  | ||||
|         'title' => $arr['title'],  | ||||
|         'id' => $arr['resource_id'],  | ||||
|         'url' => $wiki_url | ||||
|     )); | ||||
| 	$arr['body'] = '[table][tr][td][h1]New Wiki[/h1][/td][/tr][tr][td][zrl=' . $wiki_url . ']' . $name . '[/zrl][/td][/tr][/table]'; | ||||
| 	// Save the path using iconfig. The file path should not be shared with other hubs | ||||
| 	if (!set_iconfig($arr, 'wiki', 'path', $path, false)) { | ||||
| @@ -221,6 +215,30 @@ function wiki_get_page_content($arr) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| function wiki_page_history($arr) { | ||||
| 	$pagename = ((array_key_exists('page',$arr)) ? $arr['page'] : ''); | ||||
| 	$resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : ''); | ||||
| 	$w = wiki_get_wiki($resource_id); | ||||
| 	if (!$w['path']) { | ||||
| 		return array('history' => null, 'message' => 'Error reading wiki', 'success' => false); | ||||
| 	} | ||||
| 	$page_path = $w['path'].'/'.$pagename; | ||||
| 	if (!is_readable($page_path) === true) { | ||||
| 		return array('history' => null, 'message' => 'Cannot read wiki page: ' . $page_path, 'success' => false); | ||||
| 	} | ||||
| 	$reponame = ((array_key_exists('title', $w['wiki'])) ? $w['wiki']['title'] : 'repo'); | ||||
| 	if($reponame === '') { | ||||
| 		$reponame = 'repo'; | ||||
| 	} | ||||
| 	$git = new GitRepo('sys', null, false, $w['wiki']['title'], $w['path']); | ||||
| 	try { | ||||
| 		$gitlog = $git->git->log('', $page_path , array('limit' => 50)); | ||||
| 		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); | ||||
| 	}	 | ||||
| } | ||||
|  | ||||
| function wiki_save_page($arr) { | ||||
| 	$pagename = ((array_key_exists('name',$arr)) ? $arr['name'] : ''); | ||||
|   | ||||
| @@ -59,6 +59,7 @@ | ||||
|     <ul class="nav nav-tabs" id="wiki-nav-tabs"> | ||||
|       <li><a data-toggle="tab" href="#edit-pane">Edit</a></li> | ||||
|       <li class="active"><a data-toggle="tab" href="#preview-pane" id="wiki-get-preview">Preview</a></li> | ||||
|       <li><a data-toggle="tab" href="#page-history-pane" id="wiki-get-history">History</a></li> | ||||
|       {{if $showPageControls}} | ||||
|       <li class="dropdown"> | ||||
|         <a data-toggle="dropdown" class="dropdown-toggle" href="#">Page <b class="caret"></b></a> | ||||
| @@ -69,7 +70,7 @@ | ||||
|       </li> | ||||
|       {{/if}} | ||||
|     </ul> | ||||
|     <div class="tab-content" id="myTabContent"> | ||||
|     <div class="tab-content" id="wiki-page-tabs"> | ||||
|  | ||||
|       <div id="edit-pane" class="tab-pane fade"> | ||||
|         <div id="ace-editor"></div> | ||||
| @@ -79,6 +80,21 @@ | ||||
|           {{$renderedContent}} | ||||
|         </div> | ||||
|       </div> | ||||
|       <div id="page-history-pane" class="tab-pane fade"> | ||||
|         <div id="page-history-list" class="section-content-wrapper"> | ||||
|           <table class="table-striped table-responsive table-hover" style="width: 100%;"> | ||||
|           {{foreach $pageHistory as $commit}} | ||||
|             <tr><td> | ||||
|             <table> | ||||
|               <tr><td>Date</td><td>{{$commit.date}}</td></tr> | ||||
|               <tr><td>Name</td><td>{{$commit.name}}</td></tr> | ||||
|               <tr><td>Message</td><td>{{$commit.title}}</td></tr> | ||||
|             </table> | ||||
|             </td></tr> | ||||
|           {{/foreach}}           | ||||
|           </table> | ||||
|         </div> | ||||
|       </div>      | ||||
|  | ||||
|  | ||||
|     </div> | ||||
| @@ -110,6 +126,17 @@ | ||||
|     ev.preventDefault(); | ||||
|   }); | ||||
|  | ||||
|   $('#wiki-get-history').click(function (ev) { | ||||
|     $.post("wiki/{{$channel}}/history/page", {name: window.wiki_page_name, resource_id: window.wiki_resource_id}, function (data) { | ||||
|       if (data.success) { | ||||
|         $('#page-history-list').html(data.historyHTML); | ||||
|       } else { | ||||
|         window.console.log('Error getting page history.'); | ||||
|       } | ||||
|     }, 'json'); | ||||
|     ev.preventDefault(); | ||||
|   }); | ||||
|    | ||||
| function wiki_delete_wiki(wikiName, resource_id) { | ||||
|   if(!confirm('Are you sure you want to delete the entire wiki: ' + JSON.stringify(wikiName))) { | ||||
|     return; | ||||
|   | ||||
							
								
								
									
										11
									
								
								view/tpl/wiki_page_history.tpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								view/tpl/wiki_page_history.tpl
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| <table class="table-striped table-responsive table-hover" style="width: 100%;"> | ||||
|   {{foreach $pageHistory as $commit}} | ||||
|   <tr><td> | ||||
|       <table> | ||||
|         <tr><td>Date</td><td>{{$commit.date}}</td></tr> | ||||
|         <tr><td>Name</td><td>{{$commit.name}}</td></tr> | ||||
|         <tr><td>Message</td><td>{{$commit.title}}</td></tr> | ||||
|       </table> | ||||
|     </td></tr> | ||||
|   {{/foreach}}           | ||||
| </table> | ||||
		Reference in New Issue
	
	Block a user