"Fossies" - the Fresh Open Source Software Archive

Member "libs/PHPCrawler/ProcessCommunication/PHPCrawlerDocumentInfoQueue.class.php" (8 Jan 2013, 3892 Bytes) of package /linux/www/SitemapCreator.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * Queue for PHPCrawlerDocumentInfo-objects
    4  *
    5  * @package phpcrawl
    6  * @internal
    7  */
    8 class PHPCrawlerDocumentInfoQueue
    9 {
   10   protected $PDO;
   11   
   12   protected $sqlite_db_file;
   13   
   14   protected $prepared_statements_created = false;
   15   
   16   protected $working_directory = null;
   17   
   18   protected $queue_max_size = 20;
   19   
   20   /**
   21    * Initiates a PHPCrawlerDocumentInfoQueue
   22    *
   23    * @param string $file            The SQLite-fiel to use.
   24    * @param  bool  $create_tables   Defines whether all necessary tables should be created
   25    */
   26   public function __construct($file, $create_tables = false)
   27   {
   28     $this->sqlite_db_file = $file;
   29     $this->working_directory = dirname($file)."/";
   30     $this->openConnection($create_tables);
   31   }
   32   
   33   /**
   34    * Returns the current number of PHPCrawlerDocumentInfo-objects in the queue
   35    */
   36   public function getDocumentInfoCount()
   37   {
   38     $Result = $this->PDO->query("SELECT count(id) as sum FROM document_infos;");
   39     $row = $Result->fetch(PDO::FETCH_ASSOC);
   40     $Result->closeCursor();
   41     
   42     return $row["sum"];
   43   }
   44   
   45   /**
   46    * Adds a PHPCrawlerDocumentInfo-object to the queue
   47    */
   48   public function addDocumentInfo(PHPCrawlerDocumentInfo $DocInfo)
   49   {
   50     // If queue is full -> wait a little
   51     while ($this->getDocumentInfoCount() >= $this->queue_max_size)
   52     {
   53       sleep(0.5);
   54     }
   55     
   56     $this->createPreparedStatements();
   57     
   58     $ser = serialize($DocInfo);
   59     
   60     $this->PDO->exec("BEGIN EXCLUSIVE TRANSACTION");
   61     $this->prepared_insert_statement->bindParam(1, $ser, PDO::PARAM_LOB);
   62     $this->prepared_insert_statement->execute();
   63     $this->prepared_select_statement->closeCursor();
   64     $this->PDO->exec("COMMIT");
   65   }
   66   
   67    /**
   68    * Returns a PHPCrawlerDocumentInfo-object from the queue
   69    */
   70   public function getNextDocumentInfo()
   71   { 
   72     $this->createPreparedStatements();
   73     
   74     $this->prepared_select_statement->execute();
   75     $this->prepared_select_statement->bindColumn("document_info", $doc_info, PDO::PARAM_LOB);
   76     $this->prepared_select_statement->bindColumn("id", $id);
   77     $row = $this->prepared_select_statement->fetch(PDO::FETCH_BOUND);
   78     $this->prepared_select_statement->closeCursor();
   79     
   80     if ($id == null) 
   81     {
   82       return null; 
   83     }
   84     
   85     $this->PDO->exec("DELETE FROM document_infos WHERE id = ".$id.";");
   86     
   87     $DocInfo = unserialize($doc_info);
   88 
   89     return $DocInfo;
   90   }
   91   
   92   protected function createPreparedStatements()
   93   {
   94     if ($this->prepared_statements_created == false)
   95     {
   96       $this->prepared_insert_statement = $this->PDO->prepare("INSERT INTO document_infos (document_info) VALUES (?);");
   97       $this->prepared_select_statement = $this->PDO->prepare("SELECT * FROM document_infos limit 1;");
   98       
   99       $this->prepared_statements_created = true;
  100     }
  101   }
  102   
  103   /**
  104    * Creates the sqlite-db-file and opens connection to it.
  105    *
  106    * @param bool $create_tables Defines whether all necessary tables should be created
  107    */
  108   protected function openConnection($create_tables = false)
  109   {
  110     // Open sqlite-file
  111     try
  112     {
  113       $this->PDO = new PDO("sqlite:".$this->sqlite_db_file);
  114     }
  115     catch (Exception $e)
  116     {
  117       throw new Exception("Error creating SQLite-cache-file, ".$e->getMessage().", try installing sqlite3-extension for PHP.");
  118     }
  119     
  120     $this->PDO->exec("PRAGMA journal_mode = OFF");
  121     
  122     $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  123     $this->PDO->setAttribute(PDO::ATTR_TIMEOUT, 100);
  124     
  125     if ($create_tables == true)
  126     {
  127       $this->PDO->exec("CREATE TABLE IF NOT EXISTS document_infos (id integer PRIMARY KEY AUTOINCREMENT,
  128                                                                    document_info blob);");
  129       $this->PDO->exec("ANALYZE;");
  130     }
  131   }
  132 }
  133 ?>