1<?php
2/**
3 * directions Plugin - local directions component
4 *   - shows, for the current wiki page, where, within the wiki, wikinauts come from and go to :).
5 *
6 * Usage: <localdirections>
7 *
8 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author   Nuno Flores <nuno.flores@gmail.com>
10 */
11
12if (!defined('DOKU_INC')) die();
13//if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15require_once(DOKU_PLUGIN.'syntax.php');
16require_once(DOKU_INC.'inc/common.php');
17require_once(DOKU_INC.'inc/pageutils.php');
18require_once(DOKU_INC.'inc/parserutils.php');
19require_once(DOKU_INC.'inc/pluginutils.php');
20require_once(DOKU_INC.'inc/html.php');
21require_once('common.php');
22require_once('graph.php');
23
24/**
25 * All DokuWiki plugins to extend the parser/rendering mechanism
26 * need to inherit from this class
27 */
28class syntax_plugin_directions_localdirections extends DokuWiki_Syntax_Plugin {
29
30   /**
31    * Get an associative array with plugin info.
32    *
33    * <p>
34    * The returned array holds the following fields:
35    * <dl>
36    * <dt>author</dt><dd>Author of the plugin</dd>
37    * <dt>email</dt><dd>Email address to contact the author</dd>
38    * <dt>date</dt><dd>Last modified date of the plugin in
39    * <tt>YYYY-MM-DD</tt> format</dd>
40    * <dt>name</dt><dd>Name of the plugin</dd>
41    * <dt>desc</dt><dd>Short description of the plugin (Text only)</dd>
42    * <dt>url</dt><dd>Website with more information on the plugin
43    * (eg. syntax description)</dd>
44    * </dl>
45    * @param none
46    * @return Array Information about this plugin class.
47    * @public
48    * @static
49    */
50   function getInfo() {
51    return array(
52            'author' => 'Nuno Flores',
53            'email'  => 'nuno.flores@gmail.com',
54            'date'   => @file_get_contents(DOKU_PLUGIN.'directions/VERSION'),
55            'name'   => 'directions Plugin (localdirections component)',
56            'desc'   => 'shows, for the current wiki page, where, within the wiki, wikinauts come from and go to.',
57            'url'    => 'http://www.dokuwiki.org/plugin:directions',
58            );
59}
60
61   /**
62    * Get the type of syntax this plugin defines.
63    *
64    * @param none
65    * @return String <tt>'substition'</tt> (i.e. 'substitution').
66    * @public
67    * @static
68    */
69    function getType(){
70        return 'substition';
71    	//return 'nocache';
72	}
73
74    /**
75     * What kind of syntax do we allow (optional)
76     */
77//    function getAllowedTypes() {
78//        return array();
79//    }
80
81   /**
82    * Define how this plugin is handled regarding paragraphs.
83    *
84    * <p>
85    * This method is important for correct XHTML nesting. It returns
86    * one of the following values:
87    * </p>
88    * <dl>
89    * <dt>normal</dt><dd>The plugin can be used inside paragraphs.</dd>
90    * <dt>block</dt><dd>Open paragraphs need to be closed before
91    * plugin output.</dd>
92    * <dt>stack</dt><dd>Special case: Plugin wraps other paragraphs.</dd>
93    * </dl>
94    * @param none
95    * @return String <tt>'block'</tt>.
96    * @public
97    * @static
98    */
99    function getPType(){
100        return 'normal';
101    }
102
103   /**
104    * Where to sort in?
105    *
106    * @param none
107    * @return Integer <tt>6</tt>.
108    * @public
109    * @static
110    */
111    function getSort(){
112        return 999;
113    }
114
115
116   /**
117    * Connect lookup pattern to lexer.
118    *
119    * @param $aMode String The desired rendermode.
120    * @return none
121    * @public
122    * @see render()
123    */
124    function connectTo($mode) {
125      $this->Lexer->addSpecialPattern('<(?|localdirections|localdirections graph)>',$mode,'plugin_directions_localdirections');
126    }
127
128//    function postConnect() {
129//    }
130
131
132   /**
133    * Handler to prepare matched data for the rendering process.
134    *
135    * <p>
136    * The <tt>$aState</tt> parameter gives the type of pattern
137    * which triggered the call to this method:
138    * </p>
139    * <dl>
140    * <dt>DOKU_LEXER_ENTER</dt>
141    * <dd>a pattern set by <tt>addEntryPattern()</tt></dd>
142    * <dt>DOKU_LEXER_MATCHED</dt>
143    * <dd>a pattern set by <tt>addPattern()</tt></dd>
144    * <dt>DOKU_LEXER_EXIT</dt>
145    * <dd> a pattern set by <tt>addExitPattern()</tt></dd>
146    * <dt>DOKU_LEXER_SPECIAL</dt>
147    * <dd>a pattern set by <tt>addSpecialPattern()</tt></dd>
148    * <dt>DOKU_LEXER_UNMATCHED</dt>
149    * <dd>ordinary text encountered within the plugin's syntax mode
150    * which doesn't match any pattern.</dd>
151    * </dl>
152    * @param $aMatch String The text matched by the patterns.
153    * @param $aState Integer The lexer state for the match.
154    * @param $aPos Integer The character position of the matched text.
155    * @param $aHandler Object Reference to the Doku_Handler object.
156    * @return Integer The current lexer state for the match.
157    * @public
158    * @see render()
159    * @static
160    */
161    function handle($match, $state, $pos, &$handler){
162
163        switch ($state) {
164          case DOKU_LEXER_ENTER :
165            break;
166          case DOKU_LEXER_MATCHED :
167            break;
168          case DOKU_LEXER_UNMATCHED :
169            break;
170          case DOKU_LEXER_EXIT :
171			break;
172          case DOKU_LEXER_SPECIAL :
173           break;
174        }
175
176		$result = array();
177
178		// check for graph atttribute
179		$graph = trim(substr($match,-7,-1));
180		$result[] = $graph;
181
182		// parse hits file
183		$registeredOnly = ($this->getConf('registeredOnly') == 0) ? false : true;
184		$filename = DOKU_INC . $this->getConf('hitslog');
185		$result[] = dir_parseHitsFile($filename, $registeredOnly);
186
187		return $result;
188    }
189
190   /**
191    * Handle the actual output creation.
192    *
193    * <p>
194    * The method checks for the given <tt>$aFormat</tt> and returns
195    * <tt>FALSE</tt> when a format isn't supported. <tt>$aRenderer</tt>
196    * contains a reference to the renderer object which is currently
197    * handling the rendering. The contents of <tt>$aData</tt> is the
198    * return value of the <tt>handle()</tt> method.
199    * </p>
200    * @param $aFormat String The output format to generate.
201    * @param $aRenderer Object A reference to the renderer object.
202    * @param $aData Array The data created by the <tt>handle()</tt>
203    * method.
204    * @return Boolean <tt>TRUE</tt> if rendered successfully, or
205    * <tt>FALSE</tt> otherwise.
206    * @public
207    * @see handle()
208    */
209    function render($mode, &$renderer, $data) {
210
211        if($mode == 'xhtml'){
212			if (!is_array($data)) {
213				$renderer->doc .= "<b>directions plug-in (localdirections component): Returned data in 'render' function is not an array.</b>";
214				return false;
215			}
216
217			$graph =  (strcmp($data[0],'graph') == 0) ? true : false;
218
219			if ($graph) {
220				if(!in_array('graphviz',plugin_list())) {
221					$renderer->doc .= '<i>(directions plug-in: To view the graph the graphviz plug-in must be installed)</i>';
222				} else {
223					//graph
224					$renderer->doc .= dir_generateDirections($this->getInfo(),$data[1]);
225					return true;
226				}
227			}
228			$renderer->doc .= dir_generateDirections($this->getInfo(),$data[1], false, false, false);
229            return true;
230        }
231        return false;
232    }
233}
234
235//Setup VIM: ex: et ts=4 enc=utf-8 :
236?>