1<?php
2/**
3 * DokuWiki Plugin publistf (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Raphael Reitzig <code@verrech.net>
7 * @author  Hans-Nikolai Viessmann <hans@viess.mn>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
14if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
15if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16
17require_once DOKU_PLUGIN.'syntax.php';
18
19class syntax_plugin_publistf extends DokuWiki_Syntax_Plugin {
20    function getType() {
21        return 'substition';
22    }
23
24    function getPType() {
25        return 'block';
26    }
27
28    function getSort() {
29        return 105;
30    }
31
32    function connectTo($mode) {
33        $this->Lexer->addSpecialPattern('\[publist\|.+?\]',$mode,'plugin_publistf');
34    }
35
36    function handle($match, $state, $pos, Doku_Handler $handler){
37        $data = array();
38
39        // Partition properly
40        $matches = array();
41        $pattern = '/\[publist(?:\|(page|file|url):(.+?))(?:\|(wiki|html):(page|file|url):(.+?))(?:\|(.+?(?:\|.+?)*))?\]/';
42        if ( 0 === preg_match($pattern, $match, $matches) ) {
43            $data['error'] = 'Not valid publistf syntax: '.$match;
44        }
45        else {
46            $data['bibtex'] = array('type' => $matches[1], 'ref' => $matches[2]);
47            $data['template'] = array('target' => $matches[3], 'type' => $matches[4], 'ref' => $matches[5]);
48            $data['options'] = array();
49
50
51            // Set default language. Get current lang from translation plugin
52            // if installed & enabled or fall back to default lang in conf.
53            if (!plugin_isdisabled('translation')) {
54                $trans =& plugin_load('helper', 'translation');
55                global $ID;
56                $mylang = $trans->getLangPart($ID);
57            } else {
58                global $conf;
59                $mylang = $conf['lang'];
60            }
61            $data['options']['lang'] = $mylang;
62
63            if ( !empty($matches[6]) ) {
64               $matches = explode('|', $matches[6]);
65               foreach ( $matches as $opt ) {
66                 $optparts = array();
67                 if ( preg_match('/(.+?):(.+)/', $opt, $optparts) ) {
68                    $optparts[2] = explode(';', $optparts[2]);
69                    $option = array();
70                    foreach ( $optparts[2] as $single ) {
71                        $single = explode('=', $single);
72                        if (count($single) == 1 && count($optparts[2]) == 1) {
73                            $option = $single[0];
74                        }
75                        else {
76                            $option[$single[0]] = str_replace(',', '|', $single[1]);
77                        }
78                    }
79                    $data['options'][$optparts[1]] = $option;
80                 }
81               }
82            }
83
84            if ($data['options']['authors'])
85            {
86              $tmp = explode(':', $data['options']['authors'],2);
87              $data['authors'] = array('type' => $tmp[0], 'ref' => $tmp[1]);
88            }
89        }
90        return $data;
91    }
92
93    function render($mode, Doku_Renderer $renderer, $data) {
94        if($mode != 'xhtml') return false;
95
96        if ( empty($data['error']) ) {
97            // Retrieve BibTeX source
98            $bibtex = $this->_load($data, 'bibtex');
99            if ( empty($bibtex) ) {
100                $data['error'] .= $data['bibtex']['type'].' '.$data['bibtex']['ref'].' does not exist<br />';
101            }
102
103            // Retrieve Template source
104            $template = $this->_load($data, 'template');
105            if ( empty($template) ) {
106                $data['error'] .= $data['template']['type'].' '.$data['template']['ref'].' does not exist<br />';
107            }
108
109			$authors = null;
110            if ($data['authors']) {
111	            // Retrieve Authors source
112	            $authors = $this->_load($data, 'authors');
113	            if ( empty($authors) ) {
114	                $data['error'] .= $data['authors']['type'].' '.$data['authors']['ref'].' does not exist<br />';
115	            }
116            }
117
118
119            if ( !empty($bibtex) && !empty($template) ) {
120                require_once(dirname(__FILE__).'/bib2tpl/bibtex_converter.php');
121                if ( is_readable(dirname(__FILE__).'/sanitiser.php')) {
122                    include(dirname(__FILE__).'/sanitiser.php');
123                }
124                if ( empty($sanitiser) ) {
125                   $sanitiser = create_function('$i', 'return $i;');
126                }
127                $parser = new BibtexConverter($data['options'],$sanitiser,$authors);
128                $code = $parser->convert($bibtex, $template);
129
130                if ( $data['template']['target'] == 'wiki' ) {
131                    $code = p_render($mode, p_get_instructions($code), $info);
132                }
133
134                $renderer->doc .= $code;
135            }
136        }
137
138        if ( !empty($data['error']) ) {
139            $renderer->doc .= $data['error'];
140        }
141
142        /* user settable */
143        // activate caching of results
144        $renderer->info['cache'] = $this->getConf('cache');
145
146        return true;
147    }
148
149    function _load($data, $kind) {
150        global $INFO;
151
152        if ( $data[$kind]['type'] == 'url' ) {
153            return file_get_contents($data[$kind]['ref']);
154        }
155        if ( $data[$kind]['type'] == 'file' ) {
156            return file_get_contents(dirname(__FILE__).'/'.$kind.'/'.$data[$kind]['ref']);
157        }
158        else if ( $data[$kind]['type'] == 'page' ) {
159            $exists = false;
160            $id = $data[$kind]['ref'];
161            resolve_pageid($INFO['namespace'], $id, $exists);
162            if ( $exists ) {
163                return rawWiki($id);
164            }
165        }
166
167        return null;
168    }
169}
170
171// vim:ts=4:sw=4:et
172