PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/web/tmp/parser.php

https://bitbucket.org/mupenieks/dienasmediji
PHP | 99 lines | 81 code | 16 blank | 2 comment | 3 complexity | e2dc84106722f8ff385165b6a534920a MD5 | raw file
  1. <?php
  2. error_reporting(E_ALL);
  3. mysql_connect("localhost","root","");
  4. mysql_select_db("yy");
  5. mysql_query("set names utf8");
  6. $s = file_get_contents("featuresOld.txt");
  7. $lines = explode("\n",$s);
  8. function level($s){
  9. $t = ltrim($s);
  10. return ceil((strlen($s) - strlen($t)) / 2);
  11. }
  12. $out = array();
  13. $tmp = array();
  14. mysql_query("TRUNCATE TABLE `business_category`");
  15. mysql_query("INSERT INTO `business_category` SET `name` = 'ROOT', `lft` = 1, `rgt` = 2, `level` = 0")or die(mysql_error());
  16. mysql_query("TRUNCATE TABLE `product_feature`");
  17. mysql_query("INSERT INTO `product_feature` SET `name` = 'ROOT', `lft` = 1, `rgt` = 2, `level` = 0")or die(mysql_error());
  18. mysql_query("TRUNCATE TABLE `product_feature_to_category`");
  19. $leg = -1;
  20. $lastLVL = false;
  21. $lastl = -1;
  22. $lastCAT = 0;
  23. $lastCATlevel = 0;
  24. foreach($lines as $linenr=>$l){
  25. $l = substr($l,1);
  26. $t = (int)$l;
  27. $right = 0;
  28. if( !empty( $t ) && level($l)-$lastl < 2 ){ // category
  29. $name = preg_replace("/[0-9+]/","",$l);
  30. $query = mysql_query("SELECT * FROM `business_category` WHERE `level` = ".(level($l))." ORDER BY `id` DESC LIMIT 1");
  31. $row = mysql_fetch_assoc($query);
  32. $right = $row['rgt'];
  33. if(empty($right)){
  34. echo $level."<br>";
  35. echo "LINE: ".$linenr."<br>";
  36. die("Can't find parent!");
  37. }
  38. mysql_query("UPDATE `business_category` SET `rgt` = `rgt` + 2 WHERE `rgt` >= ".$right);
  39. mysql_query("INSERT INTO `business_category` SET
  40. `name` = '".mysql_escape_string( substr( $name , level($l)*2+1 ) )."',
  41. `lft` = ".($right).",
  42. `rgt` = ".($right+1).",
  43. `level` = ".(level($l)+1)."
  44. ")or die(mysql_error());
  45. $out[] = "CAT LVL ".level($l) . ": " . substr( $name , level($l)*2+1 );
  46. $lastLVL = false;
  47. $lastl = level($l);
  48. $lastCAT = mysql_insert_id();
  49. $lastCATlevel = level($l);
  50. }else{ // feature
  51. $name = $l;
  52. //$out[] = "LVL ".level($l) . ": " . substr( $name , level($l)*2 );
  53. $level = level($l) - $lastCATlevel-1;
  54. $query = mysql_query("SELECT * FROM `product_feature` WHERE `level` = ".$level." ORDER BY `id` DESC LIMIT 1");
  55. $row = mysql_fetch_assoc($query);
  56. $right = $row['rgt'];
  57. if(empty($right)){
  58. echo $level."<br>";
  59. echo "LINE: ".$linenr."<br>";
  60. die("<b>Can't find top node!</b>");
  61. }
  62. mysql_query("UPDATE `product_feature` SET `rgt` = `rgt` + 2 WHERE `rgt` >= ".$right);
  63. mysql_query("INSERT INTO `product_feature` SET
  64. `name` = '".mysql_escape_string( substr( $name , level($l)*2 ) )."',
  65. `lft` = ".($right).",
  66. `rgt` = ".($right+1).",
  67. `level` = ".($level+1)."
  68. ")or die(mysql_error());
  69. mysql_query("INSERT INTO `product_feature_to_category` SET
  70. `product_feature_id` = ".mysql_insert_id().",
  71. `category_id` = ".$lastCAT."
  72. ")or die(mysql_error());
  73. $lastLVL = true;
  74. }
  75. }
  76. //echo "<pre>";
  77. //var_dump($out);
  78. ?>DONE!