Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/fix #3924

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ private function createResponse(array $request, BridgeAbstract $bridge, FormatAb
try {
$bridge->loadConfiguration();
// Remove parameters that don't concern bridges
$input = array_diff_key($request, array_fill_keys(['action', 'bridge', 'format', '_noproxy', '_cache_timeout', '_error_time'], ''));
$remove = [
'action',
'bridge',
'format',
'_noproxy',
'_cache_timeout',
'_error_time',
'_', // Some RSS readers add a cache-busting parameter (_=<timestamp>) to feed URLs, detect and ignore them.
];
$input = array_diff_key($request, array_fill_keys($remove, ''));
$bridge->setInput($input);
$bridge->collectData();
$items = $bridge->getItems();
Expand Down
6 changes: 3 additions & 3 deletions actions/FrontpageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public function execute(array $request)
$body = '';
foreach ($bridgeClassNames as $bridgeClassName) {
if ($bridgeFactory->isEnabled($bridgeClassName)) {
$body .= BridgeCard::displayBridgeCard($bridgeClassName, $formats);
$body .= BridgeCard::displayBridgeCard($bridgeClassName);
$activeBridges++;
} elseif ($showInactive) {
$body .= BridgeCard::displayBridgeCard($bridgeClassName, $formats, false) . PHP_EOL;
$body .= BridgeCard::displayBridgeCard($bridgeClassName, false) . "\n";
}
}

// todo: cache this renderered template
// todo: cache this renderered template?
return render(__DIR__ . '/../templates/frontpage.html.php', [
'messages' => $messages,
'admin_email' => Configuration::getConfig('admin', 'email'),
Expand Down
4 changes: 3 additions & 1 deletion bridges/PresidenciaPTBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public function getIcon()

public function collectData()
{
foreach (array_keys($this->getParameters()['Section']) as $k) {
$contexts = $this->getParameters();

foreach (array_keys($contexts['Section']) as $k) {
Debug::log('Key: ' . var_export($k, true));
if ($this->getInput($k)) {
$html = getSimpleHTMLDOMCached($this->getURI() . $k);
Expand Down
53 changes: 33 additions & 20 deletions lib/BridgeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public function getMaintainer(): string
return static::MAINTAINER;
}

/**
* A more correct method name would have been "getContexts"
*/
public function getParameters(): array
{
return static::PARAMETERS;
Expand Down Expand Up @@ -128,16 +131,17 @@ public function loadConfiguration()

public function setInput(array $input)
{
$context = $input['context'] ?? null;
if ($context) {
// This is the submitted context
$contextName = $input['context'] ?? null;
if ($contextName) {
// Context hinting (optional)
$this->queriedContext = $context;
$this->queriedContext = $contextName;
unset($input['context']);
}

$parameters = $this->getParameters();
$contexts = $this->getParameters();

if (!$parameters) {
if (!$contexts) {
if ($input) {
throw new \Exception('Invalid parameters value(s)');
}
Expand All @@ -146,15 +150,16 @@ public function setInput(array $input)

$validator = new ParameterValidator();

// $input is passed by reference!
if (!$validator->validateInput($input, $parameters)) {
$invalidParameterKeys = array_column($validator->getInvalidParameters(), 'name');
// $input IS PASSED BY REFERENCE!
$errors = $validator->validateInput($input, $contexts);
if ($errors !== []) {
$invalidParameterKeys = array_column($errors, 'name');
throw new \Exception(sprintf('Invalid parameters value(s): %s', implode(', ', $invalidParameterKeys)));
}

// Guess the context from input data
if (empty($this->queriedContext)) {
$queriedContext = $validator->getQueriedContext($input, $parameters);
$queriedContext = $validator->getQueriedContext($input, $contexts);
$this->queriedContext = $queriedContext;
}

Expand All @@ -179,12 +184,12 @@ private function setInputWithContext(array $input, $queriedContext)
}

// Apply default values to missing data
$contexts = [$queriedContext];
$contextNames = [$queriedContext];
if (array_key_exists('global', $this->getParameters())) {
$contexts[] = 'global';
$contextNames[] = 'global';
}

foreach ($contexts as $context) {
foreach ($contextNames as $context) {
if (!isset($this->getParameters()[$context])) {
// unknown context provided by client, throw exception here? or continue?
}
Expand Down Expand Up @@ -240,7 +245,9 @@ private function setInputWithContext(array $input, $queriedContext)

// Only keep guessed context parameters values
if (isset($this->inputs[$queriedContext])) {
$this->inputs = [$queriedContext => $this->inputs[$queriedContext]];
$this->inputs = [
$queriedContext => $this->inputs[$queriedContext],
];
} else {
$this->inputs = [];
}
Expand All @@ -263,17 +270,20 @@ public function getKey($input)
if (!isset($this->inputs[$this->queriedContext][$input]['value'])) {
return null;
}
if (array_key_exists('global', $this->getParameters())) {
if (array_key_exists($input, $this->getParameters()['global'])) {
$context = 'global';

$contexts = $this->getParameters();

if (array_key_exists('global', $contexts)) {
if (array_key_exists($input, $contexts['global'])) {
$contextName = 'global';
}
}
if (!isset($context)) {
$context = $this->queriedContext;
if (!isset($contextName)) {
$contextName = $this->queriedContext;
}

$needle = $this->inputs[$this->queriedContext][$input]['value'];
foreach ($this->getParameters()[$context][$input]['values'] as $first_level_key => $first_level_value) {
foreach ($contexts[$contextName][$input]['values'] as $first_level_key => $first_level_value) {
if (!is_array($first_level_value) && $needle === (string)$first_level_value) {
return $first_level_key;
} elseif (is_array($first_level_value)) {
Expand All @@ -289,8 +299,11 @@ public function getKey($input)
public function detectParameters($url)
{
$regex = '/^(https?:\/\/)?(www\.)?(.+?)(\/)?$/';

$contexts = $this->getParameters();

if (
empty($this->getParameters())
empty($contexts)
&& preg_match($regex, $url, $urlMatches) > 0
&& preg_match($regex, static::URI, $bridgeUriMatches) > 0
&& $urlMatches[3] === $bridgeUriMatches[3]
Expand Down
Loading