/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1 by Suren A. Chilingaryan
Initial import
1
<?php
2
114 by Suren A. Chilingaryan
Massive rewrite of DRAW (unfinished): multiple groups and axis
3
require($ADEI_ROOTDIR . "classes/stream/stream.php");
1 by Suren A. Chilingaryan
Initial import
4
5
class EXPORT {
6
 var $req;
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
7
 var $dm;
8
 var $dmcb;		
9
1 by Suren A. Chilingaryan
Initial import
10
 var $format;
11
 var $multimode;	// STREAM is used for joining groups
12
13
 var $multigroup;	// Multiple groups expected
14
 var $multirdr;		// Multiple readers expected
40 by Suren A. Chilingaryan
Resampling, RAWPoint limits, READERTime reorganization, few bugfixes
15
 
16
 var $resample;		// Resampling
1 by Suren A. Chilingaryan
Initial import
17
18
 var $output;
19
 var $stream;
20
 var $handler;
21
22
 var $stream_args;
23
 var $expected_groups; 
24
 
25
 var $specials;
26
27
 var $filename;
28
502 by Suren A. Chilingaryan
Support real-time export mode for status displays
29
 var $rt_mode;          // Varios optimizations to reduce latency (mainly for status displays)
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
30
 var $cache_mode;	// Get data out of cache
31
 
195 by Suren A. Chilingaryan
Image Export by Arsen Sogomonyan
32
 var $export_data;
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
33
 
1 by Suren A. Chilingaryan
Initial import
34
 const MASK_STANDARD = 0;
38 by Suren A. Chilingaryan
MultiMask export option is replaced with LogGroup export option
35
 const MASK_GROUP = 1;
1 by Suren A. Chilingaryan
Initial import
36
 const MASK_SOURCE = 2;
37
 const MASK_COMPLETE = 3;
502 by Suren A. Chilingaryan
Support real-time export mode for status displays
38
39
 const RT_SKIP_HEADERS = 1;
40
 const RT_FULL = 0xFFFF;
1 by Suren A. Chilingaryan
Initial import
41
   
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
42
 function __construct(DATARequest $props = NULL, STREAMObjectInterface $h = NULL, &$format = NULL, DOWNLOADMANAGER $dm = NULL) {
369.1.2 by Artur Reymers
Fix: support local handlers
43
    global $ADEI_SETUP;
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
44
    global $TMP_PATH;      
99 by Suren A. Chilingaryan
Fixes: detecting request properties, textual interval parsing
45
    if ($props) $this->req = $props;
1 by Suren A. Chilingaryan
Initial import
46
    else $this->req = new DATARequest();
47
    
48
    if ($format) $this->format = &$format;
49
    else $this->format = $this->req->GetFormatInfo();
50
51
    if ($this->format['handler']) {
369.1.2 by Artur Reymers
Fix: support local handlers
52
	if (file_exists("setups/$ADEI_SETUP/classes/handlers/".strtolower($this->format['handler']).'.php')) {
53
	    if (!include_once("setups/$ADEI_SETUP/classes/handlers/".strtolower($this->format['handler']).'.php'))
54
		throw new ADEIException(translate("Unsupported data handler is configured: \"%s\"", $format['handler']));
55
	} else {
56
	    if (!include_once("handlers/" . strtolower($this->format['handler']) . '.php'))
1 by Suren A. Chilingaryan
Initial import
57
		throw new ADEIException(translate("Unsupported data handler is configured: \"%s\"", $format['handler']));
58
	}
59
	$handler = array(
60
	    "class" => $this->format['handler'] . "Handler"
61
	);
62
    } else {
63
	$handler = array(
64
	    "class" => "CSVHandler"
65
	);
66
    }
67
    
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
68
    if($dm){
69
      $this->dm = $dm;
70
      $this->dmcb = array($this->dm,"DlmanagerUpdate");
71
    }       
72
      
1 by Suren A. Chilingaryan
Initial import
73
    $this->handler = new $handler["class"]($this->format);
195 by Suren A. Chilingaryan
Image Export by Arsen Sogomonyan
74
    $this->export_data = ! $this->handler->data_not_needed;
127 by Suren A. Chilingaryan
Support for complex groups in Export
75
76
    if (!$this->handler->multigroup) {
77
	switch ($this->req->props['mask_mode']) {
78
	    case EXPORT::MASK_COMPLETE:
79
	    case EXPORT::MASK_SOURCE:
80
		$this->multimode = true; //!$this->handler->multigroup;
81
	    break;
82
	    case EXPORT::MASK_STANDARD:
83
	    case EXPORT::MASK_GROUP:
84
		$this->multimode = false;
85
		if ($this->req->IsVirtual()) {
86
		    $reader = $this->req->CreateReader();
87
		    $grp = $reader->CreateGroup();
88
		    if ($grp->IsComplex()) {
89
			if ($this->req->props['mask_mode']==EXPORT::MASK_STANDARD) $mask = $reader->CreateMask();
90
			else $mask = NULL;
91
			$list = $reader->CreateRequestSet($grp, $mask);
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
92
			if ($list->GetSize() != 1) {
93
			  $this->multimode = true;
94
			  if($this->dm) $this->dm->UpdateFilesRemaining($this->req->GetProp('dl_id'), $list->GetSize());
95
			}
127 by Suren A. Chilingaryan
Support for complex groups in Export
96
			unset($list);
97
98
		    }
99
		    unset($reader);
100
		}
101
	    break;
102
	    default:
103
		$this->multimode = true;
104
	}
1 by Suren A. Chilingaryan
Initial import
105
    } else $this->multimode = false;
40 by Suren A. Chilingaryan
Resampling, RAWPoint limits, READERTime reorganization, few bugfixes
106
    
107
    if (isset($this->req->props['resample'])) {
108
	$this->resample = $this->req->props['resample'];
109
    } else {
110
	$this->resample = 0;
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
111
    }      
112
 
113
    if($this->dm) $this->output = $this->dm->GetDmOutput($this->multimode, $this->handler->GetExtension(), $this->req);     
114
    else $this->output = $h;
115
    
1 by Suren A. Chilingaryan
Initial import
116
117
    if (($this->format['filter'])||($this->multimode)) {
118
	$this->stream = new STREAM($this->format, $this->output, ($this->multimode?STREAM::MULTIMODE:0)|($this->handler->filewriter?STREAM::FILESTART:0));
119
	if ($this->stream->filereader) $this->handler->RequestFilemode();
120
    } else {
140 by Suren A. Chilingaryan
STREAM interface fixes
121
	$this->stream = $this->output;
1 by Suren A. Chilingaryan
Initial import
122
    }
123
124
    if ($this->stream) {
125
	$this->handler->SetOutput($this->stream);
126
    }
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
127
    
128
    $this->cache_mode = false;
502 by Suren A. Chilingaryan
Support real-time export mode for status displays
129
    $this->rt_mode = 0;
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
130
 }
131
132
 function SetCacheMode($mode = true) {
133
    $this->cache_mode = $mode;
1 by Suren A. Chilingaryan
Initial import
134
 }
502 by Suren A. Chilingaryan
Support real-time export mode for status displays
135
 
136
 function SetRealTimeMode($mode = EXPORT::RT_FULL) {
137
    $this->rt_mode = $mode;
138
 }
1 by Suren A. Chilingaryan
Initial import
139
140
 function CheckMode() {
141
    if ((!$this->multimode)&&(!$this->handler->multigroup)) {
142
	if ((!$this->stream)||(!($this->stream instanceof STREAM))||(!$this->stream->joiner)) {
143
	    unset($this->stream);
144
	    $this->stream = new STREAM($this->format, $this->output, STREAM::MULTIMODE|($this->handler->filewriter?STREAM::FILESTART:0));
145
	    if ($this->stream->filereader) $this->handler->RequestFilemode();
146
	    $this->handler->SetOutput($this->stream);
147
148
	    if (!$this->stream->joiner) {
149
		throw new ADEIException(translate("The attempt to create joining STREAM is failed"));
150
	    }
151
152
	    $this->multimode = true;
153
	}
154
    }
155
 }
156
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
157
 private function CreateReader(SOURCERequest $req = NULL) {
158
    if (!$req) $req = $this->req;
159
    if ($this->cache_mode) $rdr = $req->CreateCacheReader();
160
    else $rdr = $req->CreateReader();
161
    return $rdr;
162
 }
195 by Suren A. Chilingaryan
Image Export by Arsen Sogomonyan
163
198 by Suren A. Chilingaryan
Update of setups
164
 function ExportNothing() {    
195 by Suren A. Chilingaryan
Image Export by Arsen Sogomonyan
165
	$rdr = $this->CreateReader();    
166
	$grp = $rdr->CreateGroup();    
167
	$msk = $rdr->CreateMask($grp);    
168
	$ivl = $rdr->CreateInterval($grp);        
169
170
	$this->Start($rdr,$grp,$msk,$ivl);    
171
	$this->SendHeaders($rdr, $grp, $msk, $ivl);    
172
	$this->handler->Start(0);    
173
	$this->handler->End();    
174
	$this->End(); 
198 by Suren A. Chilingaryan
Update of setups
175
 }
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
176
 
1 by Suren A. Chilingaryan
Initial import
177
 function Export() {
195 by Suren A. Chilingaryan
Image Export by Arsen Sogomonyan
178
    if (!$this->export_data)        
179
	return $this->ExportNothing();
198 by Suren A. Chilingaryan
Update of setups
180
127 by Suren A. Chilingaryan
Support for complex groups in Export
181
    if ($this->req->IsVirtual()) $virtual_mode = true;
182
    else $virtual_mode = false;
183
    
38 by Suren A. Chilingaryan
MultiMask export option is replaced with LogGroup export option
184
    switch($this->req->props['mask_mode']) {
185
	case EXPORT::MASK_STANDARD:
186
	    $msk = true;
127 by Suren A. Chilingaryan
Support for complex groups in Export
187
	    $grp = true;
38 by Suren A. Chilingaryan
MultiMask export option is replaced with LogGroup export option
188
	break;
189
	case EXPORT::MASK_GROUP:
190
	    $msk = NULL;
127 by Suren A. Chilingaryan
Support for complex groups in Export
191
	    $grp = true;
38 by Suren A. Chilingaryan
MultiMask export option is replaced with LogGroup export option
192
	break;
193
	case EXPORT::MASK_SOURCE:
127 by Suren A. Chilingaryan
Support for complex groups in Export
194
	    if ($virtual_mode) {
195
		$msk = NULL;
196
		$grp = false;
197
	    } else {
198
		return $this->ExportSource();
199
	    }
200
	break;
38 by Suren A. Chilingaryan
MultiMask export option is replaced with LogGroup export option
201
	case EXPORT::MASK_COMPLETE:
202
	    return $this->ExportAll();
203
	default:
204
	    throw new ADEIException(translate("Unsupported mask mode (%u)", $this->req->props['mask_mode']));
1 by Suren A. Chilingaryan
Initial import
205
    }
206
    
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
207
    $rdr = $this->CreateReader();
127 by Suren A. Chilingaryan
Support for complex groups in Export
208
    if ($grp) $grp = $rdr->CreateGroup();
209
    else {
210
	$grp = $rdr->CreateJointGroup();
211
    }
212
    if ($msk) $msk = $rdr->CreateMask($grp);
213
214
    if ($virtual_mode) {
215
	return $this->ExportVirtualGroup($rdr, $grp, $msk);
216
    }
217
    
1 by Suren A. Chilingaryan
Initial import
218
    $ivl = $rdr->CreateInterval($grp);
219
220
    $this->multigroup = false;
221
    $this->multirdr = false;
222
        
223
    $this->Start($rdr, $grp, $msk, $ivl, 1);
224
    $this->SendHeaders($rdr, $grp, $msk, $ivl);
225
    $this->ExportGroup($this->req, $rdr, $grp, $ivl, $msk);
226
    $this->End();
227
 }
228
229
 function ExportSource() {
230
    $this->CheckMode();
231
38 by Suren A. Chilingaryan
MultiMask export option is replaced with LogGroup export option
232
    $msk = NULL;
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
233
    $rdr = $this->CreateReader();
1 by Suren A. Chilingaryan
Initial import
234
235
    $this->multigroup = true;
236
    $this->multirdr = false;
237
238
    $this->Start($rdr, NULL, $msk, NULL);
239
    $this->SendHeaders($rdr, NULL, $msk, NULL);
240
241
    $groups = $rdr->GetGroups();
242
    foreach ($groups as &$grp) {
243
	$ivl = $rdr->CreateInterval($grp);
244
245
	$this->ExportGroup($this->req, $rdr, $grp, $ivl, $msk);
246
    }
247
248
    $this->End();
249
 }
250
 
251
 function ExportAll() {
252
    $this->CheckMode();
253
    
38 by Suren A. Chilingaryan
MultiMask export option is replaced with LogGroup export option
254
    $msk = NULL;
1 by Suren A. Chilingaryan
Initial import
255
256
    $this->multigroup = true;
257
    $this->multirdr = true;
258
259
    $this->Start(NULL, NULL, $msk, NULL);
260
    $this->SendHeaders(NULL, NULL, $msk, NULL);
261
35 by Suren A. Chilingaryan
Minor refactoring: REQUEST/READER/CACHE listing flags, REQUEST listing functions, eliminating FETCH_BOTH fetches in DATABASE, support for partial menu generation
262
    $list = $this->req->GetSources(REQUEST::LIST_ALL);
1 by Suren A. Chilingaryan
Initial import
263
    foreach ($list as $sreq) {
264
	try {
265
	    $dreq = $sreq->CreateDataRequest();
266
	} catch (ADEIException $e) {
267
	    $dreq = &$sreq;
268
	}
269
125 by Suren A. Chilingaryan
Support CACHEReader creation on virtual groups
270
    	$rdr = $this->CreateReader($dreq);
1 by Suren A. Chilingaryan
Initial import
271
272
	$groups = $rdr->GetGroups();
273
	foreach ($groups as &$grp) {
274
	    $ivl = $rdr->CreateInterval($grp);	    
275
	    
276
	    $this->ExportGroup($sreq, $rdr, $grp, $ivl, $msk);
277
	}
278
    }
279
280
    $this->End();
281
 }
282
127 by Suren A. Chilingaryan
Support for complex groups in Export
283
 function ExportVirtualGroup(READER $rdr, LOGGROUP $grp, MASK $mask = NULL) {
284
    $this->multigroup = true;
285
    $this->multirdr = true;
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
286
  
127 by Suren A. Chilingaryan
Support for complex groups in Export
287
    if (!$mask) $mask = new MASK($minfo = array());
288
    
289
    $this->Start($rdr, $grp, $mask, NULL);
290
    $this->SendHeaders($rdr, $grp, $mask, NULL);
291
292
    $list = $rdr->CreateRequestSet($grp, $mask, "DATARequest");
293
    foreach ($list as $req) {
502 by Suren A. Chilingaryan
Support real-time export mode for status displays
294
	$reader = $this->CreateReader($req);
127 by Suren A. Chilingaryan
Support for complex groups in Export
295
	$group = $reader->CreateGroup();
296
	$ivl = $reader->CreateInterval($group);
297
	$msk = $reader->CreateMask();
155 by Suren A. Chilingaryan
Fix group names in export of virtualgroups
298
299
	$title = $this->GetGroupName(
300
	    $reader->GetGroupServerID($group), 
301
	    $reader->GetGroupDatabaseName($group),
302
	    $reader->GetGroupID($group),
303
	    $msk?$msk->name:""
304
	);
305
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
306
	$this->ExportGroup($req, $reader, $group, $ivl, $msk, $title);	
307
	
127 by Suren A. Chilingaryan
Support for complex groups in Export
308
    }
309
    $this->End();
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
310
    
311
    
127 by Suren A. Chilingaryan
Support for complex groups in Export
312
 }
313
314
315
1 by Suren A. Chilingaryan
Initial import
316
 function Start(READER $rdr = NULL, LOGGROUP $grp = NULL, MASK $msk = NULL, INTERVAL $ivl = NULL, $expected_groups = 0) {
317
    $this->filename = false;
318
    
319
    if ($this->stream) {
320
	$this->specials = $this->stream->GetSpecials();
321
	
322
	$this->stream_args = array(
323
	    "expected_blocks" => $expected_groups,
324
	    "extension" => $this->handler->GetExtension()
325
	);
326
	
327
	$this->stream->Open($this->stream_args);
328
329
	$this->expected_groups = $expected_groups;
330
	if ($this->handler->multigroup) {
331
	    $this->filename = $this->GetName($rdr, $grp, $msk, $ivl);
332
333
	    $stream_args = $this->stream_args;
334
	    $stream_args["block_number"] = 0;
335
	    $stream_args["block_title"] = $this->filename;
336
337
	    $this->stream->BlockStart($stream_args);
338
	}
339
    } else {
340
	$this->specials = array();
341
    }
342
    
343
    $this->handler->SequenceStart();
344
 }
345
346
 function End() {
347
    $this->handler->SequenceEnd();
348
    
349
    if ($this->stream) {
350
	if ($this->handler->multigroup) $this->stream->BlockEnd();
351
	$this->stream->Close();
352
    }
353
 }
354
355
155 by Suren A. Chilingaryan
Fix group names in export of virtualgroups
356
 function ExportGroup(REQUEST $req, READER $rdr, LOGGROUP $grp, INTERVAL $ivl, MASK $msk = NULL, $title = false) {
1 by Suren A. Chilingaryan
Initial import
357
    global $ROOT_COMBIHIST_LIMIT;
358
    
155 by Suren A. Chilingaryan
Fix group names in export of virtualgroups
359
    if (!$title) $title = $this->GetGroupName($rdr->srvid, $rdr->dbname, $grp->gid, $msk?$msk->name:"");
1 by Suren A. Chilingaryan
Initial import
360
361
    $opts = &$req->GetOptions();
362
    $subseconds = !$opts->Get('ignore_subsecond');
363
    
364
    if (($this->stream)&&(!$this->handler->multigroup)) {
365
	$stream_args = array_merge($this->stream_args, array(
366
	    "expected_blocks" => $this->expected_groups,
367
	    "block_title" => $title,
368
	    "block_number" => 0
369
	));
370
371
        $stream_args["extension"] = $this->handler->GetExtension();
372
373
	if (in_array("ROOT", $this->specials)) {
374
	    if ((!isset($ROOT_COMBIHIST_LIMIT))||($ivl->GetWindowSize() < $ROOT_COMBIHIST_LIMIT))
375
		$stream_args['root__combhist'] = 1;
376
	    else
377
		$stream_args['root__combhist'] = 0;
378
	}
379
		
380
	$this->stream->BlockStart($stream_args);
381
    }
382
383
    $this->handler->GroupStart($title, $subseconds);
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
384
    
502 by Suren A. Chilingaryan
Support real-time export mode for status displays
385
    $flags = 0;
386
    if ($this->rt_mode&EXPORT::RT_SKIP_HEADERS) $flags |= READER::EXPORT_SKIP_HEADERS;
387
388
    $rdr->Export($this->handler, $grp, $msk, $ivl, $this->resample, $flags, $this->dmcb);
1 by Suren A. Chilingaryan
Initial import
389
    $this->handler->GroupEnd();    
390
391
    if (($this->stream)&&(!$this->handler->multigroup)) $this->stream->BlockEnd();    
392
 }
393
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
394
 function SendHeaders(READER $rdr = NULL, LOGGROUP $grp = NULL, MASK $msk = NULL, INTERVAL $ivl = NULL) { 
1 by Suren A. Chilingaryan
Initial import
395
    if ($this->stream) 
396
	$content_type = $this->stream->GetContentType();
397
    else
398
	$content_type = false;
399
	
400
    if (!$content_type) $content_type = $this->handler->GetContentType();
401
    if (!$content_type) {
402
	if ($this->format["content_type"]) $content_type = $this->format["content_type"];
403
	else $content_type = "application/binary";
404
    }
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
405
    
1 by Suren A. Chilingaryan
Initial import
406
    if ($this->stream)
407
	$extension = $this->stream->GetExtension();
408
    else
409
	$extension = false;
410
411
    if (!$extension) $extension = $this->handler->GetExtension();
412
    if (!$extension) {
413
	if($this->format["extension"]) $extension = $this->format["extension"];
414
	else if ($this->req->props['format']) $extension = strtolower($this->req->props['format']);
415
	else $extension = "adei";
416
    }
417
418
    if (isset($this->req->props['filename'])) {
419
	$name = &$this->req->props['filename'];
420
	if (($extension)&&(strpos($name, '.')===false)) $name .= ".$extension";
421
    } else {
422
	if ($this->filename) $name = $this->filename;
423
	else $name = $this->GetName($rdr, $grp, $msk, $ivl);
424
	if ($extension) $name .= ".$extension";
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
425
    }    
426
   
427
    //    echo "$content_type , $name\n";
428
    //    exit;
429
    if($this->dm) { 	
405 by Suren A. Chilingaryan
Improve handling of names in download manager
430
      $this->dm->SetContentType($content_type, $name, $this->dm->GetDownload());
1 by Suren A. Chilingaryan
Initial import
431
    }
210.1.2 by Suren A. Chilingaryan
Download manager by Aki Rimpelainen
432
    else {
433
      header("Cache-Control: no-cache, must-revalidate");
434
      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
435
      header("Content-type: $content_type");
436
      header("Content-Disposition: attachment; filename=\"$name\"");
437
    }	
438
  }
1 by Suren A. Chilingaryan
Initial import
439
440
 function GetGroupName($sname, $dname, $gname, $mname, $iname = false) {
45 by Suren A. Chilingaryan
Support for export to the National Instruments TDMS format
441
    if ($mname) $mname = "/$mname";
442
    
443
    if ($this->multirdr) $title = $sname . "__" . $dname . "__" . $gname . $mname;
444
    elseif ($this->multigroup)  $title = $gname . $mname;
445
    else $title = $gname . $mname;
1 by Suren A. Chilingaryan
Initial import
446
    return $title;
447
 }
448
449
 function GetName(READER $rdr = NULL, LOGGROUP $grp = NULL, MASK $msk = NULL, INTERVAL $ivl = NULL) {
450
    $name = "";
451
452
    if ((!$this->multirdr)&&($rdr)) 
453
	$name .= $rdr->srvid . "__" . $rdr->dbname;
454
455
    if ((!$this->multigroup)&&($grp)) {
456
	$name .= ($name?"__":"") . $grp->gid;
457
    }
458
	
459
    if ((!$this->multimask)&&($msk))
460
	$name .= ($name?"__":"") . $msk->name;
461
    
462
    if (!$this->multiivl) {
143 by Suren A. Chilingaryan
Support for dates prior to Jan 01, 1970
463
	if ((!$ivl)||($ivl->IsEmpty()))
464
	    $ivl = $this->req->CreateInterval();
1 by Suren A. Chilingaryan
Initial import
465
466
	$name .= ($name?"__":"") . $ivl->GetName(NAME_FORMAT_ISO8601);
467
    }
468
469
    if ($name) return $name;
470
    return "adei_data";
471
 }
472
}
473
474
195 by Suren A. Chilingaryan
Image Export by Arsen Sogomonyan
475
?>