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

Source for file csv_loader_class.php

Documentation is available at csv_loader_class.php

  1. <?php
  2. /**
  3.  * CSV loader.
  4.  * This file contains the CSV loader.
  5.  * @package HRDIODB
  6.  */
  7.  
  8. /**
  9.  * This file is part of HRDIODB.
  10.  *
  11.  * HRDIODB is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * HRDIODB is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with HRDIODB; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24.  */
  25.  
  26. /** Configuration file */
  27. require_once("conf.php");
  28. /** Debugging functions */
  29. require_once("debugging.php");
  30.  
  31. /**
  32.  * A class for loading a CSV file into the database.
  33.  * The {@link csv_loader} class requires a table name, a unique key name and
  34.  * a CSV file where the first line that is not a comment is a comma separated
  35.  * list of field names. Each following non comment line is a comma separated
  36.  * list of values for those fields. Comments are denoted by the # sign.
  37.  * @package HRDIODB
  38.  */
  39. class csv_loader {
  40.  /**
  41.   * Constructor.
  42.   * Class constructor.
  43.   */
  44.   function csv_loader ({
  45.     $this->comments array();
  46.     $this->data_lines array();
  47.     $this->filename "none";
  48.     $this->parsed FALSE;
  49.     $this->group 0;
  50.   }
  51.   
  52.  /******* Set functions ********/
  53.  
  54.  /**
  55.   * Sets the database object.
  56.   * Sets the database object. Requires a {@link hrdi_db} object.
  57.   * @param hrdi_db $db The reference to a hrdi_db object
  58.   */
  59.   function set_db (&$db{
  60.     $this->db $db;
  61.   }
  62.  
  63.  /**
  64.   * Creates a file handle.
  65.   * Takes a file name and creates a file handle.
  66.   * @param string $file Filename
  67.   */
  68.   function set_file ($file{
  69.     $this->filename $file;
  70.     $this->handle fopen($file"r");
  71.   }
  72.  
  73.  /**
  74.   * Sets table name.
  75.   * Sets the destination table name.
  76.   * @param string $table Table name
  77.   */
  78.   function set_table ($table{
  79.     $this->table $table;
  80.   }
  81.  
  82.  /**
  83.   * Sets the table / key array.
  84.   * Sets an associative array of unique keys to be used to determine if data
  85.   * exists in a table.
  86.   * @param array $table_keys Table keys array
  87.   */
  88.   function set_table_keys ($table_keys{
  89.     $this->table_keys = $table_keys;
  90.   }
  91.  
  92.  /**
  93.   * Sets field.
  94.   * Sets a field to the input line. Takes an array of the target database
  95.   * fields.
  96.   * @param string $line A single CSV formatted line containing only field names
  97.   */
  98.   function set_fields ($line{
  99.     foreach($line as $field{
  100.       $trimmed[trim($field);
  101.     }
  102.  
  103.     $table @$this->data_lines[$this->group]['table'];
  104.  
  105.     if(!$table){
  106.       error("A table must be set before fields are specified");
  107.       exit(0);
  108.     }
  109.  
  110.     // Check the fields' validity
  111.     $dbfields $this->db->get_field_names($table);
  112.  
  113.     foreach ($trimmed as $t{
  114.       if (!in_array($t$dbfields)) {
  115.         error("'$tis not a field in the table '$table'");
  116.         exit(0);
  117.       }
  118.     }
  119.     
  120.     $this->fields $trimmed;
  121.   }
  122.  
  123.  /**
  124.   * Parses the file.
  125.   * Parses all the lines of the object's file.
  126.   */
  127.   function parse_file({
  128.     while (($this->load_csv_line()) !== FALSE}
  129.     $this->parsed TRUE;
  130.   }
  131.  
  132.  /**
  133.   * Reads one csv line into an array.
  134.   * Performs cleaning actions on the data from the CSV.
  135.   * @return mixed Cleaned data
  136.   */
  137.   function get_line({
  138.     global $csv_line_character_limit;
  139.  
  140.     $line fgetcsv($this->handle$csv_line_character_limit",");
  141.  
  142.     //Trim spaces from the line if the line is an array
  143.     if (is_array($line)) {
  144.       foreach ($line as $item{
  145.         $trimmed[trim($item);
  146.       }
  147.     
  148.       // If a line is blank then read ahead to the next line
  149.       if(count($line)==&& $line[0== ""){
  150.         return $this->get_line();
  151.       }
  152.     
  153.       return $trimmed;
  154.     }
  155.     return $line;
  156.   }
  157.  
  158.  
  159.  
  160.  /**
  161.   * Parses one line of a CSV file.
  162.   * Determines if that line is a comment, the list of fields, or the data.
  163.   * @return mixed Line of data or FALSE.
  164.   */
  165.   function load_csv_line(){
  166.     $line $this->get_line();
  167.  
  168.     if ($line[0][0== '#'{
  169.       $this->comments[$line;
  170.       return 0;
  171.     }
  172.  
  173.     if ($line === FALSE{
  174.       return FALSE;
  175.     }
  176.  
  177.     // Detect a table header
  178.     if(strtolower($line[0]== "table"){
  179.       if(count($line!= 1){
  180.         error("Bad table header: ".implode($line));
  181.         exit(0);
  182.       }
  183.  
  184.       //A table header was found, read ahead to get the table name
  185.       $next_line $this->get_line();
  186.  
  187.       if(count($next_line!= 1){
  188.         error("Bad table name: ".implode($next_line", "));
  189.         exit(0);
  190.       }
  191.  
  192.       //Create a new array with an entry describing the table
  193.       $this->group++;
  194.       $dbfields $this->db->get_field_names($next_line[0]);
  195.  
  196.       if(!$dbfields){
  197.         // Error is given by db class
  198.         exit(0);
  199.       }
  200.  
  201.       $this->data_lines[$this->group]['table'$next_line[0];
  202.       
  203.       return TRUE;
  204.     }
  205.       
  206.     // Detect a field header
  207.     if (strtolower($line[0]== "fields"{
  208.       if (count($line!= 1{
  209.         error("Bad field header: ".implode($line));
  210.         exit(0);
  211.       }
  212.  
  213.       $next_line $this->get_line();
  214.  
  215.       //Create a new array with an entry describing the fields
  216.       $this->set_fields($next_line);
  217.       
  218.       return TRUE;
  219.     }
  220.  
  221.     // Check to see if we have fields
  222.     if (!isset($this->fields)) {
  223.       error("No fields declaration found");
  224.       exit(0);
  225.     }
  226.     
  227.     // Check for mismatched field and line element numbers
  228.     if (count($this->fields!= count($line)) {
  229.       error("Wrong number of fields supplied:\nRequired:\t".
  230.                     implode($this->fields", ")."\nActual:  \t".
  231.                     implode($line", "));
  232.       exit(0);
  233.     }
  234.       
  235.  
  236.     if (trim($line[0]!= ""{
  237.       //You can use following function instead in php 5
  238.       //$combined = array_combine($this->data_lines[$this->group]['fields'], $line);
  239.       
  240.       $i 0;
  241.       foreach($this->fields as $field){
  242.         $combined[$field$line[$i];
  243.         $i++;
  244.       }
  245.       $this->data_lines[$this->group][$combined;
  246.     }
  247.     return $line;
  248.   }
  249.  
  250.   /**
  251.    * Prints file information.
  252.    * Prints detailed information about a parsed file.
  253.    */
  254.   function print_file_info({
  255.     if (!$this->parsed{
  256.       error("Must parse first");
  257.       return 0;
  258.     }
  259.  
  260.     print "\tFilename: ".$this->filename;
  261.     print "\n\tComment lines: ".count($this->comments);
  262.     print "\n\tBlocks: ".count($this->data_lines);
  263.     print "\n";
  264.  
  265.     foreach($this->data_lines as $block){
  266.       print "\n\tTable: ".@$block['table'];
  267.       print "\n\tData lines: ".(count($block1);
  268.       print "\n";
  269.     }
  270.  
  271.     print "\n";
  272.  
  273.     foreach ($this->comments as $comment){
  274.       $comment implode($comment);
  275.       print "\n$comment";
  276.     }
  277.  
  278.     print "\n";
  279.   }
  280.  
  281.  
  282.   /**
  283.    * Pushes information to the database.
  284.    * Pushes parsed file information to the database.
  285.    */
  286.   function push_to_db ({
  287.     $added $updated 0;
  288.     print "\nUploading:";
  289.  
  290.     foreach ($this->data_lines as $block){
  291.       if(!isset($block['table'])){
  292.         error("Table not set");
  293.         exit(0);
  294.       }
  295.       $table $block['table'];
  296.       unset($block['table']);
  297.       
  298.       // Set the key from the table_keys array
  299.       unset($key);
  300.  
  301.       if(isset($this->table_keys[$table])){
  302.         $key $this->table_keys[$table];
  303.       }
  304.       
  305.       foreach($block as $record){
  306.         $where addslashes(@$record[$key]);
  307.  
  308.         // Test for update or add.
  309.         // Only do update check if a key exists for this table
  310.         $sql "SELECT $key
  311.                 FROM $table
  312.                 WHERE $key='$where'";
  313.         if (isset($key&& isset($record[$key]&&  $this->db->get_one($sql)) {
  314.           // update
  315.           $this->db->update($record$table$key);
  316.           $updated++;
  317.         else {
  318.           // add
  319.           $this->db->add($record$table);
  320.           $added++;
  321.         }
  322.         print(".");
  323.       }
  324.     }
  325.     print "\n\n\tRecords added$added";
  326.     print "\n\tRecords updated$updated";
  327.     print "\n\n";
  328.   }
  329. }
  330. ?>

Documentation generated on Tue, 23 Jan 2007 22:57:44 -0500 by phpDocumentor 1.3.0RC6