3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
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.
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.
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
22 require_once 'System.php';
23 require_once 'Tree/Node.php';
24 require_once 'Plugin/Skel.php';
27 /** This class describes Diogenes' plugins.
29 class Diogenes_Plugins
31 /** Directory that holds the plugins cache files */
34 /** Directory that holds the plugins */
37 /** Plugins that will be sent to trace */
40 /** Constructs a new holder for Diogenes plugins.
46 function Diogenes_Plugins(&$dbh, $plugdir, $cachedir)
49 $this->plugdir
= $plugdir;
50 $this->cachedir
= $cachedir;
54 /** Return the path of the cache file
58 function cacheFile($barrel)
60 $cachefile = $this->cachedir
. "/" . ($barrel ?
$barrel : "__diogenes__") . ".plugins";
65 /** Return the cache entry for specified plugin
72 function cacheGet($cache, $barrel, $page, $plugin)
74 $p_node = $this->cacheGetPageNode($cache, $barrel, $page);
75 return $p_node->data
[$plugin];
79 /** Return the cache entry for specified plugin
86 function cacheGetPageNode($cache, $barrel, $page)
89 $p_node = $cache->getChild($page);
97 /** List the plugins that are active in a given context
103 function cacheGetActive($cache, $barrel, $page)
105 $p_node = $this->cacheGetPageNode($cache, $barrel, $page);
107 foreach ($p_node->data
as $plugname => $plugentry)
109 //echo "cacheGetActive : examining $plugname (status {$plugentry['status']})<br/>\n";
110 if ($plugentry['status'] & PLUG_ACTIVE
)
112 //echo "cacheGetActive : adding $plugname<br/>\n";
113 $plugins[$plugname] = $plugentry;
120 /** Returns an array of cache entries representing the available plugins
127 function cacheGetVisible($cache, $barrel, $page)
129 $p_node = $this->cacheGetPageNode($cache, $barrel, $page);
130 return $p_node->data
;
134 /** Remove database references to plugins that are not available.
136 function clean_database(&$page)
139 $res = $this->dbh->query("select distinct plugin from diogenes_plugin");
140 while (list($plugname) = mysql_fetch_row($res))
142 $page->info("examining $plugname..");
143 $plug = $this->load($plugname);
144 if (!is_object($plug)) {
145 $page->info("plugin $plugname is broken, removing");
146 $this->dbh->query("delete from diogenes_plugin where plugin='$plugname'");
149 mysql_free_result($res);
154 /** Build view for current level.
156 function compileNode($node, &$outparent, $index)
159 foreach ($outparent->data
as $plugin => $parentval)
161 //echo "Processing plugin '$plugin' for <{$node->name}><br/>\n";
163 if (($parentval['status'] & PLUG_ACTIVE
) ||
!($parentval['status'] & PLUG_LOCK
))
165 //echo "* plugin '$plugin' not locked off parent level<br/>\n";
166 $outval = $parentval;
167 if ((is_array($node->data
[$plugin])) && ($node->data
[$plugin]['status'] & PLUG_SET
))
169 //echo "** plugin set at current level<br/>\n";
170 $outval['params'] = $node->data
[$plugin]['params'];
171 if (!($parentval['status'] & PLUG_LOCK
)) {
172 //echo "*** plugin status set to DB-specified value<br/>\n";
173 $outval['status'] = $node->data
[$plugin]['status'];
175 $outval['status'] = $parentval['status'];
178 //echo "** plugin unset at current level<br/>\n";
182 // add the plugin to output
183 if (is_array($outval))
185 $outvals[$plugin] = $outval;
188 $outnode = new Diogenes_Tree_Node($outvals, $node->name
);
191 // recurse into children
192 foreach ($node->children
as $cindex => $child)
194 $this->compileNode($child, $outnode, $cindex);
197 // add the produced node
198 $outparent->setChild($index, $outnode);
202 /** Compile plugin cache.
206 function compileCache($barrel, &$caller)
208 $caller->info("Recompiling " .($barrel ?
"plugin cache for barrel '$barrel'" : "global plugin cache"));
210 // get the list of all plugins present on the system
211 $allplugins = array();
212 $plugfiles = System
::find($this->plugdir
.' -type f -name *.php');
213 foreach ($plugfiles as $file) {
214 $name = basename($file);
215 $name = substr($name, 0, -4);
216 array_push($allplugins, $name);
220 // fill initial values
221 foreach ($allplugins as $plugin)
223 $plug_h = $this->load($plugin);
224 $defcache[$plugin] = $plug_h->toArray();
230 $sql_limit = $barrel ?
" where barrel='{$barrel}' or barrel=''" : "";
231 $sql = "select barrel, page, plugin, status, params from diogenes_plugin" . $sql_limit;
232 $res = $this->dbh
->query($sql);
233 while($row = mysql_fetch_row($res))
235 $c_barrel = array_shift($row);
236 $c_page = array_shift($row);
237 $plugin = array_shift($row);
240 'params' => ($row[1] ?
var_decode_bin($row[1]) : array())
242 $plug_h = $this->load($plugin, $plugentry);
243 //echo "Got params from DB for '$plugin', barrel '$c_barrel', page '$c_page' : ".$row[2]."<br/>";
244 $dbcache[$c_barrel][$c_page][$plugin] = $plug_h->toArray();
246 mysql_free_result($res);
248 // build the input tree
249 $globals_node = new Diogenes_Tree_Node($dbcache[''][0], 'globals defaults');
250 $sql_limit = $barrel ?
" where alias='{$barrel}'" : " where alias!=''";
251 $res = $this->dbh
->query("select alias from diogenes_site" . $sql_limit);
252 while(list($c_barrel) = mysql_fetch_row($res))
254 $barrel_node = new Diogenes_Tree_Node($dbcache[$c_barrel][0], "barrel '$c_barrel' defaults");
255 $res2 = $this->dbh
->query("select PID from {$c_barrel}_page");
256 while(list($page) = mysql_fetch_row($res2))
258 $page_node = new Diogenes_Tree_Node($dbcache[$c_barrel][$page], "barrel '$c_barrel' page $page");
259 $barrel_node->setChild($page, $page_node);
261 mysql_free_result($res2);
262 $globals_node->setChild($c_barrel, $barrel_node);
264 mysql_free_result($res);
267 $top_out_node = new Diogenes_Tree_Node($defcache, 'plugin defaults');
268 $this->compileNode($globals_node, $top_out_node, 'globals');
269 $globals_out_node = $top_out_node->getChild('globals');
270 //echo "<pre>" . $top_out_node->dump() . "</pre><hr/>";
274 $dump_node = $globals_out_node->getChild($barrel);
275 $dump_node->writeFile($this->cachefile($barrel));
276 $dump_node->writeFile($this->cachefile($barrel).".txt", NODE_DUMP_TEXT
);
278 $globals_out_node->writeFile($this->cachefile($barrel), NODE_DUMP_NOCHILDREN
);
279 $globals_out_node->writeFile($this->cachefile($barrel).".txt", NODE_DUMP_NOCHILDREN | NODE_DUMP_TEXT
);
280 foreach ($globals_out_node->children
as $c_barrel => $dump_node)
282 $dump_node->writeFile($this->cachefile($c_barrel));
283 $dump_node->writeFile($this->cachefile($c_barrel).".txt", NODE_DUMP_TEXT
);
289 /** Load the specified plugin
293 function load($plugin, $plugentry = '')
295 $plugfile = $this->plugdir
."/$plugin.php";
296 if (!file_exists($plugfile)) {
297 trigger_error("could not find plugin file '$plugfile'", E_USER_WARNING
);
300 include_once($plugfile);
301 if (!class_exists($plugin)) {
302 trigger_error("could not find class '$plugin'", E_USER_WARNING
);
306 // load and register plugin
307 $plug_h = new $plugin();
308 if (is_array($plugentry))
309 $plug_h->fromArray($plugentry);
315 /** Read the compiled plugin cache
320 function readCache($cachefile, $barrel)
322 if (!file_exists($cachefile)) {
326 return Diogenes_Tree_Node
::readFile($cachefile);
330 /** Prepare plugins trace for output
332 function trace_format()
334 $out = '<table class="light" style="width: 100%; font-family: monospace">'."\n";
336 foreach ($this->log
as $key => $val)
338 $trclass = $odd ?
' class="odd"' : '';
339 $out .= "<tr><th colspan=\"2\">{$val['name']} v{$val['version']}</th></tr>\n";
340 $out .= "<tr><td>status</td><td>{$val['status']}</td></tr>\n";
341 $out .= "<tr$trclass><td>type</td><td>{$val['type']}</td></tr>\n";
342 $out .= "<tr$trclass><td>description</td><td>{$val['description']}</td></tr>\n";
343 if (!empty($val['params'])) {
344 $out .= "<tr$trclass><td>parameters</td><td>".var_encode_html($val['params'])."</td></tr>\n";
348 $out .= "</table>\n";