Skip to content

Commit

Permalink
Merge pull request #380 from tdt/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
coreation committed Feb 1, 2016
2 parents b79658b + 554615c commit c55ac6b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 51 deletions.
52 changes: 49 additions & 3 deletions app/Tdt/Core/DataControllers/CSVController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Tdt\Core\Pager;
use Tdt\Core\Repositories\Interfaces\TabularColumnsRepositoryInterface;
use Tdt\Core\Repositories\Interfaces\GeoPropertyRepositoryInterface;
use Tdt\Core\Datasets\DatasetController;

ini_set('auto_detect_line_endings', true);

/**
* CSV Controller
Expand Down Expand Up @@ -34,6 +37,15 @@ public function readData($source_definition, $rest_parameters = array())
{
list($limit, $offset) = Pager::calculateLimitAndOffset();

// Get the format, if it's CSV we allow a full read of the datasource without paging limitation
list($uri, $extension) = $this->processURI(\Request::path());

$ignore_paging = false;

if (strtolower($extension) == 'csv') {
$ignore_paging = true;
}

// Disregard the paging when rest parameters are given
if (!empty($rest_parameters)) {
$limit = PHP_INT_MAX;
Expand Down Expand Up @@ -130,7 +142,7 @@ public function readData($source_definition, $rest_parameters = array())
);

if (($handle = fopen($uri, "r", false, stream_context_create($ssl_options))) !== false) {
while (($data = fgetcsv($handle, 2000000, $delimiter)) !== false) {
while (($data = fgetcsv($handle, 0, $delimiter)) !== false) {
if ($total_rows >= $start_row) {
// Create the values array, containing the (aliased) name of the column
// to the value of a the row which $data represents
Expand Down Expand Up @@ -158,12 +170,11 @@ public function readData($source_definition, $rest_parameters = array())

$total_rows++;

if ($total_rows >= 10000) {
if ($total_rows >= 10000 && !$ignore_paging) {
break;
}
}
fclose($handle);

} else {
\App::abort(500, "Cannot retrieve any data from the CSV file on location $uri.");
}
Expand Down Expand Up @@ -199,6 +210,41 @@ private function createValues($columns, $data)
return $result;
}

/**
* Parse the URI and look for the resource name and the format
*
* @param string $uri
*
* @return array
*/
private function processURI($uri)
{
$dot_position = strrpos($uri, '.');

if (!$dot_position) {
return array($uri, null);
}

// If a dot has been found, do a couple
// of checks to find out if it introduces a formatter
$uri_parts = explode('.', $uri);

$possible_extension = strtoupper(array_pop($uri_parts));

$uri = implode('.', $uri_parts);

$formatter_class = 'Tdt\\Core\\Formatters\\' . $possible_extension . 'Formatter';

if (!class_exists($formatter_class)) {
// Re-attach the dot with the latter part of the uri
$uri .= '.' . strtolower($possible_extension);

return array($uri, null);
}

return array($uri, strtolower($possible_extension));
}

/**
* Parse the columns from a CSV file and return them
* Optionally aliases can be given to columns as well as a primary key
Expand Down
26 changes: 14 additions & 12 deletions app/Tdt/Core/Datasets/DatasetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,22 +280,24 @@ private function processURI($uri)
private static function applyRestFilter($data, $rest_params)
{
foreach ($rest_params as $rest_param) {
if (is_object($data) && $key = self::propertyExists($data, $rest_param)) {
$data = $data->$key;
} elseif (is_array($data)) {
if ($key = self::keyExists($data, $rest_param)) {
$data = $data[$key];
} elseif (is_numeric($rest_param)) {
for ($i = 0; $i <= $rest_param; $i++) {
$result = array_shift($data);
}
if (!empty($rest_param)) {
if (is_object($data) && $key = self::propertyExists($data, $rest_param)) {
$data = $data->$key;
} elseif (is_array($data)) {
if ($key = self::keyExists($data, $rest_param)) {
$data = $data[$key];
} elseif (is_numeric($rest_param)) {
for ($i = 0; $i <= $rest_param; $i++) {
$result = array_shift($data);
}

$data = $result;
$data = $result;
} else {
\App::abort(404, "No property ($rest_param) has been found.");
}
} else {
\App::abort(404, "No property ($rest_param) has been found.");
}
} else {
\App::abort(404, "No property ($rest_param) has been found.");
}
}

Expand Down
4 changes: 1 addition & 3 deletions app/Tdt/Core/Repositories/DcatRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public function getDcatDocument(array $definitions, $oldest_definition)
// Create a new EasyRDF graph
$graph = new \EasyRdf_Graph();

\EasyRdf_Namespace::set('adms', 'http://www.w3.org/ns/adms#');

$all_settings = $this->settings->getAll();

$uri = \Request::root();
Expand Down Expand Up @@ -105,7 +103,7 @@ public function getDcatDocument(array $definitions, $oldest_definition)

// Backwards compatibility
if (!empty($definition['contact_point'])) {
$graph->addResource($dataset_uri, 'adms:contactPoint', $definition['contact_point']);
$graph->addResource($dataset_uri, 'dcat:contactPoint', $definition['contact_point']);
}

// Add the publisher resource to the dataset
Expand Down
34 changes: 30 additions & 4 deletions app/Tdt/Core/Validators/CustomValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
*/
class CustomValidator extends \Illuminate\Validation\Validator
{

/**
* Check if the URI can be resolved externally or locally
*/
public function validateUri($attribute, $value, $parameters)
{
try {
if (!filter_var($value, FILTER_VALIDATE_URL) === false) {
$data = $this->getRemoteData($value);
$url_pieces = parse_url($value);

return !empty($data);
if (!filter_var($value, FILTER_VALIDATE_URL) === false && ($url_pieces['scheme'] == 'http' || $url_pieces['scheme'] == 'https')) {
$status = $this->getHeadInfo($value);

return $status == 200;
} else {
$data =@ file_get_contents($value);

Expand All @@ -31,6 +32,31 @@ public function validateUri($attribute, $value, $parameters)
}
}

private function getHeadInfo($uri)
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $uri);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
curl_setopt($c, CURLOPT_TIMEOUT, 60);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($c, CURLOPT_NOBODY, true);

curl_exec($c);
$status = curl_getinfo($c);
curl_close($c);

if (!empty($status['http_code'])) {
return $status['http_code'];
} else {
return 500;
}
}

private function getRemoteData($url)
{
$c = curl_init();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
},
"require": {
"laravel/framework": "4.2.0",
"laravel/framework": "4.2.1",
"phpoffice/phpexcel": "1.8.0",
"phpunit/phpunit" : "4.0.17",
"easyrdf/easyrdf" : "0.9.1",
Expand Down
56 changes: 28 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c55ac6b

Please sign in to comment.