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";
64 /** Return the cache entry for specified plugin
71 function cacheGet($cache, $barrel, $page, $plugin)
73 $p_node = $this->cacheGetPageNode($cache, $barrel, $page);
74 return $p_node->data
[$plugin];
78 /** Return the cache entry for a plugin at a specified position
85 function cacheGetAtPos($cache, $barrel, $page, $pos)
87 $p_node = $this->cacheGetPageNode($cache, $barrel, $page);
88 foreach ($p_node->data
as $plugname => $plugentry)
90 if ($plugentry['pos'] == $pos)
92 $plugentry['plugin'] = $plugname;
99 /** Return the cache entry for specified plugin
106 function cacheGetPageNode($cache, $barrel, $page)
109 $p_node = $cache->getChild($page);
117 /** List the plugins that are active in a given context
123 function cachedActive($cache, $barrel, $page)
125 $p_node = $this->cacheGetPageNode($cache, $barrel, $page);
127 foreach ($p_node->data
as $plugname => $plugentry)
129 //echo "cachedEditable : examining $plugname (status {$plugentry['status']})<br/>\n";
130 if ($plugentry['status'] & PLUG_ACTIVE
)
132 //echo "cachedActive : adding $plugname<br/>\n";
133 $plugins[$plugname] = $plugentry;
140 /** Returns an array of cache entries representing the available plugins
147 function cachedVisible($cache, $barrel, $page)
149 $p_node = $this->cacheGetPageNode($cache, $barrel, $page);
150 return $p_node->data
;
152 $available = array();
153 foreach ($p_node->data as $plugname => $plugentry)
155 //echo "cachedEditable : examining $plugname (status {$plugentry['status']})<br/>\n";
156 if (!($plugentry['status'] & PLUG_LOCK) || !$barrel)
158 //echo "cachedEditable : adding $plugname<br/>\n";
159 $available[$plugname] = $plugentry;
167 /** Remove database references to plugins that are not available.
169 function clean_database(&$page)
172 $res = $this->dbh->query("select distinct plugin from diogenes_plugin");
173 while (list($plugname) = mysql_fetch_row($res))
175 $page->info("examining $plugname..");
176 $plug = $this->load($plugname);
177 if (!is_object($plug)) {
178 $page->info("plugin $plugname is broken, removing");
179 $this->dbh->query("delete from diogenes_plugin where plugin='$plugname'");
182 mysql_free_result($res);
187 /** Build view for current level.
189 function compileNode($node, &$outparent, $index)
192 foreach ($outparent->data
as $plugin => $parentval)
194 //echo "Processing plugin '$plugin' for <{$node->name}><br/>\n";
196 if (($parentval['status'] & PLUG_ACTIVE
) ||
!($parentval['status'] & PLUG_LOCK
))
198 //echo "* plugin '$plugin' not locked off parent level<br/>\n";
199 $outval = $parentval;
200 if (is_array($node->data
[$plugin]))
202 //echo "** plugin set at current level<br/>\n";
203 $outval['pos'] = $node->data
[$plugin]['pos'];
204 $outval['params'] = $node->data
[$plugin]['params'];
205 if (!($parentval['status'] & PLUG_LOCK
)) {
206 //echo "*** plugin status set to DB-specified value<br/>\n";
207 $outval['status'] = $node->data
[$plugin]['status'];
209 $outval['status'] = $parentval['status'];
212 //echo "** plugin unset at current level<br/>\n";
216 // add the plugin to output
217 if (is_array($outval))
219 $outvals[$plugin] = $outval;
222 $outnode = new Diogenes_Tree_Node($outvals, $node->name
);
225 // recurse into children
226 foreach ($node->children
as $cindex => $child)
228 $this->compileNode($child, $outnode, $cindex);
231 // add the produced node
232 $outparent->setChild($index, $outnode);
236 /** Compile plugin cache.
240 function compileCache($barrel, &$caller)
242 $caller->info("Recompiling " .($barrel ?
"plugin cache for barrel '$barrel'" : "global plugin cache"));
244 // get the list of all plugins present on the system
245 $allplugins = array();
246 $plugfiles = System
::find($this->plugdir
.' -type f -name *.php');
247 foreach ($plugfiles as $file) {
248 $name = basename($file);
249 $name = substr($name, 0, -4);
250 array_push($allplugins, $name);
254 // fill initial values
255 foreach ($allplugins as $plugin)
257 $plug_h = $this->load($plugin);
258 $defcache[$plugin] = $plug_h->toArray();
264 $sql_limit = $barrel ?
" where barrel='{$barrel}' or barrel=''" : "";
265 $sql = "select barrel, page, plugin, status, pos, params from diogenes_plugin" . $sql_limit;
266 $res = $this->dbh
->query($sql);
267 while($row = mysql_fetch_row($res))
269 $c_barrel = array_shift($row);
270 $c_page = array_shift($row);
271 $plugin = array_shift($row);
275 'params' => ($row[2] ?
var_decode_bin($row[2]) : array())
277 $plug_h = $this->load($plugin, $plugentry);
278 //echo "Got params from DB for '$plugin', barrel '$c_barrel', page '$c_page' : ".$row[2]."<br/>";
279 $dbcache[$c_barrel][$c_page][$plugin] = $plug_h->toArray();
281 mysql_free_result($res);
283 // build the input tree
284 $globals_node = new Diogenes_Tree_Node($dbcache[''][0], 'globals defaults');
285 $sql_limit = $barrel ?
" where alias='{$barrel}'" : " where alias!=''";
286 $res = $this->dbh
->query("select alias from diogenes_site" . $sql_limit);
287 while(list($c_barrel) = mysql_fetch_row($res))
289 $barrel_node = new Diogenes_Tree_Node($dbcache[$c_barrel][0], "barrel '$c_barrel' defaults");
290 $res2 = $this->dbh
->query("select PID from {$c_barrel}_page");
291 while(list($page) = mysql_fetch_row($res2))
293 $page_node = new Diogenes_Tree_Node($dbcache[$c_barrel][$page], "barrel '$c_barrel' page $page");
294 $barrel_node->setChild($page, $page_node);
296 mysql_free_result($res2);
297 $globals_node->setChild($c_barrel, $barrel_node);
299 mysql_free_result($res);
302 $top_out_node = new Diogenes_Tree_Node($defcache, 'plugin defaults');
303 $this->compileNode($globals_node, $top_out_node, 'globals');
304 $globals_out_node = $top_out_node->getChild('globals');
305 //echo "<pre>" . $top_out_node->dump() . "</pre><hr/>";
309 $dump_node = $globals_out_node->getChild($barrel);
310 $dump_node->writeFile($this->cachefile($barrel));
311 $dump_node->writeFile($this->cachefile($barrel).".txt", NODE_DUMP_TEXT
);
313 $globals_out_node->writeFile($this->cachefile($barrel), NODE_DUMP_NOCHILDREN
);
314 $globals_out_node->writeFile($this->cachefile($barrel).".txt", NODE_DUMP_NOCHILDREN | NODE_DUMP_TEXT
);
315 foreach ($globals_out_node->children
as $c_barrel => $dump_node)
317 $dump_node->writeFile($this->cachefile($c_barrel));
318 $dump_node->writeFile($this->cachefile($c_barrel).".txt", NODE_DUMP_TEXT
);
324 /** Load the specified plugin
328 function load($plugin, $plugentry = '')
330 $plugfile = $this->plugdir
."/$plugin.php";
331 if (!file_exists($plugfile)) {
332 trigger_error("could not find plugin file '$plugfile'", E_USER_WARNING
);
335 include_once($plugfile);
336 if (!class_exists($plugin)) {
337 trigger_error("could not find class '$plugin'", E_USER_WARNING
);
341 // load and register plugin
342 $plug_h = new $plugin();
343 if (is_array($plugentry))
344 $plug_h->fromArray($plugentry);
346 $plug_log = $plug_h->toArray();
347 //$pluglog['name'] = 'foo';
348 array_push($this->log
, $plug_log);
353 /** Read the compiled plugin cache
358 function readCache($cachefile, $barrel)
360 if (!file_exists($cachefile)) {
364 return Diogenes_Tree_Node
::readFile($cachefile);
368 /** Prepare plugins trace for output
370 function trace_format()
372 $out = '<table class="light" style="width: 100%; font-family: monospace">'."\n";
374 foreach ($this->log
as $key => $val)
376 $trclass = $odd ?
' class="odd"' : '';
377 $out .= "<tr><th colspan=\"2\">{$val['name']} v{$val['version']}</th></tr>\n";
378 if (isset($val['pos'])) {
379 $out .= "<tr><td>position</td><td>{$val['pos']}</td></tr>\n";
381 $out .= "<tr$trclass><td>type</td><td>{$val['type']}</td></tr>\n";
382 $out .= "<tr$trclass><td>description</td><td>{$val['description']}</td></tr>\n";
383 if (!empty($val['params'])) {
384 $out .= "<tr$trclass><td>parameters</td><td>".var_encode_html($val['params'])."</td></tr>\n";
388 $out .= "</table>\n";