Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions SQL/0000-00-00-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,28 @@ LOCK TABLES `query_gui_stored_queries` WRITE;
/*!40000 ALTER TABLE `query_gui_stored_queries` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `query_gui_user_files`
--
DROP TABLE IF EXISTS `query_gui_user_files`;
CREATE TABLE query_gui_user_files (
UserFileID integer auto_increment primary key,
UserID integer REFERENCES users(ID),
filename varchar(255),
downloadDate timestamp DEFAULT CURRENT_TIMESTAMP,
md5sum varchar(32),
status enum('ready', 'packaging', 'expired')
);

--
-- Dumping data for table `query_gui_user_files`
--

LOCK TABLES `query_gui_user_files` WRITE;
/*!40000 ALTER TABLE `query_gui_user_files` DISABLE KEYS */;
/*!40000 ALTER TABLE `query_gui_user_files` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `session`
--
Expand Down
8 changes: 8 additions & 0 deletions SQL/2012-02-14-Query_GUI_Download.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE query_gui_user_files (
UserFileID integer auto_increment primary key,
UserID integer REFERENCES users(ID),
filename varchar(255),
downloadDate timestamp DEFAULT CURRENT_TIMESTAMP,
md5sum varchar(32),
status enum('ready', 'packaging', 'expired')
);
1 change: 1 addition & 0 deletions htdocs/downloads
11 changes: 9 additions & 2 deletions htdocs/dqgui_step3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

function executeQuery(){
function executeQuery(sendTo){
if(sendTo == 'download') {
var oCbrainField=IFrameDoc.getElementById("download");
oCbrainField.value="execute";
var m = document.getElementById("message");
m.innerHTML = "Packaging your files. They'll be ready to download <a href=\"main.php?test_name=download_files\">here</a> in a few minutes.";
}

var oQueryField=IFrameDoc.getElementById("queryData");
var oModeField=IFrameDoc.getElementById("mode");
oModeField.value="executeQuery";
Expand Down Expand Up @@ -65,4 +72,4 @@ function querySaved(){

function doNothing(){
return true;
}
}
28 changes: 23 additions & 5 deletions htdocs/mri/jiv/get_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,28 @@
if ($blessPass) {
$imagePath = $config->getSetting('imagePath');
$imageFile = $imagePath."/".$_GET['file'];
if(!file_exists($imageFile)) exit;
$pf = fopen($imageFile, 'r');
fpassthru($pf);
fclose($pf);
if(file_exists($imageFile)) {
$pf = fopen($imageFile, 'r');
fpassthru($pf);
fclose($pf);
}
} else {
// If the file isn't blessed because it's the wrong type or starts with a /,
// it's still available if it's under DownloadPath. We just check to make sure it doesn't
// have ".." in it so that the user can't escape DownloadPath.
$downloadPath = $config->getSetting('DownloadPath');
$downloadFile = $downloadPath.$_GET['file'];
if(file_exists($downloadFile) && strrpos($_GET['file'], '..') === FALSE) {
header("Content-type: application/x-minc");
header("Content-Disposition: attachment; filename=\"".basename($_GET['file'])."\"");

$pf = fopen($downloadFile, 'r');
fpassthru($pf);
fclose($pf);
exit;
} else {
print "File not found\n";
}
}

function blessPassThrough($toBless) {
Expand All @@ -42,7 +60,7 @@ function blessPassThrough($toBless) {
return false;
}

if (strrpos($toBless, '.jpg') || strrpos($toBless, '.raw_byte.gz') || strrpos($toBless, '.header')) {
if (strrpos($toBless, '.jpg') || strrpos($toBless, '.raw_byte.gz') || strrpos($toBless, '.header') || strrpos($toBless, '.mnc')) {
return true;
}

Expand Down
13 changes: 12 additions & 1 deletion htdocs/query_gui.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,18 @@
</select>
</td></tr>
<tr>
<td><input type='button' value='Click here for your results' onclick='executeQuery()'></td></tr>
<td>
<input type='button' value='Click here for your results' onclick='executeQuery()'>
<?
$user = User::singleton();
if($user->hasPermission('download_files')) {
?>
<input type='button' value='Click here to download files' onclick="executeQuery('download')"><span id="message">&nbsp;</span>
<?
}
?>
</td>
</tr>
</table>
<p>
<div id='thang'></div>
Expand Down
49 changes: 45 additions & 4 deletions htdocs/query_gui_data_download.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
$config =& NDB_Config::singleton();
$css = $config->getSetting('css');
$studyTitle = $config->getSetting('title');
$downloadPath= $config->getSetting('DownloadPath');
print "Download Path: $downloadPath";

$query = "";

if(!empty($_GET['queryID'])) {

// get the query
$query = $DB->selectOne("SELECT query FROM query_gui_downloadable_queries WHERE queryID='$_GET[queryID]'");
$format = $_GET['format'];

// run the query
$results = array();
Expand All @@ -40,11 +42,29 @@
exit;
}
// add the description as the second row
foreach(array_keys($results[0]) AS $key) {
$desc[$key] = $DB->selectOne("SELECT Description FROM parameter_type WHERE Name='$key'");
$files_cols = array();
foreach(array_keys($results[0]) AS $i => $key) {
$parameter_type = $DB->pselectRow("SELECT Description, IsFile FROM parameter_type WHERE Name=:key", array('key' => $key));
$desc[$key] = $parameter_type['Description'];
if($parameter_type['IsFile']) {
$files_cols[] = $key;
}
}

$newResults[0] = $desc;
$files = array();
for($row=0; $row < count($results); $row++) {
foreach($files_cols as $col) {
if(!empty($results[$row][$col]) && ($format=='html' || $format=='download_files')) {
$file = $results[$row][$col];
if($format == 'html') {
$results[$row][$col] = "<a href=\"mri/jiv/get_file.php?file=$file\">$file</a>";
} else if($format == 'download_files') {
$files[] = $file;
}
}
}
}
for($i=0; $i < count($results); $i++) {
$newResults[$i+1] = $results[$i];
}
Expand All @@ -62,7 +82,13 @@
}

// build the file buffer
list($buffer, $format) = buildFileBuffer($results, $_GET['format'], $css, $studyTitle);

$buffer = "Format: $format";
if($format == 'download_files') {
list($buffer, $format) = buildFileBuffer($files, $_GET['format'], $css, $studyTitle);
} else {
list($buffer, $format) = buildFileBuffer($results, $_GET['format'], $css, $studyTitle);
}
$filename = "requested_data.".time().".$format";

// set the headers
Expand All @@ -84,6 +110,21 @@

function buildFileBuffer($data, $format, $css, $studyTitle) {
switch($format) {
case 'download_files':
$buffer = '';
foreach($data as $file) {
global $downloadPath;
print "downloadPath: $downloadPath";
chdir($downloadPath);
$file = "./$file";
//print $file;
if(file_exists($file)) {
`tar rvf /var/www/neurodb/htdocs/foo.tar $file`;
}
$buffer .= "$file\n";
}
$format = 'txt';
break;
case 'xls':
$format = 'xls';
require_once 'Spreadsheet/Excel/Writer.php';
Expand Down
49 changes: 49 additions & 0 deletions htdocs/query_gui_data_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@
// reference id, and send that to a popup
$DB->insert("query_gui_downloadable_queries", array('query'=>$query));
$queryID = $DB->lastInsertID;
$download = $_POST['download'];
if($download === 'execute') {
SendFilesToPackage($querySegments['selected_fields'], $query);
}

$script .= "window.open('query_gui_data_download.php?queryID=$queryID&format=$_REQUEST[outputFormat]', 'query_download_window');";

Expand All @@ -293,6 +297,50 @@

}

function getFiles($cols, $query, $timestamp, $prefix="cbrain") {
$DB = Database::singleton();
$fileCols= array();
$lines=explode("\n",trim($cols));
foreach($lines as $field) {
$fileCol = $DB->pselectRow("SELECT IsFile, Name FROM parameter_type WHERE ParameterTypeID=:ptid", array("ptid" => $field));
if($fileCol['IsFile'] == true) {
$fileCols[] = ParameterTypeIDToFieldName($field);
}
}

// Strip off everything before from from the query that was generated above, then just re-add the file fields
// as determined above
$new_query = "SELECT " . join(',', $fileCols) . " " . substr($query, strpos($query, "FROM"));
$DB->select($new_query, $results);
$input = "";
$fp = fopen("/tmp/$prefix.$timestamp.txt", 'w');
foreach ($results as $row) {
foreach ($row as $key => $val) {
if(!empty($val)) {
fwrite($fp, $val . "\n");
$input .= $val . "\n";
}
}
}
fclose($fp);
return "/tmp/$prefix.$timestamp.txt";
}

function SendFilesToPackage($cols, $query) {
$DB = Database::singleton();
$user = User::singleton();
$timestamp = time();
$filelist = getFiles($cols,$query,$timestamp, "download");
$user = User::singleton();
chdir("../tools");
$cmd = sprintf("cat $filelist | ./%s %s.$timestamp > %s 2>&1 & echo $! >> %s", "package_files.sh", $user->getUsername(), "../logs/download.$timestamp", $filelist);
exec($cmd);
$cmd = "rm -f $filelist";
exec($cmd);
$DB->insert("query_gui_user_files", array("UserID" => $user->getData("ID"),
"filename" => $user->getUsername() . ".$timestamp.tar.gz"
));
}
function printArray($array, $indent=""){
while(list($key,$val)=each($array)){
if(is_array($val)){
Expand Down Expand Up @@ -427,6 +475,7 @@ function treeToExpression(&$expression, &$tree, &$conditionals, $parentID=null)
<form id='saveQueryForm' method='POST' action='query_gui_data_loader.php'>
<textarea id='queryData' name='queryData'><?=$_POST['queryData']?></textarea>
<input type='text' id='mode' name='mode'>
<input type='text' id='download' name='download' />
<input type='text' id='outputFormat' name='outputFormat'>
<input type='submit'>
</form>
Expand Down
111 changes: 111 additions & 0 deletions php/libraries/NDB_Menu_Filter_download_files.class.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?
require_once 'NDB_Menu_Filter.class.inc';

class NDB_Menu_Filter_download_files extends NDB_Menu_Filter
{
function _hasAccess()
{
$user =& User::singleton();
if (PEAR::isError($user)) {
return PEAR::raiseError("User Error: " .$user->getMessage());
}
return $user->hasPermission('download_files');
}

function _setupVariables()
{
$user =& User::singleton();

$this->query = " FROM query_gui_user_files f WHERE UserID=" . $user->getData("ID");

$this->columns = array('f.filename as File', 'f.downloadDate as QueryTime', 'f.md5sum as MD5', 'f.status as Status');
$this->order_by = 'downloadDate DESC';
$this->headers = array('Filename',
'QueryTime',
'MD5',
'Status'
);
$this->validFilters = array('f.filename', 'f.status');
$this->formToFilter = array (
'Filename' => 'f.filename'
);
}

function _setFilterForm()
{
$this->form->addElement('text', 'Filename', 'Filename:', array("size"=>9, "maxlength"=>11));
}

function _setDataTableRows($count)
{
$config = NDB_Config::singleton();
$db = Database::singleton();
$paths = $config->getSetting('paths');
$base = $paths['base'];
// print out
$x = 0;
foreach ($this->list as $item) {
//count column
$this->tpl_data['items'][$x][0]['value'] = $x + $count;

$file = false;
$md5 = false;

//print out data rows
$i = 1;
foreach ($item as $key => $val) {
$this->tpl_data['items'][$x][$i]['name'] = $key;
switch($key) {
case 'File': $file = $val; break;
case 'MD5': $md5 = $val; break;
case 'Status':
if($file && file_exists("$base/userdownloads/$file")) {
if(!$md5) {
$md5 = md5_file("$base/userdownloads/$file");
// We're in the Status, MD5 was the previous column
$this->tpl_data['items'][$x][$i-1]['value'] = $md5;
$db->update("query_gui_user_files", array("status" => 'ready', 'md5sum' => $md5), array("filename" => $file));

}
$val = 'ready';
} else {
// If we're in this block, it means the file doesn't exist.
// If there's no MD5 sum calculated yet, the file was never there. So it's packaging
// If there is an MD5 sum but the file doesn't exist, it was deleted from the
// file system for space reasons, and it's "expired" for the user

if(!$val || $val == 'ready') {
if(!$md5) {
$db->update("query_gui_user_files", array("status" => 'packaging'), array("filename" => $file));
$val = 'packaging';
} else {
$db->update("query_gui_user_files", array("status" => 'expired'), array("filename" => $file));
$val = 'expired';

}
}
}
break;
}

$this->tpl_data['items'][$x][$i]['value'] = $val;
$i++;
}
/*
$this->tpl_data['items'][$x][$i]['name'] = 'Status';
if($file && file_exists("$base/userdownloads/$file")) {
$this->tpl_data['items'][$x][$i]['value'] = 'Ready for download';
}
} else {
$this->tpl_data['items'][$x][$i]['value'] = 'Packaging files';
$db->update("query_gui_user_files", array("status" => 'packaging'), array("filename" => $file));
}
*/

$x++;
}

return true;
}

}
Loading