cache_relations.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env php
  2. <?php
  3. mysql_connect('localhost', 'root', 'kontem11on');
  4. mysql_select_db('SES_USERS2');
  5. mysql_query("set names utf8");
  6. class FindCorelations {
  7. private $TABLE, $TABLE_REF, $TABLE_DEST, $DEPTH;
  8. public $results = array();
  9. private $nodesCache = array();
  10. private $corelations = array(
  11. 'nip' => array('nip'),
  12. 'regon' => array('regon'),
  13. 'krs' => array('krs'),
  14. 'pesel' => array('pesel'),
  15. 'adres' => array('TERYT_SYM', 'TERYT_SYM_UL', 'nrDomu', 'nrLokalu'),
  16. );
  17. function __construct($TABLE, $TABLE_REF, $TABLE_DEST, $DEPTH) {
  18. $this->TABLE = $TABLE;
  19. $this->TABLE_REF = $TABLE_REF;
  20. $this->TABLE_DEST = $TABLE_DEST;
  21. $this->DEPTH = $DEPTH;
  22. }
  23. public function findNodes($ID, $corelation = "", $path = array()) {
  24. if (isset($path[$ID])) {
  25. // echo $ID . "\n";
  26. return false;
  27. }
  28. $path[$ID] = $corelation;
  29. $query = "select * from {$this->TABLE} where ID = '{$ID}'";
  30. if (!($result = mysql_query($query))) return false;
  31. $row = mysql_fetch_object($result);
  32. if (in_array($row->REMOTE_TABLE, $this->TABLE_DEST)) {
  33. $this->results[] = $path;
  34. return true;
  35. }
  36. if (count($path) > $this->DEPTH) {
  37. return false;
  38. }
  39. $dupaNodes = array();
  40. if (isset($this->nodesCache[$ID])) {
  41. $nodes = $this->nodesCache[$ID];
  42. } else {
  43. $nodes = array();
  44. $joins = array();
  45. $items = "";
  46. foreach ($this->corelations as $name => $columns) {
  47. $ok = 0;
  48. $join = array();
  49. foreach ($columns as $column) {
  50. $join[] = "t1.{$column} = t2.{$column}";
  51. if ($row->$column) {
  52. $ok++;
  53. }
  54. }
  55. if ($ok / count($columns) > 0.5) {
  56. $joins[] = "(" . implode(" and ", $join) . ")";
  57. $items .= ", if(" . implode(" and ", $join) . ", 1, 0) as {$name}";
  58. }
  59. }
  60. if (!$joins) return false;
  61. $query = "select t2.ID as ID {$items} from {$this->TABLE} t1 join {$this->TABLE} t2 on " . implode(" or ", $joins) . " join BI_audit_ALL_usable usable on t2.ID = usable.ID_BI_audit_ALL" .
  62. // " where t1.ID = '{$ID}' and t2.ID not in (" . implode(",", array_keys($path)) . ")";
  63. " where t1.ID = '{$ID}' and t2.ID not in ({$ID})";
  64. // echo $query."\n";
  65. if ($result = mysql_query($query)) {
  66. while ($item = mysql_fetch_object($result)) {
  67. $names = array();
  68. foreach ($this->corelations as $name => $v) {
  69. if (isset($item->$name) && $item->$name) $names[] = $name;
  70. }
  71. // if (isset($path[$item->ID])) echo $item->ID . " :: " . implode("->", array_keys($path)) . "\n";
  72. //if (!isset($path[$item->ID])) {
  73. // $dupaNodes[$item->ID] = $name;
  74. //}
  75. $nodes[$item->ID] = implode(', ', $names);
  76. }
  77. }
  78. // $query = "select ref.ID2 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.ID2 not in (" . implode(",", array_keys($path)) . ")";
  79. $query = "select ref.ID2 from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}'";
  80. if ($result = mysql_query($query)) {
  81. while ($item = mysql_fetch_object($result)) {
  82. // if (!isset($path[$item->ID2]))
  83. //if (!array_key_exists($item->ID2, $path)) $dupaNodes[$item->ID2] = $row->REMOTE_TABLE;
  84. $nodes[$item->ID2] = $row->REMOTE_TABLE;
  85. }
  86. }
  87. if ($nodes) {
  88. ksort($nodes);
  89. $this->nodesCache[$ID] = $nodes;
  90. }
  91. }
  92. if ($nodes) {
  93. foreach ($nodes as $node => $corel) {
  94. // if (!isset($path[$node]))
  95. $this->findNodes($node, $corel, $path);
  96. }
  97. }
  98. // ksort($dupaNodes);
  99. // foreach ($dupaNodes as $node => $corel) echo "{$node}\n";
  100. }
  101. }
  102. $TABLE = "BI_audit_ALL";
  103. $TABLE_REF = "BI_audit_ALL_ref";
  104. $TABLE_DEST = array("BI_audit_ENERGA_RUM_KONTRAHENCI", "BI_audit_KW_requested_person");
  105. $query = "select ID from {$TABLE} where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
  106. //$query = "select ref.ID2 as ID from {$TABLE} `all` join BI_audit_ALL_ref ref on `all`.ID = ref.ID1 where all.krs='0000457722';";
  107. $result = mysql_query($query);
  108. $results = array();
  109. $test = new FindCorelations($TABLE, $TABLE_REF, $TABLE_DEST, 7);
  110. while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);
  111. $results = $test->results;
  112. //print_r($test->badNodes);
  113. $data = base64_encode(gzcompress(json_encode($results)));
  114. echo $data;