<?php
/**
* Plugin xsviewlog: simple plugin to view log files
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Taggic <taggic@t-online.de>
*/
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_xsviewlog extends DokuWiki_Syntax_Plugin {
/******************************************************************************/
/* return some info
*/
function getInfo(){
return confToHash
(dirname(__FILE__).'/plugin.info.txt');
}
function getType(){ return 'substition';}
function getPType(){ return 'block';}
function getSort(){ return 999;}
/******************************************************************************/
/* Connect pattern to lexer
*/
function connectTo($mode){
$this->Lexer->addSpecialPattern('\{\(xsviewlog\>.*?\)\}',$mode,'plugin_xsviewlog');
}
/******************************************************************************/
/* handle the match
*/
function handle($match, $state, $pos, &$handler) {
global $ID;
$match = substr($match,strlen('{(xsviewlog>'),-2); //strip markup from start and end
//handle params
/******************************************************************************/
/* parameters can be:
{(xsviewlog>[file path],[from line],[to line],[type])}
[file path] ... path to the file (either it is in DokuWiki media directory or windows fileshare, etc.)
[from line] ... the first line, which should be displayed
[to line] ... the last line, which should be displayed
[type] ... the type of content to chose appropriate description ini file
/******************************************************************************/
$params = explode(",",$match); // if you will have more parameters and choose ',' to delim them
if ((!$params) || (count($params)<3)) {
msg('Syntax of xsviewlog detected but to less parameters attached.', -1);
}
else {
// Values
$xsviewlog['filepath'] = $params[0];
$xsviewlog['from'] = $params[1];
$xsviewlog['until'] = $params[2];
$xsviewlog['type'] = $params[3];
return $xsviewlog;
}
}
/******************************************************************************/
/* render output
* @author Taggic <taggic@t-online.de>
*/
function render($mode, &$renderer, $xsviewlog) {
global $ID;
if($xsviewlog['filepath'] === '') $xsviewlog['filepath'] = DOKU_PLUGIN.'xsviewlog/testfiles/tst.log';
if(!$xsviewlog['type']) $xsviewlog['type'] = 'txt';
// 1. check if $xsviewlog['filepath'] exist, else error message
msg('file '.$xsviewlog['filepath'].' not found',-1);
return false;
}
// 2. retrieve file content
$records = file($xsviewlog['filepath']);
// 3. parse file content
if(!$xsviewlog['until']) $xsviewlog['until']=count($records);
foreach ($records as $line_num => $line) {
if(($line_num>=$xsviewlog['from']-1) && ($line_num<=$xsviewlog['until']-1)) {
// check if left 3 sign are matching '>> ' or '<< ' to differ between send and receive
$log_block .= '<tr><td class="xsviewlog_col1" align="right">'.$line_num.'</td>
<td class="xsviewlog_col2"><img src="'.DOKU_BASE.'lib/plugins/xsviewlog/images/in.png" title="receive" /></td>
<td><span class="xsviewlog0">'.substr($line,strlen(">>"),strlen($line)).' </span></td>
</tr>'.NL;
}
elseif(strpos($line, '<<')===0) {
$log_block .= '<tr><td class="xsviewlog_col1" align="right">'.$line_num.'</td>
<td class="xsviewlog_col2"><img src="'.DOKU_BASE.'lib/plugins/xsviewlog/images/out.png" title="send" /></td>
<td><span class="xsviewlog1">'.substr($line,strlen("<<"),strlen($line)).' </span></td>
</tr>'.NL;
}
else {
$log_block .= '<tr><td class="xsviewlog_col1" align="right">'.$line_num.'</td>
<td class="xsviewlog_col2"> </td>
<td><span class="xsviewlog2">'.$line.' </span></td>
</tr>'.NL;
}
}
if ($line_num>$xsviewlog['until']) break;
}
// 4. append output to renderer
$renderer->doc .= '<div class="xsviewlog"><table class="xsviewlog">'.
'<tr><th><span class="viewlog_txt">viewlog: </span></th><th colspan="2">'.$xsviewlog['filepath'].'</th></tr>'.
$log_block.'</table></div>';
}
}