Skip to content

Commit 6e52465

Browse files
committed
Merge pull request #295 from tdt/development
Pull request for v5.6.0
2 parents 7e595c3 + 1b78ed1 commit 6e52465

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3455
-945
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ composer.phar
1515
app/config/workbench.php
1616
public/packages
1717
workbench
18+
node_modules/

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
[![Latest Stable Version](https://poser.pugx.org/tdt/core/version.png)](https://packagist.org/packages/tdt/core)
77
[![Build Status](https://travis-ci.org/tdt/core.png?branch=development)](https://travis-ci.org/tdt/core) [![Dependency Status](https://www.versioneye.com/php/tdt:core/badge.png)](https://www.versioneye.com/php/tdt:core)
88

9-
The DataTank core is the framework in which the main application of The DataTank is built. The DataTank aims at publishing data to URI's in web readable formats. This means that you provide a nice JSON, XML, PHP, ... serialization on a certain URI from which the data resides somewhere in a CSV, XLS, XML, JSON, SHP, ... file.
9+
The DataTank core is the framework in which the main application of The DataTank is built. The DataTank is a web application that publishes data to URIs in web readable formats. This means that you provide a nice JSON, XML, PHP, ... serialization on a certain URI of data that resides somewhere in a CSV, XLS, XML, JSON, SHP, ... file or any other machine readable data container. In short, it provides an instant REST API on top of any machine readable data.
1010

1111
# Read more
1212

1313
If you want to read more about The DataTank project visit our [website](http://thedatatank.com) or take a look at our [documentation](http://docs.thedatatank.com).
1414

15-
The DataTank is free software (AGPL, © 2011,2012 iRail NPO, 2012 OKFN Belgium) to create an API for non-local/dynamic data in no time.
15+
The DataTank is free software (AGPL, © 2011,2012 iRail NPO, 2012 Open Knowledge Belgium) to create an API for data in no time.
1616

17-
Any questions? Add a support issue.
17+
Any questions? Add a support issue!

app/Tdt/Core/BaseController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
/**
66
* The base controller
7+
*
78
* @copyright (C) 2011, 2014 by OKFN Belgium vzw/asbl
89
* @license AGPLv3
910
* @author Michiel Vancoillie <[email protected]>

app/Tdt/Core/DataControllers/CSVController.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function __construct(TabularColumnsRepositoryInterface $tabular_columns,
3232

3333
public function readData($source_definition, $rest_parameters = array())
3434
{
35-
3635
list($limit, $offset) = Pager::calculateLimitAndOffset();
3736

3837
// Disregard the paging when rest parameters are given
@@ -41,6 +40,9 @@ public function readData($source_definition, $rest_parameters = array())
4140
$offset = 0;
4241
}
4342

43+
// Get the current column configuration from the CSV file
44+
$parsed_columns = self::parseColumns($source_definition);
45+
4446
// Check the given URI
4547
if (!empty($source_definition['uri'])) {
4648
$uri = $source_definition['uri'];
@@ -56,6 +58,34 @@ public function readData($source_definition, $rest_parameters = array())
5658
// Get CSV columns
5759
$columns = $this->tabular_columns->getColumns($source_definition['id'], 'CsvDefinition');
5860

61+
// Check if they match with the freshly parsed columns
62+
if (count($parsed_columns) != count($columns)) {
63+
64+
// Save the new config
65+
$this->tabular_columns->deleteBulk($source_definition['id'], 'CsvDefinition');
66+
$this->tabular_columns->storeBulk($source_definition['id'], 'CsvDefinition', $columns);
67+
68+
} else {
69+
foreach ($parsed_columns as $parsed_column) {
70+
71+
$column = array_shift($columns);
72+
73+
foreach ($parsed_column as $key => $val) {
74+
if ($val != $column[$key]) {
75+
76+
// Save the new config
77+
$this->tabular_columns->deleteBulk($source_definition['id'], 'CsvDefinition');
78+
$this->tabular_columns->storeBulk($source_definition['id'], 'CsvDefinition', $columns);
79+
80+
break;
81+
}
82+
}
83+
}
84+
}
85+
86+
// In any case (changed column configuration or not) we can set the columns to the parsed ones
87+
$columns = $parsed_columns;
88+
5989
// Get the geo properties
6090
$geo_properties = $this->geo_properties->getGeoProperties($source_definition['id'], 'CsvDefinition');
6191

@@ -102,11 +132,10 @@ public function readData($source_definition, $rest_parameters = array())
102132

103133
if ($total_rows >= $start_row) {
104134

105-
$num = count($data);
106-
107135
// Create the values array, containing the (aliased) name of the column
108136
// to the value of a the row which $data represents
109137
$values = $this->createValues($columns, $data);
138+
110139
if ($offset <= $hits && $offset + $limit > $hits) {
111140

112141
$obj = new \stdClass();
@@ -128,6 +157,7 @@ public function readData($source_definition, $rest_parameters = array())
128157
}
129158
$hits++;
130159
}
160+
131161
$total_rows++;
132162
}
133163
fclose($handle);

app/Tdt/Core/DataControllers/JSONController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
class JSONController extends ADataController
1818
{
19+
public static function getParameters()
20+
{
21+
return [];
22+
}
1923

2024
public function readData($source_definition, $rest_parameters = array())
2125
{

app/Tdt/Core/DataControllers/JSONLDController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
class JSONLDController extends ADataController
1818
{
19+
public static function getParameters()
20+
{
21+
return [];
22+
}
1923

2024
public function readData($source_definition, $rest_parameters = array())
2125
{
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Tdt\Core\DataControllers;
4+
5+
use Tdt\Core\Datasets\Data;
6+
use Tdt\Core\Pager;
7+
use Tdt\Core\Repositories\Interfaces\TabularColumnsRepositoryInterface;
8+
use Tdt\Core\Repositories\Interfaces\GeoPropertyRepositoryInterface;
9+
use Illuminate\Database\QueryException;
10+
11+
/**
12+
* MySQL controller
13+
*
14+
* @copyright (C) 2011, 2014 by OKFN Belgium vzw/asbl
15+
* @license AGPLv3
16+
* @author Jan Vansteenlandt <[email protected]>
17+
*/
18+
class MYSQLController extends ADataController
19+
{
20+
private $columnsRepo;
21+
22+
public function __construct(TabularColumnsRepositoryInterface $columnsRepo, GeoPropertyRepositoryInterface $geoRepo)
23+
{
24+
$this->columnsRepo = $columnsRepo;
25+
$this->geoRepo = $geoRepo;
26+
}
27+
28+
public function readData($source_definition, $rest_parameters = array())
29+
{
30+
//Check if the query is already paged.
31+
$limitInQuery = false;
32+
33+
if (stripos($source_definition['query'], 'limit')) {
34+
$limitInQuery = true;
35+
} else {
36+
list($limit, $offset) = Pager::calculateLimitAndOffset();
37+
}
38+
39+
// Disregard the paging when rest parameters are given
40+
if (!empty($rest_parameters)) {
41+
$limit = 500;
42+
$offset = 0;
43+
}
44+
45+
// Get the columns from the repository
46+
$columns = $this->columnsRepo->getColumns($source_definition['id'], 'MysqlDefinition');
47+
48+
// Get the geo properties
49+
$geo_properties = $this->geoRepo->getGeoProperties($source_definition['id'], 'MysqlDefinition');
50+
51+
$geo = array();
52+
53+
foreach ($geo_properties as $geo_prop) {
54+
$geo[$geo_prop['property']] = $geo_prop['path'];
55+
}
56+
57+
if (!$columns) {
58+
// 500 error because this shouldn't happen in normal conditions
59+
// Columns are parsed upon adding a CSV resource and are always present
60+
\App::abort(500, "Cannot find the columns of the MySQL table file, this might be due to a corrupted database or a broken configuration.");
61+
}
62+
63+
// Create aliases for the columns
64+
$aliases = $this->columnsRepo->getColumnAliases($source_definition['id'], 'MysqlDefinition');
65+
$pk = null;
66+
67+
foreach ($columns as $column) {
68+
69+
if (!empty($column['is_pk'])) {
70+
$pk = $column['column_name_alias'];
71+
}
72+
}
73+
74+
// Connect to the database
75+
$db_config = array(
76+
'driver' => 'mysql',
77+
'host' => $source_definition['host'],
78+
'database' => $source_definition['database'],
79+
'username' => $source_definition['username'],
80+
'password' => $source_definition['password'],
81+
'charset' => 'utf8',
82+
'collation' => $source_definition['collation'],
83+
);
84+
85+
// Configure a connection
86+
\Config::set('database.connections.mysqltmp', $db_config);
87+
88+
// Make a database connection
89+
$db = \DB::connection('mysqltmp');
90+
91+
try {
92+
93+
$query = $source_definition['query'];
94+
95+
// Get the total amount of records for the query for pagination
96+
preg_match("/select.*?(from.*)/msi", $query, $matches);
97+
98+
if (empty($matches[1])) {
99+
\App::abort(400, 'Failed to make a count statement, make sure the SQL query is valid.');
100+
}
101+
102+
$count_query = 'select count(*) as count ' . $matches[1];
103+
104+
$count_result = $db->select($count_query);
105+
106+
$total_rows = $count_result[0]->count;
107+
108+
if (!$limitInQuery) {
109+
110+
if (!empty($limit)) {
111+
$query .= ' limit ' . $limit;
112+
}
113+
114+
if (!empty($offset)) {
115+
$query .= ' offset ' . $offset;
116+
}
117+
}
118+
119+
$result = $db->select($query);
120+
121+
} catch (QueryException $ex) {
122+
\App::abort(400, "A bad query has been made, make sure all passed statements are SQL friendly. The error message was: " . $ex->getMessage());
123+
}
124+
125+
// Get the paging headers
126+
$paging = Pager::calculatePagingHeaders($limit, $offset, $total_rows);
127+
128+
$data_result = new Data();
129+
$data_result->data = $result;
130+
$data_result->paging = $paging;
131+
$data_result->geo = $geo;
132+
$data_result->preferred_formats = $this->getPreferredFormats();
133+
134+
return $data_result;
135+
}
136+
}

app/Tdt/Core/DataControllers/RDFController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
*/
1515
class RDFController extends ADataController
1616
{
17+
public static function getParameters()
18+
{
19+
return [];
20+
}
1721

1822
public function readData($source_definition, $rest_parameters = array())
1923
{

0 commit comments

Comments
 (0)