<commit_before><?php
JLoader::import('Hubzero.Api.Controller');

class ToolsControllerApi extends Hubzero_Api_Controller
{
	function execute()
	{
		JLoader::import('joomla.environment.request');
		JLoader::import('joomla.application.component.helper');
		
		//include tool utils
		include_once( JPATH_ROOT . DS . 'components' . DS . 'com_tools' . DS . 'helpers' . DS . 'utils.php' );
		
		switch($this->segments[0]) 
		{
			case 'index':		$this->index();				break;
			case 'info':		$this->info();				break;
			case 'screenshot':	$this->screenshot();		break;
			case 'invoke':		$this->invoke();			break;
			case 'view':		$this->view();				break;
			case 'stop':		$this->stop();				break;
			case 'unshare':		$this->unshare();			break;
			
			case 'storage':		$this->storage();			break;
			case 'purge':		$this->purge();				break;
			
			default:			$this->index();
		}
	}
	
	/**
	 * Short description for 'not_found'
	 *
	 * Long description (if any) ...
	 *
	 * @return     void
	 */
	private function not_found()
	{
		$response = $this->getResponse();
		$response->setErrorMessage(404,'Not Found');
	}
	
	/**
	 * Method to report errors. creates error node for response body as well
	 *
	 * @param	$code		Error Code
	 * @param	$message	Error Message
	 * @param	$format		Error Response Format
	 *
	 * @return     void
	 */
	private function errorMessage( $code, $message, $format = 'json' )
	{
		//build error code and message
		$object = new stdClass();
		$object->error->code = $code;
		$object->error->message = $message;
		
		//set http status code and reason
		$response = $this->getResponse();
		$response->setErrorMessage( $object->error->code, $object->error->message );
		
		//add error to message body
		$this->setMessageType( $format );
		$this->setMessage( $object );
	}
	
	/**
	 * Method to get list of tools
	 *
	 * @return     void
	 */
	private function index()
	{
		//get the userid and attempt to load user profile
		$userid = JFactory::getApplication()->getAuthn('user_id');
		$result = Hubzero_User_Profile::getInstance($userid);
		
		//make sure we have a user
		if ($result === false)	return $this->not_found();
		
		//include needed tool libraries
		ximport('Hubzero_Tool');
		ximport('Hubzero_Tool_Version');
		
		//instantiate database object
		$database = JFactory::getDBO();
		
		//get any request vars
		$format = JRequest::getVar('format', 'json');
		
		//get list of tools
		$tools = Hubzero_Tool::getMyTools();
		
		//get the supported tag
		$rconfig = JComponentHelper::getParams('com_resources');
		$supportedtag = $rconfig->get('supportedtag', '');
		
		//get supportedtag usage
		include_once(JPATH_ROOT . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'tags.php');
		$resource_tags = new ResourcesTags($database);
		$supportedtagusage = $resource_tags->getTagUsage($supportedtag, 'alias');
		
		//create list of tools
		$t = array();
		foreach($tools as $k => $tool)
		{
			$t[$k]['alias']			= $tool->alias;
			$t[$k]['title']			= $tool->title;
			$t[$k]['description'] 	= $tool->description;
			$t[$k]['version'] 		= $tool->revision;
			$t[$k]['supported'] 	= (in_array($tool->alias, $supportedtagusage)) ? 1 : 0;
		}
		
		//encode and return result
		$object = new stdClass();
		$object->tools = $t;
		$this->setMessageType( $format );
		$this->setMessage( $object );
	}
	
	/**
	 * Method to get tool information
	 *
	 * @return     void
	 */
	private function info()
	{
		//get the userid and attempt to load user profile
		$userid = JFactory::getApplication()->getAuthn('user_id');
		$result = Hubzero_User_Profile::getInstance($userid);
		
		//make sure we have a user
		if ($result === false)	return $this->not_found();
		
		//instantiate database object
		$database =& JFactory::getDBO();
		
		//get request vars
		$tool 		= JRequest::getVar('tool', '');
		$version 	= JRequest::getVar('version', 'current');
		$format 	= JRequest::getVar('format', 'json');
		
		//we need a tool to continue
		if($tool == '')
		{
			$this->errorMessage(400, 'Tool Alias Required.');
			return;
		}
		
		//poll database for tool matching alias
		$sql = "SELECT r.id, r.alias, tv.toolname, tv.title, tv.description, tv.toolaccess as access, tv.mw, tv.instance, tv.revision, r.fulltxt as abstract, r.created 
				FROM #__resources as r, #__tool_version as tv
				WHERE r.published=1
				AND r.type=7
				AND r.standalone=1
				AND r.access!=4
				AND r.alias=tv.toolname
				AND tv.state=1
				AND r.alias='{$tool}'";
		$database->setQuery($sql);
		$tool_info = $database->loadObject();
		
		//veryify we have result
		if($tool_info == null)
		{
			$this->errorMessage(404, 'No Tool Found Matching the Alias: "' . $tool . '"');
			return;
		}
		
		//add tool alias to tool info from db
		$tool_info->alias = $tool;
		
		//remove tags and slashes from abastract
		$tool_info->abstract = stripslashes(strip_tags($tool_info->abstract));
		
		//get the supported tag<commit_msg>only return one tool entry with array of revisions<commit_after><?php
JLoader::import('Hubzero.Api.Controller');

class ToolsControllerApi extends Hubzero_Api_Controller
{
	function execute()
	{
		JLoader::import('joomla.environment.request');
		JLoader::import('joomla.application.component.helper');
		
		//include tool utils
		include_once( JPATH_ROOT . DS . 'components' . DS . 'com_tools' . DS . 'helpers' . DS . 'utils.php' );
		
		switch($this->segments[0]) 
		{
			case 'index':		$this->index();				break;
			case 'info':		$this->info();				break;
			case 'screenshot':	$this->screenshot();		break;
			case 'invoke':		$this->invoke();			break;
			case 'view':		$this->view();				break;
			case 'stop':		$this->stop();				break;
			case 'unshare':		$this->unshare();			break;
			
			case 'storage':		$this->storage();			break;
			case 'purge':		$this->purge();				break;
			
			default:			$this->index();
		}
	}
	
	/**
	 * Short description for 'not_found'
	 *
	 * Long description (if any) ...
	 *
	 * @return     void
	 */
	private function not_found()
	{
		$response = $this->getResponse();
		$response->setErrorMessage(404,'Not Found');
	}
	
	/**
	 * Method to report errors. creates error node for response body as well
	 *
	 * @param	$code		Error Code
	 * @param	$message	Error Message
	 * @param	$format		Error Response Format
	 *
	 * @return     void
	 */
	private function errorMessage( $code, $message, $format = 'json' )
	{
		//build error code and message
		$object = new stdClass();
		$object->error->code = $code;
		$object->error->message = $message;
		
		//set http status code and reason
		$response = $this->getResponse();
		$response->setErrorMessage( $object->error->code, $object->error->message );
		
		//add error to message body
		$this->setMessageType( $format );
		$this->setMessage( $object );
	}
	
	/**
	 * Method to get list of tools
	 *
	 * @return     void
	 */
	private function index()
	{
		//get the userid and attempt to load user profile
		$userid = JFactory::getApplication()->getAuthn('user_id');
		$result = Hubzero_User_Profile::getInstance($userid);
		
		//make sure we have a user
		if ($result === false)	return $this->not_found();
		
		//include needed tool libraries
		ximport('Hubzero_Tool');
		ximport('Hubzero_Tool_Version');
		
		//instantiate database object
		$database = JFactory::getDBO();
		
		//get any request vars
		$format = JRequest::getVar('format', 'json');
		
		//get list of tools
		$tools = Hubzero_Tool::getMyTools();
		
		//get the supported tag
		$rconfig = JComponentHelper::getParams('com_resources');
		$supportedtag = $rconfig->get('supportedtag', '');
		
		//get supportedtag usage
		include_once(JPATH_ROOT . DS . 'components' . DS . 'com_resources' . DS . 'helpers' . DS . 'tags.php');
		$resource_tags = new ResourcesTags($database);
		$supportedtagusage = $resource_tags->getTagUsage($supportedtag, 'alias');
		
		//create list of tools
		$t = array();
		foreach($tools as $k => $tool)
		{
			if (isset($t[$tool->alias]))
			{
				$t[$tool->alias]['versions'][] = $tool->revision;
				continue;
			}
			
			$t[$tool->alias]['alias']			= $tool->alias;
			$t[$tool->alias]['title']			= $tool->title;
			$t[$tool->alias]['description'] 	= $tool->description;
			$t[$tool->alias]['versions'] 		= array($tool->revision);
			$t[$tool->alias]['supported'] 	    = (in_array($tool->alias, $supportedtagusage)) ? 1 : 0;
		}
		
		//encode and return result
		$object = new stdClass();
		$object->tools = array_values($t);
		$this->setMessageType( $format );
		$this->setMessage( $object );
	}
	
	/**
	 * Method to get tool information
	 *
	 * @return     void
	 */
	private function info()
	{
		//get the userid and attempt to load user profile
		$userid = JFactory::getApplication()->getAuthn('user_id');
		$result = Hubzero_User_Profile::getInstance($userid);
		
		//make sure we have a user
		if ($result === false)	return $this->not_found();
		
		//instantiate database object
		$database =& JFactory::getDBO();
		
		//get request vars
		$tool 		= JRequest::getVar('tool', '');
		$version 	= JRequest::getVar('version', 'current');
		$format 	= JRequest::getVar('format', 'json');
		
		//we need a tool to continue
		if($tool == '')
		{
			$this->errorMessage(400, 'Tool Alias Required.');
			return;
		}
		
		//poll database for tool matching alias
		$sql = "SELECT r.id, r.alias, tv.toolname, tv.title, tv.description, tv.toolaccess as access, tv.mw, tv.instance, tv.revision, r.fulltxt as abstract, r.created 
				FROM #__resources as r, #__tool_version as tv
				WHERE r.published=1
				AND r.type=7
				AND r.standalone=1
				AND r.access!=4
				AND r.alias=tv.toolname
				AND tv.state=1
				AND r.alias='{$tool}'
				ORDER BY revision DESC";
		$database->setQuery($sql);
		$tool_info = $database->loadObject();
		
		//veryify we have result
		if($tool_info == null)
		{
			$this->errorMessage(404, 'No Tool Found Matching the Alias: "' . $tool . '"');
			return;
		}
		
		//add tool alias to tool info from db
		$tool_info->alias = $tool;
		
		//remove tags and slashes from abastract
		$tool_info->abstract = stripslashes(strip_tags($tool_info->abstract));
		
		//get the supported tag
{'fraction_non_alphanumeric': 0.16580721935739787, 'fraction_numerical': 0.0033716779055930186, 'mean_word_length': 3.339500860585198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1676135', 'n_tokens_mistral': 3863, 'n_tokens_neox': 3301, 'n_words': 840}