ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilFileXMLWriter.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once "./Services/Xml/classes/class.ilXmlWriter.php";
6
22{
23 public static $CONTENT_ATTACH_NO = 0;
24 public static $CONTENT_ATTACH_ENCODED = 1;
27 public static $CONTENT_ATTACH_COPY = 4;
28 // begin-patch fm
29 public static $CONTENT_ATTACH_REST = 5;
30 // end-patch fm
42 public $file;
43 public $omit_header = false;
44
45
55 public function __construct()
56 {
57 parent::__construct();
58 $this->attachFileContents = ilFileXMLWriter::$CONTENT_ATTACH_NO;
59 }
60
61
62 public function setFile(ilObjFile $file)
63 {
64 $this->file = &$file;
65 }
66
67
73 public function setOmitHeader($a_val)
74 {
75 $this->omit_header = $a_val;
76 }
77
78
84 public function getOmitHeader()
85 {
86 return $this->omit_header;
87 }
88
89
96 public function setFileTargetDirectories($a_rel, $a_abs)
97 {
98 $this->target_dir_relative = $a_rel;
99 $this->target_dir_absolute = $a_abs;
100 }
101
102
111 {
112 if ($attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED && !function_exists("gzencode")) {
113 throw new ilFileException("Inflating with gzip is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
114 }
115 if ($attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED && !function_exists("gzcompress")) {
116 throw new ilFileException("Inflating with zlib (compress/uncompress) is not supported", ilFileException::$ID_DEFLATE_METHOD_MISMATCH);
117 }
118 $this->attachFileContents = $attachFileContents;
119 }
120
121
122 public function start()
123 {
124 $this->__buildHeader();
125
126 $attribs = array(
127 "obj_id" => "il_" . IL_INST_ID . "_file_" . $this->file->getId(),
128 "version" => $this->file->getVersion(),
129 "size" => $this->file->getFileSize(),
130 "type" => $this->file->getFileType(),
131 );
132
133 $this->xmlStartTag("File", $attribs);
134 $this->xmlElement("Filename", null, $this->file->getFileName());
135
136 $this->xmlElement("Title", null, $this->file->getTitle());
137 $this->xmlElement("Description", null, $this->file->getDescription());
138 $this->xmlElement("Rating", null, (int) $this->file->hasRating());
139
140 if ($this->attachFileContents) {
141 $filename = $this->file->getDirectory($this->file->getVersion()) . "/" . $this->file->getFileName();
142 if (@is_file($filename)) {
143 if ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_COPY) {
144 $attribs = array("mode" => "COPY");
145 copy($filename, $this->target_dir_absolute . "/" . $this->file->getFileName());
146 $content = $this->target_dir_relative . "/" . $this->file->getFileName();
147 $this->xmlElement("Content", $attribs, $content);
148 } // begin-patch fm
149 elseif ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_REST) {
150 $attribs = array('mode' => "REST");
151 include_once './Services/WebServices/Rest/classes/class.ilRestFileStorage.php';
152 $fs = new ilRestFileStorage();
153 $tmpname = $fs->storeFileForRest(base64_encode(@file_get_contents($filename)));
154 $this->xmlElement("Content", $attribs, $tmpname);
155 } // end-patch fm
156 else {
157 $content = @file_get_contents($filename);
158 $attribs = array("mode" => "PLAIN");
159 if ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_ZLIB_ENCODED) {
160 $attribs ["mode"] = "ZLIB";
161 $content = @gzcompress($content, 9);
162 } elseif ($this->attachFileContents == ilFileXMLWriter::$CONTENT_ATTACH_GZIP_ENCODED) {
163 $attribs ["mode"] = "GZIP";
164 $content = @gzencode($content, 9);
165 }
166 $content = base64_encode($content);
167 $this->xmlElement("Content", $attribs, $content);
168 }
169 }
170 }
171
172 include_once("./Services/History/classes/class.ilHistory.php");
173
174 $versions = ilHistory::_getEntriesForObject($this->file->getId(), $this->file->getType());
175
176 if (count($versions)) {
177 $this->xmlStartTag("Versions");
178 foreach ($versions as $version) {
179 $info_params = $version["info_params"];
180 list($filename, $history_id) = explode(",", $info_params);
181 $attribs = array(
182 "id" => $history_id,
183 "date" => ilUtil::date_mysql2time($version["date"]),
184 "usr_id" => "il_" . IL_INST_ID . "_usr_" . $version["user_id"],
185 );
186 $this->xmlElement("Version", $attribs);
187 }
188 $this->xmlEndTag("Versions");
189 }
190
191 $this->xmlEndTag("File");
192
193 $this->__buildFooter();
194
195 return true;
196 }
197
198
199 public function getXML()
200 {
201 return $this->xmlDumpMem(false);
202 }
203
204
205 public function __buildHeader()
206 {
207 if (!$this->getOmitHeader()) {
208 $this->xmlSetDtdDef("<!DOCTYPE File PUBLIC \"-//ILIAS//DTD FileAdministration//EN\" \"" . ILIAS_HTTP_PATH . "/xml/ilias_file_3_8.dtd\">");
209 $this->xmlSetGenCmt("Exercise Object");
210 $this->xmlHeader();
211 }
212
213 return true;
214 }
215
216
217 public function __buildFooter()
218 {
219 }
220}
An exception for terminatinating execution or to throw for unit testing.
Class to report exception.
XML writer class.
setAttachFileContents($attachFileContents)
set attachment content mode
setFileTargetDirectories($a_rel, $a_abs)
Set file target directories.
setOmitHeader($a_val)
Set omit header.
getOmitHeader()
Get omit header.
setFile(ilObjFile $file)
static _getEntriesForObject($a_obj_id, $a_obj_type="")
get all history entries for an object
Class ilObjFile.
File storage handling.
static date_mysql2time($mysql_date_time)
make time object from mysql_date_time
XML writer class.
xmlEndTag($tag)
Writes an endtag.
xmlSetGenCmt($genCmt)
Sets generated comment.
xmlDumpMem($format=true)
Returns xml document from memory.
xmlHeader()
Writes xml header @access public.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
xmlSetDtdDef($dtdDef)
Sets dtd definition.