class.tx_nG6_eid.php 9.67 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php

// Inclusion des librairies nécessaires
require_once(PATH_t3lib.'class.t3lib_befunc.php');
require_once(PATH_t3lib.'stddb/tables.php');
require_once(t3lib_extMgm::extPath('cms','ext_tables.php'));
require_once(PATH_t3lib.'class.t3lib_db.php');


class tx_nG6_eid {


    /* Méthode principale */
    function main() {

    	/* connect to the databse */
    	tslib_eidtools::connectDB();
    	
    	$html = '';
    	
    	$id = trim(t3lib_div::_GP('id'));
    	$type = trim(t3lib_div::_GP('type'));
    	
    	if ($type == 'project') {    	
			
	    	// First all project runs
	    	$p_runs = $this->get_project_runs($id);
			foreach($p_runs as $run_id => $run_values) {
				$html .= '<li class="jstree-closed" id=\''.$run_id.'\'> <a href=\'#\'>'.$run_values["name"].'</a></li>';
			}
			
	    	// Then all project analysis
	    	$p_analysis = $this->get_project_analysis($id);
			foreach($p_analysis as $analyse_id => $analyse_values) {  
				$html .= '<li id=\''.$analyse_values["directory"].'\' name=\''.$analyse_values["directory"].'\'> <a href=\'#\'>'.$analyse_values["name"].'</a></li>';
			}
			
	    	print $html;
	    	
    	} else if ($type == 'run'){
			
			$r_analysis = $this->get_run_analysis($id);
			
			$html .= '<li id=\'data_'.$id.'\' > <a href=\'#\'>Donn&eacute;es brutes</a></li>';
			foreach($r_analysis as $analyse_id => $analyse_values) {   
				$html .= '<li id=\''.$analyse_values["directory"].'\' name=\''.$analyse_values["directory"].'\'> <a href=\'#\'>'.$analyse_values["name"].'</a></li>';
			}
			
	    	print $html;
    	} else {
    		print 'test';
    	}

    }
    
    function get_run_analysis($run_id) {

		$results = array();
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
						'tx_nG6_analyze.name AS analyze_name, '.
						'tx_nG6_analyze.date AS analyze_date, '.
						'tx_nG6_analyze.description AS analyze_description',
			'FROM' => 'tx_nG6_run_analyze JOIN tx_nG6_analyze ON '.
					  'tx_nG6_analyze.uid=tx_nG6_run_analyze.analyze_id',
			'WHERE' => 'tx_nG6_run_analyze.run_id='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_analyze.name',
			'LIMIT' => ''
		);

		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$analyze_id = $row['analyze_id'];
			if (! isset ($results['analyse_'.$analyze_id]) )
			{				
				$results['analyse_'.$analyze_id] = array( 
					'directory'					=> $row['analyze_directory'], 
					'name' 						=> $row['analyze_name'],
					'date'						=> $row['analyze_date'],
					'description' 				=> $row['analyse_description']				
				);
			}						
		}
		return $results;
    }
    
    function get_project_analysis($project_id) {

		$results = array();
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
						'tx_nG6_analyze.name AS analyze_name, '.
						'tx_nG6_analyze.date AS analyze_date, '.
						'tx_nG6_analyze.description AS analyze_description',
			'FROM' => 'tx_nG6_project_analyze JOIN tx_nG6_analyze ON '.
					  'tx_nG6_analyze.uid=tx_nG6_project_analyze.analyze_id',
			'WHERE' => 'tx_nG6_project_analyze.project_id='.$project_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_analyze.name',
			'LIMIT' => ''
		);

		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$analyze_id = $row['analyze_id'];
			if (! isset ($results['analyse_'.$analyze_id]) )
			{				
				$results['analyse_'.$analyze_id] = array( 
					'directory'					=> $row['analyze_directory'], 
					'name' 						=> $row['analyze_name'],
					'date'						=> $row['analyze_date'],
					'description' 				=> $row['analyse_description']				
				);
			}						
		}
		return $results;
    }
    
    function get_project_runs($project_id) {
		
		$results = array();
		$queryParts = Array( 
			'SELECT' => 'tx_nG6_run.uid AS run_id,'.
					 	'tx_nG6_run.directory AS run_directory,'.
						'tx_nG6_run.description AS run_description, '.
						'tx_nG6_run.name AS run_name, '.
						'tx_nG6_run.nb_sequences, '.
						'tx_nG6_run.date AS run_date',
			'FROM' => 'tx_nG6_project_run JOIN tx_nG6_run ON '.
					  'tx_nG6_run.uid=tx_nG6_project_run.run_id',
			'WHERE' => 'tx_nG6_project_run.project_id='.$project_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_run.date DESC',
			'LIMIT' 	=> ''
		); 

		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$run_id = $row['run_id'];
			if (! isset ($results['run_'.$run_id]) )
			{				
				$results['run_'.$run_id] = array( 
					'directory'					=> $row['run_directory'], 
					'name' 						=> $row['run_name'],
					'nb_sequences' 				=> $row['nb_sequences'],
					'date'						=> $row['run_date'],
					'description' 				=> $row['run_description']				
				);
			}						
		}
		return $results;
    }

	//Return archive's filepath.random_name
	function createRandomArchiveName(){
		$randomKey = $this->createRandomKey(10);
		$pathname = $this->internal['temp_directory'].'/archiveNG6_'.$randomKey;
		// 1 chance over 49**10 is still a risk
		while( file_exists($pathname) )
			$pathname = $this->internal['temp_directory'].'/archiveNG6_'.$randomKey;
		return $pathname;
	}

	//Create a random string for a given length ($amount) 
   	function createRandomKey($amount) {
		// From http://snipplr.com/view/6347/php-random-key-generator/
		$keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   		$randkey = "";
   		for ($i=0; $i<$amount; $i++)
	   		$randkey .= substr($keyset, rand(0, strlen($keyset)-1), 1);
   		return $randkey;
   	}

	//Add a / to a directory pathname 
	function dirSlash($dir){
		if (substr( $dir, -1) == "/")
			return $dir;
		return $dir."/";
	}	

	/**
	 * Archivate data from $directories_src to an archive (structurated as $directories_dest)
	 *
	 * @param	array			$directories_src: list of directories to archivate
	 * @param	array			$directories_dest: list of directories to archivate
	 * @return	string		HTML link to the archive
	 */
	function archiveZip($directories_src , $directories_dest)	
	{		
		$dataDir = $this->internal['data'];
		

		$zip = Array() ; //array of files
		
		##Do an exhaustive list of files from the directories list
		foreach($directories_src as $index => $dir_src)
		{
			$dir_src = $dataDir . $dir_src ;
			$dir_dest = $directories_dest[$index];
			//$zip = Array() ; //array of files
			$subzip = $this->copyDirectoryAndList($dir_src, $dir_dest);		
			$zip = array_merge  ( $zip , $subzip );	
		}
		if( count($zip)==0 ){
			return "\n\n".'No file to zip';
		}							

		//TODO: chunk into small buffers (about 100MB)   
      //change cwd to the $this->internal['archive_root']
      $normal_cwd = getcwd();
      $this->internal['archive_root'] = $this->dirSlash($this->internal['archive_root']);
      chdir(dirname($this->internal['archive_root'])); 
		//write out zipped file 
		switch($this->internal["zip_option"]) 
		{
			case 'zip':		//use zip.lib.php
				foreach($zip as $key => $file){ #remove path above the archive
					$zip[$key] = substr( $file, strlen("fileadmin/temp/") , strlen($file));
				}			
				
				$outfile = basename($this->internal['archive_root']).'.zip'; 
				## calls to zip.lib.php library
				$ziper = new zipfile();
				$ziper->addFiles($zip);  //array of files
				$ziper->output($outfile);
				break;
		
			case 'tar.gz': //system call
				$archive_name = basename($this->internal['archive_root']);			
				$outfile = $archive_name.".tar";
				system("tar -Pcf ".$outfile." ".$archive_name." --remove-files");  
				system("gzip ".$outfile);	
				$outfile .= '.gz';	
				system("rm -rf ".$archive_name); 
				break;
		}
		//restore cwd 
		chdir($normal_cwd);
		unset($zip); //free memory //target="_blank" 
		return "\n\n".'<a id="download_link" href="'.$this->internal['temp_directory'].'/'.$outfile.'" > '.basename($outfile).'</a>'; 
	} 	 

	/**
	 * Recursively copy the content of $dir_src into $dir_dest
	 *
	 * @param	string		$dir_src: name of the source directory
	 * @param	string		$dir_dest: name of the destination directory
	 * @return	None
	 */
	function copyDirectoryAndList($dir_src, $dir_dest)	
	{
		$zip = Array();
		$dir_dest = $this->dirSlash($dir_dest);
		$dir_src = $this->dirSlash($dir_src);	 
		if (! file_exists($dir_src)) {
			return $zip; //nothing to do
		}
			
		//Recursively build destination structure
		if (! file_exists($dir_dest))
			mkdir($dir_dest, 0755, true);
		
		$iterator = new DirectoryIterator($dir_src);
		foreach ($iterator as $fileinfo) 
		{ 			
			$filename = $fileinfo->getFilename() ;			
			$isNotRecursiveLink = ($filename!="." and $filename!="..") ;	
			$isNotCrap = ($isNotRecursiveLink and $filename!="index.html" and $filename!="run.cfg" );
			if ($fileinfo->isDir() and $isNotCrap)
			{  // get recursively subdirectories files
			
				$dirpathname =  $this->dirSlash($fileinfo->getPathname());
				// then actualize the list of file to zip
				$subzip = $this->copyDirectoryAndList( $dirpathname , $dir_dest.$fileinfo->getFilename() ) ;
				$zip = array_merge  ( $zip , $subzip );
			}		
			elseif ($fileinfo->isFile() and $isNotCrap) 
			{  // copy file in new structure				
				$pathname_src = $fileinfo->getPathname(); 
		    	$pathname_dest = $dir_dest . $fileinfo->getFilename(); 
		    	copy( $pathname_src , $pathname_dest );	
	    	
		    	// add to zip  
		    	$zip[] = $pathname_dest;
			}    
		} 

		return $zip; //Done		
	}
    
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_eid.php']) {
    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_eid.php']);
}


// instanciation
$SOBE = t3lib_div::makeInstance('tx_nG6_eid');
$SOBE->main();

?>