/dev/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/dev/trunk

« back to all changes in this revision

Viewing changes to classes/export.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
require("stream/stream.php");
 
4
 
 
5
class EXPORT {
 
6
 var $req;
 
7
 
 
8
 var $format;
 
9
 var $multimode;        // STREAM is used for joining groups
 
10
 
 
11
 var $multigroup;       // Multiple groups expected
 
12
 var $multirdr;         // Multiple readers expected
 
13
 
 
14
 var $output;
 
15
 var $stream;
 
16
 var $handler;
 
17
 
 
18
 var $stream_args;
 
19
 var $expected_groups; 
 
20
 
 
21
 var $specials;
 
22
 
 
23
 var $filename;
 
24
 
 
25
 const MASK_STANDARD = 0;
 
26
 const MASK_MULTI = 1;
 
27
 const MASK_SOURCE = 2;
 
28
 const MASK_COMPLETE = 3;
 
29
   
 
30
 function __construct(DATARequest &$props = NULL, STREAMObjectInterface &$h = NULL, &$format = NULL) {
 
31
    if ($props) $this->req = &$props;
 
32
    else $this->req = new DATARequest();
 
33
    
 
34
    if ($format) $this->format = &$format;
 
35
    else $this->format = $this->req->GetFormatInfo();
 
36
 
 
37
    if ($this->format['handler']) {
 
38
        if (!include_once("handlers/" . strtolower($this->format['handler']) . '.php')) {
 
39
                throw new ADEIException(translate("Unsupported data handler is configured: \"%s\"", $format['handler']));
 
40
        }
 
41
        $handler = array(
 
42
            "class" => $this->format['handler'] . "Handler"
 
43
        );
 
44
    } else {
 
45
        $handler = array(
 
46
            "class" => "CSVHandler"
 
47
        );
 
48
    }
 
49
    
 
50
    $this->handler = new $handler["class"]($this->format);
 
51
    
 
52
    if ($this->req->props['mask_mode']) {
 
53
        $this->multimode = !$this->handler->multigroup;
 
54
    } else $this->multimode = false;
 
55
 
 
56
    $this->ouput = &$h;
 
57
 
 
58
    if (($this->format['filter'])||($this->multimode)) {
 
59
        $this->stream = new STREAM($this->format, $this->output, ($this->multimode?STREAM::MULTIMODE:0)|($this->handler->filewriter?STREAM::FILESTART:0));
 
60
        if ($this->stream->filereader) $this->handler->RequestFilemode();
 
61
    } else {
 
62
        $this->stream = &$this->output;
 
63
    }
 
64
 
 
65
    if ($this->stream) {
 
66
        $this->handler->SetOutput($this->stream);
 
67
    }
 
68
 }
 
69
 
 
70
 function CheckMode() {
 
71
    if ((!$this->multimode)&&(!$this->handler->multigroup)) {
 
72
        if ((!$this->stream)||(!($this->stream instanceof STREAM))||(!$this->stream->joiner)) {
 
73
            unset($this->stream);
 
74
            $this->stream = new STREAM($this->format, $this->output, STREAM::MULTIMODE|($this->handler->filewriter?STREAM::FILESTART:0));
 
75
            if ($this->stream->filereader) $this->handler->RequestFilemode();
 
76
            $this->handler->SetOutput($this->stream);
 
77
 
 
78
            if (!$this->stream->joiner) {
 
79
                throw new ADEIException(translate("The attempt to create joining STREAM is failed"));
 
80
            }
 
81
 
 
82
            $this->multimode = true;
 
83
        }
 
84
    }
 
85
 }
 
86
 
 
87
 function Export() {
 
88
    if ($this->req->props['mask_mode'] > 1) {
 
89
        switch($this->req->props['mask_mode']) {
 
90
            case EXPORT::MASK_SOURCE:
 
91
                return $this->ExportSource();
 
92
            case EXPORT::MASK_COMPLETE:
 
93
                return $this->ExportAll();
 
94
            default:
 
95
                throw new ADEIException(translate("Unsupported mask mode (%u)", $this->req->props['mask_mode']));
 
96
        }
 
97
    }
 
98
    
 
99
    $rdr = $this->req->CreateReader();
 
100
    $grp = $rdr->CreateGroup();
 
101
    $ivl = $rdr->CreateInterval($grp);
 
102
    $msk = $rdr->CreateMask($grp);
 
103
 
 
104
    $this->multigroup = false;
 
105
    $this->multirdr = false;
 
106
        
 
107
    $this->Start($rdr, $grp, $msk, $ivl, 1);
 
108
    $this->SendHeaders($rdr, $grp, $msk, $ivl);
 
109
    $this->ExportGroup($this->req, $rdr, $grp, $ivl, $msk);
 
110
    $this->End();
 
111
 }
 
112
 
 
113
 function ExportSource() {
 
114
    $this->CheckMode();
 
115
 
 
116
    $msk = new MASK();
 
117
    $rdr = $this->req->CreateReader();
 
118
 
 
119
    $this->multigroup = true;
 
120
    $this->multirdr = false;
 
121
 
 
122
    $this->Start($rdr, NULL, $msk, NULL);
 
123
    $this->SendHeaders($rdr, NULL, $msk, NULL);
 
124
 
 
125
    $groups = $rdr->GetGroups();
 
126
    foreach ($groups as &$grp) {
 
127
        $ivl = $rdr->CreateInterval($grp);
 
128
 
 
129
        $this->ExportGroup($this->req, $rdr, $grp, $ivl, $msk);
 
130
    }
 
131
 
 
132
    $this->End();
 
133
 }
 
134
 
 
135
 function ExportAll() {
 
136
    $this->CheckMode();
 
137
    
 
138
    $msk = new MASK();
 
139
 
 
140
    $this->multigroup = true;
 
141
    $this->multirdr = true;
 
142
 
 
143
    $this->Start(NULL, NULL, $msk, NULL);
 
144
    $this->SendHeaders(NULL, NULL, $msk, NULL);
 
145
 
 
146
    $list = $this->req->GetSourceList(REQUEST::LIST_ALL);
 
147
    foreach ($list as $sreq) {
 
148
        try {
 
149
            $dreq = $sreq->CreateDataRequest();
 
150
        } catch (ADEIException $e) {
 
151
            $dreq = &$sreq;
 
152
        }
 
153
 
 
154
        $rdr = $dreq->CreateReader();
 
155
 
 
156
        $groups = $rdr->GetGroups();
 
157
        foreach ($groups as &$grp) {
 
158
            $ivl = $rdr->CreateInterval($grp);      
 
159
            
 
160
            $this->ExportGroup($sreq, $rdr, $grp, $ivl, $msk);
 
161
        }
 
162
    }
 
163
 
 
164
    $this->End();
 
165
 }
 
166
 
 
167
 function Start(READER $rdr = NULL, LOGGROUP $grp = NULL, MASK $msk = NULL, INTERVAL $ivl = NULL, $expected_groups = 0) {
 
168
    $this->filename = false;
 
169
    
 
170
    if ($this->stream) {
 
171
        $this->specials = $this->stream->GetSpecials();
 
172
        
 
173
        $this->stream_args = array(
 
174
            "expected_blocks" => $expected_groups,
 
175
            "extension" => $this->handler->GetExtension()
 
176
        );
 
177
        
 
178
        $this->stream->Open($this->stream_args);
 
179
 
 
180
        $this->expected_groups = $expected_groups;
 
181
        if ($this->handler->multigroup) {
 
182
            $this->filename = $this->GetName($rdr, $grp, $msk, $ivl);
 
183
 
 
184
            $stream_args = $this->stream_args;
 
185
            $stream_args["block_number"] = 0;
 
186
            $stream_args["block_title"] = $this->filename;
 
187
 
 
188
            $this->stream->BlockStart($stream_args);
 
189
        }
 
190
    } else {
 
191
        $this->specials = array();
 
192
    }
 
193
    
 
194
    $this->handler->SequenceStart();
 
195
 }
 
196
 
 
197
 function End() {
 
198
    $this->handler->SequenceEnd();
 
199
    
 
200
    if ($this->stream) {
 
201
        if ($this->handler->multigroup) $this->stream->BlockEnd();
 
202
        $this->stream->Close();
 
203
    }
 
204
 }
 
205
 
 
206
 
 
207
 function ExportGroup(REQUEST $req, READER $rdr, LOGGROUP $grp, INTERVAL $ivl, MASK $msk) {
 
208
    global $ROOT_COMBIHIST_LIMIT;
 
209
    
 
210
    if ($this->multirdr) $title = $this->GetGroupName($rdr->srvid, $rdr->dbname, $grp->gid, $msk->name);
 
211
    elseif ($this->multigroup) $title = $this->GetGroupName(false, false, $grp->gid, $msk->name);
 
212
    else $title = $this->GetGroupName(false, false, false, $msk->name);
 
213
 
 
214
 
 
215
    $opts = &$req->GetOptions();
 
216
    $subseconds = !$opts->Get('ignore_subsecond');
 
217
    
 
218
    if (($this->stream)&&(!$this->handler->multigroup)) {
 
219
        $stream_args = array_merge($this->stream_args, array(
 
220
            "expected_blocks" => $this->expected_groups,
 
221
            "block_title" => $title,
 
222
            "block_number" => 0
 
223
        ));
 
224
 
 
225
        $stream_args["extension"] = $this->handler->GetExtension();
 
226
 
 
227
        if (in_array("ROOT", $this->specials)) {
 
228
            if ((!isset($ROOT_COMBIHIST_LIMIT))||($ivl->GetWindowSize() < $ROOT_COMBIHIST_LIMIT))
 
229
                $stream_args['root__combhist'] = 1;
 
230
            else
 
231
                $stream_args['root__combhist'] = 0;
 
232
        }
 
233
                
 
234
        $this->stream->BlockStart($stream_args);
 
235
    }
 
236
 
 
237
    $this->handler->GroupStart($title, $subseconds);
 
238
    $rdr->Export($this->handler, $grp, $msk, $ivl->GetWindowStart(), $ivl->GetWindowEnd());
 
239
    $this->handler->GroupEnd();    
 
240
 
 
241
    if (($this->stream)&&(!$this->handler->multigroup)) $this->stream->BlockEnd();    
 
242
 }
 
243
 
 
244
 function SendHeaders(READER $rdr = NULL, LOGGROUP $grp = NULL, MASK $msk = NULL, INTERVAL $ivl = NULL) {
 
245
    if ($this->stream) 
 
246
        $content_type = $this->stream->GetContentType();
 
247
    else
 
248
        $content_type = false;
 
249
        
 
250
    if (!$content_type) $content_type = $this->handler->GetContentType();
 
251
    if (!$content_type) {
 
252
        if ($this->format["content_type"]) $content_type = $this->format["content_type"];
 
253
        else $content_type = "application/binary";
 
254
    }
 
255
 
 
256
    if ($this->stream)
 
257
        $extension = $this->stream->GetExtension();
 
258
    else
 
259
        $extension = false;
 
260
 
 
261
    if (!$extension) $extension = $this->handler->GetExtension();
 
262
    if (!$extension) {
 
263
        if($this->format["extension"]) $extension = $this->format["extension"];
 
264
        else if ($this->req->props['format']) $extension = strtolower($this->req->props['format']);
 
265
        else $extension = "adei";
 
266
    }
 
267
 
 
268
    if (isset($this->req->props['filename'])) {
 
269
        $name = &$this->req->props['filename'];
 
270
        if (($extension)&&(strpos($name, '.')===false)) $name .= ".$extension";
 
271
    } else {
 
272
        if ($this->filename) $name = $this->filename;
 
273
        else $name = $this->GetName($rdr, $grp, $msk, $ivl);
 
274
        if ($extension) $name .= ".$extension";
 
275
    }
 
276
    
 
277
//    echo "$content_type , $name\n";
 
278
//    exit;
 
279
 
 
280
    header("Cache-Control: no-cache, must-revalidate");
 
281
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 
282
    header("Content-type: $content_type"); 
 
283
    header("Content-Disposition: attachment; filename=\"$name\"");
 
284
 }
 
285
 
 
286
 function GetGroupName($sname, $dname, $gname, $mname, $iname = false) {
 
287
    if ($this->multirdr) $title = $sname . "__" . $dname . "__" . $gname . "/" . $mname;
 
288
    elseif ($this->multigroup)  $title = $gname . "/" . $mname;
 
289
    else $title = $mname;
 
290
    return $title;
 
291
 }
 
292
 
 
293
 function GetName(READER $rdr = NULL, LOGGROUP $grp = NULL, MASK $msk = NULL, INTERVAL $ivl = NULL) {
 
294
    $name = "";
 
295
 
 
296
    if ((!$this->multirdr)&&($rdr)) 
 
297
        $name .= $rdr->srvid . "__" . $rdr->dbname;
 
298
 
 
299
    if ((!$this->multigroup)&&($grp)) {
 
300
        $name .= ($name?"__":"") . $grp->gid;
 
301
    }
 
302
        
 
303
    if ((!$this->multimask)&&($msk))
 
304
        $name .= ($name?"__":"") . $msk->name;
 
305
    
 
306
    if (!$this->multiivl) {
 
307
        if (!$ivl) 
 
308
            $ivl = new INTERVAL($this->req->GetIntervalInfo());
 
309
 
 
310
        $name .= ($name?"__":"") . $ivl->GetName(NAME_FORMAT_ISO8601);
 
311
    }
 
312
 
 
313
    if ($name) return $name;
 
314
    return "adei_data";
 
315
 }
 
316
}
 
317
 
 
318
 
 
319
?>
 
 
b'\\ No newline at end of file'