ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilObjBibliographic.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4 require_once "Services/Object/classes/class.ilObject2.php";
5 require_once "Modules/Bibliographic/classes/class.ilBibliographicEntry.php";
6 
17 {
18  const FILETYPE_RIS = "ris";
19  const FILETYPE_BIB = "bib";
31  protected $filename;
37  protected $entries;
43  protected $overviewModels;
49  protected $is_online;
50 
51 
57  public function initType()
58  {
59  $this->type = "bibl";
60  }
61 
62 
70  public function __construct($existant_bibl_id = 0)
71  {
72  if ($existant_bibl_id) {
73  $this->setId($existant_bibl_id);
74  $this->doRead();
75  }
76  parent::__construct($existant_bibl_id, false);
77  }
78 
79 
85  protected function doCreate()
86  {
87  global $DIC;
88 
89  $upload = $DIC->upload();
90  if ($upload->hasUploads() && !$upload->hasBeenProcessed()) {
91  $upload->process();
92  $this->moveUploadedFile($upload);
93  }
94 
95  $DIC->database()->insert("il_bibl_data", [
96  "id" => ["integer", $this->getId()],
97  "filename" => ["text", $this->getFilename()],
98  "is_online" => ["integer", $this->getOnline()],
99  ]);
100  }
101 
102 
103  protected function doRead()
104  {
105  global $DIC;
106  $ilDB = $DIC['ilDB'];
107  $set = $ilDB->query("SELECT * FROM il_bibl_data " . " WHERE id = "
108  . $ilDB->quote($this->getId(), "integer"));
109  while ($rec = $ilDB->fetchAssoc($set)) {
110  if (!$this->getFilename()) {
111  $this->setFilename($rec["filename"]);
112  }
113  $this->setOnline($rec['is_online']);
114  }
115  }
116 
117 
118  public function doUpdate()
119  {
120  global $DIC;
121 
122  $upload = $DIC->upload();
123  if ($_POST['override_entries'] && $upload->hasUploads() && !$upload->hasBeenProcessed()) {
124  $upload->process();
125  $this->deleteFile();
126  $this->moveUploadedFile($upload);
127  }
128 
129  // Delete the object, but leave the db table 'il_bibl_data' for being able to update it using WHERE, and also leave the file
130  $this->doDelete(true, true);
131 
132  $DIC->database()->update("il_bibl_data", [
133  "filename" => ["text", $this->getFilename()],
134  "is_online" => ["integer", $this->getOnline()],
135  ], ["id" => ["integer", $this->getId()]]);
136 
138  }
139 
140 
145  protected function doDelete($leave_out_il_bibl_data = false, $leave_out_delete_file = false)
146  {
147  global $DIC;
148  $ilDB = $DIC['ilDB'];
149  if (!$leave_out_delete_file) {
150  $this->deleteFile();
151  }
152  //il_bibl_attribute
153  $ilDB->manipulate("DELETE FROM il_bibl_attribute WHERE il_bibl_attribute.entry_id IN "
154  . "(SELECT il_bibl_entry.id FROM il_bibl_entry WHERE il_bibl_entry.data_id = "
155  . $ilDB->quote($this->getId(), "integer") . ")");
156  //il_bibl_entry
157  $ilDB->manipulate("DELETE FROM il_bibl_entry WHERE data_id = "
158  . $ilDB->quote($this->getId(), "integer"));
159  if (!$leave_out_il_bibl_data) {
160  //il_bibl_data
161  $ilDB->manipulate("DELETE FROM il_bibl_data WHERE id = "
162  . $ilDB->quote($this->getId(), "integer"));
163  }
164  // delete history entries
166  }
167 
168 
172  public function getFileDirectory()
173  {
174  return "{$this->getType()}/{$this->getId()}";
175  }
176 
177 
181  protected function moveUploadedFile(\ILIAS\FileUpload\FileUpload $upload)
182  {
183  $result = array_values($upload->getResults())[0];
185  $this->deleteFile();
186  $upload->moveFilesTo($this->getFileDirectory(), \ILIAS\FileUpload\Location::STORAGE);
187  $this->setFilename($result->getName());
188  }
189  }
190 
191 
195  private function copyFile($file_to_copy)
196  {
197  $target = $this->getFileDirectory() . '/' . basename($file_to_copy);
198  $this->getFileSystem()->copy($file_to_copy, $target);
199  }
200 
201 
205  protected function deleteFile()
206  {
207  $path = $this->getFileDirectory();
208  try {
209  $this->getFileSystem()->deleteDir($path);
210  } catch (\ILIAS\Filesystem\Exception\IOException $e) {
211  return false;
212  }
213 
214  return true;
215  }
216 
217 
221  private function getFileSystem()
222  {
223  global $DIC;
224 
225  return $DIC["filesystem"]->storage();
226  }
227 
228 
234  public function getFilePath($without_filename = false)
235  {
236  global $DIC;
237  $ilDB = $DIC['ilDB'];
238  $set = $ilDB->query("SELECT filename FROM il_bibl_data " . " WHERE id = "
239  . $ilDB->quote($this->getId(), "integer"));
240  $rec = $ilDB->fetchAssoc($set);
241  {
242  if ($without_filename) {
243  return substr($rec['filename'], 0, strrpos($rec['filename'], DIRECTORY_SEPARATOR));
244  } else {
245  return $rec['filename'];
246  }
247  }
248  }
249 
250 
254  public function setFilename($filename)
255  {
256  $this->filename = $filename;
257  }
258 
259 
263  public function getFilename()
264  {
265  return $this->filename;
266  }
267 
268 
273  public function getFileAbsolutePath()
274  {
275  return $this->getFileDirectory() . DIRECTORY_SEPARATOR . $this->getFilename();
276  }
277 
278 
279  public function getLegacyAbsolutePath()
280  {
281  $stream = $this->getFileSystem()->readStream($this->getFileAbsolutePath());
282 
283  return $stream->getMetadata('uri');
284  }
285 
286 
290  public function getFiletype()
291  {
292  //return bib for filetype .bibtex:
293  $filename = $this->getFilename();
294  if (strtolower(substr($filename, -6)) == "bibtex") {
295  return self::FILETYPE_BIB;
296  }
297 
298  //else return its true filetype
299  return strtolower(substr($filename, -3));
300  }
301 
302 
306  public static function getAllOverviewModels()
307  {
308  global $DIC;
309  $ilDB = $DIC['ilDB'];
311  $set = $ilDB->query('SELECT * FROM il_bibl_overview_model');
312  while ($rec = $ilDB->fetchAssoc($set)) {
313  if ($rec['literature_type']) {
314  $overviewModels[$rec['filetype']][$rec['literature_type']] = $rec['pattern'];
315  } else {
316  $overviewModels[$rec['filetype']] = $rec['pattern'];
317  }
318  }
319 
320  return $overviewModels;
321  }
322 
332  public function doCloneObject($new_obj, $a_target_id, $a_copy_id = null, $a_omit_tree = false)
333  {
334  assert($new_obj instanceof ilObjBibliographic);
335  //copy online status if object is not the root copy object
336  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
337 
338  if (!$cp_options->isRootNode($this->getRefId())) {
339  $new_obj->setOnline($this->getOnline());
340  }
341 
342  $new_obj->cloneStructure($this->getId());
343 
344  return $new_obj;
345  }
346 
347 
357  public function cloneStructure($original_id)
358  {
359  $original = new ilObjBibliographic($original_id);
360  $this->setFilename($original->getFilename());
361  $this->copyFile($original->getFileAbsolutePath());
362  $this->setDescription($original->getDescription());
363  $this->setTitle($original->getTitle());
364  $this->setType($original->getType());
365  $this->doUpdate();
366  }
367 
368 
375  protected static function __removeSpacesAndDashesAtBeginning($input)
376  {
377  for ($i = 0; $i < strlen($input); $i++) {
378  if ($input[$i] != " " && $input[$i] != "-") {
379  return substr($input, $i);
380  }
381  }
382  }
383 
384 
390  public function writeSourcefileEntriesToDb()
391  {
392  //Read File
393  $entries_from_file = array();
394  $filetype = $this->getFiletype();
395  switch ($filetype) {
396  case(self::FILETYPE_RIS):
397  $ilRis = new ilRis();
398  $ilRis->readContent($this->getFileAbsolutePath());
399 
400  $entries_from_file = $ilRis->parseContent();
401  break;
402  case(self::FILETYPE_BIB):
403  $bib = new ilBibTex();
404  $bib->readContent($this->getFileAbsolutePath());
405 
406  $entries_from_file = $bib->parseContent();
407  break;
408  }
409  //fill each entry into a ilBibliographicEntry object and then write it to DB by executing doCreate()
410  foreach ($entries_from_file as $file_entry) {
411  $type = null;
412  $x = 0;
413  $parsed_entry = array();
414  foreach ($file_entry as $key => $attribute) {
415  // if the attribute is an array, make a comma separated string out of it
416  if (is_array($attribute)) {
417  $attribute = implode(", ", $attribute);
418  }
419  // reduce the attribute strings to a maximum of 4000 (ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) characters, in order to fit in the database
420  //if (mb_strlen($attribute, 'UTF-8') > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
421  if (ilStr::strLen($attribute) > self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH) {
422  // $attribute = mb_substr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH - 3, 'UTF-8') . '...';
423  $attribute = ilStr::subStr($attribute, 0, self::ATTRIBUTE_VALUE_MAXIMAL_TEXT_LENGTH
424  - 3) . '...';
425  }
426  // ty (RIS) or entryType (BIB) is the type and is treated seperately
427  if (strtolower($key) == 'ty' || strtolower($key) == 'entrytype') {
428  $type = $attribute;
429  continue;
430  }
431  //TODO - Refactoring for ILIAS 4.5 - get rid off array restructuring
432  //change array structure (name not as the key, but under the key "name")
433  $parsed_entry[$x]['name'] = $key;
434  $parsed_entry[$x++]['value'] = $attribute;
435  }
436  //create the entry and fill data into database by executing doCreate()
437  $entry_model = ilBibliographicEntry::getInstance($this->getFiletype());
438  $entry_model->setType($type);
439  $entry_model->setAttributes($parsed_entry);
440  $entry_model->setBibliographicObjId($this->getId());
441  $entry_model->doCreate();
442  }
443  }
444 
445 
449  public function setOnline($a_online)
450  {
451  $this->is_online = $a_online;
452  }
453 
454 
458  public function getOnline()
459  {
460  return $this->is_online;
461  }
462 }
static strLen($a_string)
Definition: class.ilStr.php:78
setType($a_type)
setDescription($a_desc)
$result
$x
Definition: example_009.php:98
global $DIC
Definition: saml.php:7
Class BaseForm.
Class ilBibTex.
doCloneObject($new_obj, $a_target_id, $a_copy_id=null, $a_omit_tree=false)
Clone BIBL.
static subStr($a_str, $a_start, $a_length=null)
Definition: class.ilStr.php:15
$stream
PHP stream implementation.
Class ilRis.
Definition: class.ilRis.php:9
doDelete($leave_out_il_bibl_data=false, $leave_out_delete_file=false)
__construct($existant_bibl_id=0)
If bibliographic object exists, read it&#39;s data from database, otherwise create it.
static _getInstance($a_copy_id)
Get instance of copy wizard options.
writeSourcefileEntriesToDb()
Reads out the source file and writes all entries to the database.
static __removeSpacesAndDashesAtBeginning($input)
setTitle($a_title)
static getInstance($file_type, $entry_id=null)
Create styles array
The data for the language used.
cloneStructure($original_id)
Attention only use this for objects who have not yet been created (use like: $x = new ilObjDataCollec...
global $ilDB
$i
Definition: disco.tpl.php:19
static _removeEntriesForObject($a_obj_id)
remove all history entries for an object
Class ilObject2 This is an intermediate progress of ilObject class.
Class ilObjBibliographic.
moveUploadedFile(\ILIAS\FileUpload\FileUpload $upload)
getFilePath($without_filename=false)
$key
Definition: croninfo.php:18
$_POST["username"]
Class FlySystemFileAccessTest.