Plugins.php

00001 <?php
00002 /*
00003  * Copyright (C) 2003-2004 Polytechnique.org
00004  * http://opensource.polytechnique.org/
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 // dependency on PEAR
00022 require_once 'System.php';
00023 
00026 class Diogenes_Plugins
00027 {
00029   var $loaded = array();
00030 
00032   var $cachedir;
00033   
00035   var $plugdir;
00036   
00037   
00044   function Diogenes_Plugins(&$dbh, $plugdir, $cachedir)
00045   {
00046     $this->dbh =& $dbh;
00047     $this->plugdir = $plugdir;
00048     $this->cachedir = $cachedir;    
00049   }
00050   
00051   
00056   function cacheFile($barrel)
00057   {
00058     $cachefile = $this->cachedir . "/" . ($barrel ? $barrel : "__diogenes__") . ".plugins";
00059     return $cachefile;
00060   }
00061     
00062   
00070   function cacheGet($cache, $barrel, $page, $plugin)
00071   {
00072     foreach ($cache as $plugentry)
00073     {      
00074       if (($plugentry['plugin'] == $plugin) && ($plugentry['page'] == $page))
00075       {
00076         return $plugentry;
00077       }
00078     }
00079     return;     
00080   }
00081 
00082   
00090   function cacheGetAtPos($cache, $barrel, $page, $pos)
00091   {
00092     foreach ($cache as $plugentry)
00093     {      
00094       if (($plugentry['pos'] == $pos) && ($plugentry['page'] == $page))
00095       {
00096         return $plugentry;
00097       }
00098     }  
00099   }  
00100 
00101   
00108   function cachedActive($cache, $barrel, $page)
00109   {
00110     $plugins = array();
00111     foreach ($cache as $plug)
00112     {
00113       if ($plug['page'] == $page) 
00114       {
00115         array_push($plugins, $plug);
00116       }
00117     }
00118     return $plugins;    
00119   }
00120 
00121     
00129   function cachedAvailable($cache, $barrel, $page)
00130   {      
00131     $available = array();
00132     foreach ($cache as $plugentry)
00133     {      
00134       $plugfile = $this->plugdir."/".$plugentry['plugin'].".php";
00135       if (file_exists($plugfile) and ($plugentry['page'] == 0) and (!$page or $plugentry['active']))
00136       {
00137         array_push($available, $plugentry['plugin']);
00138       }
00139     }
00140     return $available;    
00141   }
00142   
00143   
00146   function clean_database(&$page)
00147   {
00148     /*
00149     $res = $this->dbh->query("select distinct plugin from diogenes_plugin");
00150     while (list($plugname) = mysql_fetch_row($res))
00151     {
00152       $page->info("examining $plugname..");
00153       $plug = $this->load($plugname);
00154       if (!is_object($plug)) {
00155         $page->info("plugin $plugname is broken, removing");
00156         $this->dbh->query("delete from diogenes_plugin where plugin='$plugname'");
00157       }
00158     }
00159     mysql_free_result($res);
00160     */
00161   }
00162 
00163     
00169   function compileCache($cachefile, $barrel)
00170   {
00171     if (!$fp = fopen($cachefile, "w")) {
00172       trigger_error("failed to open '$cachefile' for writing", E_USER_ERROR);
00173     }
00174 
00175     // get the list of available plugins
00176     $available = array();            
00177     if (!$barrel) {
00178     
00179       $plugfiles = System::find($this->plugdir.' -type f -name *.php');  
00180       foreach ($plugfiles as $file) {
00181         $name = basename($file);
00182         $name = substr($name, 0, -4);      
00183         array_push($available, $name);
00184       }      
00185       
00186     } else {
00187             
00188       $sql = "select plugin from diogenes_plugin where page=0 AND barrel=''";
00189       $res = $this->dbh->query($sql);
00190       while (list($plugin) = mysql_fetch_row($res))
00191       {
00192         array_push($available, $plugin);
00193       }
00194       mysql_free_result($res);
00195    }
00196     
00197 /*
00198    echo "compile : available <pre>";
00199    print_r($available);
00200    echo "</pre>";
00201 */   
00202    // get active plugins
00203    $sql = "select page, pos, plugin, params from diogenes_plugin where barrel='{$barrel}' order by page, pos";
00204    $res = $this->dbh->query($sql);
00205    $active = array();
00206    while ($row = mysql_fetch_row($res))
00207    {
00208      $plugin = $row[2];
00209      if (in_array($plugin, $available)) {
00210        array_unshift($row, 1); 
00211        fputs($fp, join("\t", $row) . "\n");       
00212        if (!$row[1]) {
00213          array_push($active, $plugin);
00214          //echo "compileCache : adding active plugin $plugin<br/>";       
00215        }
00216      }
00217    }
00218    mysql_free_result($res);    
00219    
00220    // add inactive plugins
00221    foreach ($available as $plugin)
00222    {
00223      if (!in_array($plugin, $active))
00224      {
00225        //echo "compileCache : adding inactive plugin $plugin<br/>";
00226        $row = array(0, 0, 0, $plugin, '');
00227        fputs($fp, join("\t", $row) . "\n");     
00228      }
00229    }
00230    
00231    fclose($fp);
00232    
00233    //$this->log("rcs_commit","{$this->alias}:$dir/$file:$message");
00234 
00235   }
00236   
00237    
00242   function load($plugentry)
00243   {
00244     $plugin = $plugentry['plugin'];
00245     $plugfile = $this->plugdir."/$plugin.php";
00246     if (!file_exists($plugfile)) {
00247       trigger_error("could not find plugin file '$plugfile'", E_USER_WARNING);
00248       return;
00249     }
00250      
00251     include_once($plugfile);
00252   
00253     if (!class_exists($plugin)) {
00254       trigger_error("could not find class '$plugin'", E_USER_WARNING);
00255       return;
00256     }
00257     
00258     // load and register plugin
00259     $plug = new $plugin();
00260     $plug->pos = $plugentry['pos'];
00261     $plug->active = $plugentry['active'];
00262     $plug->setParams($plugentry['params']);
00263     $this->loaded[$plugin] =& $plug;      
00264     
00265     return $plug;
00266   }
00267     
00268 
00274   function readCache($cachefile, $barrel)
00275   {
00276     if (!file_exists($cachefile)) {
00277         return array();
00278     }
00279     
00280     if (!$fp = fopen($cachefile, "r")) {
00281       trigger_error("failed to open '$cachefile' for reading", E_USER_WARNING);
00282       return;
00283     }
00284     
00285     $plugins = array();
00286     while ($line = fgets($fp))
00287     {
00288       // drop end of line
00289       $line = substr($line, 0, -1);
00290       $bits = explode("\t", $line);
00291       $plug = array(
00292         'active' => $bits[0],
00293         'page'   => $bits[1],
00294         'pos'    => $bits[2],
00295         'plugin' => $bits[3],
00296         'params' => $bits[4],
00297       );        
00298       array_push($plugins, $plug);
00299     }
00300         
00301     fclose($fp);
00302     
00303     return $plugins;
00304   }
00305 
00306       
00309   function trace_format()
00310   {
00311     $out = "";
00312     foreach ($this->loaded as $key => $val)
00313     {      
00314       $out .= '<table class="light" style="width: 100%; font-family: monospace">';
00315       $out .= '<tr><th colspan="2">'.$key.' v'.$val->version.'</th></tr>';
00316       if (isset($val->pos)) {
00317         $out .= '<tr><td>position</td><td>'.$val->pos.'</td></tr>';
00318       }
00319       $out .= '<tr><td>type</td><td>'.$val->type.'</td></tr>';
00320       $out .= '<tr><td>description</td><td>'.$val->description.'</td></tr>';
00321       if (empty($val->params)) {
00322         $out .= '<tr class="odd"><td colspan="2">parameters</td></tr>';
00323         foreach ($val->params as $skey => $sval) 
00324         {
00325           $out .= "<tr><td>$skey</td><td>$sval</td></tr>";
00326         }
00327       }
00328       $out .= "</table><br/>";
00329     }
00330     return $out;
00331   }
00332   
00333   
00334   
00335 }
00336 
00337 ?>

Generated on Fri Jan 11 01:20:08 2008 for Diogenes by  doxygen 1.5.1