diff --git a/Zotlabs/Module/Siteinfo.php b/Zotlabs/Module/Siteinfo.php index 41f6e9f0b..a15e2896d 100644 --- a/Zotlabs/Module/Siteinfo.php +++ b/Zotlabs/Module/Siteinfo.php @@ -27,28 +27,11 @@ class Siteinfo extends \Zotlabs\Web\Controller { else { $version = $commit = ''; } - $visible_plugins = array(); - if(is_array(\App::$plugins) && count(\App::$plugins)) { - $r = q("select * from addon where hidden = 0"); - if(count($r)) - foreach($r as $rr) - $visible_plugins[] = $rr['name']; - } - - $plugins_list = ''; - if(count($visible_plugins)) { - $plugins_text = t('Installed plugins/addons/apps:'); - $sorted = $visible_plugins; - $s = ''; - sort($sorted); - foreach($sorted as $p) { - if(strlen($p)) { - if(strlen($s)) $s .= ', '; - $s .= $p; - } - } - $plugins_list .= $s; - } + + $plugins_list = implode(', ',visible_plugin_list()); + + if($plugins_list) + $plugins_text = t('Installed plugins/addons/apps:'); else $plugins_text = t('No installed plugins/addons/apps'); diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index 9e7d151b5..bef831de8 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -136,6 +136,16 @@ class Wiki extends \Zotlabs\Web\Controller { // Render the Markdown-formatted page content in HTML require_once('library/markdown.php'); + $wikiModalID = random_string(3); + $wikiModal = replace_macros( + get_markup_template('generic_modal.tpl'), array( + '$id' => $wikiModalID, + '$title' => t('Revision Comparison'), + '$ok' => t('Revert'), + '$cancel' => t('Cancel') + ) + ); + $o .= replace_macros(get_markup_template('wiki.tpl'),array( '$wikiheaderName' => $wikiheaderName, '$wikiheaderPage' => $wikiheaderPage, @@ -157,7 +167,10 @@ class Wiki extends \Zotlabs\Web\Controller { '$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'] + '$pageHistory' => $pageHistory['history'], + '$wikiModal' => $wikiModal, + '$wikiModalID' => $wikiModalID, + '$commit' => 'HEAD' )); head_add_js('library/ace/ace.js'); // Ace Code Editor return $o; @@ -412,7 +425,7 @@ class Wiki extends \Zotlabs\Web\Controller { json_return_and_die(array('success' => false)); } } - $reverted = wiki_revert_page(array('commitHash' => $commitHash, 'observer' => \App::get_observer(), 'resource_id' => $resource_id, 'pageUrlName' => $pageUrlName)); + $reverted = wiki_revert_page(array('commitHash' => $commitHash, 'resource_id' => $resource_id, 'pageUrlName' => $pageUrlName)); if($reverted['success']) { json_return_and_die(array('content' => $reverted['content'], 'message' => '', 'success' => true)); } else { @@ -420,6 +433,32 @@ class Wiki extends \Zotlabs\Web\Controller { } } + // Compare page revisions + if ((argc() === 4) && (argv(2) === 'compare') && (argv(3) === 'page')) { + $resource_id = $_POST['resource_id']; + $pageUrlName = $_POST['name']; + $compareCommit = $_POST['compareCommit']; + $currentCommit = $_POST['currentCommit']; + // Determine if observer has permission to revert 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['read']) { + logger('Wiki read permission denied.' . EOL); + json_return_and_die(array('success' => false)); + } + } + $compare = wiki_compare_page(array('currentCommit' => $currentCommit, 'compareCommit' => $compareCommit, 'resource_id' => $resource_id, 'pageUrlName' => $pageUrlName)); + if($compare['success']) { + $diffHTML = '
Current RevisionSelected Revision
' . $compare['diff']; + json_return_and_die(array('diff' => $diffHTML, 'message' => '', 'success' => true)); + } else { + json_return_and_die(array('diff' => '', 'message' => 'Error comparing page', 'success' => false)); + } + } + // Rename a page if ((argc() === 4) && (argv(2) === 'rename') && (argv(3) === 'page')) { $resource_id = $_POST['resource_id']; diff --git a/include/attach.php b/include/attach.php index dae658d8e..dbcb0f930 100644 --- a/include/attach.php +++ b/include/attach.php @@ -618,7 +618,7 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) { ); if($r) { $overwrite = get_pconfig($channel_id,'system','overwrite_dup_files'); - if($overwrite) { + if(($overwrite) || ($options === 'import')) { $options = 'replace'; $existing_id = $x[0]['id']; $existing_size = intval($x[0]['filesize']); @@ -802,7 +802,7 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) { if($is_photo) { - $args = array( 'source' => $source, 'visible' => $visible, 'resource_id' => $hash, 'album' => basename($pathname), 'os_path' => $os_basepath . $os_relpath, 'filename' => $filename, 'getimagesize' => $gis, 'directory' => $direct); + $args = array( 'source' => $source, 'visible' => $visible, 'resource_id' => $hash, 'album' => basename($pathname), 'os_path' => $os_basepath . $os_relpath, 'filename' => $filename, 'getimagesize' => $gis, 'directory' => $direct, 'options' => $options ); if($arr['contact_allow']) $args['contact_allow'] = $arr['contact_allow']; if($arr['group_allow']) diff --git a/include/network.php b/include/network.php index 062f2c2bf..41d13a40e 100644 --- a/include/network.php +++ b/include/network.php @@ -1992,14 +1992,7 @@ function get_site_info() { else $service_class = false; - $visible_plugins = array(); - if(is_array(App::$plugins) && count(App::$plugins)) { - $r = q("select * from addon where hidden = 0"); - if(count($r)) - foreach($r as $rr) - $visible_plugins[] = $rr['aname']; - } - sort($visible_plugins); + $visible_plugins = visible_plugin_list(); if(@is_dir('.git') && function_exists('shell_exec')) $commit = trim(@shell_exec('git log -1 --format="%h"')); diff --git a/include/plugin.php b/include/plugin.php index 9b84039a6..c95f8cbf9 100755 --- a/include/plugin.php +++ b/include/plugin.php @@ -167,6 +167,12 @@ function reload_plugins() { } } +function visible_plugin_list() { + $r = q("select * from addon where hidden = 0 order by aname asc"); + return(($r) ? ids_to_array($r,'aname') : array()); +} + + /** * @brief registers a hook. diff --git a/include/text.php b/include/text.php index 22f4556a8..4f28c6dbc 100644 --- a/include/text.php +++ b/include/text.php @@ -2050,7 +2050,7 @@ function ids_to_array($arr,$idx = 'id') { $t = array(); if($arr) { foreach($arr as $x) { - if(! in_array($x[$idx],$t)) { + if(array_key_exists($idx,$x) && strlen($x[$idx]) && (! in_array($x[$idx],$t))) { $t[] = $x[$idx]; } } diff --git a/include/wiki.php b/include/wiki.php index d60f4a3a7..a89db3358 100644 --- a/include/wiki.php +++ b/include/wiki.php @@ -347,7 +347,7 @@ function wiki_revert_page($arr) { $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); + return array('content' => $content, 'message' => 'No commit was provided', 'success' => false); } $w = wiki_get_wiki($resource_id); if (!$w['path']) { @@ -378,6 +378,48 @@ function wiki_revert_page($arr) { } } +function wiki_compare_page($arr) { + $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : ''); + $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : ''); + $currentCommit = ((array_key_exists('currentCommit',$arr)) ? $arr['currentCommit'] : 'HEAD'); + $compareCommit = ((array_key_exists('compareCommit',$arr)) ? $arr['compareCommit'] : null); + if (! $compareCommit) { + return array('message' => 'No compare commit was provided', 'success' => false); + } + $w = wiki_get_wiki($resource_id); + if (!$w['path']) { + return array('message' => 'Error reading wiki', 'success' => false); + } + $page_path = $w['path'].'/'.$pageUrlName.'.md'; + if (is_readable($page_path) === true) { + $reponame = ((array_key_exists('title', $w['wiki'])) ? urlencode($w['wiki']['title']) : 'repo'); + if($reponame === '') { + $reponame = 'repo'; + } + $git = new GitRepo('', null, false, $w['wiki']['title'], $w['path']); + $compareContent = $currentContent = ''; + try { + foreach ($git->git->tree($currentCommit) as $object) { + if ($object['type'] == 'blob' && $object['file'] === $pageUrlName.'.md' ) { + $currentContent = $git->git->cat->blob($object['hash']); + } + } + foreach ($git->git->tree($compareCommit) as $object) { + if ($object['type'] == 'blob' && $object['file'] === $pageUrlName.'.md' ) { + $compareContent = $git->git->cat->blob($object['hash']); + } + } + require_once('library/class.Diff.php'); + $diff = Diff::toTable(Diff::compare($currentContent, $compareContent)); + } catch (\PHPGit\Exception\GitException $e) { + return array('message' => 'GitRepo error thrown', 'success' => false); + } + return array('diff' => $diff, 'message' => '', 'success' => true); + } else { + return array('message' => 'Page file not writable', 'success' => false); + } +} + function wiki_git_commit($arr) { $files = ((array_key_exists('files', $arr)) ? $arr['files'] : null); $all = ((array_key_exists('all', $arr)) ? $arr['all'] : false); diff --git a/library/class.Diff.php b/library/class.Diff.php new file mode 100644 index 000000000..689abe9e7 --- /dev/null +++ b/library/class.Diff.php @@ -0,0 +1,386 @@ += $start && $end2 >= $start + && $sequence1[$end1] == $sequence2[$end2]){ + $end1 --; + $end2 --; + } + + // compute the table of longest common subsequence lengths + $table = self::computeTable($sequence1, $sequence2, $start, $end1, $end2); + + // generate the partial diff + $partialDiff = + self::generatePartialDiff($table, $sequence1, $sequence2, $start); + + // generate the full diff + $diff = array(); + for ($index = 0; $index < $start; $index ++){ + $diff[] = array($sequence1[$index], self::UNMODIFIED); + } + while (count($partialDiff) > 0) $diff[] = array_pop($partialDiff); + for ($index = $end1 + 1; + $index < ($compareCharacters ? strlen($sequence1) : count($sequence1)); + $index ++){ + $diff[] = array($sequence1[$index], self::UNMODIFIED); + } + + // return the diff + return $diff; + + } + + /* Returns the diff for two files. The parameters are: + * + * $file1 - the path to the first file + * $file2 - the path to the second file + * $compareCharacters - true to compare characters, and false to compare + * lines; this optional parameter defaults to false + */ + public static function compareFiles( + $file1, $file2, $compareCharacters = false){ + + // return the diff of the files + return self::compare( + file_get_contents($file1), + file_get_contents($file2), + $compareCharacters); + + } + + /* Returns the table of longest common subsequence lengths for the specified + * sequences. The parameters are: + * + * $sequence1 - the first sequence + * $sequence2 - the second sequence + * $start - the starting index + * $end1 - the ending index for the first sequence + * $end2 - the ending index for the second sequence + */ + private static function computeTable( + $sequence1, $sequence2, $start, $end1, $end2){ + + // determine the lengths to be compared + $length1 = $end1 - $start + 1; + $length2 = $end2 - $start + 1; + + // initialise the table + $table = array(array_fill(0, $length2 + 1, 0)); + + // loop over the rows + for ($index1 = 1; $index1 <= $length1; $index1 ++){ + + // create the new row + $table[$index1] = array(0); + + // loop over the columns + for ($index2 = 1; $index2 <= $length2; $index2 ++){ + + // store the longest common subsequence length + if ($sequence1[$index1 + $start - 1] + == $sequence2[$index2 + $start - 1]){ + $table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1; + }else{ + $table[$index1][$index2] = + max($table[$index1 - 1][$index2], $table[$index1][$index2 - 1]); + } + + } + } + + // return the table + return $table; + + } + + /* Returns the partial diff for the specificed sequences, in reverse order. + * The parameters are: + * + * $table - the table returned by the computeTable function + * $sequence1 - the first sequence + * $sequence2 - the second sequence + * $start - the starting index + */ + private static function generatePartialDiff( + $table, $sequence1, $sequence2, $start){ + + // initialise the diff + $diff = array(); + + // initialise the indices + $index1 = count($table) - 1; + $index2 = count($table[0]) - 1; + + // loop until there are no items remaining in either sequence + while ($index1 > 0 || $index2 > 0){ + + // check what has happened to the items at these indices + if ($index1 > 0 && $index2 > 0 + && $sequence1[$index1 + $start - 1] + == $sequence2[$index2 + $start - 1]){ + + // update the diff and the indices + $diff[] = array($sequence1[$index1 + $start - 1], self::UNMODIFIED); + $index1 --; + $index2 --; + + }elseif ($index2 > 0 + && $table[$index1][$index2] == $table[$index1][$index2 - 1]){ + + // update the diff and the indices + $diff[] = array($sequence2[$index2 + $start - 1], self::INSERTED); + $index2 --; + + }else{ + + // update the diff and the indices + $diff[] = array($sequence1[$index1 + $start - 1], self::DELETED); + $index1 --; + + } + + } + + // return the diff + return $diff; + + } + + /* Returns a diff as a string, where unmodified lines are prefixed by ' ', + * deletions are prefixed by '- ', and insertions are prefixed by '+ '. The + * parameters are: + * + * $diff - the diff array + * $separator - the separator between lines; this optional parameter defaults + * to "\n" + */ + public static function toString($diff, $separator = "\n"){ + + // initialise the string + $string = ''; + + // loop over the lines in the diff + foreach ($diff as $line){ + + // extend the string with the line + switch ($line[1]){ + case self::UNMODIFIED : $string .= ' ' . $line[0];break; + case self::DELETED : $string .= '- ' . $line[0];break; + case self::INSERTED : $string .= '+ ' . $line[0];break; + } + + // extend the string with the separator + $string .= $separator; + + } + + // return the string + return $string; + + } + + /* Returns a diff as an HTML string, where unmodified lines are contained + * within 'span' elements, deletions are contained within 'del' elements, and + * insertions are contained within 'ins' elements. The parameters are: + * + * $diff - the diff array + * $separator - the separator between lines; this optional parameter defaults + * to '
' + */ + public static function toHTML($diff, $separator = '
'){ + + // initialise the HTML + $html = ''; + + // loop over the lines in the diff + foreach ($diff as $line){ + + // extend the HTML with the line + switch ($line[1]){ + case self::UNMODIFIED : $element = 'span'; break; + case self::DELETED : $element = 'del'; break; + case self::INSERTED : $element = 'ins'; break; + } + $html .= + '<' . $element . '>' + . htmlspecialchars($line[0]) + . ''; + + // extend the HTML with the separator + $html .= $separator; + + } + + // return the HTML + return $html; + + } + + /* Returns a diff as an HTML table. The parameters are: + * + * $diff - the diff array + * $indentation - indentation to add to every line of the generated HTML; this + * optional parameter defaults to '' + * $separator - the separator between lines; this optional parameter + * defaults to '
' + */ + public static function toTable($diff, $indentation = '', $separator = '
'){ + + // initialise the HTML + $html = $indentation . "\n"; + + // loop over the lines in the diff + $index = 0; + while ($index < count($diff)){ + + // determine the line type + switch ($diff[$index][1]){ + + // display the content on the left and right + case self::UNMODIFIED: + $leftCell = + self::getCellContent( + $diff, $indentation, $separator, $index, self::UNMODIFIED); + $rightCell = $leftCell; + break; + + // display the deleted on the left and inserted content on the right + case self::DELETED: + $leftCell = + self::getCellContent( + $diff, $indentation, $separator, $index, self::DELETED); + $rightCell = + self::getCellContent( + $diff, $indentation, $separator, $index, self::INSERTED); + break; + + // display the inserted content on the right + case self::INSERTED: + $leftCell = ''; + $rightCell = + self::getCellContent( + $diff, $indentation, $separator, $index, self::INSERTED); + break; + + } + + // extend the HTML with the new row + $html .= + $indentation + . " \n" + . $indentation + . ' \n" + . $indentation + . ' \n" + . $indentation + . " \n"; + + } + + // return the HTML + return $html . $indentation . "
' + . $leftCell + . "' + . $rightCell + . "
\n"; + + } + + /* Returns the content of the cell, for use in the toTable function. The + * parameters are: + * + * $diff - the diff array + * $indentation - indentation to add to every line of the generated HTML + * $separator - the separator between lines + * $index - the current index, passes by reference + * $type - the type of line + */ + private static function getCellContent( + $diff, $indentation, $separator, &$index, $type){ + + // initialise the HTML + $html = ''; + + // loop over the matching lines, adding them to the HTML + while ($index < count($diff) && $diff[$index][1] == $type){ + $html .= + '' + . htmlspecialchars($diff[$index][0]) + . '' + . $separator; + $index ++; + } + + // return the HTML + return $html; + + } + +} + +?> diff --git a/view/tpl/wiki.tpl b/view/tpl/wiki.tpl index dc78aad9f..aa0b88545 100644 --- a/view/tpl/wiki.tpl +++ b/view/tpl/wiki.tpl @@ -96,17 +96,6 @@
- - {{foreach $pageHistory as $commit}} - - {{/foreach}} -
- - - - -
Date{{$commit.date}}
Name{{$commit.name}}
Message{{$commit.title}}
-
@@ -120,16 +109,22 @@ {{/if}} +{{$wikiModal}} + diff --git a/view/tpl/wiki_page_history.tpl b/view/tpl/wiki_page_history.tpl index 6ce3ce204..fef5d93a5 100644 --- a/view/tpl/wiki_page_history.tpl +++ b/view/tpl/wiki_page_history.tpl @@ -1,12 +1,52 @@ + {{foreach $pageHistory as $commit}} {{/foreach}} -
- + +
Date{{$commit.date}} -
Date{{$commit.date}} + +

+ +
Name{{$commit.name}} <{{$commit.email}}>
Message{{$commit.title}}
\ No newline at end of file +