ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
RepositoryOpenGraphExposer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
33
38{
39 private LOMServices $lom_services;
40
41 private bool $fetch_tile_image = false;
42
43
44 public function __construct(\ILIAS\DI\Container $dic)
45 {
46 $this->lom_services = $dic->learningObjectMetadata();
48 }
49
51 {
52 // the exposer is interested in any context, BUT the repository context
53 // will be handled differently.
54 return $this->context_collection
55 ->internal()
56 ->external()
57 ->repository();
58 }
59
60 public function getContentModification(CalledContexts $screen_context_stack): ?ContentModification
61 {
62 $current_context = $this->ensureRepoContext($screen_context_stack)->current();
63 $ref_id = $current_context->getReferenceId()->toInt();
64
65 if (
66 $ref_id > 0
67 && $this->dic->access()->checkAccess('visible', '', $ref_id)
68 && null !== ($object = $this->getObjectOfContext($current_context))
69 ) {
70 $this->exposeObjectOpenGraphMetaData($object);
71 } else {
73 }
74
75 return null;
76 }
77
78 private function exposeObjectOpenGraphMetaData(\ilObject $object): void
79 {
80 $object_translation = $object->getObjectProperties()->getPropertyTranslations();
81
82 $additional_locale_count = 0;
83 $additional_locales = [];
84
85 foreach ($this->getLanguageCodesFromLOM($object->getId(), $object->getType()) as $language_code) {
86 if ($language_code !== $object_translation->getDefaultLanguage()) {
87 $additional_locales[] = $language_code;
88 $additional_locale_count++;
89 }
90 }
91
92 $uri = $this->data->uri(\ilLink::_getStaticLink($object->getRefId(), $object->getType()));
93
94 $image = $this->getPresentationImage($object);
95
96 $this->globalScreen()->layout()->meta()->addOpenGraphMetaDatum(
97 $this->data->openGraphMetadata()->website(
98 $uri,
99 $image,
100 $object->getPresentationTitle(),
101 $uri->getHost(),
102 $object->getLongDescription() . ' ', // we add a space to ensure the description is not cut off
103 $object_translation->getDefaultLanguage(),
104 (1 < $additional_locale_count) ? array_slice($additional_locales, 1) : []
105 )
106 );
107 }
108
109 private function exposeDefaultOpenGraphMetaData(): void
110 {
111 $uri = $this->data->uri(ILIAS_HTTP_PATH);
112
113 $this->globalScreen()->layout()->meta()->addOpenGraphMetaDatum(
114 $this->data->openGraphMetadata()->website(
115 $uri,
116 $this->getDefaultImage(),
117 $this->dic->language()->txt('permission_denied'),
118 $uri->getHost(),
119 )
120 );
121 }
122
124 {
125 if (!$context->hasReferenceId()) {
126 return null;
127 }
128
129 try {
130 $current_object = \ilObjectFactory::getInstanceByRefId($context->getReferenceId()->toInt());
132 $current_object = null;
133 } finally {
134 return $current_object;
135 }
136 }
137
138 protected function getDefaultImage(): OGImage
139 {
140 $image_path_resolver = new \ilImagePathResolver();
141
142 return $this->data->openGraphMetadata()->image(
143 $this->data->uri(
144 ILIAS_HTTP_PATH . ltrim(
145 $image_path_resolver->resolveImagePath(
146 'logo/Sharing.jpg'
147 ),
148 '.'
149 )
150 ),
151 'image/jpg'
152 );
153 }
154
158 protected function getLanguageCodesFromLOM(int $object_id, string $object_type): \Generator
159 {
160 $languages_path = $this->lom_services->paths()->languages();
161 $reader = $this->lom_services->read($object_id, 0, $object_type, $languages_path);
162 foreach ($reader->allData($languages_path) as $lang_data) {
163 yield $lang_data->value();
164 }
165 }
166
167 protected function ensureRepoContext(CalledContexts $screen_context_stack): CalledContexts
168 {
169 $collection = new ContextCollection(
170 $this->dic->globalScreen()->tool()->context()->availableContexts()
171 );
172 $collection = $collection->repository();
173
174 if (!$screen_context_stack->hasMatch($collection)) {
175 $screen_context_stack = $screen_context_stack->repository();
176 }
177 return $screen_context_stack;
178 }
179
180 protected function getPresentationImage(\ilObject $object): OGImage
181 {
182 $image_factory = $this->dic->ui()->factory()->image();
183 $image = $this->getDefaultImage();
184 if (!$this->fetch_tile_image) {
185 return $image;
186 }
187 try {
188 // Use the tile image if available
189 $tile_image = $object->getObjectProperties()->getPropertyTileImage()->getTileImage();
190 if ($tile_image !== null && $tile_image->getRid() !== null) {
191 $uri_string = $tile_image->getImage($image_factory)->getAdditionalHighResSources()['960']
192 ?? $tile_image->getImage($image_factory)->getSource();
193
194 $image = $this->data->openGraphMetadata()->image(
195 $this->data->uri($uri_string),
196 'image/jpg'
197 );
198 }
199 } catch (\Throwable $e) {
200 }
201 return $image;
202 }
203}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
The Collection of all available Contexts in the System.
Class handles translation mode for an object.
getContentModification(CalledContexts $screen_context_stack)
@inheritDoc
Class ilDatabaseException.
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
Class ilObject Basic functions for all objects.
getLongDescription()
get object long description (stored in object_description)
getPresentationTitle()
get presentation title Normally same as title Overwritten for sessions
$ref_id
Definition: ltiauth.php:66
$dic
Definition: ltiresult.php:33
Class HTTPServicesTest.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$context
Definition: webdav.php:31