Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
coreation committed Dec 10, 2015
2 parents bcca705 + fed5547 commit 77e6047
Show file tree
Hide file tree
Showing 160 changed files with 8,203 additions and 5,031 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ composer.phar
/app/config/local
/app/config/staging
/app/config/production
/app/config/packages
/bootstrap/compiled.php
/installed/*
!/installed/.gitkeep
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ language: php
services: mysql
php:
- "5.4"
- "5.6"
env:
- DB=mysql
before_script:
- mysql -e 'CREATE DATABASE core_test;'
- sudo hostname test.travis-ci.org
- pecl install dbase
- echo "extension = dbase.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- composer install

script:
Expand Down
94 changes: 94 additions & 0 deletions app/Tdt/Core/Analytics/GaTracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Tdt\Core\Analytics;

/**
* GA Tracker class.
*
* @copyright (C) 2011, 2014 by OKFN Belgium vzw/asbl
* @license AGPLv3
* @author Jan Vansteenlandt <[email protected]>
* @author appreciate.be
*/

class GaTracker implements TrackerInterface
{
private $IGNORE = ['api', 'discovery'];

public function track($request, $tracker_id)
{
$path = $request->path();
$extension = '';

if (strpos($path, '.') !== false) {
$uri_parts = explode('.', $request->path());

$extension = strtolower(array_pop($uri_parts));
$path = implode('', $uri_parts);
} else {
$extension = 'html';
}

// Get some meta-data from the definition to add to the GA as a dimension
$definitions = \App::make('Tdt\Core\Repositories\Interfaces\DefinitionRepositoryInterface');
$definition = $definitions->getByIdentifier($path);

$license = 'Not provided';
$theme = 'Not provided';

if (!empty($definition['rights'])) {
$license = $definition['rights'];
}

if (!empty($definition['theme'])) {
$theme = $definition['theme'];
}

// Get the target audience and category.
$segments = $request->segments();

if (count($segments) >= 2 && !in_array($segments[0], $this->IGNORE)) {
// The URL of the GA
$url = 'http://www.google-analytics.com/collect';

// The version of the Google Analytics Measurement Protocol.
$data['v'] = 1;

// The tracker ID.
$data['tid'] = $tracker_id;

// GA requires a user ID, but since all requests are anonymous
// we generate a unique string to use as ID.
$data['cid'] = sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);

// The type of 'hit', in this case we use pageview.
$data['t'] = 'pageview';

// The url to track, required when using 'pageview' as type.
$data['dp'] = $path;
$data['cd1'] = $extension;
$data['cd2'] = $theme;
$data['cd3'] = $license;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, utf8_encode(http_build_query($data)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
}
}
}
8 changes: 8 additions & 0 deletions app/Tdt/Core/Analytics/TrackerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tdt\Core\Analytics;

interface TrackerInterface
{
public function track($request, $tracker_id);
}
17 changes: 17 additions & 0 deletions app/Tdt/Core/Analytics/TrackerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tdt\Core\Analytics;

use Illuminate\Support\ServiceProvider;

class TrackerServiceProvider extends ServiceProvider
{

public function register()
{
\App::bind(
'Tdt\Core\Analytics\TrackerInterface',
'Tdt\Core\Analytics\GaTracker'
);
}
}
1 change: 0 additions & 1 deletion app/Tdt/Core/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* @license AGPLv3
* @author Jan Vansteenlandt <[email protected]>
*/

abstract class ApiController extends \Controller
{

Expand Down
20 changes: 8 additions & 12 deletions app/Tdt/Core/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ public function handleRequest($uri)
$uri = strtolower(rtrim($uri, '/'));

// Check first segment of the request
switch(\Request::segment(1)){
switch (\Request::segment(1)) {
case 'discovery':
// Discovery document
$controller = 'Tdt\\Core\\Definitions\\DiscoveryController';
break;
case 'api':
switch(\Request::segment(2)){

switch (\Request::segment(2)) {
case 'definitions':
// Definitions request
$controller = 'Tdt\\Core\\Definitions\\DefinitionController';
Expand All @@ -51,6 +50,10 @@ public function handleRequest($uri)
$controller = 'Tdt\\Core\\Definitions\\LanguageController';
$uri = str_replace('api/languages', '', $uri);
break;
case 'geoprojections':
$controller = 'Tdt\\Core\\Definitions\\GeoprojectionController';
$uri = str_replace('api/geoprojections', '', $uri);
break;
case 'licenses':
// Supported licenses request
$controller = 'Tdt\\Core\\Definitions\\LicenseController';
Expand All @@ -71,15 +74,6 @@ public function handleRequest($uri)
break;
}

break;
case 'discovery':
// Discovery document
$controller = 'Tdt\\Core\\Definitions\\DiscoveryController';
break;
case 'spectql':
// SPECTQL request
$uri = str_ireplace('spectql', '', $original_uri);
$controller = 'Tdt\\Core\\Definitions\\SpectqlController';
break;
case '':
// Home URL requests
Expand All @@ -99,6 +93,8 @@ public function handleRequest($uri)
if ($response instanceof \Illuminate\Http\RedirectResponse) {
// Redirect and that's it
return $response;
} else if ($response instanceof \Symfony\Component\HttpFoundation\BinaryFileResponse) {
return $response;
} else {
// Make sure cross origin requests are allowed for GET
$response->header('Access-Control-Allow-Origin', '*');
Expand Down
183 changes: 183 additions & 0 deletions app/Tdt/Core/Commands/DcatLicenses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

namespace Tdt\Core\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

use Tdt\Core\Repositories\Interfaces\DefinitionRepositoryInterface;
use Tdt\Core\Commands\Ie\Definitions;
use Tdt\Core\Commands\Ie\Users;
use Tdt\Core\Commands\Ie\Groups;

class DcatLicenses extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'datatank:licenses';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Seed the licenses for the datasets. By default it will install a set of internationally known licenses.';

/**
* The URI for the default licenses
*
* @var string
*/
protected $licenses_uri = 'https://raw.githubusercontent.com/tdt/licenses/master/';

/**
* The default license
*
* @var string
*/
protected $DEFAULT_LICENSE = 'international_licenses';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* Ask the user which functionality he wants to use (definitions|users)
* then based on the input, proceed with the logic for chosen option.
*
* This option is given as an input argument upon making the command.
*
* @return void
*/
public function fire()
{
$this->seedLicenses();
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('name', InputArgument::OPTIONAL, 'The name of the licenses document hosted on https://github.com/tdt/licenses that should be seeded into the datatank. Default value is international_licenses.', $this->DEFAULT_LICENSE),
);
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
);
}

/**
* Seed the themes
*
* @return @void
*/
private function seedLicenses()
{
$this->info('---- Seeding new licenses ----');

$license_name = $this->argument('name');

$license_uri = $this->licenses_uri . $license_name;

if (substr($license_uri, -5) != '.json') {
$license_uri .= '.json';
}

// Try to get the themes from the ns.thedatatank.com (semantic data)
try {
$this->info('Trying to fetch triples from the uri: ' . $license_uri);

try {
$licenses_graph = \EasyRdf_Graph::newAndLoad($license_uri, 'jsonld');
} catch (\EasyRdf_Http_Exception $ex) {
$this->info('We could not fetch licenses from the URL ' . $license_uri . ', defaulting to ' . $this->DEFAULT_LICENSE . '.');

$licenses_graph = $this->fetchDefaultGraph();
}

if ($licenses_graph->isEmpty()) {
$this->info('We could not fetch licenses from the URL ' . $license_uri . ', defaulting to ' . $this->DEFAULT_LICENSE . '.');

$licenses_graph = $this->fetchDefaultGraph();

} else {
$this->info('Fetched new licenses, removing the old ones.');

// Empty the licenses table
\License::truncate();
}

// Fetch the resources with a skos:conceptScheme relationship
$licenses = $licenses_graph->allOfType('cc:License');

$taxonomy_uris = array();

foreach ($licenses as $license) {
$url = '';
$title = '';
$identifier = '';

$title_resource = $license->getLiteral('dc:title');
$identifier_resource = $license->getLiteral('dc:identifier');

if (!empty($title_resource)) {
$title = $title_resource->getValue();
}

if (!empty($identifier_resource)) {
$identifier = $identifier_resource->getValue();
}

if (!$license->isBNode()) {
$url = $license->getUri();
}

\License::create([
'url' => $url,
'title' => $title,
'license_id' => $identifier
]);

$this->info('Added license "' . $identifier . '" with title "' . $title . '" and URI ' . $url);
}

} catch (EasyRdf_Exception $ex) {
$this->info('An error occurred when we tried to fetch online themes.');
}
}

/**
* Fetch the default licenses
*
* @return \EasyRdf_Graph
*/
private function fetchDefaultGraph()
{
$license_uri = $this->licenses_uri . $this->DEFAULT_LICENSE . '.json';

return \EasyRdf_Graph::newAndLoad($license_uri, 'jsonld');
}
}
2 changes: 1 addition & 1 deletion app/Tdt/Core/Commands/DcatThemes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DcatThemes extends Command
*
* @var string
*/
protected $name = 'datatank:theme';
protected $name = 'datatank:themes';

/**
* The console command description.
Expand Down
Loading

0 comments on commit 77e6047

Please sign in to comment.