function getCSV($res, $fileName)
{
    session_start();
    if (!empty($_SESSION['download_' . $fileName]) && time() - $_SESSION['download_' . $fileName] < 10) {
        exit;
    }
    // Check, then exit if you have requested before in 10 seconds
    $_SESSION['download_' . $fileName] = time();
    $header = "Semester;Code;Subject Name";
    $data = "";
    foreach ($res as $row) {
        $line = '';
        foreach ($row as $value) {
            if (!isset($value) || $value == "") {
                $value = ";";
            } else {
                $value = str_replace('"', '""', $value);
                $value = '"' . $value . '"' . ";";
            }
            $line .= $value;
        }
        $data .= trim($line) . "\n";
    }
    $data = str_replace("\r", "", $data);
    if ($data == "") {
        $data = "\n(0) Records Found!\n";
    }
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: text/x-csv");
    header("Content-Transfer-Encoding: binary");
    header("Content-Disposition: attachment; filename=" . $fileName . ".csv");
    $csv = "{$header}\n{$data}";
    $csv = mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8');
    //print $csv;
    writeCSV($csv, $fileName);
    session_destroy();
}
예제 #2
0
 $new_conflicts = getNewConflicts($current_conflicts, $detected_conflicts);
 $conflicts_to_be_excluded = detectConflictsTobeExcluded($instrument, $commentids, $current_conflicts);
 /**
 * Only Create report for the  confliclits
 1) that should be inserted into the conflicts_unresolved table
 2) that should be deleted from the conflicts_unresolved table
 */
 if (!$change && !$change_all) {
     //just show the conflicts
     print "This will only display the conflicts needed to\n                be inserted \n";
     print "To re-create the conflict, run this script with \n                -c option \n";
     if (empty($new_conflicts) && empty($conflicts_to_be_excluded)) {
         print "No new conflicts or current conflicts to be\n                removed are detected for instrument {$instrument} \n";
     } else {
         writeCSV($new_conflicts, $dataDir, $instrument, $visit_label, "Conflicts_to_be_inserted");
         writeCSV($conflicts_to_be_excluded, $dataDir, $instrument, $visit_label, "Conflicts_to_be_removed");
     }
 }
 /**
  * Only insert those conflicts which are new and are not currently
  * in the conflict_uresolved
  * 1)remove/clear the existing conflicts
  * 2) And then re-create the conflicts
  */
 ///if the instrument is not provided, run it only for all the instruments
 if ($change) {
     if (empty($new_conflicts) || !isset($new_conflicts)) {
         die("There are No new conflicts to be inserted ");
     }
     foreach ($new_conflicts as $conflict) {
         if ($conflict != null && !empty($conflict)) {
예제 #3
0
require "vendor/autoload.php";
function writeCSV($file, $data)
{
    $handle = fopen($file, "w");
    foreach ($data as $datum) {
        fputcsv($handle, $datum);
    }
    fclose($handle);
}
print "It takes a while to produce about 50mb of CSV data...\n";
unlink("authors.csv");
unlink("categories.csv");
unlink("posts.csv");
$faker = Faker\Factory::create();
$authors = [];
for ($i = 0; $i < 100; $i++) {
    $authors[] = [$i, $faker->title, $faker->firstName, $faker->lastName, $faker->address, $faker->city, $faker->country, $faker->postcode, $faker->email];
}
writeCSV("authors.csv", $authors);
$categories = [];
for ($i = 0; $i < 50; $i++) {
    $categories[] = [$i, $faker->unique()->word];
}
writeCSV("categories.csv", $categories);
$posts = [];
for ($i = 0; $i < 10000; $i++) {
    $posts[] = [$i, $faker->randomElement($authors)[0], $faker->randomElement($categories)[0], $faker->sentence, join("\n\n", $faker->paragraphs(3)), join("\n\n", $faker->paragraphs(30)), join(", ", $faker->words(5))];
}
writeCSV("posts.csv", $posts);
예제 #4
0
function fetchProductInfo($collection)
{
    extract($collection);
    echo "\n" . $producturl;
    $html = file_get_html($producturl);
    $data = getShippingCostToBrazil($productid);
    $shippingCostToBrazil = $data->freight[0]->status;
    $ordersCount = str_replace("pedidos", "", $html->find('.orders-count', 0)->plaintext);
    $ordersCount = explode(",", $ordersCount);
    $ordersCount = trim($ordersCount[0]);
    if (intval($store_min_order) > intval($ordersCount)) {
        echo $productid . " {$ordersCount} - {$store_min_order} is less then the threshold. Skipping product...";
        return false;
    }
    $productRating = trim(str_replace("Average Star Rating:", "", $html->find('.ui-rating-star', 0)->title));
    $productName = trim(html_entity_decode($html->find('.product-name', 0)->plaintext));
    $images = array();
    $thumbs = array();
    $productImages = $html->find('.image-nav', 0);
    foreach ($productImages->find('img') as $key => $value) {
        $thumbs[] = $value->src;
        $imgLink = str_replace(".jpg_50x50.jpg", ".jpg", $value->src);
        $images[] = $imgLink;
    }
    $images = implode(";", $images);
    $thumbs = implode(";", $thumbs);
    $discountPrice = trim($html->find('.ui-cost', 0)->plaintext);
    $promos = $html->find('.store-promotion-discount-list', 0)->find('ul', 0)->find('li');
    $promocodes = array();
    foreach ($promos as $pk => $pv) {
        $promocodes[] = html_entity_decode($pv->plaintext);
    }
    $key = count($promocodes) - 1;
    unset($promocodes[$key]);
    $promocodes = trim(implode(" | ", $promocodes));
    $productPrice = trim($html->find('#sku-price', 0)->plaintext);
    $colors = array();
    if ($html->find('#sku-color', 0)) {
        $colorsvariations = $html->find('#sku-color', 0)->find('a');
        foreach ($colorsvariations as $ck => $cv) {
            $colors[] = $cv->title;
        }
        $colors = implode("| ", $colors);
    } else {
        $colors = "";
    }
    $sizes = array();
    if ($html->find('.product-info-size', 0)) {
        $colorsvariations = $html->find('.product-info-size', 0)->find('a');
        foreach ($colorsvariations as $ck => $cv) {
            $sizes[] = $cv->plaintext;
        }
        $sizes = implode("| ", $sizes);
    } else {
        $sizes = "";
    }
    $description = $html->find('#custom-description', 0);
    $otherImages = array();
    $otherImage = $description->find('img');
    foreach ($otherImages as $ik => $iv) {
        $otherImages[] = $iv->src;
    }
    if ($html->find('.ui-breadcrumb')) {
        $category = $html->find('.ui-breadcrumb h2');
    }
    if ($html->find('.col-main')) {
        $category = $html->find('.m-sop-crumb', 0)->find('b');
    }
    $key = count($category) - 1;
    $category = str_replace(" ", "_", trim($category[$key]->plaintext));
    $otherImages = implode(";", $otherImages);
    $description = html_entity_decode($description->plaintext);
    print_r(array($store_id, $category, $shippingCostToBrazil, $ordersCount, $productRating, $productName, $discountPrice, $promocodes, $productPrice, $colors, $sizes, $description, $images, $thumbs, $otherImages));
    writeCSV($category, array($store_id, $category, $shippingCostToBrazil, $ordersCount, $productRating, $productName, $discountPrice, $promocodes, $productPrice, $colors, $sizes, $description, $images, $thumbs, $otherImages));
}
예제 #5
0
    fclose($file);
    //Timer
    $time = round((microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) * 1000);
    //Log time taken
    $logger->logInfo("Read from cache " . $url . " in [" . $time . "ms]");
    writeCSV("READ," . $time . "," . $url . "," . $cachedFilePath);
} else {
    //Generate Command to render using PhantomJS
    $cmd = PHANTOMJS_BIN_LOCATION . ' ' . RENDER_UP_JS_PATH . ' ' . $url . ' > ' . $cachedFilePath;
    $r = exec($cmd, $resp, $ret);
    $file = fopen($cachedFilePath, "r") or die("Unable to open file!");
    echo fread($file, filesize($cachedFilePath));
    fclose($file);
    $time = round((microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) * 1000);
    $logger->logInfo("Cached " . $url . " in [" . $time . "ms]");
    writeCSV("CACHE," . $time . "," . $url . "," . $cachedFilePath);
}
function getCacheFileLocation($url)
{
    $urlHash = md5($url);
    $f1 = substr($urlHash, 0, 2);
    $f2 = substr($urlHash, 2, 2);
    $file = substr($urlHash, 4);
    $dir = CACHE_FILE_LOCATION . $f1 . "/" . $f2;
    if (!file_exists($dir)) {
        mkdir($dir, 0777, true);
    }
    return $dir . "/" . $file . ".html";
}
function writeCSV($msg)
{
                    $sessionid = $session_info['ID'];
                    $visit_label = $session_info['Visit_label'];
                    if ($sessionid != null) {
                        $commentid = getCommentIDs($instrument, $visit_label, $sessionid, $candid, $pscid, $subprojectid['subprojectid']);
                        $size = sizeof($commentid);
                        if ($size >= 2) {
                            $commentids[] = $commentid;
                        }
                    }
                }
            }
        }
        if (empty($commentids)) {
            print "No duplicated Data detected for {$instrument} \n";
        }
        writeCSV($commentids, $dataDir, $instrument, "detect_duplicated_commentids");
    }
}
/**
*Get the commentids for the given instrument, candidate and visit_label
*
* @param String $test_name    The instrument been searched
* @param string $visit_label  The VisitLabel Placed in the CSV file
* @param string $sid          The SessionID been searched
* @param string $candid       The candid been searched
* @param string $pscid        The PSCID been searched
* @param string $subprojectid The subprojecitd been searched
* 
* @return array $commentids An array of commentids found
* 
*/