ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilBibliographicDataSet.php
Go to the documentation of this file.
1 <?php
20 
28 {
29  private const EXP_DIRECTORY = "/Modules/Bibliographic/set_1/expDir_1/";
30  private const EXP_DIRECTORY_NEW = "/components/ILIAS/Bibliographic/set_1/expDir_1/";
34  protected $storage;
35  protected \ilObjBibliographicStakeholder $stakeholder;
39  protected $import_bib_object;
43  protected $user;
44  protected array $import_temp_refs = [];
45  protected array $import_temp_refs_props = [];
46 
47 
48  public function __construct()
49  {
50  global $DIC;
51  $ilDB = $DIC['ilDB'];
52  $ilUser = $DIC['ilUser'];
53  $IRSS = $DIC['resource_storage'];
55  $this->db = $ilDB;
56  $this->user = $ilUser;
57  $this->storage = $IRSS;
58  $this->stakeholder = new ilObjBibliographicStakeholder();
59  }
60 
61 
62 
63  public function getSupportedVersions(): array
64  {
65  return ['4.5.0'];
66  }
67 
68 
69  public function getXmlNamespace(string $a_entity, string $a_schema_version): string
70  {
71  return 'http://www.ilias.de/xml/Modules/Bibliographic/' . $a_entity;
72  }
73 
74 
75  public function importRecord(
76  string $a_entity,
77  array $a_types,
78  array $a_rec,
79  ilImportMapping $a_mapping,
80  string $a_schema_version
81  ): void {
82  if ($a_entity === 'bibl') {
83  if ($new_id = $a_mapping->getMapping('components/ILIAS/Container', 'objs', $a_rec['id'])) {
84  // container content
85  $new_obj = ilObjectFactory::getInstanceByObjId($new_id, false);
86  } else {
87  $new_obj = new ilObjBibliographic();
88  }
92  $new_obj->setTitle($a_rec['title']);
93  $new_obj->setDescription($a_rec['description']);
94  $new_obj->setFilename($a_rec['fileName']);
95  if ($new_obj->getId() === 0) {
96  $new_obj->create();
97  }
98  $new_obj->getObjectProperties()->storePropertyIsOnline(
99  new ilObjectPropertyIsOnline(false)
100  );
101  $this->import_bib_object = $new_obj;
102  $a_mapping->addMapping('components/ILIAS/Bibliographic', 'bibl', $a_rec['id'], $new_obj->getId());
103  $this->importLibraryFile($a_mapping);
104  }
105  }
106 
107 
111  protected function getTypes(string $a_entity, string $a_version): array
112  {
113  return match ($a_entity) {
114  'bibl' => ["id" => "integer", "title" => "text", "description" => "text", "filename" => "text"],
115  default => [],
116  };
117  }
118 
119 
124  protected function getDependencies(
125  string $a_entity,
126  string $a_version,
127  ?array $a_rec = null,
128  ?array $a_ids = null
129  ): array {
130  return [];
131  }
132 
133 
134  public function readData(string $a_entity, string $a_version, array $a_ids): void
135  {
136  $this->data = [];
137  if (!is_array($a_ids)) {
138  $a_ids = [$a_ids];
139  }
140  $this->_readData($a_entity, $a_ids);
141  }
142 
143 
147  protected function _readData(string $a_entity, array $a_ids): void
148  {
149  switch ($a_entity) {
150  case 'bibl':
151  foreach ($a_ids as $bibl_id) {
152  if (ilObject::_lookupType($bibl_id) === 'bibl') {
153  $obj = new ilObjBibliographic($bibl_id);
154  $data = ['id' => $bibl_id, 'title' => $obj->getTitle(), 'description' => $obj->getDescription(), 'fileName' => $obj->getFilename()];
155  $this->data[] = $data;
156  }
157  }
158  break;
159  default:
160  }
161  }
162 
163 
164  public function exportLibraryFile(int $a_id, string $absolute_export_dir): void
165  {
166  $obj = new ilObjBibliographic($a_id);
167  if (($rid = $obj->getResourceId()) === null) {
168  return;
169  }
170  $fileAbsolutePath = $this->irss->consume()->stream($rid)->getStream()->getMetadata()['uri'] ?? null;
171  ilFileUtils::makeDirParents($absolute_export_dir . self::EXP_DIRECTORY_NEW);
172  copy($fileAbsolutePath, $absolute_export_dir . self::EXP_DIRECTORY_NEW . $obj->getFilename());
173  }
174 
175 
179  public function importLibraryFile(\ilImportMapping $a_mapping): void
180  {
181  $bib_id = $this->import_bib_object->getId();
182  $filename = $this->import_bib_object->getFilename();
183  $import_path_legacy = $this->getImportDirectory() . self::EXP_DIRECTORY . $filename;
184  $import_path_new = $this->getImportDirectory() . self::EXP_DIRECTORY_NEW . $filename;
185  if (file_exists($import_path_legacy)) {
186  $import_path = $import_path_legacy;
187  } elseif (file_exists($import_path_new)) {
188  $import_path = $import_path_new;
189  } else {
190  return;
191  }
192 
193  // create new resource from stream
194  $resource = @fopen($import_path, 'rb');
195 
196  $stream = Streams::ofResource($resource);
197  $identification = $this->storage->manage()->stream($stream, $this->stakeholder, $filename);
198 
199  // insert rid of the new resource into the data table
200  $this->db->manipulateF(
201  'UPDATE `il_bibl_data` SET `rid` = %s WHERE `id` = %s;',
202  ['text', 'integer'],
203  [
204  $identification->serialize(),
205  $bib_id,
206  ]
207  );
208  $this->import_bib_object->setResourceId($identification);
209  $this->import_bib_object->setMigrated(true);
210  $this->import_bib_object->update();
211  $this->import_bib_object->parseFileToDatabase();
212  }
213 }
importLibraryFile(\ilImportMapping $a_mapping)
readData(string $a_entity, string $a_version, array $a_ids)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Bibliographic dataset class.
getXmlNamespace(string $a_entity, string $a_schema_version)
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
Return dependencies form entities to other entities (in our case these are all the DB relations) ...
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
_readData(string $a_entity, array $a_ids)
Build data array, data is read from cache except bibl object itself.
getMapping(string $a_comp, string $a_entity, string $a_old_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getTypes(string $a_entity, string $a_version)
Map XML attributes of entities to datatypes (text, integer...)
Class ilObjBibliographicStakeholder.
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
Needs to be overwritten for import use case.
global $DIC
Definition: shib_login.php:25
$filename
Definition: buildRTE.php:78
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
__construct(Container $dic, ilPlugin $plugin)
ilObjBibliographicStakeholder $stakeholder
Class ilObjBibliographic.
static _lookupType(int $id, bool $reference=false)
exportLibraryFile(int $a_id, string $absolute_export_dir)