1<?php
2/**
3 * Prolog plug-in : Rule-based System for Groupware.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Paweł Kupka <pawel.kupka@gmail.com>
7 */
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins');
11if(!defined('DOKU_PAGES')) define('DOKU_PAGES',DOKU_INC.'data/pages');
12require_once(DOKU_INC . 'inc/parserutils.php');
13require_once(DOKU_PLUGIN.'syntax.php');
14require_once('prolog_tag.php');
15require_once('attribute_include.php');
16require_once('attribute_options.php');
17require_once('attribute_maxruntime.php');
18
19/**
20 * All DokuWiki plugins to extend the parser/rendering mechanism
21 * need to inherit from this class
22 */
23class syntax_plugin_prolog extends DokuWiki_Syntax_Plugin {
24
25	var $prologTag;
26	var $attrMaxRunTime;
27	var $attrInclude;
28	var $attrOptions;
29
30	/**
31	 * Constructor
32	 */
33	function syntax_plugin_prolog()
34	{
35		$this->prologTag = & new PrologTag();
36		$this->attrInclude = & new AttributeInclude();
37		$this->attrOptions = & new AttributeOptions();
38		$this->attrMaxRunTime = & new AttributeMaxRunTime();
39	}
40
41	/**
42	 * Returns the information about syntax prolog plug-in
43	 * @return array information
44	 */
45	function getInfo()
46	{
47		return array
48					 (
49						 'author' => 'Paweł Kupka',
50						 'email'  => 'pawel.kupka@gmail.com',
51						 'date'   => '2008-05-02',
52						 'name'   => 'Syntax prolog plug-in',
53						 'desc'   => 'Rule-based System for Groupware.',
54						 'url'    => 'https://ai.ia.agh.edu.pl'
55					 );
56	}
57
58	/**
59	 * Defines to which type of syntax the plug-in belongs
60	 * @return string mode type
61	 */
62	function getType()
63	{
64		return 'protected';
65	}
66
67	/**
68	 * Tells the parser which other syntax modes are permitted inside the plug-in
69	 * @return array allowed mode types
70	 */
71	function getAllowedTypes()
72	{
73		return array();
74	}
75
76	/**
77	 * Defines how this syntax is handled regarding paragraphs
78	 * @return string kind of the paragraph's operation
79	 */
80	function getPType()
81	{
82		return 'normal';
83	}
84
85	/**
86	 * Returns sort number
87	 * @return integer sort number
88	 */
89	function getSort()
90	{
91		return 205;
92	}
93
94	/**
95	 * Registers the enter pattern of the prolog syntax plug-in
96	 * @param string syntax mode type
97	 */
98	function connectTo($mode)
99	{
100		$this->Lexer->addEntryPattern('<prolog.*?>(?=.*?</prolog>)',$mode,'plugin_prolog');
101	}
102
103	/**
104	 * Registers the exit pattern of the prolog syntax plug-in
105	 */
106	function postConnect()
107	{
108		$this->Lexer->addExitPattern('</prolog>', 'plugin_prolog');
109	}
110
111
112	/**
113	 * Handles the match
114	 */
115	function handle($match, $state, $pos, &$handler)
116	{
117		switch ($state)
118		{
119			case DOKU_LEXER_ENTER :
120				$_SESSION['prolog']['maxruntimeValue'] = $this->attrMaxRunTime->getAttributeValue($match);
121				$_SESSION['prolog']['includeValue'] = $this->attrInclude->getAttributeValue($match);
122				$_SESSION['prolog']['optionsValue'] = $this->attrOptions->getAttributeValue($match);
123				return;
124			case DOKU_LEXER_UNMATCHED :
125				if(isset($_SESSION['prolog']['match']))
126					$_SESSION['prolog']['match'] .= $match."\n";
127				else
128					$_SESSION['prolog']['match'] = $match."\n";
129				return;
130			case DOKU_LEXER_EXIT :
131				$maxRunTime = $_SESSION['prolog']['maxruntimeValue'];
132				$options = $this->attrOptions->getOptions($_SESSION['prolog']['optionsValue']);
133				$includedFilesList = $this->attrInclude->getFilesList($_SESSION['prolog']['includeValue']);
134				$prologCode = $this->attrInclude->readCode($includedFilesList) . $_SESSION['prolog']['match'];
135				unset($_SESSION['prolog']);
136				return array($state, array($prologCode, $maxRunTime, $includedFilesList, $options));
137		}
138		return array();
139	}
140
141	/**
142	 * Creates output
143	 */
144	function render($mode, &$renderer, $data)
145	{
146		if($mode == 'xhtml')
147		{
148			list($state, $match) = $data;
149			switch ($state)
150			{
151				case DOKU_LEXER_ENTER :
152					break;
153				case DOKU_LEXER_UNMATCHED :
154					break;
155				case DOKU_LEXER_EXIT :
156					list($prologCode, $maxRunTime, $includedFilesList, $options) = $match;
157					$executedCode = $this->prologTag->execute($prologCode, $maxRunTime);
158					foreach($options as $option)
159					{
160						$this->attrOptions->authorizeOption($option);
161						$renderer->doc .= $this->attrOptions->handleOptionDebug($executedCode[1]);
162						$renderer->doc .= $this->attrOptions->handleOptionFiles($includedFilesList);
163						$renderer->doc .= $this->attrOptions->handleOptionSource(str_replace("\n",'<br />',$prologCode));
164					}
165					$instructions = p_get_instructions($executedCode[0]);
166					foreach($instructions as $instruction)
167						call_user_func_array(array($renderer, $instruction[0]),$instruction[1]);
168					break;
169			}
170			return true;
171		}
172		return false;
173	}
174}
175?>