3 * FusionForge plugin system
5 * Copyright 2002, 2009, Roland Mas
6 * Copyright 2014, Franck Villaume - TrivialDev
7 * http://fusionforge.org
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 class PluginManager extends Error {
27 var $plugins_to_hooks;
28 var $hooks_to_plugins;
29 var $returned_values = array();
32 * PluginManager() - constructor
35 function PluginManager() {
37 $this->plugins_objects = array();
38 $this->plugins_to_hooks = array();
39 $this->hooks_to_plugins = array();
41 //to work with Codendi files
42 static function instance() {
43 global $PLUGINMANAGER_OBJ;
44 if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
45 $PLUGINMANAGER_OBJ = new PluginManager;
47 return $PLUGINMANAGER_OBJ;
51 * GetPlugins() - get a list of installed plugins
53 * @return array hash of plugin id => plugin names
55 function GetPlugins() {
56 $this->plugins_data = array();
57 $res = db_query_params('SELECT plugin_id, plugin_name FROM plugins',
59 $rows = db_numrows($res);
60 for ($i=0; $i<$rows; $i++) {
61 $plugin_id = db_result($res, $i, 'plugin_id');
62 $this->plugins_data[$plugin_id] = db_result($res, $i, 'plugin_name');
64 return $this->plugins_data;
68 * GetPluginObject() - get a particular plugin object
70 * @param string $pluginname name of plugin
71 * @return object plugin object or false if not available
73 function GetPluginObject($pluginname) {
74 if (!isset($this->plugins_objects[$pluginname])) {
77 return $this->plugins_objects[$pluginname];
80 //Added for Codendi compatibility
81 function getPluginByName($pluginname) {
82 return @$this->plugins_objects[$pluginname];
86 * PluginIsInstalled() - is a plugin installed?
88 * @param string $pluginname name of plugin
89 * @return bool true if installed
91 function PluginIsInstalled($pluginname) {
92 $plugins_data = $this->getPlugins();
93 foreach ($plugins_data as $p_id => $p_name) {
94 if ($p_name == $pluginname) {
102 * isPluginAvailable() - is a plugin available?
104 * @param string $plugin name of plugin
105 * @return bool true if installed
107 function isPluginAvailable($plugin) {
108 $pluginname = $plugin->GetName();
109 $plugins_data = $this->getPlugins();
110 foreach ($plugins_data as $p_id => $p_name) {
111 if ($p_name == $pluginname) {
117 function activate($pluginname) {
118 $query_exists = 'SELECT plugin_id, plugin_name FROM plugins WHERE plugin_name=$1';
119 $res = db_query_params($query_exists, array($pluginname));
120 if (db_numrows($res) == 0) {
121 $res = db_query_params('INSERT INTO plugins (plugin_name,plugin_desc) VALUES ($1,$2)',
122 array($pluginname, "This is the $pluginname plugin"));
125 $res = db_query_params($query_exists, array($pluginname));
126 if (db_numrows($res) == 1) {
127 $plugin_id = db_result($res, 0, 'plugin_id');
128 $this->plugins_data[$plugin_id] = db_result($res,0,'plugin_name');
135 function deactivate($pluginname) {
136 $res = db_query_params('DELETE FROM plugins WHERE plugin_name = $1', array($pluginname));
140 if (!isset($this->plugins_data)) {
141 $this->plugins_data = array();
143 foreach ($this->plugins_data as $i => $n) {
144 if ($n == $pluginname) {
149 unset($this->plugins_data[$p_id]);
155 * LoadPlugin() - load a specific plugin
158 function LoadPlugin($p_name) {
159 global $gfplugins, $gfcommon, $gfwww;
161 $include_path = forge_get_config('plugins_path');
162 $filename = $include_path . '/'. $p_name . "/common/".$p_name."-init.php";
163 if (file_exists($filename)) {
164 require_once ($filename);
166 $filename = $include_path . '/'. $p_name . "/common/".$p_name."Plugin.class.php";
167 if (file_exists($filename)) {
168 require_once($filename);
169 $p_class = $p_name.'Plugin';
170 register_plugin (new $p_class);
177 * LoadPlugins() - load available plugins
180 function LoadPlugins() {
181 $plugins_data = $this->GetPlugins();
182 foreach ($plugins_data as $p_id => $p_name) {
183 if (!$this->LoadPlugin($p_name)) {
184 // we can't find the plugin so we remove it from the array
185 unset($this->plugins_data[$p_id]);
192 * SetupHooks() - setup all hooks for installed plugins
195 function SetupHooks() {
196 $this->hooks_to_plugins = array();
197 foreach ($this->plugins_to_hooks as $p_name => $hook_list) {
198 foreach ($hook_list as $hook_name) {
199 if (!isset($this->hooks_to_plugins[$hook_name])) {
200 $this->hooks_to_plugins[$hook_name] = array();
202 $this->hooks_to_plugins[$hook_name][] = $p_name;
209 * RegisterPlugin() - register a plugin
211 * @param object $pluginobject an object of a subclass of the Plugin class
214 function RegisterPlugin(&$pluginobject) {
215 if (!$pluginobject->GetName()) {
216 exit_error(_("Some plugin did not provide a name. I'd gladly tell you which one, but obviously I cannot. Sorry."),'');
218 $p_name = $pluginobject->GetName();
219 $this->plugins_objects[$p_name] =& $pluginobject;
220 $this->plugins_to_hooks[$p_name] = array_unique($pluginobject->GetHooks());
225 * RunHooks() - call hooks from a particular point
227 * @param string $hookname - name of the hook
228 * @param array $params - array of extra parameters
230 * @return boolean, true if all returned true.
232 function RunHooks($hookname, & $params) {
234 $this->returned_values[$hookname] = array();
235 if (isset($this->hooks_to_plugins[$hookname])) {
236 $p_list = $this->hooks_to_plugins[$hookname];
237 foreach ($p_list as $p_name) {
238 $p_obj = $this->plugins_objects[$p_name];
239 if (method_exists($p_obj, $hookname)) {
240 $returned = $p_obj->$hookname($params);
242 $returned = $p_obj->CallHook($hookname, $params);
244 $this->returned_values[$hookname][$p_name] = $returned;
245 $result = $result && $returned;
248 // Return true only if all the plugins have returned true.
252 function getReturnedValues($hookname) {
253 return $this->returned_values[$hookname];
256 function getReturnedValuesAsString($hookname) {
259 if (isset($this->returned_values[$hookname])) {
260 foreach ($this->returned_values[$hookname] as $value) {
269 * CountHookListeners() - number of listeners on a particular hook
271 * @param string $hookname name of the hook
272 * @return int nb of listeners for this hookname
274 function CountHookListeners($hookname) {
275 if (isset($this->hooks_to_plugins[$hookname])) {
276 $p_list = $this->hooks_to_plugins[$hookname];
277 return count($p_list);
284 * GetHookListeners() - list of plugins listening on a particular hook
286 * @param string $hookname name of the hook
287 * @return array array of plugin names, listening for this hookname
289 function GetHookListeners($hookname) {
290 if (isset($this->hooks_to_plugins[$hookname])) {
291 return $this->hooks_to_plugins[$hookname];
297 function isPluginAllowedForProject($p, $group_id) {
298 $Group = group_get_object($group_id);
299 return $Group->usesPlugin($p->getName());
304 * plugin_manager_get_object() - get the PluginManager object
306 * @return PluginManager the PluginManager object
308 function &plugin_manager_get_object() {
309 global $PLUGINMANAGER_OBJ;
310 if (!isset($PLUGINMANAGER_OBJ) || !$PLUGINMANAGER_OBJ) {
311 $PLUGINMANAGER_OBJ = new PluginManager;
313 return $PLUGINMANAGER_OBJ;
317 * plugin_get_object() - get a particular Plugin object
319 * @param string $pluginname - a plugin name
320 * @return Plugin The Plugin object
322 function &plugin_get_object($pluginname) {
323 global $PLUGINMANAGER_OBJ;
324 $result=$PLUGINMANAGER_OBJ->Getpluginobject($pluginname);
329 * register_plugin() - register a plugin
331 * @param pluginobject - an object of a subclass of the Plugin class
334 function register_plugin(&$pluginobject) {
335 $pm =& plugin_manager_get_object();
336 return $pm->RegisterPlugin($pluginobject);
340 * plugin_hook() - run a set of hooks
342 * @param string $hookname - name of the hook
343 * @param array $params - parameters for the hook
346 function plugin_hook($hookname, $params = array()) {
347 $pm =& plugin_manager_get_object();
348 return $pm->RunHooks($hookname, $params);
352 * plugin_hook_by_reference() - run a set of hooks with params passed by reference
354 * @param string $hookname - name of the hook
355 * @param array $params - parameters for the hook
358 function plugin_hook_by_reference($hookname, &$params) {
359 $pm =& plugin_manager_get_object();
360 return $pm->RunHooks($hookname, $params);
364 * plugin_hook_listeners() - count the number of listeners on a hook
366 * @param string $hookname - name of the hook
369 function plugin_hook_listeners($hookname) {
370 $pm =& plugin_manager_get_object();
371 return $pm->CountHookListeners($hookname);
375 * setup_plugin_manager() - initialise the plugin infrastructure
378 function setup_plugin_manager() {
379 $pm =& plugin_manager_get_object();
387 // c-file-style: "bsd"