Source for file csv_loader_class.php
Documentation is available at csv_loader_class.php
* This file contains the CSV loader.
* This file is part of HRDIODB.
* HRDIODB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* HRDIODB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with HRDIODB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/** Configuration file */
require_once("conf.php");
/** Debugging functions */
require_once("debugging.php");
* A class for loading a CSV file into the database.
* The {@link csv_loader} class requires a table name, a unique key name and
* a CSV file where the first line that is not a comment is a comma separated
* list of field names. Each following non comment line is a comma separated
* list of values for those fields. Comments are denoted by the # sign.
$this->comments = array();
$this->data_lines = array();
$this->filename = "none";
/******* Set functions ********/
* Sets the database object.
* Sets the database object. Requires a {@link hrdi_db} object.
* @param hrdi_db $db The reference to a hrdi_db object
* Takes a file name and creates a file handle.
* @param string $file Filename
$this->handle = fopen($file, "r");
* Sets the destination table name.
* @param string $table Table name
* Sets the table / key array.
* Sets an associative array of unique keys to be used to determine if data
* @param array $table_keys Table keys array
* Sets a field to the input line. Takes an array of the target database
* @param string $line A single CSV formatted line containing only field names
foreach($line as $field) {
$trimmed[] = trim($field);
$table = @$this->data_lines[$this->group]['table'];
error("A table must be set before fields are specified");
// Check the fields' validity
$dbfields = $this->db->get_field_names($table);
foreach ($trimmed as $t) {
error("'$t' is not a field in the table '$table'");
$this->fields = $trimmed;
* Parses all the lines of the object's file.
* Reads one csv line into an array.
* Performs cleaning actions on the data from the CSV.
* @return mixed Cleaned data
$line = fgetcsv($this->handle, $csv_line_character_limit, ",");
//Trim spaces from the line if the line is an array
foreach ($line as $item) {
$trimmed[] = trim($item);
// If a line is blank then read ahead to the next line
if(count($line)== 1 && $line[0] == ""){
* Parses one line of a CSV file.
* Determines if that line is a comment, the list of fields, or the data.
* @return mixed Line of data or FALSE.
if ($line[0][0] == '#') {
$this->comments[] = $line;
//A table header was found, read ahead to get the table name
if(count($next_line) != 1){
//Create a new array with an entry describing the table
$dbfields = $this->db->get_field_names($next_line[0]);
// Error is given by db class
$this->data_lines[$this->group]['table'] = $next_line[0];
//Create a new array with an entry describing the fields
// Check to see if we have fields
if (!isset ($this->fields)) {
error("No fields declaration found");
// Check for mismatched field and line element numbers
error("Wrong number of fields supplied:\nRequired:\t".
implode($this->fields, ", "). "\nActual: \t".
if (trim($line[0]) != "") {
//You can use following function instead in php 5
//$combined = array_combine($this->data_lines[$this->group]['fields'], $line);
foreach($this->fields as $field){
$combined[$field] = $line[$i];
$this->data_lines[$this->group][] = $combined;
* Prints file information.
* Prints detailed information about a parsed file.
error("Must parse first");
print "\tFilename: ". $this->filename;
print "\n\tComment lines: ". count($this->comments);
print "\n\tBlocks: ". count($this->data_lines);
foreach($this->data_lines as $block){
print "\n\tTable: ". @$block['table'];
print "\n\tData lines: ". (count($block) - 1);
foreach ($this->comments as $comment){
* Pushes information to the database.
* Pushes parsed file information to the database.
foreach ($this->data_lines as $block){
if(!isset ($block['table'])){
$table = $block['table'];
// Set the key from the table_keys array
foreach($block as $record){
// Test for update or add.
// Only do update check if a key exists for this table
if (isset ($key) && isset ($record[$key]) && $this->db->get_one($sql)) {
$this->db->update($record, $table, $key);
$this->db->add($record, $table);
print "\n\n\tRecords added: $added";
print "\n\tRecords updated: $updated";
|