Advertisement
Guest User

daily programmer 83int index - taion809

a guest
Aug 1st, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.38 KB | None | 0 0
  1. <?php
  2.  
  3. class Index
  4. {
  5.     private $DB = NULL;
  6.    
  7.     public function __construct()
  8.     {
  9.         $this->openDB();
  10.     }
  11.    
  12.     public function __destruct()
  13.     {
  14.         $this->closeDB();
  15.     }
  16.    
  17.     public function begin_index($list = NULL)
  18.     {
  19.         $dir = scandir("files");
  20.         $start_count = intval($this->getCount());
  21.        
  22.         if($dir != FALSE)
  23.         {
  24.  
  25.             foreach($dir as $item)
  26.             {
  27.                 if($item != "." && $item != "..")
  28.                 {
  29.                     $index_array = array();
  30.                     $n = 0;
  31.                    
  32.                     $file = fopen("files/".$item, "r");
  33.                    
  34.                     do
  35.                     {
  36.                        
  37.                         $file_line = fgets($file);
  38.                         $n++;
  39.  
  40.                         $value = filter_var($file_line, FILTER_SANITIZE_SPECIAL_CHARS);
  41.  
  42.                         if($value != "")
  43.                         {
  44.                             $index_array[] = array(":word" => $value, ":file" => $item, ":line" => intval($n));
  45.                         }
  46.  
  47.                     }while($file_line !== false);
  48.                    
  49.                     fclose($file);
  50.                    
  51.                     $this->insertArray($index_array);
  52.                 }
  53.             }
  54.         }
  55.        
  56.         echo "Inserted " . (intval($this->getCount()) - $start_count) . " objects into database. <br/>";
  57.     }
  58.    
  59.     private function insertArray($items)
  60.     {
  61.         $n = intval($this->getCount());
  62.         $result = false;
  63.        
  64.         foreach($items as $item)
  65.         {
  66.             try
  67.             {                
  68.                 $this->DB->beginTransaction();
  69.                 $handler = $this->DB->prepare("INSERT INTO ft_words (file, word, line)
  70.                                               SELECT :file, :word, :line
  71.                                               WHERE NOT EXISTS (SELECT 1 FROM ft_words WHERE file = :file AND word = :word AND line = :line);");
  72.                 $result = $handler->execute($item);
  73.                                
  74.                 $this->DB->commit();
  75.             }
  76.             catch(PDOException $e)
  77.             {
  78.                 $this->DB->rollBack();
  79.                 echo $e->getMessage();
  80.             }
  81.         }
  82.        
  83.         return $result;
  84.     }
  85.    
  86.     private function getCount()
  87.     {
  88.         try
  89.         {
  90.             $count_handler = $this->DB->prepare("SELECT COUNT(word) FROM ft_words");
  91.             $count_handler->execute();
  92.             $count_handler->setFetchMode(PDO::FETCH_NUM);
  93.             $count = $count_handler->fetchAll();
  94.            
  95.             if($count !== FALSE)
  96.                 return $count[0][0];
  97.             else
  98.                 return 0;
  99.         }
  100.         catch(PDOException $e)
  101.         {
  102.             echo $e->getMessage();
  103.             return 0;
  104.         }    
  105.     }
  106.    
  107.     private function openDB()
  108.     {
  109.         try
  110.         {
  111.             $this->DB = new PDO("sqlite:index.db");
  112.             $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  113.  
  114.             return true;
  115.         }
  116.         catch(PDOException $e)
  117.         {
  118.             echo $e->getMessage();
  119.             return false;
  120.         }
  121.     }
  122.    
  123.     private function closeDB()
  124.     {
  125.         $DB = NULL;
  126.     }
  127. }
  128.  
  129. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement