dienasmediji /web/tmp/parser.php

Language PHP Lines 99
MD5 Hash e2dc84106722f8ff385165b6a534920a Estimated Cost $1,730 (why?)
Repository https://bitbucket.org/mupenieks/dienasmediji.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?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!
Back to Top