ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Crawler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Data\URI;
24 
28 class Crawler
29 {
30  private const XPATH = '/wopi-discovery/net-zone/app';
31  private array $crawl_actions = [];
32  private ?string $content = null;
33  private ?\SimpleXMLElement $discovery = null;
37  private ?array $xml_app_elements = null;
38 
39  public function __construct()
40  {
41  $this->crawl_actions = [
42  ActionTarget::VIEW->value,
43  ActionTarget::EMBED_VIEW->value,
44  ActionTarget::EDIT->value,
45  ActionTarget::EMBED_EDIT->value,
46  ActionTarget::CONVERT->value,
47  ];
48  }
49 
50  public function validate(URI $discovery_url): bool
51  {
52  try {
53  $this->content = file_get_contents((string) $discovery_url) ?: null;
54  if ($this->content === null) {
55  return false;
56  }
57 
58  $this->discovery = simplexml_load_string($this->content) ?: null;
59  if ($this->discovery === null) {
60  return false;
61  }
62  $this->xml_app_elements = $this->discovery->xpath(self::XPATH);
63 
64  return is_array($this->xml_app_elements);
65  } catch (\Throwable $t) {
66  return false;
67  }
68  }
69 
70  public function crawl(URI $discovery_url): ?Apps
71  {
72  if (!$this->validate($discovery_url)) {
73  return null;
74  }
75 
76  // read wopi-discovery XML from $discovery_url and parse Apps with it's Actions
77  $apps = [];
78  foreach ($this->xml_app_elements as $app) {
79  $actions = [];
80  foreach ($app->action as $action) {
81  $action_name = $action['name'] ?? null;
82  $action_ext = $action['ext'] ?? null;
83  $action_urlsrc = $action['urlsrc'] ?? null;
84  $target_text = isset($action['targetext']) ? (string) $action['targetext'] : null;
85  if (!$action_name instanceof \SimpleXMLElement) {
86  continue;
87  }
88  if (!$action_ext instanceof \SimpleXMLElement) {
89  continue;
90  }
91  if (!$action_urlsrc instanceof \SimpleXMLElement) {
92  continue;
93  }
94 
95  if (!in_array((string) $action_name, $this->crawl_actions, true)) {
96  continue;
97  }
98 
99  $uri_string = rtrim((string) $action_urlsrc, '?');
100  // remove all after ?
101  $uri = explode('?', $uri_string);
102  $uri_string = $uri[0];
103  $actions[] = new Action(
104  0,
105  (string) $action_name,
106  (string) $action_ext,
107  new URI($uri_string),
108  $uri[1] ?? null,
109  $target_text
110  );
111  }
112  if ($actions === []) {
113  continue;
114  }
115 
116  $app_name = $app['name'] ?? null;
117  if ($app_name === null) {
118  continue;
119  }
120  $app_fav_icon_url = $app['favIconUrl'] ?? null;
121  $apps[] = new App(
122  0,
123  (string) $app_name,
124  $actions,
125  $app_fav_icon_url === null ? null : new URI((string) $app_fav_icon_url)
126  );
127  }
128  return new Apps($apps);
129  }
130 }
$app
Definition: cli.php:39
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:18
validate(URI $discovery_url)
Definition: Crawler.php:50
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Crawler.php:21