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';
24 /** This class describes Diogenes' plugins.
26 class Diogenes_Plugins
28 /** Array of currently loaded plugins */
29 var $loaded = array();
31 /** Directory that holds the plugins cache files */
34 /** Directory that holds the plugins */
38 /** Constructs a new holder for Diogenes plugins.
44 function Diogenes_Plugins(&$dbh, $plugdir, $cachedir)
47 $this->plugdir
= $plugdir;
48 $this->cachedir
= $cachedir;
52 /** Return the path of the cache file
56 function cacheFile($barrel)
58 $cachefile = $this->cachedir
. "/" . ($barrel ?
$barrel : "__diogenes__") . ".plugins";
63 /** Return the cache entry for specified plugin
70 function cacheGet($cache, $barrel, $page, $plugin)
72 foreach ($cache as $plugentry)
74 if (($plugentry['plugin'] == $plugin) && ($plugentry['page'] == $page))
83 /** Return the cache entry for a plugin at a specified position
90 function cacheGetAtPos($cache, $barrel, $page, $pos)
92 foreach ($cache as $plugentry)
94 if (($plugentry['pos'] == $pos) && ($plugentry['page'] == $page))
102 /** List the plugins that are active in a given context
108 function cachedActive($cache, $barrel, $page)
111 foreach ($cache as $plug)
113 if ($plug['page'] == $page)
115 array_push($plugins, $plug);
122 /** Returns an array of cache entries representing the available plugins
129 function cachedAvailable($cache, $barrel, $page)
131 $available = array();
132 foreach ($cache as $plugentry)
134 $plugfile = $this->plugdir
."/".$plugentry['plugin'].".php";
135 if (file_exists($plugfile) and ($plugentry['page'] == 0) and (!$page or $plugentry['active']))
137 array_push($available, $plugentry['plugin']);
144 /** Remove database references to plugins that are not available.
146 function clean_database(&$page)
149 $res = $this->dbh->query("select distinct plugin from diogenes_plugin");
150 while (list($plugname) = mysql_fetch_row($res))
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'");
159 mysql_free_result($res);
164 /** Compile plugin cache.
169 function compileCache($cachefile, $barrel)
171 if (!$fp = fopen($cachefile, "w")) {
172 trigger_error("failed to open '$cachefile' for writing", E_USER_ERROR
);
175 // get the list of available plugins
176 $available = array();
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);
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))
192 array_push($available, $plugin);
194 mysql_free_result($res);
198 echo "compile : available <pre>";
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);
206 while ($row = mysql_fetch_row($res))
209 if (in_array($plugin, $available)) {
210 array_unshift($row, 1);
211 fputs($fp, join("\t", $row) . "\n");
213 array_push($active, $plugin);
214 //echo "compileCache : adding active plugin $plugin<br/>";
218 mysql_free_result($res);
220 // add inactive plugins
221 foreach ($available as $plugin)
223 if (!in_array($plugin, $active))
225 //echo "compileCache : adding inactive plugin $plugin<br/>";
226 $row = array(0, 0, 0, $plugin, '');
227 fputs($fp, join("\t", $row) . "\n");
233 //$this->log("rcs_commit","{$this->alias}:$dir/$file:$message");
238 /** Load the specified plugin
242 function load($plugentry)
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
);
251 include_once($plugfile);
253 if (!class_exists($plugin)) {
254 trigger_error("could not find class '$plugin'", E_USER_WARNING
);
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;
269 /** Read the compiled plugin cache
274 function readCache($cachefile, $barrel)
276 if (!file_exists($cachefile)) {
280 if (!$fp = fopen($cachefile, "r")) {
281 trigger_error("failed to open '$cachefile' for reading", E_USER_WARNING
);
286 while ($line = fgets($fp))
289 $line = substr($line, 0, -1);
290 $bits = explode("\t", $line);
292 'active' => $bits[0],
295 'plugin' => $bits[3],
296 'params' => $bits[4],
298 array_push($plugins, $plug);
307 /** Prepare plugins trace for output
309 function trace_format()
312 foreach ($this->loaded
as $key => $val)
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>';
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)
325 $out .= "<tr><td>$skey</td><td>$sval</td></tr>";
328 $out .= "</table><br/>";