Skip to content

Instantly share code, notes, and snippets.

@famoser
Last active June 6, 2016 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save famoser/e04aaf0df6b190e1ea2e236256671432 to your computer and use it in GitHub Desktop.
Save famoser/e04aaf0df6b190e1ea2e236256671432 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: famoser
* Date: 19.05.2016
* Time: 10:00
* This file helps with imports
*/
function outputLine($line) {
$fWrite = fopen(__DIR__ . "/log.txt", "a");
fwrite($fWrite, $line . "\n");
fclose($fWrite);
echo "<p>" . nl2br($line) . "</p>";
}
function outputCriticalFailure($line) {
outputLine($line);
$_SESSION["faillures"]++;
if ($_SESSION["faillures"] > $_SESSION["maxFaillures"]) {
outputLine("stopping execution because of critical faillures");
stopImport();
}
}
function getConnection()
{
$database = new PDO(DB_DSN, DB_USER, DB_PASS);
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$database->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$database->exec("PRAGMA encoding=\"UTF-8\"");
return $database;
}
function executeSql($sql, PDO $connection) {
$stmt = $connection->prepare($sql);
if ($stmt->execute()) {
outputLine("executed successfully: " . $sql);
}
else {
outputLine("failed executing: " . $sql);
}
}
function executeSqlFile($filePath, $database)
{
$sqls = file_get_contents($filePath);
$queries = explode(";", $sqls);
foreach ($queries as $query) {
if (trim($query) != "")
executeSql($query, $database);
}
}
function getContent($tableName, PDO $connection) {
$request = $connection->prepare("SELECT * FROM " . $tableName);
$request->execute();
return $request->fetchAll(PDO::FETCH_ASSOC);
}
function insertContent($tableName, $content, PDO $connection) {
if (count($content) == 0) {
outputLine("content has no entries for table " . $tableName . "!");
return false;
}
$sql = "INSERT INTO " . $tableName . "(";
foreach ($content as $key => $val) {
$sql .= $key . ",";
}
$sql = substr($sql, 0, -1);
$sql .= ") VALUES (";
foreach ($content as $key => $val) {
$sql .= ":" . $key . ",";
}
$sql = substr($sql, 0, -1);
$sql .= ")";
$request = $connection->prepare($sql);
if (!$request->execute($content)) {
return false;
}
return $connection->lastInsertId();
}
function startImport($allowedCriticalFaillures = 5)
{
if (ob_get_level() == 0) ob_start();
$_SESSION["faillures"] = 0;
$_SESSION["maxFaillures"] = $allowedCriticalFaillures;
outputLine("started");
$_SESSION["start_import_time"] = time();
}
function sendResults()
{
ob_flush();
flush();
}
function stopImport()
{
outputLine("stopped");
$time = time() - $_SESSION["start_import_time"];
outputLine("finished after " . $time . " seconds");
ob_end_flush();
exit;
}
/*
How to (example code):
startImport(false);
$handle = fopen(CMS_ROOT . '/public/downloads/import.csv', 'r');
$lineNumber = 0;
while (($data = fgetcsv($handle, null, ";")) !== false) {
if ($lineNumber == 0)
{
if ($data[0] != "Expected") {
outputCriticalFailure("wrong File!");
stopImport();
}
else {
$lineNumber++;
continue;
}
}
$insertArr = array();
$insertArr["public_id"] = md5(uniqid());
$insertArr["excel_import_line_nr"] = $counter++;
$insertArr["is_imported"] = true;
$res = insertContent("tablle", $insertArr, $newDatabase);
if ($institutionId == false) {
outputCriticalFailure("cannot insert: " . json_encode($institutionArr));
continue;
}
outputLine("imported " . $lineNumber);
$lineNumber++;
sendResults();
}
outputLine("imported: " . $lineNumber);
stopImport();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment