<commit_before><?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Plugins Helper Class
 *
 * PHP version 5
 * LICENSE: This source file is subject to GPLv3 license 
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/copyleft/gpl.html
 * @author     <NAME> <<EMAIL>> 
 * @package	   SwiftRiver - http://github.com/ushahidi/Swiftriver_v2
 * @category Helpers
 * @copyright  Ushahidi - http://www.ushahidi.com
 * @license    http://www.gnu.org/copyleft/gpl.html GNU General Public License v3 (GPLv3) 
 */
class Swiftriver_Plugins {
	
	/**
	 * List of plugins configured as channels
	 * @var array
	 */
	private static $channels = array();
	
	/**
	 * Load all active plugins into the Kohana system
	 *
	 * @return	void
	 */
	public static function load()
	{
		$active_plugins = ORM::factory("plugin")
			->where("plugin_enabled", "=", 1)
			->find_all();
		
		$plugin_entries = array();
		foreach ($active_plugins as $plugin)
		{
			$plugin_entries[$plugin->plugin_path] = PLUGINPATH.$plugin->plugin_path;
		}
		
		// Add the plugin entries to the list of Kohana modules
		Kohana::modules(Kohana::modules() + $plugin_entries);
	}
	
	/**
	 * Find and load all the plugin config files regardless of whether
	 * they've been loaded by the Kohana system or not
	 *
	 * @return	array $configs
	 */
	public static function load_configs()
	{
		$d = @dir(PLUGINPATH); 
		
		// Trap for errors
		if ( ! $d)
		{
			Kohana::$log->add(Log::ERROR, "Failed opening directory :dir for reading", 
			    array(':dir' => PLUGINPATH));
			
			return FALSE;
		}
		
		$directories = array();
		$configs = array();
		while (($entry = $d->read()) !== FALSE )
		{
			// Don't include hidden folders (only applies to UNIX-like systems)
			if ($entry[0] == '.') continue;
			{
				$directories[$entry] = FALSE;
			}
		}
		
		// Cycle through each plugin directory and load config files
		foreach ($directories as $dir => $found)
		{
			$file = PLUGINPATH.$dir.'/config/plugin.php';
			if (file_exists($file))
			{
				$config_array = include $file;
				if (is_array($config_array))
				{
					$configs = array_merge($configs, $config_array);
				}
			}
		}
		
		return $configs;
	}

	/**
	 * Find all the active plugins with channel services
	 *
	 * @param   bool $reload Reloads the channel configs when TRUE
	 * @return	array $channels
	 */
	public static function channels($reload = FALSE)
	{
		if ( ! $reload AND ! empty(self::$channels))
		{
			return self::$channels;
		}
		else
		{
			$channels = array();
		
			// Load the plugin configs and fetch only those that 
			// have the channel property set to TRUE
			$plugins = Kohana::$config->load('plugin');
			foreach($plugins as $key => $plugin)
			{
				if (isset($plugin['channel']) AND $plugin['channel'] == TRUE)
				{
					$active = ORM::factory('plugin')
						->where('plugin_path', '=', $key)
						->where('plugin_enabled', '=', '1')
						->find();
					
					if ($active->loaded())
					{
						$channel_config = array();
					
						// Validate the channel configuration
						if ( ! self::_validate_channel_plugin_config($plugin, $channel_config))
							continue;
					
						// Set the plugin name
						$channel_config['name'] = $plugin['name'];

						$channels[$key] = $channel_config;
					}
				}
			}
		
			self::$channels = $channels;
		}
		
		return self::$channels;
	}

	/**
	 * Gets the configuration data of a channel
	 *
	 * @param string $channel Name of the channel
	 * @return mixed An array with the channel configuration on succeed, FALSE otherwise
	 */
	public static function get_channel_config($channel)
	{
		return isset(self::$channels[$channel]) ? self::$channels[$channel] : FALSE;
	}
	
	/**
	 * Validates the configuration of a channel plugin
	 *
	 * @param array $plugin Plugin configuration
	 * @param array $channel_config Channel config parameters to store
	 * @return bool TRUE if the config for the channel plugin is in order, 
	 *     FALSE otherwise
	 */
	private static function _validate_channel_plugin_config($plugin, array & $channel_config)
	{
		// Get the plugin name<commit_msg>Load the plugin configs if the internal array is empty<commit_after><?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Plugins Helper Class
 *
 * PHP version 5
 * LICENSE: This source file is subject to GPLv3 license 
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/copyleft/gpl.html
 * @author     <NAME> <<EMAIL>> 
 * @package	   SwiftRiver - http://github.com/ushahidi/Swiftriver_v2
 * @category Helpers
 * @copyright  Ushahidi - http://www.ushahidi.com
 * @license    http://www.gnu.org/copyleft/gpl.html GNU General Public License v3 (GPLv3) 
 */
class Swiftriver_Plugins {
	
	/**
	 * List of plugins configured as channels
	 * @var array
	 */
	private static $channels = array();
	
	/**
	 * Load all active plugins into the Kohana system
	 *
	 * @return	void
	 */
	public static function load()
	{
		$active_plugins = ORM::factory("plugin")
			->where("plugin_enabled", "=", 1)
			->find_all();
		
		$plugin_entries = array();
		foreach ($active_plugins as $plugin)
		{
			$plugin_entries[$plugin->plugin_path] = PLUGINPATH.$plugin->plugin_path;
		}
		
		// Add the plugin entries to the list of Kohana modules
		Kohana::modules(Kohana::modules() + $plugin_entries);
	}
	
	/**
	 * Find and load all the plugin config files regardless of whether
	 * they've been loaded by the Kohana system or not
	 *
	 * @return	array $configs
	 */
	public static function load_configs()
	{
		$d = @dir(PLUGINPATH); 
		
		// Trap for errors
		if ( ! $d)
		{
			Kohana::$log->add(Log::ERROR, "Failed opening directory :dir for reading", 
			    array(':dir' => PLUGINPATH));
			
			return FALSE;
		}
		
		$directories = array();
		$configs = array();
		while (($entry = $d->read()) !== FALSE )
		{
			// Don't include hidden folders (only applies to UNIX-like systems)
			if ($entry[0] == '.') continue;
			{
				$directories[$entry] = FALSE;
			}
		}
		
		// Cycle through each plugin directory and load config files
		foreach ($directories as $dir => $found)
		{
			$file = PLUGINPATH.$dir.'/config/plugin.php';
			if (file_exists($file))
			{
				$config_array = include $file;
				if (is_array($config_array))
				{
					$configs = array_merge($configs, $config_array);
				}
			}
		}
		
		return $configs;
	}

	/**
	 * Find all the active plugins with channel services
	 *
	 * @param   bool $reload Reloads the channel configs when TRUE
	 * @return	array $channels
	 */
	public static function channels($reload = FALSE)
	{
		if ( ! $reload AND ! empty(self::$channels))
		{
			return self::$channels;
		}
		else
		{
			$channels = array();
		
			// Load the plugin configs and fetch only those that 
			// have the channel property set to TRUE
			$plugins = Kohana::$config->load('plugin');
			foreach($plugins as $key => $plugin)
			{
				if (isset($plugin['channel']) AND $plugin['channel'] == TRUE)
				{
					$active = ORM::factory('plugin')
						->where('plugin_path', '=', $key)
						->where('plugin_enabled', '=', '1')
						->find();
					
					if ($active->loaded())
					{
						$channel_config = array();
					
						// Validate the channel configuration
						if ( ! self::_validate_channel_plugin_config($plugin, $channel_config))
							continue;
					
						// Set the plugin name
						$channel_config['name'] = $plugin['name'];

						$channels[$key] = $channel_config;
					}
				}
			}
		
			self::$channels = $channels;
		}
		
		return self::$channels;
	}

	/**
	 * Gets the configuration data of a channel
	 *
	 * @param string $channel Name of the channel
	 * @return mixed An array with the channel configuration on succeed, FALSE otherwise
	 */
	public static function get_channel_config($channel)
	{
		if (empty(self::$channels))
		{
			// Load the channels
			self::channels();
		}

		return isset(self::$channels[$channel]) ? self::$channels[$channel] : FALSE;
	}
	
	/**
	 * Validates the configuration of a channel plugin
	 *
	 * @param array $plugin Plugin configuration
	 * @param array $channel_config Channel config parameters to store
	 * @return bool TRUE if the config for the channel plugin is in order, 
	 *     FALSE otherwise
	 */
	private static function _validate_channel_plugin_config($plugin, array & $channel_config)
	{
		// Get the plugin name
{'fraction_non_alphanumeric': 0.13577148320784033, 'fraction_numerical': 0.0019122744113780328, 'mean_word_length': 3.066083576287658, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6259696', 'n_tokens_mistral': 3073, 'n_tokens_neox': 2541, 'n_words': 756}