ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ActionDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Data\URI;
24 
29 {
30  private const TABLE_NAME = 'wopi_action';
31  private array $edit_actions = [ActionTarget::EDIT, ActionTarget::EMBED_EDIT];
32  private array $view_actions = [ActionTarget::VIEW, ActionTarget::EMBED_VIEW];
33 
34  public function __construct(
35  private \ilDBInterface $db
36  ) {
37  }
38 
39  private function implodeTargets(ActionTarget ...$targets): string
40  {
41  return implode(
42  ', ',
43  array_map(
44  fn(ActionTarget $target): string => $this->db->quote($target->value, 'text'),
45  $targets
46  )
47  );
48  }
49 
50  public function hasActionForSuffix(
51  string $suffix,
52  ActionTarget ...$action_target
53  ): bool {
54  static $cache = [];
55 
56  $implode_targets = $this->implodeTargets(...$action_target);
57 
58  if (isset($cache[$suffix][$implode_targets])) {
59  return $cache[$suffix][$implode_targets];
60  }
61 
62  $query = 'SELECT * FROM '
63  . self::TABLE_NAME
64  . ' WHERE ext = %s AND name IN(' . $implode_targets . ')';
65 
66  $result = $this->db->queryF(
67  $query,
68  ['text'],
69  [strtolower($suffix)]
70  );
71  return $cache[$suffix][$implode_targets] = $result->numRows() > 0;
72  }
73 
74  public function hasEditActionForSuffix(string $suffix): bool
75  {
76  return $this->hasActionForSuffix($suffix, ...$this->edit_actions);
77  }
78 
79  public function hasViewActionForSuffix(string $suffix): bool
80  {
81  return $this->hasActionForSuffix($suffix, ...$this->view_actions);
82  }
83 
84  public function getActionForSuffix(
85  string $suffix,
86  ActionTarget $action_target
87  ): ?Action {
88  if (!$this->hasActionForSuffix($suffix, $action_target)) {
89  return null;
90  }
91 
92  $query = 'SELECT * FROM '
93  . self::TABLE_NAME
94  . ' WHERE ext = %s AND name = %s';
95 
96  $result = $this->db->queryF($query, ['text', 'text'], [strtolower($suffix), $action_target->value]);
97  $row = $this->db->fetchAssoc($result);
98  return $this->fromDBRow($row);
99  }
100 
101  public function getEditActionForSuffix(string $suffix): ?Action
102  {
103  foreach ($this->edit_actions as $action_target) {
104  $action = $this->getActionForSuffix($suffix, $action_target);
105  if ($action !== null) {
106  return $action;
107  }
108  }
109  return null;
110  }
111 
112  public function getViewActionForSuffix(string $suffix): ?Action
113  {
114  foreach ($this->view_actions as $action_target) {
115  $action = $this->getActionForSuffix($suffix, $action_target);
116  if ($action !== null) {
117  return $action;
118  }
119  }
120  return null;
121  }
122 
123  public function getActions(): array
124  {
125  $actions = [];
126  $query = 'SELECT * FROM ' . self::TABLE_NAME;
127  $result = $this->db->query($query);
128  while ($row = $this->db->fetchAssoc($result)) {
129  $actions[] = $this->fromDBRow($row);
130  }
131  return $actions;
132  }
133 
134  public function getActionsForTarget(ActionTarget $action_target): array
135  {
136  $query = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE name = %s';
137  $result = $this->db->queryF($query, ['text'], [$action_target->value]);
138  $actions = [];
139  while ($row = $this->db->fetchAssoc($result)) {
140  $actions[] = $this->fromDBRow($row);
141  }
142  return $actions;
143  }
144 
145  public function getActionsForTargets(ActionTarget ...$action_target): array
146  {
147  $actions = [];
148  foreach ($action_target as $target) {
149  $actions += $this->getActionsForTarget($target);
150  }
151  return $actions;
152  }
153 
154  public function getSupportedSuffixes(ActionTarget ...$action_target): array
155  {
156  $query = 'SELECT ext FROM '
157  . self::TABLE_NAME
158  . ' WHERE name IN (' . $this->implodeTargets(...$action_target) . ')';
159  $result = $this->db->query($query);
160  $suffixes = [];
161  while ($row = $this->db->fetchAssoc($result)) {
162  $suffixes[] = $row['ext'];
163  }
164  return $suffixes;
165  }
166 
167  public function getActionsForApp(App $app): array
168  {
169  $actions = [];
170  $query = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE app_id = %s';
171  $result = $this->db->queryF($query, ['integer'], [$app->getId()]);
172  while ($row = $this->db->fetchAssoc($result)) {
173  $actions[] = $this->fromDBRow($row);
174  }
175  return $actions;
176  }
177 
178  private function fromDBRow(array $row): Action
179  {
180  return new Action(
181  (int) $row['id'],
182  (string) $row['name'],
183  (string) $row['ext'],
184  new URI((string) $row['urlsrc']),
185  empty($row['url_appendix']) ? null : (string) $row['url_appendix'],
186  empty($row['target_text']) ? null : (string) $row['target_text']
187  );
188  }
189 
190  public function clear(): void
191  {
192  $query = 'DELETE FROM ' . self::TABLE_NAME;
193  $this->db->manipulate($query);
194  }
195 
196  public function clearSuperfluous(Action ...$actions): void
197  {
198  $collected_ids = array_map(
199  static fn(Action $act): int => $act->getId(),
200  $actions
201  );
202  $query = 'DELETE FROM ' . self::TABLE_NAME . ' WHERE ' . $this->db->in('id', $collected_ids, true, 'integer');
203  $this->db->manipulate($query);
204  }
205 
206  public function store(Action $action, App $for_app): void
207  {
208  // store only actions with extensions
209  if (empty($action->getExtension())) {
210  return;
211  }
212 
213  // check for existing action to update them
214  $query = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE name = %s AND ext = %s AND target_ext = %s';
215  $result = $this->db->queryF(
216  $query,
217  ['text', 'text', 'text'],
218  [$action->getName(), $action->getExtension(), $action->getTargetExtension()]
219  );
220 
221  if ($this->db->numRows($result) > 0) {
222  $row = $this->db->fetchAssoc($result);
223  $action = $action->withId((int) $row['id']);
224  }
225 
226  if ($this->db->queryF(
227  'SELECT * FROM ' . self::TABLE_NAME . ' WHERE id = %s',
228  ['integer'],
229  [$action->getId()]
230  )->numRows() === 0) {
231  $next_id = (int) $this->db->nextId(self::TABLE_NAME);
232  $this->db->insert(self::TABLE_NAME, [
233  'id' => ['integer', $next_id],
234  'name' => ['text', $action->getName()],
235  'ext' => ['text', strtolower($action->getExtension())],
236  'urlsrc' => ['text', $action->getLauncherUrl()],
237  'app_id' => ['integer', $for_app->getId()],
238  'url_appendix' => ['text', $action->getUrlAppendix()],
239  'target_ext' => ['text', $action->getTargetExtension()],
240  ]);
241  $action = $action->withId($next_id);
242  } else {
243  $this->db->update(self::TABLE_NAME, [
244  'name' => ['text', $action->getName()],
245  'ext' => ['text', strtolower($action->getExtension())],
246  'urlsrc' => ['text', $action->getLauncherUrl()],
247  'app_id' => ['integer', $for_app->getId()],
248  'url_appendix' => ['text', $action->getUrlAppendix()],
249  'target_ext' => ['text', $action->getTargetExtension()],
250  ], [
251  'id' => ['integer', $action->getId()],
252  ]);
253  }
254  }
255 }
getActionsForTargets(ActionTarget ... $action_target)
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:34
ActionTarget
Officialy supported action targets, see https://learn.microsoft.com/en-us/openspecs/office_protocols/...
hasActionForSuffix(string $suffix, ActionTarget ... $action_target)
getSupportedSuffixes(ActionTarget ... $action_target)
getActionForSuffix(string $suffix, ActionTarget $action_target)