2023-08-22 21:44:36 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This action is used by the frontpage form search.
|
|
|
|
* It finds a bridge based off of a user input url.
|
|
|
|
* It uses bridges' detectParameters implementation.
|
|
|
|
*/
|
|
|
|
class FindfeedAction implements ActionInterface
|
|
|
|
{
|
2024-08-29 23:48:59 +03:00
|
|
|
private BridgeFactory $bridgeFactory;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
BridgeFactory $bridgeFactory
|
|
|
|
) {
|
|
|
|
$this->bridgeFactory = $bridgeFactory;
|
|
|
|
}
|
|
|
|
|
2024-08-07 01:21:06 +03:00
|
|
|
public function __invoke(Request $request): Response
|
2023-08-22 21:44:36 +03:00
|
|
|
{
|
2024-01-29 23:51:34 +03:00
|
|
|
$url = $request->get('url');
|
2024-01-25 18:06:24 +03:00
|
|
|
$format = $request->get('format');
|
2023-08-22 21:44:36 +03:00
|
|
|
|
2024-01-29 23:51:34 +03:00
|
|
|
if (!$url) {
|
2023-08-22 21:44:36 +03:00
|
|
|
return new Response('You must specify a url', 400);
|
|
|
|
}
|
|
|
|
if (!$format) {
|
|
|
|
return new Response('You must specify a format', 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = [];
|
2024-08-29 23:48:59 +03:00
|
|
|
foreach ($this->bridgeFactory->getBridgeClassNames() as $bridgeClassName) {
|
|
|
|
if (!$this->bridgeFactory->isEnabled($bridgeClassName)) {
|
2023-08-22 21:44:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-29 23:48:59 +03:00
|
|
|
$bridge = $this->bridgeFactory->create($bridgeClassName);
|
2023-08-22 21:44:36 +03:00
|
|
|
|
2024-01-29 23:51:34 +03:00
|
|
|
$bridgeParams = $bridge->detectParameters($url);
|
2023-08-22 21:44:36 +03:00
|
|
|
|
|
|
|
if ($bridgeParams === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's allowed to have no 'context' in a bridge (only a default context without any name)
|
|
|
|
// In this case, the reference to the parameters are found in the first element of the PARAMETERS array
|
|
|
|
|
|
|
|
$context = $bridgeParams['context'] ?? 0;
|
|
|
|
|
|
|
|
$bridgeData = [];
|
|
|
|
// Construct the array of parameters
|
|
|
|
foreach ($bridgeParams as $key => $value) {
|
|
|
|
// 'context' is a special case : it's a bridge parameters, there is no "name" for this parameter
|
|
|
|
if ($key == 'context') {
|
|
|
|
$bridgeData[$key]['name'] = 'Context';
|
|
|
|
$bridgeData[$key]['value'] = $value;
|
|
|
|
} else {
|
|
|
|
$bridgeData[$key]['name'] = $this->getParameterName($bridge, $context, $key);
|
|
|
|
$bridgeData[$key]['value'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$bridgeParams['bridge'] = $bridgeClassName;
|
|
|
|
$bridgeParams['format'] = $format;
|
|
|
|
$content = [
|
2023-12-08 08:24:43 +03:00
|
|
|
'url' => './?action=display&' . http_build_query($bridgeParams),
|
2023-08-22 21:44:36 +03:00
|
|
|
'bridgeParams' => $bridgeParams,
|
|
|
|
'bridgeData' => $bridgeData,
|
|
|
|
'bridgeMeta' => [
|
|
|
|
'name' => $bridge::NAME,
|
|
|
|
'description' => $bridge::DESCRIPTION,
|
|
|
|
'parameters' => $bridge::PARAMETERS,
|
|
|
|
'icon' => $bridge->getIcon(),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$results[] = $content;
|
|
|
|
}
|
|
|
|
if ($results === []) {
|
|
|
|
return new Response(Json::encode(['message' => 'No bridge found for given url']), 404, ['content-type' => 'application/json']);
|
|
|
|
}
|
|
|
|
return new Response(Json::encode($results), 200, ['content-type' => 'application/json']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get parameter name in the actual context, or in the global parameter
|
|
|
|
private function getParameterName($bridge, $context, $key)
|
|
|
|
{
|
|
|
|
if (isset($bridge::PARAMETERS[$context][$key]['name'])) {
|
|
|
|
$name = $bridge::PARAMETERS[$context][$key]['name'];
|
|
|
|
} else if (isset($bridge::PARAMETERS['global'][$key]['name'])) {
|
|
|
|
$name = $bridge::PARAMETERS['global'][$key]['name'];
|
|
|
|
} else {
|
|
|
|
$name = 'Variable "' . $key . '" (No name provided)';
|
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|