Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to include backlinking pages #247

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 148 additions & 80 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -297,18 +294,24 @@ function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null, $
*
* @author Michael Klier <[email protected]>
*/
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);
}

$ns = getNS($page);
$num = count($ins);

Expand Down Expand Up @@ -455,7 +458,7 @@ function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id, $inc
} else {
$footer_lvl = $lvl_max;
}
}
}
}
}

Expand Down Expand Up @@ -533,7 +536,7 @@ function _editbtn(&$ins, $page, $sect, $sect_title, $root_id) {

/**
* Convert instruction item for a permalink header
*
*
* @author Michael Klier <[email protected]>
*/
function _permalink(&$ins, $page, $sect, $flags) {
Expand Down Expand Up @@ -619,11 +622,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 <[email protected]>
*/
*/
function _get_section(&$ins, $sect) {
$num = count($ins);
$offset = false;
Expand All @@ -634,12 +637,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
Expand All @@ -654,7 +657,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
Expand Down Expand Up @@ -692,6 +696,58 @@ function _get_firstsec(&$ins, $page, $flags) {
}
}
}

/**
* Only show text inside given WRAPs
*
* @author Anders Jarmund <[email protected]>
*/
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] + 1;
$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
Expand All @@ -714,6 +770,27 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) {
$pages[] = $pagearray['id'];
}
}
break;
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, $key_tag);
foreach ($tags as $tag){
$tagss[] = $tag['id'];
}
if(!empty($pagearrays)){
foreach ($pagearrays as $pagearray) {
if(in_array($pagearray,$tagss)){
$pages[] = $pagearray;
}
}
}else{
$pages[] = 'nobacklinksyet';
}
break;
case 'tagtopic':
if (!$this->taghelper)
Expand All @@ -735,63 +812,54 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) {
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;
}

Expand Down Expand Up @@ -826,7 +894,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"
Expand Down Expand Up @@ -855,18 +923,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;

Expand Down
4 changes: 4 additions & 0 deletions syntax/include.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down Expand Up @@ -58,6 +60,8 @@ 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');
$this->Lexer->addSpecialPattern("{{WRAP>.+?}}", $mode, 'plugin_include_include');
}

/**
Expand Down