<?php

class Logfile
{
	public $Name;
	public $Type = "Modell";
	public $Filename;
	public $Modified;
	public $LastChangedDays;
	public $Errors = array();
	public $ErrorLevel = 5;
	public $Sources = array();
	public $Summary;

	function __construct($filename)
	{
		$this->Filename = $filename;
		$pathinfo = pathinfo($filename);
		$this->Name = $pathinfo['filename'];
		$this->Modified = str_replace(" ", "T", date("Y-m-d H:i:s", filemtime($filename)));
		$this->LastChangedDays = $this->dateDiff($this->Modified, date("Y-m-d H:i:s"));
		if (preg_match("/\.xml/", $this->Filename)) {
			$this->Type = (stripos($this->Filename, "ver")  == false) ? "Portal" : "Versand";
			$errors = json_decode(implode("",file($this->Filename)));
			if (isset($errors->errors)) {
				if (count($errors->errors) > 0) {
					$this->ErrorLevel = 2;
				}
				$this->Errors = $errors->errors;
				$this->Sources = $errors->sources;
				$this->Summary = $errors->summary;
			}
		} else if (preg_match("/\.\D+\.log$/", $this->Filename)) {
			$this->Type = "Workflow";
			$this->Errors = array(implode("\r\n", $this->Content()));
			$this->ErrorLevel = 3;
		} else {
			$this->transformerErrors();
			$this->benchmark();
			if ($this->ErrorLevel <= 2 && $this->Summary) {
				$this->ErrorLevel = 3;
			}
			if (!$this->Summary) {
				$this->ErrorLevel = 2;
			}
		}
	}

	function Content()
	{
		$content = file_get_contents($this->Filename);
		$content = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, "UTF-8, ISO-8859-1, Windows-1252, CP850", true));
		return explode("\r\n", $content);
	}

	function Link() {
		$link = $this->Name;
		if (preg_match("/(.*)\.\d+$/", $link, $match)) {
			$link = $match[1];
		}
		return "<a href=\"http://wiki.global-cube.de/{$link}\">{$this->Name}</a>";
	}

	private function webCreatorErrors()
	{
		foreach ($this->Content() as $line) {
			if (preg_match("/^\w+/", $line) && !preg_match("/^(com|Source)/", $line)) {
				$e = new WebCreatorError($line);
				if ($e->Level < $this->ErrorLevel)
					$this->ErrorLevel = $e->Level;
				$this->Errors[] = $e;
			}
		}
	}
	
	private function copyAndReplaceErrors()
	{
		foreach ($this->Content() as $line) {
			$e = new CopyAndReplaceError($line);
			if ($e->Level < $this->ErrorLevel)
				$this->ErrorLevel = $e->Level;
			$this->Errors[] = $e;
		}
	}
	
	private function transformerErrors()
	{
		foreach ($this->Content() as $line) {
			if (preg_match("/\(TR\d*\)|DMS-E-GENERAL/", $line)) {
				$e = new TransformerError($line);
				if ($e->Level < $this->ErrorLevel)
					$this->ErrorLevel = $e->Level;
				$this->Errors[] = $e;
			}
		}
	}

	private function benchmark()
	{
		$current = null;
		foreach ($this->Content() as $line) {
			if (preg_match("/Verarbeitung von (\d*) Datens.+tzen der Datenquelle `(.*)` wird beendet/", $line, $match)) {
				$current = new Source($match[2], $match[1]);
			} else if ($current && preg_match("/DATENQUELLE GELESEN,\s*([\d:]+)/", $line, $match)) {
				$current->Duration = $match[1];
				$this->Sources[] = $current;
				$current = null;
			} else if (preg_match("/Durchgang \d+ wird ausgef.+hrt\. Es verbleiben (\d*) Zeilen und (\d*) Kategorien/", $line, $match)) {
				$this->Summary = new Summary($match[1], $match[2]);
			} else if ($this->Summary && preg_match("/ZEIT INSGESAMT \(CUBE ERSTELLEN\),\s*([\d:]+)/", $line, $match)) {
				$this->Summary->Duration = $match[1];
			}
		}
	}

	private function dateDiff($d1, $d2)
	{
		$diff = round((strtotime($d2) - strtotime($d1)) / (24 * 60 * 60));
		return $diff;
	}
	
	private function formatMessage($message)
	{
		$message = str_replace("'", "`", $message);
		$message = str_replace("\"", "`", $message);
		$message = str_replace("ä", "ae", $message);
		$message = str_replace("ö", "oe", $message);
		$message = str_replace("ü", "ue", $message);
		$message = str_replace("ß", "ss", $message);
		$message = str_replace("Ä", "Ae", $message);
		$message = str_replace("Ö", "Oe", $message);
		$message = str_replace("Ü", "Ue", $message);
		$message = str_replace("[->OK]", "", $message);
		$message = str_replace("\r", "", $message);
		$message = str_replace("\n", "", $message);
		return $message;
	}
}

class TransformerError
{
	public $Number;
	public $Message;
	public $Timestamp;
	public $Level;

	function __construct($line = 0)
	{
		if ($line) {
			$cols = explode("\t", $line);
			$this->Timestamp = $this->formatLogDate($cols[0]);
			$this->Level = $cols[1];
			if (preg_match("/\((TR\d*)\) (.*)/", $cols[3], $match)) {
				$this->Number = $match[1];
				if ($this->Number == "TR0220") {
					$this->Level = 1;
				}
				$this->Message = $match[2];
			} if (preg_match("/DMS-E-GENERAL (.*)/", $cols[3], $match)) {
				$this->Number = "TR0109";
				$this->Message = $cols[3];
				$this->Level = 1;
			}
		}
	}

	private function formatLogDate($message)
	{
		$message = substr($message, 3);
		$message = str_replace(" Jan ", ".01.", $message);
		$message = str_replace(" Feb ", ".02.", $message);
		$message = str_replace(" Mrz ", ".03.", $message);
		$message = str_replace(" Apr ", ".04.", $message);
		$message = str_replace(" Mai ", ".05.", $message);
		$message = str_replace(" Jun ", ".06.", $message);
		$message = str_replace(" Jul ", ".07.", $message);
		$message = str_replace(" Aug ", ".08.", $message);
		$message = str_replace(" Sep ", ".09.", $message);
		$message = str_replace(" Okt ", ".10.", $message);
		$message = str_replace(" Nov ", ".11.", $message);
		$message = str_replace(" Dez ", ".12.", $message);
		if (preg_match("/(\d{2}).(\d{2}).(\d{4})\s*([\d:]+)/", $message, $match)) {
			return "{$match[3]}-{$match[2]}-{$match[1]}T{$match[4]}";
		}
		return "";
	}
}

class WebCreatorError
{
	public $Number = "WC0000";
	public $Message;
	public $Timestamp;
	public $Level = 2;

	function __construct($line = 0)
	{
		if ($line) {
			if (preg_match("/^Description: (.*)/", $line, $match)) {
				$this->Message = $match[1];
				if (preg_match("/Division durch Null/", $this->Message)) {
					$this->Level = 4;
					$this->Number = "WC0001";
				}
				if (preg_match("/Kategorieindex/", $this->Message)) {
					$this->Level = 4;
					$this->Number = "WC0002";
				}
				
			} else {
				$this->Message = $line;
			}
		}
	}
}

class CopyAndReplaceError
{
	public $Number = "CR0000";
	public $Message;
	public $Timestamp;
	public $Level = 3;

	function __construct($line = 0)
	{
		$this->Message = $line;
	}
}

class WorkflowError
{
	public $Filename;
	public $User;
	public $Report;
	public $ReportModified;
	public $Layer;
	public $MailTo;
	public $Modified;
	
	public function __toString() {
		return $this->Report . ".ppr, " . $this->Layer . ": " . substr($this->Filename, -3) . " " . $this->Modified;
	}
}


class Source
{
	public $Filename;
	public $Report;
	public $Layer;
	public $Entities;
	public $Duration;

	function __construct($filename, $entities = 0)
	{
		$this->Filename = $filename;
		$this->Entities = $entities;
	}
}

class Summary
{
	public $Entities;
	public $Categories;
	public $Duration;

	function __construct($entities, $categories = 0)
	{
		$this->Entities = $entities;
		$this->Categories = $categories;
	}
}
?>