ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilBibliographicDataSet.php
Go to the documentation of this file.
1 <?php
2 
22 
30 {
34  private const EXP_DIRECTORY = "/Modules/Bibliographic/set_1/expDir_1/";
38  private const EXP_DIRECTORY_NEW = "/components/ILIAS/Bibliographic/set_1/expDir_1/";
42  protected $storage;
43  protected \ilObjBibliographicStakeholder $stakeholder;
47  protected $import_bib_object;
51  protected $user;
52  protected array $import_temp_refs = [];
53  protected array $import_temp_refs_props = [];
54 
55 
56  public function __construct()
57  {
58  global $DIC;
59  $ilDB = $DIC['ilDB'];
60  $ilUser = $DIC['ilUser'];
61  $IRSS = $DIC['resource_storage'];
63  $this->db = $ilDB;
64  $this->user = $ilUser;
65  $this->storage = $IRSS;
66  $this->stakeholder = new ilObjBibliographicStakeholder();
67  }
68 
69 
70 
71  public function getSupportedVersions(): array
72  {
73  return ['4.5.0'];
74  }
75 
76 
77  public function getXmlNamespace(string $a_entity, string $a_schema_version): string
78  {
79  return 'http://www.ilias.de/xml/Modules/Bibliographic/' . $a_entity;
80  }
81 
82 
83  public function importRecord(
84  string $a_entity,
85  array $a_types,
86  array $a_rec,
87  ilImportMapping $a_mapping,
88  string $a_schema_version
89  ): void {
90  if ($a_entity === 'bibl') {
91  if ($new_id = $a_mapping->getMapping('components/ILIAS/Container', 'objs', $a_rec['id'])) {
92  // container content
93  $new_obj = ilObjectFactory::getInstanceByObjId($new_id, false);
94  } else {
95  $new_obj = new ilObjBibliographic();
96  }
100  $new_obj->setTitle($a_rec['title']);
101  $new_obj->setDescription($a_rec['description']);
102  $new_obj->setFilename($a_rec['fileName']);
103  if ($new_obj->getId() === 0) {
104  $new_obj->create();
105  }
106  $new_obj->getObjectProperties()->storePropertyIsOnline(
107  new Online(false)
108  );
109  $this->import_bib_object = $new_obj;
110  $a_mapping->addMapping('components/ILIAS/Bibliographic', 'bibl', $a_rec['id'], $new_obj->getId());
111  $this->importLibraryFile($a_mapping);
112  }
113  }
114 
115 
119  protected function getTypes(string $a_entity, string $a_version): array
120  {
121  return match ($a_entity) {
122  'bibl' => ["id" => "integer", "title" => "text", "description" => "text", "filename" => "text"],
123  default => [],
124  };
125  }
126 
127 
132  #[\Override]
133  protected function getDependencies(
134  string $a_entity,
135  string $a_version,
136  ?array $a_rec = null,
137  ?array $a_ids = null
138  ): array {
139  return [];
140  }
141 
142 
143  public function readData(string $a_entity, string $a_version, array $a_ids): void
144  {
145  $this->data = [];
146  if (!is_array($a_ids)) {
147  $a_ids = [$a_ids];
148  }
149  $this->_readData($a_entity, $a_ids);
150  }
151 
152 
156  protected function _readData(string $a_entity, array $a_ids): void
157  {
158  switch ($a_entity) {
159  case 'bibl':
160  foreach ($a_ids as $bibl_id) {
161  if (ilObject::_lookupType($bibl_id) === 'bibl') {
162  $obj = new ilObjBibliographic($bibl_id);
163  $data = ['id' => $bibl_id, 'title' => $obj->getTitle(), 'description' => $obj->getDescription(), 'fileName' => $obj->getFilename()];
164  $this->data[] = $data;
165  }
166  }
167  break;
168  default:
169  }
170  }
171 
172 
173  public function exportLibraryFile(int $a_id, string $absolute_export_dir): void
174  {
175  $obj = new ilObjBibliographic($a_id);
176  if (($rid = $obj->getResourceId()) === null) {
177  return;
178  }
179  $fileAbsolutePath = $this->irss->consume()->stream($rid)->getStream()->getMetadata()['uri'] ?? null;
180  ilFileUtils::makeDirParents($absolute_export_dir . self::EXP_DIRECTORY_NEW);
181  copy($fileAbsolutePath, $absolute_export_dir . self::EXP_DIRECTORY_NEW . $obj->getFilename());
182  }
183 
184 
188  public function importLibraryFile(\ilImportMapping $a_mapping): void
189  {
190  $bib_id = $this->import_bib_object->getId();
191  $filename = $this->import_bib_object->getFilename();
192  $import_path_legacy = $this->getImportDirectory() . self::EXP_DIRECTORY . $filename;
193  $import_path_new = $this->getImportDirectory() . self::EXP_DIRECTORY_NEW . $filename;
194  if (file_exists($import_path_legacy)) {
195  $import_path = $import_path_legacy;
196  } elseif (file_exists($import_path_new)) {
197  $import_path = $import_path_new;
198  } else {
199  return;
200  }
201 
202  // create new resource from stream
203  $resource = @fopen($import_path, 'rb');
204 
205  $stream = Streams::ofResource($resource);
206  $identification = $this->storage->manage()->stream($stream, $this->stakeholder, $filename);
207 
208  // insert rid of the new resource into the data table
209  $this->db->manipulateF(
210  'UPDATE `il_bibl_data` SET `rid` = %s WHERE `id` = %s;',
211  ['text', 'integer'],
212  [
213  $identification->serialize(),
214  $bib_id,
215  ]
216  );
217  $this->import_bib_object->setResourceId($identification);
218  $this->import_bib_object->setMigrated(true);
219  $this->import_bib_object->update();
220  $this->import_bib_object->parseFileToDatabase();
221  }
222 }
importLibraryFile(\ilImportMapping $a_mapping)
readData(string $a_entity, string $a_version, array $a_ids)
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.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
_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:26
$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)