seek_quarry
[ class tree: seek_quarry ] [ index: seek_quarry ] [ all elements ]

Source for file datasource_manager.php

Documentation is available at datasource_manager.php

  1. <?php
  2. /**
  3.  *  SeekQuarry/Yioop --
  4.  *  Open Source Pure PHP Search Engine, Crawler, and Indexer
  5.  *
  6.  *  Copyright (C) 2009 - 2013  Chris Pollett chris@pollett.org
  7.  *
  8.  *  LICENSE:
  9.  *
  10.  *  This program is free software: you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation, either version 3 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  22.  *
  23.  *  END LICENSE
  24.  *
  25.  * @author Chris Pollett chris@pollett.org
  26.  * @package seek_quarry
  27.  * @subpackage datasource_manager
  28.  * @license http://www.gnu.org/licenses/ GPL3
  29.  * @link http://www.seekquarry.com/
  30.  * @copyright 2009 - 2013
  31.  * @filesource
  32.  */
  33.  
  34. if(!defined('BASE_DIR')) {echo "BAD REQUEST"exit();}
  35.  
  36. /**  For timer function, if debug level set to include query statistics */
  37. require_once BASE_DIR."/lib/utility.php";
  38.  
  39. /**
  40.  *
  41.  * This abstract class defines the interface through which
  42.  * the seek_quarry program communicates with a database and the
  43.  * filesystem.
  44.  *
  45.  * @author Chris Pollett
  46.  * @package seek_quarry
  47.  * @subpackage datasource_manager
  48.  */
  49. abstract class DatasourceManager
  50. {
  51.     /**
  52.      * Used to store statistics about what queries have been run depending on
  53.      * the debug level
  54.      * @var string 
  55.      */
  56.     var $query_log;
  57.     /**
  58.      * Used to store the total time taken to execute queries
  59.      * @var int 
  60.      */
  61.     var $total_time;
  62.  
  63.     /** Sets up the query_log for query statistics */
  64.     function __construct({
  65.         $this->query_log = array();
  66.         $this->total_time = 0;
  67.     }
  68.  
  69.     /**
  70.      * Connects to a DBMS using data provided or from config.php
  71.      *
  72.      * @param string $db_host the hostname of where the database is located
  73.      *       (not used in all dbms's)
  74.      * @param string $db_user the user to connect as
  75.      * @param string $db_password the password of the user to connect as
  76.      * @return mixed return false if not successful and some kind of
  77.      *       connection object/identifier otherwise
  78.      */
  79.  
  80.     abstract function connect($db_host DB_HOST$db_user DB_USER,
  81.         $db_password DB_PASSWORD);
  82.  
  83.     /**
  84.      *  Connects to the correct DB on that system
  85.      */
  86.     abstract function selectDB($db_name);
  87.  
  88.     /**
  89.      *  Closes connections to DBMS
  90.      *
  91.      */
  92.     abstract function disconnect();
  93.  
  94.     /**
  95.      * Hook Method for execute(). Executes the sql command on the database
  96.      *
  97.      * This method operates on either query or data manipulation statements
  98.      *
  99.      * @param string $sql  SQL statement to execute
  100.      * @return mixed false if query fails, resource or true otherwise
  101.      */
  102.     abstract function exec($sql);
  103.  
  104.  
  105.  
  106.     /**
  107.      * Returns the number of rows affected by the last sql statement
  108.      *
  109.      * @return int the number of rows affected by the last
  110.      *  insert, update, delete
  111.      */
  112.     abstract function affectedRows();
  113.  
  114.     /**
  115.      * Returns the ID generated by the last insert statement
  116.      * if table has an auto increment key column
  117.      *
  118.      * @return string  the ID of the insert
  119.      */
  120.     abstract function insertID();
  121.  
  122.     /**
  123.      * Returns the next row from the provided result set
  124.      *
  125.      * @param resource $result   result set reference of a query
  126.      * @return array the next row from the result set as an
  127.      *  associative array in the form column_name => value
  128.      */
  129.     abstract function fetchArray($result);
  130.  
  131.     /**
  132.      * Used to escape strings before insertion in the
  133.      * database to avoid SQL injection
  134.      *
  135.      * @param string $str  string to escape
  136.      * @return string a string which is safe to insert into the db
  137.      */
  138.     abstract function escapeString($str);
  139.  
  140.     /**
  141.      * Executes the supplied sql command on the database, depending on debug
  142.      * levels computes query statistics
  143.      *
  144.      * This method operates either query or data manipulation statements
  145.      *
  146.      * @param string $sql  SQL statement to execute
  147.      * @return mixed false if query fails, resource or true otherwise
  148.  
  149.      */
  150.     function execute($sql)
  151.     {
  152.         if(QUERY_STATISTICS{
  153.             $query_info array();
  154.             $query_info['QUERY'$sql;
  155.             $start_time microtime();
  156.         }
  157.         $result =$this->exec($sql);
  158.         if(QUERY_STATISTICS{
  159.             $query_info['ELAPSED_TIME'changeInMicrotime($start_time);
  160.             $this->total_time += $query_info['ELAPSED_TIME'];
  161.             $this->query_log[$query_info;
  162.         }
  163.         return $result;
  164.     }
  165.  
  166.     /**
  167.      * Recursively delete a directory
  168.      *
  169.      * @param string $dir Directory name
  170.      * @param boolean $deleteRootToo Delete specified top directory as well
  171.      */
  172.     function unlinkRecursive($dir$deleteRootToo true)
  173.     {
  174.         $this->traverseDirectory($dir"deleteFileOrDir"$deleteRootToo);
  175.     }
  176.  
  177.     /**
  178.      * Recursively chmod a directory to 0777
  179.      *
  180.      * @param string $dir Directory name
  181.      * @param boolean $chmodRootToo chmod specified top-level directory as well
  182.      */
  183.     function setWorldPermissionsRecursive($dir$chmodRootToo true)
  184.     {
  185.         $this->traverseDirectory($dir"setWorldPermissions"$chmodRootToo);
  186.     }
  187.  
  188.     /**
  189.      * Returns arrays of filesizes and file modifcations times of files in
  190.      * a directory
  191.      */
  192.     function fileInfoRecursive($dir$chmodRootToo true)
  193.     {
  194.         return $this->traverseDirectory($dir,
  195.             "fileInfo"$chmodRootToo);
  196.     }
  197.  
  198.     /**
  199.      * Recursively copies a source directory to a destination directory
  200.      *
  201.      * It would have been cool to use traverseDirectory to implement this, but
  202.      * it was a little bit too much of a stretch to shoehorn the code to match
  203.      *
  204.      * @param string $source_dir the name of the source directory
  205.      * @param string $desitnation_dir the name of the destination directory
  206.      */
  207.     function copyRecursive($source_dir$destination_dir)
  208.     {
  209.         if(!$dh @opendir($source_dir)) {
  210.             return;
  211.         }
  212.         if(!file_exists($destination_dir)) {
  213.             @mkdir($destination_dir);
  214.             if(!file_exists($destination_dir)) {
  215.                 return;
  216.             }
  217.             chmod($destination_dir0777);
  218.         }
  219.         while(false !== $obj readdir($dh)) ) {
  220.             if (( $obj != '.' && $obj != '..' )) {
  221.                 if is_dir($source_dir '/' $obj) ) {
  222.                     $this->copyRecursive($source_dir '/' .
  223.                         $obj$destination_dir '/' $obj);
  224.                 }
  225.                 else {
  226.                     copy($source_dir '/' .
  227.                         $obj$destination_dir '/' $obj);
  228.                     chmod($destination_dir '/' $obj0777);
  229.                 }
  230.             }
  231.         }
  232.         closedir($dh);
  233.     }
  234.  
  235.  
  236.     /**
  237.      * Recursively traverse a directory structure and call a callback function
  238.      *
  239.      * @param string $dir Directory name
  240.      * @param function $callback Function to call as traverse structure
  241.      * @return array results computed by performing the traversal
  242.      */
  243.     function traverseDirectory($dir$callback$rootToo true)
  244.     {
  245.         $results array();
  246.         if(!$dh @opendir($dir)) {
  247.             return $results;
  248.         }
  249.  
  250.         while (false !== ($obj readdir($dh))) {
  251.             if($obj == '.' || $obj == '..'{
  252.                 continue;
  253.             }
  254.             if (is_dir($dir '/' $obj)) {
  255.                 $subdir_results =
  256.                     $this->traverseDirectory($dir.'/'.$obj$callbacktrue);
  257.                 $results array_merge($results$subdir_results);
  258.             }
  259.             $obj_results @$callback($dir '/' $obj);
  260.             if(is_array($obj_results)) {
  261.                 $results array_merge($results$obj_results);
  262.             }
  263.  
  264.         }
  265.  
  266.         closedir($dh);
  267.  
  268.         if ($rootToo{
  269.             $obj_results @$callback($dir);
  270.             if(is_array($obj_results)) {
  271.                 $results array_merge($results$obj_results);
  272.             }
  273.         }
  274.  
  275.         return $results;
  276.  
  277.     }
  278.  
  279.     /**
  280.      * Returns string for given DBMS CREATE TABLE equivalent to auto_increment
  281.      * (at least as far as Yioop requires).
  282.      *
  283.      * @param array $dbinfo contains strings DBMS, DB_HOST, DB_USER, DB_PASSWORD
  284.      * @return string to achieve auto_increment function for the given DBMS
  285.      */
  286.     function autoIncrement($dbinfo)
  287.     {
  288.         $auto_increment "AUTOINCREMENT";
  289.         if(in_array($dbinfo['DBMS']array("mysql"))) {
  290.             $auto_increment "AUTO_INCREMENT";
  291.         }
  292.         if(in_array($dbinfo['DBMS']array("sqlite"))) {
  293.             $auto_increment "";
  294.                 /* in sqlite2 a primary key column will act
  295.                    as auto_increment if don't give value
  296.                  */
  297.         }
  298.         if($dbinfo['DBMS'== 'pdo'{
  299.             if(stristr($dbinfo['DB_HOST']'SQLITE')) {
  300.                 $auto_increment "";
  301.             else if(stristr($dbinfo['DB_HOST']'PGSQL')) //POSTGRES
  302.                 $auto_increment "SERIAL";
  303.             else if(stristr($dbinfo['DB_HOST']'OCI')) // ORACLE
  304.                 $auto_increment "DEFAULT SYS_GUID()";
  305.             else if(stristr($dbinfo['DB_HOST']'IBM')) //DB2
  306.                 $auto_increment "GENERATED ALWAYS AS IDENTITY ".
  307.                     "(START WITH 1 INCREMENT BY 1)";
  308.             else if(stristr($dbinfo['DB_HOST']'DBLIB')) //MS SQL
  309.                 $auto_increment "IDENTITY (1,1)";
  310.             }
  311.         }
  312.  
  313.         return $auto_increment;
  314.     }
  315.  
  316. }
  317. ?>

Documentation generated by phpDocumentor 1.4.3