ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilOerHarvester.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 
12 {
16  private $logger = null;
17 
21  private $cronresult = null;
22 
23 
27  private $settings = null;
28 
34  {
35  global $DIC;
36 
37  $this->logger = $DIC->logger()->meta();
38  $this->cronresult = $result;
40  }
41 
46  public function run()
47  {
48  try {
49  $obj_ids = $this->collect();
50  $obj_ids = $this->filter($obj_ids);
51  $num = $this->harvest($obj_ids);
52 
53  $message = 'Created ' . $num . ' new objects. <br />';
54 
55  $deleted = $this->deleteDeprecated();
56 
57  $message .= 'Deleted ' . $deleted . ' deprecated objects.';
58 
59  if (!$deleted && !$num) {
60  $this->cronresult->setStatus(ilCronJobResult::STATUS_NO_ACTION);
61  } else {
62  $this->cronresult->setStatus(ilCronJobResult::STATUS_OK);
63  }
64  $this->cronresult->setMessage($message);
65  return $this->cronresult;
66  } catch (Exception $e) {
67  $this->cronresult->setStatus(ilCronJobResult::STATUS_FAIL);
68  $this->cronresult->setMessage($e->getMessage());
69  return $this->cronresult;
70  }
71  }
72 
77  protected function collect()
78  {
79  $collectable_types = $this->settings->getHarvestingTypes();
80  $copyright_ids = $this->settings->getCopyRightTemplatesInLomFormat();
81 
82  $collectable_obj_ids = ilMDRights::lookupRightsByTypeAndCopyright(
83  $collectable_types,
84  $copyright_ids
85  );
86 
87  $this->logger->debug('Found ' . count($collectable_types) . ' collectable objects.');
88  $this->logger->dump($collectable_obj_ids, ilLogLevel::DEBUG);
89 
90  return $collectable_obj_ids;
91  }
92 
96  protected function filter($a_collectable_obj_ids)
97  {
98  global $DIC;
99 
100  $tree = $DIC->repositoryTree();
101 
102  $filtered = [];
103  foreach ($a_collectable_obj_ids as $obj_id) {
104  $status = new ilOerHarvesterObjectStatus($obj_id);
105  if ($status->isCreated()) {
106  $this->logger->debug('Object already created: ' . $obj_id);
107  continue;
108  }
109  if ($status->isBlocked()) {
110  $this->logger->debug('Object creation is blocked: ' . $obj_id);
111  continue;
112  }
113 
114  $exists = false;
115  foreach (ilObject::_getAllReferences($obj_id) as $ref_id => $tmp) {
116  if (!$tree->isDeleted($ref_id)) {
117  $exists = true;
118  }
119  }
120  if (!$exists) {
121  $this->logger->notice('Ignoring deleted object: ' . $obj_id);
122  continue;
123  }
124  $filtered[] = $obj_id;
125  }
126 
127  $this->logger->debug('Result after filtering.');
128  $this->logger->dump($filtered, ilLogLevel::DEBUG);
129 
130  return $filtered;
131  }
132 
137  protected function harvest($a_collectable_obj_ids)
138  {
139  $num = 0;
140  foreach ($a_collectable_obj_ids as $obj_id) {
141  $ref_ids = ilObject::_getAllReferences($obj_id);
142  $ref_id = end($ref_ids);
143 
144  $object = ilObjectFactory::getInstanceByRefId($ref_id, false);
145 
146  if (!$object instanceof ilObject) {
147  $this->logger->warning('Found invalid reference: ' . $ref_id);
148  }
149  $this->logger->debug('Creating new reference for object: ' . $obj_id);
150  $this->harvestObject($object);
151  $num++;
152  }
153  return $num;
154  }
155 
160  protected function harvestObject(ilObject $object)
161  {
162  $this->logger->debug('Create new reference');
163  $new_ref_id = $object->createReference();
164  $this->logger->debug('Put in tree');
165  $object->putInTree($this->settings->getTarget());
166  $this->logger->debug('Set pernissions');
167  $object->setPermissions($this->settings->getTarget());
168 
169  $this->logger->debug('Set status');
170  $status = new ilOerHarvesterObjectStatus($object->getId());
171  $status->setHarvestRefId($new_ref_id);
172  $status->setBlocked(false);
173  $status->save();
174 
175  return true;
176  }
177 
181  protected function deleteObject($a_ref_id)
182  {
183  $object = ilObjectFactory::getInstanceByRefId($a_ref_id, false);
184 
185  if (!$object instanceof ilObject) {
186  $this->logger->warning('Found invalid reference: ' . $a_ref_id);
187  return false;
188  }
189  $this->logger->debug('Deleting reference...');
190  $object->delete();
191 
192 
193  $status = new ilOerHarvesterObjectStatus(
195  );
196  $status->delete();
197  }
198 
202  protected function deleteDeprecated()
203  {
204  $num_deleted = 0;
205  foreach (ilOerHarvesterObjectStatus::lookupHarvested() as $ref_id) {
206  $obj_id = ilObject::_lookupObjId($ref_id);
207 
208  // blocked items are always deleted
209  $status = new ilOerHarvesterObjectStatus($obj_id);
210  if ($status->isBlocked()) {
211  $this->logger->debug('Deleting blocked object ressource.');
212  $this->deleteObject($ref_id);
213  $num_deleted++;
214  continue;
215  }
216 
217  $copyright = ilMDRights::_lookupDescription($obj_id, $obj_id);
218  $is_valid = false;
219  foreach ($this->settings->getCopyRightTemplatesInLomFormat() as $cp) {
220  if (strcmp($copyright, $cp) === 0) {
221  $is_valid = true;
222  }
223  }
224 
225  if (!$is_valid) {
226  $this->logger->debug('Deleting deprecated object with ref_id: ' . $ref_id);
227  $this->deleteObject($ref_id);
228  $num_deleted++;
229  }
230  }
231  return $num_deleted;
232  }
233 }
deleteDeprecated()
Delete deprecated.
settings()
Definition: settings.php:2
harvest($a_collectable_obj_ids)
$result
global $DIC
Definition: saml.php:7
Cron job for definition for oer harvesting.
static _getAllReferences($a_id)
get all reference ids of object
createReference()
creates reference for object
collect()
Collect all obj_ids with copyright settings which are collectable.
static _lookupDescription($a_rbac_id, $a_obj_id)
Lookup description (copyright)
catch(Exception $e) $message
run()
run harvester
getId()
get object id public
static _lookupObjId($a_id)
__construct(ilCronJobResult $result)
ilOerHarvester constructor.
filter($a_collectable_obj_ids)
putInTree($a_parent_ref)
maybe this method should be in tree object!?
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
harvestObject(ilObject $object)
Harvest object.
Cron job result data container.
static lookupRightsByTypeAndCopyright(array $a_types, array $a_copyright)
setPermissions($a_parent_ref)
set permissions of object
deleteObject($a_ref_id)
Delete object.