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