-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListexporterController.php
156 lines (135 loc) · 5.07 KB
/
ListexporterController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Listexporter component controller. This controller is used for demonstration and export
* purposes of the listexporter.
*
* This file is part of the {@link http://amsl.technology amsl} project.
*
* @author Sebastian Nuck
* @copyright Copyright (c) 2015, {@link http://ub.uni-leipzig.de Leipzig University Library}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
class ListexporterController extends OntoWiki_Controller_Component
{
private $resourceQuery;
private $valueQuery;
private $mergedQuery;
public function init()
{
parent::init();
$this->resourceQuery = unserialize(gzinflate($_GET['resourceQuery']));
$this->valueQuery = unserialize(gzinflate($_GET['valueQuery']));
$this->mergedQuery = $this->merge($this->resourceQuery, $this->valueQuery);
}
public function exportAction()
{
$this->view->placeholder('main.window.title')->set('Listexporter');
$this->addModuleContext('main.window.listexporter.export');
OntoWiki::getInstance()->getNavigation()->disableNavigation();
if (isset($_GET['filename'])) {
$filename = $_GET['filename'] . '_' . date("Ymd-His");
} else {
$filename = '_' . date("Ymd-His");
}
$convertUris = true;
if (isset($_GET['convertUris'])) {
$convertUris = false;
}
//query selected model
$result = $this->_owApp->selectedModel->sparqlQuery($this->mergedQuery->getSparql(), array(
'result_format' => 'csv'
));
if ($convertUris) {
$result = $this->enrichWithTitles($result);
}
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout()->disableLayout();
$response = $this->getResponse();
$contentType = 'text/csv';
$filename = "export_$filename.csv";
$response->setHeader('Content-Type', $contentType, true);
$response->setHeader('Content-Disposition', ('filename="' . $filename . '"'));
$response->setBody($result);
return;
}
public function viewAction()
{
$this->view->placeholder('main.window.title')->set('Listexporter');
$this->addModuleContext('main.window.listexporter.export');
OntoWiki::getInstance()->getNavigation()->disableNavigation();
$this->view->resourceQuery = $this->resourceQuery;
$this->view->valueQuery = $this->valueQuery;
$this->view->mergedQuery = $this->mergedQuery;
}
private function merge($resourceQuery, $valueQuery)
{
$mergedQuery = new Erfurt_Sparql_Query2();
// selection variables
foreach ($valueQuery->getProjectionVars() as $value) {
$mergedQuery->addProjectionVar($value);
}
// where clause from resourceQuery
$where = clone $resourceQuery->getWhere();
// + optionals from valueQuery
foreach ($valueQuery->getWhere()->getElements() as $value) {
if ($value instanceof Erfurt_Sparql_Query2_OptionalGraphPattern) {
$where->addElement($value);
}
}
$mergedQuery->setFroms($resourceQuery->getFroms());
$mergedQuery->setWhere($where);
$order = new Erfurt_Sparql_Query2_OrderClause();
$mergedQuery->setOrder($resourceQuery->getOrder());
return $mergedQuery;
}
private function enrichWithTitles($result)
{
$titleHelper = new OntoWiki_Model_TitleHelper();
$lines = explode("\r\n", $result);
$resultWithTitles = null;
foreach ($lines as $line) {
$lineValues = str_getcsv($line);
$rowWithTitle = array();
$isFirstElement = true;
foreach ($lineValues as $value) {
if ($isFirstElement) {
$rowWithTitle[] = $value;
$isFirstElement = false;
} else {
if ($this->isUrl($value)) {
$title = $titleHelper->getTitle($value);
$rowWithTitle[] = $title;
} else {
$rowWithTitle[] = $value;
}
}
}
$resultWithTitles .= $this->getCSV($rowWithTitle);
}
return $resultWithTitles;
}
/**
* outputCSV creates a line of CSV and outputs it to browser
*/
function outputCSV($row) {
$fp = fopen('php://output', 'w'); // this file actual writes to php output
fputcsv($fp, $row);
fclose($fp);
}
/**
* getCSV creates a line of CSV and returns it.
*/
function getCSV($row) {
ob_start(); // buffer the output ...
$this->outputCSV($row);
return ob_get_clean(); // ... then return it as a string!
}
/**
* FInd out if a string is a url.
* @param $text
* @return bool
*/
function isUrl( $text ) {
return filter_var( $text, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) !== false;
}
}