-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.php
95 lines (51 loc) · 1.69 KB
/
indexer.php
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
<?php
$servername = "localhost";
$username = "root";
$password = "Skeppend,11";
$dbname = "search_engine";
$connection = mysqli_connect($servername, $username, $password, $dbname) or die("Connection Failed!");
function insertURL($pageid, $url)
{
$conn = $GLOBALS['connection'];
$sql = "INSERT INTO pages (PageID, URL) VALUES ($pageid, '$url')";
mysqli_query($conn, $sql);
}//insertURL()
function insert($pageid, $word, $priority, $position)
{
$conn = $GLOBALS['connection'];
$sql = "SELECT MAX(WordID) AS max FROM lexicon";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$wordid = $row['max'] + 1;
$sql = "INSERT INTO lexicon (WordID,Word) VALUES ($wordid,'$word')";
mysqli_query($conn, $sql);
$sql = "SELECT WordID FROM lexicon WHERE Word = '$word'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$wordid = $row["WordID"];
$sql = "INSERT INTO indexes (PageID,WordID,Priority,Position) VALUES ($pageid,$wordid,$priority,$position)";
mysqli_query($conn, $sql);
}//insert()
$count = count(glob("Words/"."*"));
echo "Indexing $count pages <br><br>";
for ($pageid=1; $pageid<=$count; $pageid++)
{
$file = fopen("Words/".$pageid.".txt","r");
$url = fgets($file);
insertURL($pageid, $url);
$word;
$priority;
$position = 0;
while(!feof($file))
{
$line = explode(" ",fgets($file));
$position++;
$word = $line[0];
$priority = (int)$line[1];
insert($pageid,$word,$priority,$position);
}
fclose($file);
echo "page $pageid has indexed...<br>";
}
mysqli_close($connection);
?>