#!/usr/bin/env php
<?php

mysql_connect('192.168.61.253', 'root', 'kontem11on');
mysql_select_db('SES_USERS2');
mysql_query("set names utf8");

class FindCorelations {

	private $TABLE_REF, $DEPTH;
	public $results = array();
	private $nodesCache = array();
	public $cache = array();
	public $firstNodes = array();
	private $ends = array();
	private $relations = array();

	function __construct($TABLE_REF, $DEPTH) {
		$this->TABLE_REF = $TABLE_REF;
		$this->DEPTH = $DEPTH;
	}

	private function cachePath($path) {
		$last = 0;
		foreach ($path as $node => $rel) {
			if ($last) $this->cache[$last][$node] = $rel;
			$last = $node;
		}
	}

	public function loadCache() {
		$query = "select ID1, ID2, RELATION, END from {$this->TABLE_REF}";
		$result = mysql_query($query);
		while ($row = mysql_fetch_object($result)) {
			$relations = explode(", ", $row->RELATION);
			$rel = 0;
			foreach ($relations as $relation) {
				if (!isset($this->relations[$relation])) {
					if (!$this->relations) $this->relations[$relation] = 1;
					else $this->relations[$relation] = 2 * max($this->relations);
				}
				$rel += $this->relations[$relation];
			}
			$this->nodesCache[$row->ID1][$row->ID2] = $rel;
			if ($row->END) $this->ends[$row->ID2] = true;
		}
	}

	public function findNodes($ID, $path = array(), $relation = "") {
		if (!$path) $this->firstNodes[] = $ID;
		if (isset($path[$ID])) {
			return false;
		}

		$path[$ID] = $relation;

		if (isset($this->ends[$ID])) {
//			$this->cachePath($path);
			$this->results[] = $path;
			return true;
		}

//		if (isset($this->cache[$ID])) {
//			$this->cachePath($path);
//			return true;
//		}

		if (count($path) > $this->DEPTH) {
			return false;
		}

		$nodes = array();
		if (isset($this->nodesCache[$ID])) {
			$nodes = $this->nodesCache[$ID];
//		} else {
//			$nodes = array();
//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}'";
//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}'";
//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
//			if ($result = mysql_query($query)) {
//				while ($row = mysql_fetch_object($result)) {
					//if (!in_array($row->ID2, $nodes)) 
			//		$nodes[$row->ID2] = array('relation' => explode(", ", $row->RELATION), 'end' => $row->END);
//					$nodes[$row->ID2] = $row->RELATION;
//					if ($row->END && (!in_array($row->ID2, $this->ends))) $this->ends[] = $row->ID2;
//				}
//			}
//			if ($nodes) {
//				ksort($nodes);
//				$this->nodesCache[$ID] = $nodes;
//			}
//		}

//		if ($nodes) {
			foreach ($nodes as $node => $rel) {
				if (!($relation && ($relation & $rel)))
					$this->findNodes($node, $path, $rel);
			}
		}
	}
}



$TABLE = "BI_audit_ALL";
$TABLE_REF = "BI_audit_ALL_ref";

$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
//$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where pracownicy.source = 'ENERGA' and REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";

$result = mysql_query($query);
$test = new FindCorelations($TABLE_REF, 13);
echo "Loading cache...";
$test->loadCache();
echo " done.\n";
//$test->generateReverseCache();
//print_r($test->cache);
//die();
while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);
//$result = mysql_query($query);
//while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);

//print_r($test->cache);
//die();

$results = $test->results;

print_r($results);
/*
echo "Przykladowe powiazanie:\n";
$result = $results[2];
print_r($result);
foreach ($result as $node => $rel) {
	if (isset($test->cache[$node])) {
		echo "\nCache dla {$node}:\n";
		print_r($test->cache[$node]);
	}
}
die();
*/

/*
$max = 0;
foreach ($results as $result) if (($c = count($result)) > $max) {
	$max = $c;
	$test = $result;
}
$results = array($test);
*/

//print_r($test->cache);
//die();
$data = base64_encode(gzcompress(json_encode($results)));
//echo $data;
