simplify postinst script by removing the following actions:
[diogenes.git] / include / Plugins.php
1 <?php
2 /*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 // dependency on PEAR
22 require_once 'System.php';
23
24 /** This class describes Diogenes' plugins.
25 */
26 class Diogenes_Plugins
27 {
28 /** Array of currently loaded plugins */
29 var $loaded = array();
30
31 /** Directory that holds the plugins cache files */
32 var $cachedir;
33
34 /** Directory that holds the plugins */
35 var $plugdir;
36
37
38 /** Constructs a new holder for Diogenes plugins.
39 *
40 * @param $dbh
41 * @param $plugdir
42 * @param $cachedir
43 */
44 function Diogenes_Plugins(&$dbh, $plugdir, $cachedir)
45 {
46 $this->dbh =& $dbh;
47 $this->plugdir = $plugdir;
48 $this->cachedir = $cachedir;
49 }
50
51
52 /** Return the path of the cache file
53 *
54 * @param $barrel
55 */
56 function cacheFile($barrel)
57 {
58 $cachefile = $this->cachedir . "/" . ($barrel ? $barrel : "__diogenes__") . ".plugins";
59 return $cachefile;
60 }
61
62
63 /** Return the cache entry for specified plugin
64 *
65 * @param $cache
66 * @param $barrel
67 * @param $page
68 * @param $plugin
69 */
70 function cacheGet($cache, $barrel, $page, $plugin)
71 {
72 foreach ($cache as $plugentry)
73 {
74 if (($plugentry['plugin'] == $plugin) && ($plugentry['page'] == $page))
75 {
76 return $plugentry;
77 }
78 }
79 return;
80 }
81
82
83 /** Return the cache entry for a plugin at a specified position
84 *
85 * @param $cache
86 * @param $barrel
87 * @param $page
88 * @param $pos
89 */
90 function cacheGetAtPos($cache, $barrel, $page, $pos)
91 {
92 foreach ($cache as $plugentry)
93 {
94 if (($plugentry['pos'] == $pos) && ($plugentry['page'] == $page))
95 {
96 return $plugentry;
97 }
98 }
99 }
100
101
102 /** List the plugins that are active in a given context
103 *
104 * @param $cache
105 * @param $barrel
106 * @param $page
107 */
108 function cachedActive($cache, $barrel, $page)
109 {
110 $plugins = array();
111 foreach ($cache as $plug)
112 {
113 if ($plug['page'] == $page)
114 {
115 array_push($plugins, $plug);
116 }
117 }
118 return $plugins;
119 }
120
121
122 /** Returns an array of cache entries representing the available plugins
123 * in a given context
124 *
125 * @param $cache
126 * @param $barrel
127 * @param $page
128 */
129 function cachedAvailable($cache, $barrel, $page)
130 {
131 $available = array();
132 foreach ($cache as $plugentry)
133 {
134 $plugfile = $this->plugdir."/".$plugentry['plugin'].".php";
135 if (file_exists($plugfile) and ($plugentry['page'] == 0) and (!$page or $plugentry['active']))
136 {
137 array_push($available, $plugentry['plugin']);
138 }
139 }
140 return $available;
141 }
142
143
144 /** Remove database references to plugins that are not available.
145 */
146 function clean_database(&$page)
147 {
148 /*
149 $res = $this->dbh->query("select distinct plugin from diogenes_plugin");
150 while (list($plugname) = mysql_fetch_row($res))
151 {
152 $page->info("examining $plugname..");
153 $plug = $this->load($plugname);
154 if (!is_object($plug)) {
155 $page->info("plugin $plugname is broken, removing");
156 $this->dbh->query("delete from diogenes_plugin where plugin='$plugname'");
157 }
158 }
159 mysql_free_result($res);
160 */
161 }
162
163
164 /** Compile plugin cache.
165 *
166 * @param $cachefile
167 * @param $barrel
168 */
169 function compileCache($cachefile, $barrel)
170 {
171 if (!$fp = fopen($cachefile, "w")) {
172 trigger_error("failed to open '$cachefile' for writing", E_USER_ERROR);
173 }
174
175 // get the list of available plugins
176 $available = array();
177 if (!$barrel) {
178
179 $plugfiles = System::find($this->plugdir.' -type f -name *.php');
180 foreach ($plugfiles as $file) {
181 $name = basename($file);
182 $name = substr($name, 0, -4);
183 array_push($available, $name);
184 }
185
186 } else {
187
188 $sql = "select plugin from diogenes_plugin where page=0 AND barrel=''";
189 $res = $this->dbh->query($sql);
190 while (list($plugin) = mysql_fetch_row($res))
191 {
192 array_push($available, $plugin);
193 }
194 mysql_free_result($res);
195 }
196
197 /*
198 echo "compile : available <pre>";
199 print_r($available);
200 echo "</pre>";
201 */
202 // get active plugins
203 $sql = "select page, pos, plugin, params from diogenes_plugin where barrel='{$barrel}' order by page, pos";
204 $res = $this->dbh->query($sql);
205 $active = array();
206 while ($row = mysql_fetch_row($res))
207 {
208 $plugin = $row[2];
209 if (in_array($plugin, $available)) {
210 array_unshift($row, 1);
211 fputs($fp, join("\t", $row) . "\n");
212 if (!$row[1]) {
213 array_push($active, $plugin);
214 //echo "compileCache : adding active plugin $plugin<br/>";
215 }
216 }
217 }
218 mysql_free_result($res);
219
220 // add inactive plugins
221 foreach ($available as $plugin)
222 {
223 if (!in_array($plugin, $active))
224 {
225 //echo "compileCache : adding inactive plugin $plugin<br/>";
226 $row = array(0, 0, 0, $plugin, '');
227 fputs($fp, join("\t", $row) . "\n");
228 }
229 }
230
231 fclose($fp);
232
233 //$this->log("rcs_commit","{$this->alias}:$dir/$file:$message");
234
235 }
236
237
238 /** Load the specified plugin
239 *
240 * @param $plugentry
241 */
242 function load($plugentry)
243 {
244 $plugin = $plugentry['plugin'];
245 $plugfile = $this->plugdir."/$plugin.php";
246 if (!file_exists($plugfile)) {
247 trigger_error("could not find plugin file '$plugfile'", E_USER_WARNING);
248 return;
249 }
250
251 include_once($plugfile);
252
253 if (!class_exists($plugin)) {
254 trigger_error("could not find class '$plugin'", E_USER_WARNING);
255 return;
256 }
257
258 // load and register plugin
259 $plug = new $plugin();
260 $plug->pos = $plugentry['pos'];
261 $plug->active = $plugentry['active'];
262 $plug->setParams($plugentry['params']);
263 $this->loaded[$plugin] =& $plug;
264
265 return $plug;
266 }
267
268
269 /** Read the compiled plugin cache
270 *
271 * @param $cachefile
272 * @param $barrel
273 */
274 function readCache($cachefile, $barrel)
275 {
276 if (!file_exists($cachefile)) {
277 return array();
278 }
279
280 if (!$fp = fopen($cachefile, "r")) {
281 trigger_error("failed to open '$cachefile' for reading", E_USER_WARNING);
282 return;
283 }
284
285 $plugins = array();
286 while ($line = fgets($fp))
287 {
288 // drop end of line
289 $line = substr($line, 0, -1);
290 $bits = explode("\t", $line);
291 $plug = array(
292 'active' => $bits[0],
293 'page' => $bits[1],
294 'pos' => $bits[2],
295 'plugin' => $bits[3],
296 'params' => $bits[4],
297 );
298 array_push($plugins, $plug);
299 }
300
301 fclose($fp);
302
303 return $plugins;
304 }
305
306
307 /** Prepare plugins trace for output
308 */
309 function trace_format()
310 {
311 $out = "";
312 foreach ($this->loaded as $key => $val)
313 {
314 $out .= '<table class="light" style="width: 100%; font-family: monospace">';
315 $out .= '<tr><th colspan="2">'.$key.' v'.$val->version.'</th></tr>';
316 if (isset($val->pos)) {
317 $out .= '<tr><td>position</td><td>'.$val->pos.'</td></tr>';
318 }
319 $out .= '<tr><td>type</td><td>'.$val->type.'</td></tr>';
320 $out .= '<tr><td>description</td><td>'.$val->description.'</td></tr>';
321 if (empty($val->params)) {
322 $out .= '<tr class="odd"><td colspan="2">parameters</td></tr>';
323 foreach ($val->params as $skey => $sval)
324 {
325 $out .= "<tr><td>$skey</td><td>$sval</td></tr>";
326 }
327 }
328 $out .= "</table><br/>";
329 }
330 return $out;
331 }
332
333
334
335 }
336
337 ?>