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