ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
AutomaticPublisher.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ilLogger;
29use ILIAS\MetaData\Copyright\Search\FactoryInterface as CopyrightSearchFactory;
34
36{
37 public function __construct(
38 protected PublisherInterface $publisher,
39 protected SettingsInterface $settings,
40 protected ObjectHandler $object_handler,
41 protected StatusRepository $status_repository,
42 protected ExposedRecordRepository $exposed_record_repository,
43 protected CopyrightSearchFactory $copyright_search_factory,
44 protected LOMRepository $lom_repository,
45 protected SimpleDCXMLWriter $xml_writer,
46 protected ilLogger $logger
47 ) {
48 }
49
50 public function run(Result $result): Result
51 {
52 try {
53 $messages = [];
54
55 $harvestable_obj_ids = $this->findHarvestableObjectIDs();
56 $currently_harvested_obj_ids = iterator_to_array($this->status_repository->getAllHarvestedObjIDs());
57
58 $deletion_count = $this->withdrawDeprecatedObjects(
59 $harvestable_obj_ids,
60 $currently_harvested_obj_ids
61 );
62 $messages[] = 'Withdrew ' . $deletion_count . ' deprecated objects.';
63
64 $exposure_count = $this->updatePublishedObjects();
65 $messages[] = 'Updated ' . $exposure_count . ' published objects.';
66
67 $harvest_count = 0;
68 if ($this->settings->isAutomaticPublishingEnabled()) {
69 $harvest_count = $this->publishObjects(
70 $harvestable_obj_ids,
71 $currently_harvested_obj_ids
72 );
73 $messages[] = 'Published or submitted for review ' . $harvest_count . ' new objects.';
74 }
75
76 if ($deletion_count !== 0 || $harvest_count !== 0 || $exposure_count !== 0) {
77 $result = $result->withStatus(JobResult::STATUS_OK);
78 } else {
79 $result = $result->withStatus(JobResult::STATUS_NO_ACTION);
80 }
81 return $result->withMessage(implode('<br>', $messages));
82 } catch (\Exception $e) {
83 return $result->withStatus(JobResult::STATUS_FAIL)
84 ->withMessage($e->getMessage());
85 }
86 }
87
88 protected function findHarvestableObjectIDs(): array
89 {
90 $searcher = $this->copyright_search_factory->get()
91 ->withRestrictionToRepositoryObjects(true);
92 foreach ($this->settings->getObjectTypesSelectedForPublishing() as $type) {
93 $searcher = $searcher->withAdditionalTypeFilter($type);
94 }
95 $search_results = [];
96 foreach ($searcher->search(
97 $this->lom_repository,
98 ...$this->settings->getCopyrightEntryIDsSelectedForPublishing()
99 ) as $ressource_id) {
100 $search_results[] = $ressource_id->objID();
101 }
102
103 $unblocked = $this->status_repository->filterOutBlockedObjects(...$search_results);
104 $results = [];
105 foreach ($unblocked as $obj_id) {
106 if ($this->object_handler->isObjectDeleted($obj_id)) {
107 continue;
108 }
109 $results[] = $obj_id;
110 }
111 return $results;
112 }
113
119 protected function withdrawDeprecatedObjects(
120 array $harvestable_obj_ids,
121 array $currently_harvested_obj_ids
122 ): int {
123 $count = 0;
124 foreach ($currently_harvested_obj_ids as $obj_id) {
125 if (
126 in_array($obj_id, $harvestable_obj_ids) &&
127 $this->object_handler->doesReferenceExist($this->status_repository->getHarvestRefID($obj_id))
128 ) {
129 continue;
130 }
131
132 $this->logDebug('Withdrawing deprecated object with obj: ' . $obj_id);
133 try {
134 $this->publisher->withdraw($obj_id);
135 } catch (\Exception $e) {
136 $this->logError(
137 'Error when withdrawing from publishing object with obj_id ' .
138 $obj_id . ': ' . $e->getMessage()
139 );
140 continue;
141 }
142 $count++;
143 }
144 return $count;
145 }
146
152 protected function publishObjects(
153 array $harvestable_obj_ids,
154 array $currently_harvested_obj_ids
155 ): int {
156 $count = 0;
157
158 foreach ($harvestable_obj_ids as $obj_id) {
159 if (in_array($obj_id, $currently_harvested_obj_ids)) {
160 continue;
161 }
162
163 $this->logDebug('Publishing object with obj_id: ' . $obj_id);
164 try {
165 if ($this->settings->isEditorialStepEnabled()) {
166 $this->publisher->submit($obj_id);
167 } else {
168 $type = $this->object_handler->getTypeOfObject($obj_id);
169 $this->publisher->publish($obj_id, $type);
170 }
171 } catch (\Exception $e) {
172 $this->logError(
173 'Error when publishing object with obj_id ' .
174 $obj_id . ': ' . $e->getMessage()
175 );
176 continue;
177 }
178
179 $count++;
180 }
181 return $count;
182 }
183
187 protected function updatePublishedObjects(): int
188 {
189 $count = 0;
190
191 foreach ($this->exposed_record_repository->getRecords() as $record) {
192 $obj_id = $record->infos()->objID();
193 $ref_id = $this->status_repository->getHarvestRefID($obj_id);
194
195 if (!$ref_id && $record->infos()->isDeleted()) {
196 continue;
197 }
198
199 $simple_dc_xml = $this->xml_writer->writeSimpleDCMetaData(
200 $obj_id,
201 $ref_id,
202 $this->object_handler->getTypeOfObject($obj_id)
203 );
204
205 if (
206 $record->infos()->isDeleted() ||
207 $simple_dc_xml->saveXML() !== $record->metadata()->saveXML()
208 ) {
209 // TODO should also be done by the publisher
210 $this->logDebug('Updating exposed record for object with obj_id: ' . $obj_id);
211 $this->exposed_record_repository->updateRecord($obj_id, false, $simple_dc_xml);
212 $count++;
213 }
214 }
215
216 $this->cleanUpDeletedRecords();
217
218 return $count;
219 }
220
221 protected function cleanUpDeletedRecords(): void
222 {
223 $this->exposed_record_repository->deleteRecordsMarkedAsDeletedOlderThan(
224 new \DateInterval('P30D')
225 );
226 }
227
228 protected function logDebug(string $message): void
229 {
230 $this->logger->debug($message);
231 }
232
233 protected function logError(string $message): void
234 {
235 $this->logger->error($message);
236 }
237}
final const int STATUS_FAIL
Definition: JobResult.php:30
final const int STATUS_NO_ACTION
Definition: JobResult.php:26
final const int STATUS_OK
Definition: JobResult.php:27
withdrawDeprecatedObjects(array $harvestable_obj_ids, array $currently_harvested_obj_ids)
Returns number of deletions.
updatePublishedObjects()
Returns number of changed published records.
publishObjects(array $harvestable_obj_ids, array $currently_harvested_obj_ids)
Returns number of published/submitted objects.
__construct(protected PublisherInterface $publisher, protected SettingsInterface $settings, protected ObjectHandler $object_handler, protected StatusRepository $status_repository, protected ExposedRecordRepository $exposed_record_repository, protected CopyrightSearchFactory $copyright_search_factory, protected LOMRepository $lom_repository, protected SimpleDCXMLWriter $xml_writer, protected ilLogger $logger)
Component logger with individual log levels by component id.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
$ref_id
Definition: ltiauth.php:66
$results