====== Include User Page Plugin ======
---- plugin ----
description: Includes a page based on a namespace and the user name of a logged in user.
author : Jean-Marc Lagace
email : joe@example.come
type : syntax
lastupdate : 2005-12-17
compatible : 2006-11-06
depends :
conflicts :
similar : include
tags : include, users, !broken
----
===== Description =====
This plugin is useful when you want to have a home page with some generic base text and add more text depending on the user. The most basic syntax, to include the user page from the same namespace is:
{{userpage>.}}
You may also include the page from a different namespace: ''%%{{userpage>namespace}}%%''. The [[:namespaces]] shortcuts do also work: ''%%{{userpage>:}}%%'' (top namespace) or ''%%{{userpage>.namespace}}%%'' (subnamespace).
Optionally you can limit the included page to a specific section (including its subsections):
{{userpage>#sectionname}}
===== Plugin =====
To install, put the following PHP file in ''/lib/plugins/includeuserpage/syntax.php''.
:}} for the user page in top namespace
* {{userpage>namespace}} for the user page in namespace "namespace"
* {{userpage>.namespace}} for the user page in subnamespace "namespace"
* {{userpage>#section}} for the section
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Jean-Marc Lagace
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_includeuserpage extends DokuWiki_Syntax_Plugin {
/**
* return some info
*/
function getInfo(){
return array(
'author' => 'Jean-Marc Lagace',
'email' => 'jean-marc.lagace@m2i3.com',
'date' => '2005-12-17',
'name' => 'Include Plugin using the UserName',
'desc' => 'Displays a wiki page within another based on the UserName of the connected user. Based on a plugin by Esther Brunner available at http://www.dokuwiki.org/plugin:include',
'url' => 'http://www.dokuwiki.org/plugin:includeuserpage',
);
}
/**
* What kind of syntax are we?
*/
function getType(){
return 'substition';
}
/**
* Where to sort in?
*/
function getSort(){
return 303;
}
/**
* Paragraph Type
*/
function getPType(){
return 'block';
}
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern("{{userpage>.+?}}",$mode,'plugin_includeuserpage');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler){
$match = substr($match,11,-2); // strip markup
$match = preg_split('/\#/u',$match,2); // split hash from filename
return array($match[0],cleanID($match[1]));
}
function getPageName($namespaceSettings){
global $_SERVER;
if ($namespaceSettings == ':') {
return $namespaceSettings . $_SERVER['REMOTE_USER'];
} else {
return $namespaceSettings . ':' . $_SERVER['REMOTE_USER'];
}
}
/**
* Create output
*/
function render($mode, &$renderer, $data) {
global $ID;
if($mode == 'xhtml'){
// prevent caching to ensure the included page is always fresh
$renderer->info['cache'] = FALSE;
$pagename = $this->getPageName($data[0]);
resolve_pageid(getNS($ID),$pagename,$exists); // resolve shortcuts
// check for existence and permission
if ((!$exists) || (auth_quickaclcheck($pagename) < 1)) return false;
$file = wikiFN($pagename);
if (!@file_exists($file)) return false;
// get instructions
$instr = p_cached_instructions($file, false);
// filter section if given
if ($data[1]) $instr = $this->_getSection($data[1],$instr);
// correct relative internal links and media
$instr = $this->_correctRelNS($instr, $pagename);
// render the instructructions on the fly
$text = p_render('xhtml',$instr,$info);
// remove toc, section edit buttons and category tags
$patterns = array('!.*?(
\n)!s',
'##e',
'!.*?
!s');
$replace = array('','','');
$text = preg_replace($patterns,$replace,$text);
// prevent caching to ensure the included page is always fresh
$renderer->info['cache'] = FALSE;
// embed the included page
$renderer->doc .= '';
$renderer->doc .= $text;
$renderer->doc .= '
';
return true;
}
return false;
}
/**
* Get a section including its subsections
*/
function _getSection($title,$instructions){
foreach ($instructions as $instruction){
if ($instruction[0] == 'header'){
// found the right header
if (cleanID($instruction[1][0]) == $title){
$level = $instruction[1][1];
$i[] = $instruction;
// next header of the same level -> exit
} elseif ($instruction[1][1] == $level){
return $i;
}
// add instructions from our section
} elseif (isset($level)){
$i[] = $instruction;
}
}
return $i;
}
/**
* Corrects relative internal links and media
*/
function _correctRelNS($instr,$incl){
global $ID;
// check if included page is in same namespace
$iNS = getNS($incl);
if (getNS($ID) == $iNS) return $instr;
// convert internal links and media from relative to absolute
$n = count($instr);
for($i = 0; $i < $n; $i++){
if (substr($instr[$i][0], 0, 8) == 'internal'){
// relative subnamespace
if ($instr[$i][1][0]{0} == '.'){
$instr[$i][1][0] = $iNS.':'.substr($instr[$i][1][0], 1);
// relative link
} elseif (strpos($instr[$i][1][0],':') === false) {
$instr[$i][1][0] = $iNS.':'.$instr[$i][1][0];
}
}
}
return $instr;
}
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
?>
===== Style =====
The included page is set into a ''
'' tag of class ''include''. So you can for example draw a border and change font size with CSS:
.includeuserpage {
padding: 0.5em;
border: 1px dotted #8cacbb;
font-size: 90%;
}
===== Changes =====
* 2005-12-18:
* Original publication of the plugin. Thanks to Esther Brunner for the original code of the [[include]] plugin.
===== Discussion =====
==== Suggestion ====
=== User Group Instead ===
Can this be changed to match a page on the user group of the signed on user? It could but based on this macro feature request I've seen on the original include plugin I think I'd drop this plugin and focus on improving the original plugin.
=== Demo or Playground ===
Is there a demo page or a playground using this plugin? --- //[[sebastian.spiess@outotec.com|Sebastian Spiess]] 2007-10-10 08:04//