From 173748fa96a1608271b1eb9d951eefbb2189ac92 Mon Sep 17 00:00:00 2001 From: andjar Date: Thu, 23 Apr 2020 17:36:23 +0200 Subject: [PATCH 1/6] Added option for backlinks --- syntax/include.php | 1 + 1 file changed, 1 insertion(+) diff --git a/syntax/include.php b/syntax/include.php index 5184541..4be0340 100644 --- a/syntax/include.php +++ b/syntax/include.php @@ -58,6 +58,7 @@ function connectTo($mode) { $this->Lexer->addSpecialPattern("{{section>.+?}}", $mode, 'plugin_include_include'); $this->Lexer->addSpecialPattern("{{namespace>.+?}}", $mode, 'plugin_include_include'); $this->Lexer->addSpecialPattern("{{tagtopic>.+?}}", $mode, 'plugin_include_include'); + $this->Lexer->addSpecialPattern("{{backlinks>.+?}}", $mode, 'plugin_include_include'); } /** From 4885cc3c6e01866946a8bcc7f1f07d7bb3b847e2 Mon Sep 17 00:00:00 2001 From: andjar Date: Thu, 23 Apr 2020 17:40:03 +0200 Subject: [PATCH 2/6] Simple backlink support --- helper.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/helper.php b/helper.php index 24ae26b..570420e 100644 --- a/helper.php +++ b/helper.php @@ -729,6 +729,26 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { $pages[] = $pagearray['id']; } break; + case 'backlinks': + $page = $this->_apply_macro($page, $parent_id); + resolve_pageid(getNS($parent_id), $page, $exists); + @require_once(DOKU_INC.'inc/fulltext.php'); + $pagearrays = ft_backlinks($page,true); + $this->taghelper =& plugin_load('helper', 'tag'); + $tags = $this->taghelper->getTopic('notes', null, 'note'); + foreach ($tags as $tag){ + $tagss[] = $tag['id']; + } + if(!empty($pagearrays)){ + foreach ($pagearrays as $pagearray) { + if(in_array($pagearray,$tagss)){ + $pages[] = $pagearray; + } + } + }else{ + $pages[] = 'dummy'; + } + break; default: $page = $this->_apply_macro($page, $parent_id); resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID From 5ed54955dce89daf491ad4fc4b3d52f5013e09f5 Mon Sep 17 00:00:00 2001 From: andjar Date: Fri, 24 Apr 2020 19:53:17 +0200 Subject: [PATCH 3/6] Updated the backlinks syntax --- helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helper.php b/helper.php index 570420e..782dd7c 100644 --- a/helper.php +++ b/helper.php @@ -735,7 +735,7 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { @require_once(DOKU_INC.'inc/fulltext.php'); $pagearrays = ft_backlinks($page,true); $this->taghelper =& plugin_load('helper', 'tag'); - $tags = $this->taghelper->getTopic('notes', null, 'note'); + $tags = $this->taghelper->getTopic(getNS($parent_id), null, $sect); foreach ($tags as $tag){ $tagss[] = $tag['id']; } @@ -746,7 +746,7 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { } } }else{ - $pages[] = 'dummy'; + $pages[] = 'nobacklinksyet'; } break; default: From 6e8315303991d16db1e758c2f92457707d4a930b Mon Sep 17 00:00:00 2001 From: andjar Date: Sat, 25 Apr 2020 01:50:25 +0200 Subject: [PATCH 4/6] Added WRAP --- syntax/include.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/syntax/include.php b/syntax/include.php index 4be0340..762bb57 100644 --- a/syntax/include.php +++ b/syntax/include.php @@ -7,6 +7,8 @@ * {{page>namespace:page}} for "page" in namespace "namespace" * {{page>.namespace:page}} for "page" in subnamespace "namespace" * {{page>page#section}} for a section of "page" + * {{backlinks>page#tag}} for backlinks to "page" tagged "tag" + * {{WRAP>page#class}} for WRAP of type "class" on "page", supports multiple instances * * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @author Esther Brunner @@ -59,6 +61,7 @@ function connectTo($mode) { $this->Lexer->addSpecialPattern("{{namespace>.+?}}", $mode, 'plugin_include_include'); $this->Lexer->addSpecialPattern("{{tagtopic>.+?}}", $mode, 'plugin_include_include'); $this->Lexer->addSpecialPattern("{{backlinks>.+?}}", $mode, 'plugin_include_include'); + $this->Lexer->addSpecialPattern("{{WRAP>.+?}}", $mode, 'plugin_include_include'); } /** From 04ffaa6d9ae1876cab4b37737ac9a2019910ad07 Mon Sep 17 00:00:00 2001 From: andjar Date: Sat, 25 Apr 2020 01:52:03 +0200 Subject: [PATCH 5/6] Added WRAP --- helper.php | 244 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 147 insertions(+), 97 deletions(-) diff --git a/helper.php b/helper.php index 782dd7c..8c26774 100644 --- a/helper.php +++ b/helper.php @@ -224,9 +224,6 @@ function get_flags($setflags) { case 'noreadmore': $flags['readmore'] = 0; break; - case 'exclude': - $flags['exclude'] = $value; - break; } } // the include_content URL parameter overrides flags @@ -279,7 +276,7 @@ function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $ $ins = array(); } - $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id, $included_pages); + $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id, $included_pages, $mode); } return $ins; } @@ -297,18 +294,26 @@ function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $ * * @author Michael Klier */ - function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $included_pages = array()) { + function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $included_pages = array(), $mode) { global $conf; - // filter instructions if needed - if(!empty($sect)) { - $this->_get_section($ins, $sect); // section required - } + if($mode == 'page' || $mode == 'section'){ + // filter instructions if needed + if(!empty($sect)) { + $this->_get_section($ins, $sect); // section required + } - if($flags['firstsec']) { - $this->_get_firstsec($ins, $page, $flags); // only first section + if($flags['firstsec']) { + $this->_get_firstsec($ins, $page, $flags); // only first section + } } - + + if($mode == 'WRAP'){ + $this->_get_WRAP($ins, $sect); + //$this->_get_WRAP($ins, $sect); + //$this->_get_section($ins, $sect); + } + $ns = getNS($page); $num = count($ins); @@ -455,7 +460,7 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc } else { $footer_lvl = $lvl_max; } - } + } } } @@ -533,7 +538,7 @@ function _editbtn(&$ins, $page, $sect, $sect_title, $root_id) { /** * Convert instruction item for a permalink header - * + * * @author Michael Klier */ function _permalink(&$ins, $page, $sect, $flags) { @@ -619,11 +624,11 @@ private function adapt_links(&$ins, $page, $included_pages = null) { } } - /** - * Get a section including its subsections + /** + * Get a section including its subsections * * @author Michael Klier - */ + */ function _get_section(&$ins, $sect) { $num = count($ins); $offset = false; @@ -634,12 +639,12 @@ function _get_section(&$ins, $sect) { $check = array(); // used for sectionID() in order to get the same ids as the xhtml renderer for($i=0; $i<$num; $i++) { - if ($ins[$i][0] == 'header') { + if ($ins[$i][0] == 'header') { - // found the right header + // found the right header if (sectionID($ins[$i][1][0], $check) == $sect) { $offset = $i; - $lvl = $ins[$i][1][1]; + $lvl = $ins[$i][1][1]; } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) { $end = $i - $offset; $endpos = $ins[$i][1][2]; // the position directly after the found section, needed for the section edit button @@ -654,7 +659,8 @@ function _get_section(&$ins, $sect) { // store the end position in the include_closelastsecedit instruction so it can generate a matching button $ins[] = array('plugin', array('include_closelastsecedit', array($endpos))); } - } + } + /** * Only display the first section of a page and a readmore link @@ -692,6 +698,58 @@ function _get_firstsec(&$ins, $page, $flags) { } } } + + /** + * Only show text inside given WRAPs + * + * @author Anders Jarmund + */ + function _get_WRAP(&$ins, $sect) { + $num = count($ins); + $offset = false; + $lvl = false; + $end = false; + $insideSpecificedWRAP = false; + $endpos = null; // end position in the input text, needed for section edit buttons + + $numOfWRAPs = 0; + for($i=0; $i<$num; $i++) { + if ($ins[$i][1][0] == 'wrap_divwrap' && $ins[$i][1][1][1] == $sect) { + $numOfWRAPs++; + } + } + + $cc = 0; //Counter for the WRAPs + + for($i=0; $i<$num; $i++) { + if ($ins[$i][1][0] == 'wrap_divwrap') { + //Found a WRAP + if ($ins[$i][1][1][1] == $sect) { //Found the right WRAP + $offsets[$cc] = $i; + $insideSpecificedWRAP = TRUE; + } elseif ($offsets[$cc] && $insideSpecificedWRAP) { + $ends[$cc] = $i - $offsets[$cc]; + $endposs[$cc] = $ins[$i][2]; // the position directly after the found section, needed for the section edit button + $cc++; + $insideSpecificedWRAP = FALSE; + } + } + } + for($i=0; $i<$numOfWRAPs; $i++){ + $offsets[$i] = $offsets[$i] ? $offsets[$i] : 0; + $ends[$i] = $ends[$i] ? $ends[$i] : ($num - 1); + } + if(is_array($ins)) { + $ins_orig = $ins; + $ins = array_slice($ins_orig, $offsets[0], $ends[0]); + for($i=1; $i<$numOfWRAPs; $i++){ + $temp = array_slice($ins_orig, $offsets[$i], $ends[$i]); + foreach($temp as $temp_el){ + $ins[] = $temp_el; + } + } + } + } /** * Gives a list of pages for a given include statement @@ -715,27 +773,14 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { } } break; - case 'tagtopic': - if (!$this->taghelper) - $this->taghelper =& plugin_load('helper', 'tag'); - if(!$this->taghelper) { - msg('You have to install the tag plugin to use this functionality!', -1); - return array(); - } - $tag = $page; - $sect = ''; - $pagearrays = $this->taghelper->getTopic('', null, $tag); - foreach ($pagearrays as $pagearray) { - $pages[] = $pagearray['id']; - } - break; - case 'backlinks': + case 'backlinks': + $key_tag = $sect; $page = $this->_apply_macro($page, $parent_id); resolve_pageid(getNS($parent_id), $page, $exists); @require_once(DOKU_INC.'inc/fulltext.php'); $pagearrays = ft_backlinks($page,true); $this->taghelper =& plugin_load('helper', 'tag'); - $tags = $this->taghelper->getTopic(getNS($parent_id), null, $sect); + $tags = $this->taghelper->getTopic(getNS($parent_id), null, $key_tag); foreach ($tags as $tag){ $tagss[] = $tag['id']; } @@ -744,74 +789,79 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { if(in_array($pagearray,$tagss)){ $pages[] = $pagearray; } - } + } }else{ $pages[] = 'nobacklinksyet'; } break; + case 'tagtopic': + if (!$this->taghelper) + $this->taghelper =& plugin_load('helper', 'tag'); + if(!$this->taghelper) { + msg('You have to install the tag plugin to use this functionality!', -1); + return array(); + } + $tag = $page; + $sect = ''; + $pagearrays = $this->taghelper->getTopic('', null, $tag); + foreach ($pagearrays as $pagearray) { + $pages[] = $pagearray['id']; + } + break; default: $page = $this->_apply_macro($page, $parent_id); resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID if (auth_quickaclcheck($page) >= AUTH_READ) $pages[] = $page; } - - if (isset($flags['exclude'])) - $pages = array_filter($pages, function ($page) use ($flags) { - if (@preg_match($flags['exclude'], $page)) - return FALSE; - return TRUE; - }); - - if (count($pages) > 1) { - if ($flags['order'] === 'id') { - if ($flags['rsort']) { - usort($pages, array($this, '_r_strnatcasecmp')); - } else { - natcasesort($pages); - } - } else { - $ordered_pages = array(); - foreach ($pages as $page) { - $key = ''; - switch ($flags['order']) { - case 'title': - $key = p_get_first_heading($page); - break; - case 'created': - $key = p_get_metadata($page, 'date created', METADATA_DONT_RENDER); - break; - case 'modified': - $key = p_get_metadata($page, 'date modified', METADATA_DONT_RENDER); - break; - case 'indexmenu': - $key = p_get_metadata($page, 'indexmenu_n', METADATA_RENDER_USING_SIMPLE_CACHE); - if ($key === null) - $key = ''; - break; - case 'custom': - $key = p_get_metadata($page, 'include_n', METADATA_RENDER_USING_SIMPLE_CACHE); - if ($key === null) - $key = ''; - break; + if (count($pages) > 1) { + if ($flags['order'] === 'id') { + if ($flags['rsort']) { + usort($pages, array($this, '_r_strnatcasecmp')); + } else { + natcasesort($pages); } - $key .= '_'.$page; - $ordered_pages[$key] = $page; - } - if ($flags['rsort']) { - uksort($ordered_pages, array($this, '_r_strnatcasecmp')); } else { - uksort($ordered_pages, 'strnatcasecmp'); + $ordered_pages = array(); + foreach ($pages as $page) { + $key = ''; + switch ($flags['order']) { + case 'title': + $key = p_get_first_heading($page); + break; + case 'created': + $key = p_get_metadata($page, 'date created', METADATA_DONT_RENDER); + break; + case 'modified': + $key = p_get_metadata($page, 'date modified', METADATA_DONT_RENDER); + break; + case 'indexmenu': + $key = p_get_metadata($page, 'indexmenu_n', METADATA_RENDER_USING_SIMPLE_CACHE); + if ($key === null) + $key = ''; + break; + case 'custom': + $key = p_get_metadata($page, 'include_n', METADATA_RENDER_USING_SIMPLE_CACHE); + if ($key === null) + $key = ''; + break; + } + $key .= '_'.$page; + $ordered_pages[$key] = $page; + } + if ($flags['rsort']) { + uksort($ordered_pages, array($this, '_r_strnatcasecmp')); + } else { + uksort($ordered_pages, 'strnatcasecmp'); + } + $pages = $ordered_pages; } - $pages = $ordered_pages; } - } - - $result = array(); - foreach ($pages as $page) { - $exists = page_exists($page); - $result[] = array('id' => $page, 'exists' => $exists, 'parent_id' => $parent_id); - } + $result = array(); + foreach ($pages as $page) { + $exists = page_exists($page); + $result[] = array('id' => $page, 'exists' => $exists, 'parent_id' => $parent_id); + } return $result; } @@ -846,7 +896,7 @@ function _get_included_pages_from_meta_instructions($instructions) { } return $pages; } - + /** * Get wiki language from "HTTP_ACCEPT_LANGUAGE" * We allow the pattern e.g. "ja,en-US;q=0.7,en;q=0.3" @@ -875,18 +925,18 @@ function _get_language_of_wiki($id, $parent_id) { break; } } - } - } - return cleanID($result); + } + } + return cleanID($result); } - + /** * Makes user or date dependent includes possible */ function _apply_macro($id, $parent_id) { global $INFO; global $auth; - + // if we don't have an auth object, do nothing if (!$auth) return $id; From b17ab755bfd9ad22909830438c65102386785887 Mon Sep 17 00:00:00 2001 From: andjar Date: Sat, 25 Apr 2020 12:17:46 +0200 Subject: [PATCH 6/6] Close the WRAP porperly --- helper.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/helper.php b/helper.php index 8c26774..5cd87f5 100644 --- a/helper.php +++ b/helper.php @@ -310,8 +310,6 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc if($mode == 'WRAP'){ $this->_get_WRAP($ins, $sect); - //$this->_get_WRAP($ins, $sect); - //$this->_get_section($ins, $sect); } $ns = getNS($page); @@ -728,7 +726,7 @@ function _get_WRAP(&$ins, $sect) { $offsets[$cc] = $i; $insideSpecificedWRAP = TRUE; } elseif ($offsets[$cc] && $insideSpecificedWRAP) { - $ends[$cc] = $i - $offsets[$cc]; + $ends[$cc] = $i - $offsets[$cc] + 1; $endposs[$cc] = $ins[$i][2]; // the position directly after the found section, needed for the section edit button $cc++; $insideSpecificedWRAP = FALSE;