Location: PHPKode > scripts > NabiCI > CI/Task/Task.php
<?php
require_once(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
		"../Process.php");
require_once(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
		"../../include/html_utilities/browser.php");
require_once(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
		"../TaskSeparate.php");


 // checkout" is the name of the Task where a checkout will be executed
 define("checkout_task","checkout");

/**
 * class Task exends from Process and contains functions to execute the Tasks
 *
 * @author Derya Oez
 * @version 1.0
 * @package NabidooCI
 */

class Task extends Process
{
	/**
	 * The mode of a Task is read out from the configuration file and is necessary to find out which 
	 * specific method is to use to execute the command
	 * 
	 * @access private
	 * @var String
	 */
	
	private $taskMode;
	
	/**
	 * The command of a Task is read out from the configuration file, which will be executed
	 * 
	 * @access private
	 * @var String
	 */
	
	private $command;

	/**
	 * The expected result of a Task is read out from the configuration file
	 * 
	 * @access private
	 * @var String
	 */
	
	private $expResult;
	
	/**
	 * The returned actual result of a Task after execution 
	 * 
	 * @access private
	 * @var String
	 */
	
	private $result; 
	
	/**
	 * The returned revision number after checkout a project
	 * 
	 * @access private
	 * @var String
	 */
	
	private $revision;


	/**
	 * function __construct($name,$taskmode,$expectedRes,$com)
	 * 
	 * @access public
	 * @param String $name
	 * name of the Task
	 * @param String $taskmode
	 * mode of the Task
	 * @param String $expectedRes
	 * expected result of the Task
	 * @param String $com
	 * command of the Task
	 */

	public function __construct($name,$taskmode,$expectedRes,$com)
	{
		$this->name=$name;
		$this->taskMode=$taskmode;
		$this->command=$com;
		$this->expResult=$expectedRes;

	}

	/**
	 * function run() is used to set if the Task is running or not
	 * set the start- and endtime of the Task
	 * analyze the task mode and according to it uses a special run-function
	 * 
	 * @access public
	 */

	public function run()
	{
		$this->running=true; //Task is running
		$this->startTime = time(); //fix start time of the Task
		//--------------------------------
		if($this->taskMode == "url")
		{
			$this->runUrl();
		}
		//-----------------------------------
		elseif($this->taskMode == "system")
		{
			$this->runSystem();
		}
		//--------------------------------------
		elseif($this->taskMode == "url_list")
		{
			$this->runUrlList();
		}
		//--------------------------------------
		elseif($this->taskMode == "system_list")
		{
			$this->runSystemList();
		}
		//------------------------------------------
		elseif($this->taskMode == "system_linewise")
		{
			$this->runSystemLinewise();
		}
		//------------------------------------
		$this->endTime = time(); //fix endtime of the Task
		$this->running=false; //Task is finish
	}

	/**
	 * function runUrl() is used in case of the task mode is url,
	 * it executes the command, checks the result of the Task and 
	 * according to this gives a message
	 * 
	 * @access private
	 */
	
	private private function runUrl()
	{
		//create an Object of browser
		$brow = new browser();
		//$result is the result from the execution of the "url-command"
		$this->result = $brow->get($this->command);
		//in case of expected result is not empty and the actual result does not contains the expected result
		if($this->expResult != "" and strpos($this->result,$this->expResult)===false)
		{
			//set the message "NOT OK" in red color
			$this->message= "<font color=red> NOT OK </font><a name=NOK><a><br>";
			//save that the process has failed
			$this->successful=false;
		}
		else
		{
			//set the message "OK" in green color
			$this->message= "<font color=green> OK </font><br>";
			//save the process is succeeded
			$this->successful=true;
		}
	}

	/**
	 * function runSystem() is used in case of the task mode is "system",
	 * it executes the command, checks the result of the Task and according 
	 * to this gives a message
	 * 
	 * @access private
	 */

	private private function runSystem()
	{
		$output=array(); //output of the executed command
		$syst_result="";
		//execution of the command
		$execResult=exec($this->command,$output,$syst_result);
		//find out the part of the revision number in a String
		$strpos= stripos($execResult, 'Revision');
		$revision = substr($execResult, $strpos);
		//In case of the Task is checkout, set the revision number
		if($this->name==checkout_task)
		{
			$this->revision = $revision;
		}
		//convert the output array in a String
		$this->result = implode("\n",$output);
		//if anything is ok with the execution of this command
		if ($syst_result == 0){
			//if the expected Result is not empty and the actual result does not contain the expected result
			if($this->expResult != "" and strpos($this->result,$this->expResult)===false){
				//set the message "NOT OK" in red color
				$this->message =  "<font color=red> NOT OK </font><a name=NOK><a><br>";
				//save that the process has failed
				$this->successful=false;
			}
			//if the expected result is returned
			else
			{	//set the message "OK" in green color
				$this->message = "<font color=green> OK </font><br>";
				//save that the process has succeeded
				$this->successful=true;
			}
			//if the command is not ok
		}else{
			//set the message that the 'system command failed' in red color
			$this->message =  "<font color=red> SYSTEM COMMAND FAILED </font><a name=NOK><a><br>";
			//the result will be also 'system command failed' in red color
			$this->result =  "<font color=red> SYSTEM COMMAND FAILED </font><a name=NOK><a><br>";
			//save that the process has failed
			$this->successful=false;
		}
	}

	/**
	 * function runUrlList() is used in case of the task mode is "url_list",
	 * it executes the command, checks the result of the Task and according
	 * to this gives a message
	 * 
	 * @access private
	 */

	private function runUrlList()
	{
		//if the command does not contain the String "Warning: No such file or directory in")
		if (strpos($this->command,'Warning: No such file or directory in ')===false)
		{
			//create an Object of browser
			$brow = new browser();
			//use the get-function of the browser-Object to execute the command
			$this->result = $brow->get($this->command);
			//if the expected result is not empty and the actual result does not contain the expected result
			if($this->expResult != "" and strpos($this->result,$this->expResult)===false)
			{
				//set the message "NOT OK" in red color
				$this->message= "<font color=red> NOT OK </font><a name=NOK><a><br>";
				//save that the process has failed
				$this->successful=false;

			}
			//if the expected result is returned
			else
			{
				//set the message "OK" in green color
				$this->message= "<font color=green> OK </font><br>";
				//save that the process has succeeded
				$this->successful=true;
			}
		}
		//if the command is a String as 'Warning: No such file or directory in '
		else {
			//set the message "NOT OK" in red color
			$this->message =  "<font color=red> NOT OK </font><a name=NOK><a><br>";
			//the result will be the command as a String in red color
			$this->result="<font color=red><b>".$this->command."</b></font><br>";
			//save that the process has failed
			$this->successful=false;
		}
	}

	/**
	 * function runSystemList() is used in case of the task mode is "system_list",
	 * it checks the result of the Task and according to this gives a message
	 * 
	 * @access private
	 */

	private function runSystemList()
	{
		$output=array(); //output of the executed command
		//if the command does not contain the String "Warning: No such file or directory in")
		if (strpos($this->command,'Warning: No such file or directory in ')===false)
		{
			$syst_result;
			//execution of the command
			$execResult=exec($this->command,$output,$syst_result);
			//if anything is ok with the execution of this command
			if ($syst_result==0){
				//find out the part of the revision number in a String
				$strpos= stripos($execResult, 'Revision');
				$revision = substr($execResult, $strpos);
				//In case of the Task is checkout, set the revision number
				if($this->name==checkout_task)
				{
					$this->revision = $revision;
				}
				//convert the output array in a String
				$this->result = implode("\n",$output);
				//if the expected Result is not empty and the actual result does not contain the expected result
				if($this->expResult != "" and strpos($this->result,$this->expResult)===false){
					//set the message "NOT OK" in red color
					$this->message =  "<font color=red> NOT OK </font><a name=NOK><a><br>";
					//save that the process has failed
					$this->successful=false;
				}
				//if the expected result is returned
				else
				{	//set the message "OK" in green color
					$this->message = "<font color=green> OK </font><br>";
					//save that the process has succeeded
					$this->successful=true;
				}
			}
			//if anything is not ok with the execution of this command
			else{
				//set the message that the 'system command failed' in red color
				$this->message =  "<font color=red> SYSTEM COMMAND FAILED </font><a name=NOK><a><br>";
				//the result will be also 'system command failed' in red color
				$this->result = "<font color=red> SYSTEM COMMAND FAILED </font><a name=NOK><a><br>";
				//save that the process has failed
				$this->successful=false;
			}
		}
		//if the command is a String as 'Warning: No such file or directory in '
		else
		{	//set the message "NOT OK" in red color
			$this->message =  "<font color=red> NOT OK </font><a name=NOK><a><br>";
			//the result will be the command as a String in red color
			$this->result="<font color=red><b>".$this->command."</b></font><br>";
			//save that the process has failed
			$this->successful=false;
		}
	}

	/**
	 * function runSystemLinewise() is used in case of the task mode is "system_linewise",
	 * it executes the command, checks the result of the Task and according to this gives a message
	 * 
	 * @access private
	 */
	
	private function runSystemLinewise()
	{
		$task_result="";
		$output=array(); //output of the executed command
		$result=exec($this->command,$output,$task_result);
		//if anything is ok with the execution of this command
		if ($task_result == 0){
			//save that the process has succeeded
			$this->successful=true;
			//checking the actual result linewise
			foreach ($output as $line)
			{	//if this line is not the expected result
				if(strpos($line,$this->expResult)===false){
					//save that the process has failed
					$this->successful=false;
					//the result will be the this line in red color
					$this->result = $this->result."<font color=red>".$line."</font><br>";
				}
				//if this line is the expected result
				else{
					//the result will be the this line in green color
					$this->result = $this->result."<font color=green>".$line."</font><br>";
				}
			}
			//if the process is successful
			if ($this->successful){
				//set the message "OK" in green color
				$this->message = "<font color=green> OK </font><br>";
			//if the process not successful
			}else{
				//set the message "NOT OK" in red color
				$this->message =  "<font color=red> NOT OK </font><a name=NOK><a><br>";
			}
		
		}
		//if anything is not ok with the execution of this command
		else{
			//set the message that the 'system command failed' in red color
			$this->message =  "<font color=red> SYSTEM COMMAND FAILED </font><a name=NOK><a><br>";
			//the result will be also 'system command failed' in red color
			$this->result = "<font color=red> SYSTEM COMMAND FAILED </font><a name=NOK><a><br>";
		}
	}
	
	/**
	 * function getExpResult()is used to get the 'expected result' of a Task
	 * 
	 * @access public
	 *
	 * @return String 
	 * 'expected result' of a Task
	 */
	
	public function getExpResult()
	{
		return $this->expResult;
	}
	
	/**
	 * function getTaskMode()is used to get the 'task mode'
	 * 
	 * @access public
	 * @return String
	 * 'task mode'
	 */
	
	public function getTaskMode()
	{
		return $this->taskMode;
	}
	
	/**
	 * function getCommand()is used to get the 'command' of a Task
	 *
	 * @access public
	 * @return String
	 * 'command' of a Task
	 */

	public function getCommand()
	{
		return $this->command;
	}
	
	/**
	 * function getResult()is used to get the actual result of a Task
	 * 
	 * @access public
	 * @return String
	 * actual result of a Task
	 */

	public function getResult()
	{
		return $this->result;
	}
	
	/**
	 * function getRevision()is used to get the revision number of a project which is checked out
	 * Task called "checkout"
	 * 
	 * @access public
	 * @return String
	 * revision number of a project which is checked out
	 */
	
	public function getRevision()
	{
		return $this->revision;
	}
}

//if a Task should be separately executed from the web page
if (isset($_GET["loopStatus"])||isset($_GET["taskName"])|| isset($_GET["taskMode"])|| isset($_GET["taskExpRes"])|| isset($_GET["command"]))
{	
	$loopStatus = $_GET["loopStatus"]; //Current oder Result

	$tName = $_GET["taskName"]; //name of the Task
	$tMode = $_GET["taskMode"]; // mode of the Task
	$tExpRes = $_GET["taskExpRes"]; //expected Result of the Task
	$tCom = $_GET["command"]; //command of the Task
	//create a Task Object called '$taskAlone'
	$taskAlone= new Task($tName,$tMode,$tExpRes,$tCom);
	$curdir = getcwd();
	chdir("..");
	//use the run function to execute wanted Task
	$taskAlone->run();
	chdir($curdir);
	//create the web page to show the result of the Task
	$a =new TaskSeparate($taskAlone,$loopStatus);
	//the web page which will offer the link to the created web page (Task result)
	echo  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
    <html xmlns=\"http://www.w3.org/1999/xhtml\">
						<head>
						<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8889-1\" />
						<title>NabiCI Continuous Integration Task Attribute Page</title>
						<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/styles.css\">
						</head>";
	echo "<body>";
	echo "<a href=".$a->getLink()."> Go to the Task Result </a>";
	echo "</body>";



}
/*
 //The part to test execution of Task separately without use the Link at the web page
  
 $tName = "task2";
 $tMode = "url";
 $tExpRes = "google";
 $tCom = "http://www.google.de/";
 $loopStatus="Result";

 $taskAlone= new Task($tName,$tMode,$tExpRes,$tCom);
 $taskAlone->run();
 new TaskSeparate($taskAlone,$loopStatus); */




?>
Return current item: NabiCI