/web/tmp/parser.php
PHP | 99 lines | 81 code | 16 blank | 2 comment | 3 complexity | e2dc84106722f8ff385165b6a534920a MD5 | raw file
- <?php
- error_reporting(E_ALL);
- mysql_connect("localhost","root","");
- mysql_select_db("yy");
- mysql_query("set names utf8");
- $s = file_get_contents("featuresOld.txt");
- $lines = explode("\n",$s);
- function level($s){
- $t = ltrim($s);
- return ceil((strlen($s) - strlen($t)) / 2);
- }
- $out = array();
- $tmp = array();
- mysql_query("TRUNCATE TABLE `business_category`");
- mysql_query("INSERT INTO `business_category` SET `name` = 'ROOT', `lft` = 1, `rgt` = 2, `level` = 0")or die(mysql_error());
- mysql_query("TRUNCATE TABLE `product_feature`");
- mysql_query("INSERT INTO `product_feature` SET `name` = 'ROOT', `lft` = 1, `rgt` = 2, `level` = 0")or die(mysql_error());
- mysql_query("TRUNCATE TABLE `product_feature_to_category`");
- $leg = -1;
-
- $lastLVL = false;
- $lastl = -1;
- $lastCAT = 0;
- $lastCATlevel = 0;
- foreach($lines as $linenr=>$l){
- $l = substr($l,1);
- $t = (int)$l;
-
- $right = 0;
- if( !empty( $t ) && level($l)-$lastl < 2 ){ // category
- $name = preg_replace("/[0-9+]/","",$l);
- $query = mysql_query("SELECT * FROM `business_category` WHERE `level` = ".(level($l))." ORDER BY `id` DESC LIMIT 1");
- $row = mysql_fetch_assoc($query);
- $right = $row['rgt'];
- if(empty($right)){
- echo $level."<br>";
- echo "LINE: ".$linenr."<br>";
- die("Can't find parent!");
- }
-
- mysql_query("UPDATE `business_category` SET `rgt` = `rgt` + 2 WHERE `rgt` >= ".$right);
- mysql_query("INSERT INTO `business_category` SET
- `name` = '".mysql_escape_string( substr( $name , level($l)*2+1 ) )."',
- `lft` = ".($right).",
- `rgt` = ".($right+1).",
- `level` = ".(level($l)+1)."
- ")or die(mysql_error());
-
- $out[] = "CAT LVL ".level($l) . ": " . substr( $name , level($l)*2+1 );
- $lastLVL = false;
-
- $lastl = level($l);
- $lastCAT = mysql_insert_id();
- $lastCATlevel = level($l);
-
- }else{ // feature
- $name = $l;
- //$out[] = "LVL ".level($l) . ": " . substr( $name , level($l)*2 );
- $level = level($l) - $lastCATlevel-1;
-
- $query = mysql_query("SELECT * FROM `product_feature` WHERE `level` = ".$level." ORDER BY `id` DESC LIMIT 1");
- $row = mysql_fetch_assoc($query);
- $right = $row['rgt'];
- if(empty($right)){
- echo $level."<br>";
- echo "LINE: ".$linenr."<br>";
- die("<b>Can't find top node!</b>");
- }
-
- mysql_query("UPDATE `product_feature` SET `rgt` = `rgt` + 2 WHERE `rgt` >= ".$right);
- mysql_query("INSERT INTO `product_feature` SET
- `name` = '".mysql_escape_string( substr( $name , level($l)*2 ) )."',
- `lft` = ".($right).",
- `rgt` = ".($right+1).",
- `level` = ".($level+1)."
- ")or die(mysql_error());
- mysql_query("INSERT INTO `product_feature_to_category` SET
- `product_feature_id` = ".mysql_insert_id().",
- `category_id` = ".$lastCAT."
- ")or die(mysql_error());
-
- $lastLVL = true;
-
- }
- }
- //echo "<pre>";
- //var_dump($out);
- ?>DONE!