Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Index
- {
- private $DB = NULL;
- public function __construct()
- {
- $this->openDB();
- }
- public function __destruct()
- {
- $this->closeDB();
- }
- public function begin_index($list = NULL)
- {
- $dir = scandir("files");
- $start_count = intval($this->getCount());
- if($dir != FALSE)
- {
- foreach($dir as $item)
- {
- if($item != "." && $item != "..")
- {
- $index_array = array();
- $n = 0;
- $file = fopen("files/".$item, "r");
- do
- {
- $file_line = fgets($file);
- $n++;
- $value = filter_var($file_line, FILTER_SANITIZE_SPECIAL_CHARS);
- if($value != "")
- {
- $index_array[] = array(":word" => $value, ":file" => $item, ":line" => intval($n));
- }
- }while($file_line !== false);
- fclose($file);
- $this->insertArray($index_array);
- }
- }
- }
- echo "Inserted " . (intval($this->getCount()) - $start_count) . " objects into database. <br/>";
- }
- private function insertArray($items)
- {
- $n = intval($this->getCount());
- $result = false;
- foreach($items as $item)
- {
- try
- {
- $this->DB->beginTransaction();
- $handler = $this->DB->prepare("INSERT INTO ft_words (file, word, line)
- SELECT :file, :word, :line
- WHERE NOT EXISTS (SELECT 1 FROM ft_words WHERE file = :file AND word = :word AND line = :line);");
- $result = $handler->execute($item);
- $this->DB->commit();
- }
- catch(PDOException $e)
- {
- $this->DB->rollBack();
- echo $e->getMessage();
- }
- }
- return $result;
- }
- private function getCount()
- {
- try
- {
- $count_handler = $this->DB->prepare("SELECT COUNT(word) FROM ft_words");
- $count_handler->execute();
- $count_handler->setFetchMode(PDO::FETCH_NUM);
- $count = $count_handler->fetchAll();
- if($count !== FALSE)
- return $count[0][0];
- else
- return 0;
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- return 0;
- }
- }
- private function openDB()
- {
- try
- {
- $this->DB = new PDO("sqlite:index.db");
- $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
- return true;
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- return false;
- }
- }
- private function closeDB()
- {
- $DB = NULL;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement