-
Notifications
You must be signed in to change notification settings - Fork 15
/
wikiParser.class.php
206 lines (174 loc) · 7.1 KB
/
wikiParser.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/*
* @package PHP5 Wiki Parser
* @author Dan Goldsmith
* @copyright Dan Goldsmith 2012
* @link http://d2g.org.uk/
* @version {SUBVERSION_BUILD_NUMBER}
*
* @licence MPL 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
class wikiParser
{
private $parser_plugins = null;
private static $config_ini = null;
public function __construct()
{
$this->parser_plugins = array();
if(!is_dir(dirname(__FILE__) . '/plugins/'))
{
throw new Exception("Unable to Find:" . dirname(__FILE__) . '/plugins/');
}
$directory = opendir(dirname(__FILE__) . '/plugins/');
if($directory === false)
{
throw new Exception("Unable to Read Directory:" . dirname(__FILE__) . '/plugins/');
}
$config = wikiParser::getConfigINI();
$isEnabled = true;
$plugin_list = array();
if(array_key_exists('MAIN',$config) && array_key_exists('DEFAULT_MODE', $config['MAIN']) && strtoupper($config['MAIN']['DEFAULT_MODE']) == 'DISABLED')
{
$isEnabled = false;
}
if($isEnabled)
{
//So the Default is to enable plugins.
if(array_key_exists('MAIN',$config) && array_key_exists('DISABLED_PLUGINS',$config['MAIN']) && is_array($config['MAIN']['DISABLED_PLUGINS']))
{
$plugin_list = $config['MAIN']['DISABLED_PLUGINS']; //List Of Disabled Plugins
}
}
else
{
//So the defualt is to disable
if(array_key_exists('MAIN',$config) && array_key_exists('ENABLED_PLUGINS',$config['MAIN']) && is_array($config['MAIN']['ENABLED_PLUGINS']))
{
$plugin_list = $config['MAIN']['ENABLED_PLUGINS']; //List Of Enabled Plugins
}
}
while(false !== ($file=readdir($directory)))
{
if($file != "." && $file != ".." && $file != ".svn")
{
//Now we need to work out the class name.
$class_name = explode(".",$file);
$class_name = $class_name[0];
//Ok Lets Check if it's enabled or disabled in the INI
if($isEnabled && !in_array($class_name, $plugin_list) || !$isEnabled && in_array($class_name, $plugin_list))
{
//Defult Mode is Enabled and It's Not Expressly Disabled
//Or It's default is disabled but it's been enabled.
if(!is_dir(dirname(__FILE__) . '/plugins/' . $file))
{
require_once(dirname(__FILE__) . '/plugins/' . $file);
$this->parser_plugins[] = new $class_name();
}
}
}
}
closedir($directory);
}
public static function getConfigINI()
{
if(wikiParser::$config_ini === null)
{
wikiParser::$config_ini = parse_ini_file(dirname(__FILE__) ."/config.ini", true);
}
return wikiParser::$config_ini;
}
public function parse($wiki_text)
{
//Parse Section
//For each section on the config.ini
$parser_order_config = wikiParser::getConfigINI();
$file_parsing_order = $parser_order_config['FileParsingOrder'];
ksort($file_parsing_order);
foreach($file_parsing_order as $parsing_section_name)
{
if(strtoupper($parsing_section_name) == "LINE")
{
$line_parsing_order = $parser_order_config['LineParsingOrder'];
ksort($line_parsing_order);
$wiki_lines = explode("\n", $wiki_text);
for($i = 0;$i < count($wiki_lines);$i++)
{
foreach($line_parsing_order as $parsing_line_section_name)
{
$wiki_lines[$i] = $this->parseSection($parsing_line_section_name, $wiki_lines[$i]);
}
}
$wiki_text = implode(PHP_EOL, $wiki_lines);
}
else
{
//Parse file
$wiki_text = $this->parseSection($parsing_section_name, $wiki_text);
}
}
return $wiki_text;
}
// Ok this is fine for parse the whole file but not for each line.
private function parseSection($section_name,$wiki_text)
{
$parser_order_config = wikiParser::getConfigINI();
$parser_order_int = array();
foreach($parser_order_config[$section_name] as $priority => $plugin_name)
{
$parser_order_int[(int)$priority] = $plugin_name;
}
ksort($parser_order_int);
foreach($parser_order_int as $priority => $plugin_name)
{
//Only Process those with a priority lower than 0
if($priority > 0)
{
break;
}
foreach($this->parser_plugins as $plugin)
{
if($plugin instanceof $section_name && $plugin instanceof $plugin_name)
{
$wiki_text = $plugin->$section_name($wiki_text);
break;
}
}
}
//Process all the plugins we don't have a priority for (Equiv to them all having zero)
$parsers = $this->parser_plugins;
if(is_array($parser_order_config) && array_key_exists('ParsingDirection', $parser_order_config) && array_key_exists($section_name, $parser_order_config['ParsingDirection']) && strtolower($parser_order_config['ParsingDirection'][$section_name]) == 'reverse')
{
$parsers = array_reverse($this->parser_plugins);
}
foreach($parsers as $plugin)
{
if(!in_array(get_class($plugin), $parser_order_int) && $plugin instanceof $section_name)
{
//echo "Plugin Name:" . get_class($plugin) . " : " . $section_name . " : <-----\"" . $wiki_text . "\"----->\n\n";
$wiki_text = $plugin->$section_name($wiki_text);
}
}
foreach($parser_order_int as $priority => $plugin_name)
{
//Only Process those with a priority higher than 0
if($priority <= 0)
{
continue;
}
foreach($this->parser_plugins as $plugin)
{
if($plugin instanceof $section_name && $plugin instanceof $plugin_name)
{
$wiki_text = $plugin->$section_name($wiki_text);
break;
}
}
}
return $wiki_text;
}
}
?>