From f0e5ef0fc58f3796bb4c03caa2c5e7ca78de119e Mon Sep 17 00:00:00 2001 From: Bocki Date: Mon, 6 Mar 2023 20:01:51 +0100 Subject: [PATCH] [Various] getKey replacements and docu (#3283) * [Various] getKey replacements and docu * more bridges and fix to the abstract * linting * revert bandcampdaily. doing more than i thought --- bridges/AllocineFRBridge.php | 10 +--------- bridges/AnimeUltimeBridge.php | 12 ++---------- bridges/BrutBridge.php | 9 ++------- bridges/GBAtempBridge.php | 6 +----- bridges/GithubTrendingBridge.php | 3 +-- bridges/InternetArchiveBridge.php | 4 +--- bridges/NineGagBridge.php | 23 +++-------------------- bridges/NpciBridge.php | 8 ++------ bridges/PepperBridgeAbstract.php | 4 +--- bridges/PixivBridge.php | 7 +------ bridges/SplCenterBridge.php | 6 +----- bridges/TwitScoopBridge.php | 5 +---- bridges/UnogsBridge.php | 16 +--------------- bridges/WebfailBridge.php | 5 +---- docs/06_Helper_functions/index.md | 29 +++++++++++++++++++++++++++++ lib/BridgeAbstract.php | 10 +++++++++- 16 files changed, 57 insertions(+), 100 deletions(-) diff --git a/bridges/AllocineFRBridge.php b/bridges/AllocineFRBridge.php index b93bccd2bac..e7b2adb2785 100644 --- a/bridges/AllocineFRBridge.php +++ b/bridges/AllocineFRBridge.php @@ -75,11 +75,7 @@ private function getLastSeasonURI($category) public function getName() { if (!is_null($this->getInput('category'))) { - return self::NAME . ' : ' - . array_search( - $this->getInput('category'), - self::PARAMETERS[$this->queriedContext]['category']['values'] - ); + return self::NAME . ' : ' . $this->getKey('category'); } return parent::getName(); @@ -89,10 +85,6 @@ public function collectData() { $html = getSimpleHTMLDOM($this->getURI()); - $category = array_search( - $this->getInput('category'), - self::PARAMETERS[$this->queriedContext]['category']['values'] - ); foreach ($html->find('div[class=gd-col-left]', 0)->find('div[class*=video-card]') as $element) { $item = []; diff --git a/bridges/AnimeUltimeBridge.php b/bridges/AnimeUltimeBridge.php index 23d093a63d1..15353b0a257 100644 --- a/bridges/AnimeUltimeBridge.php +++ b/bridges/AnimeUltimeBridge.php @@ -25,10 +25,7 @@ class AnimeUltimeBridge extends BridgeAbstract public function collectData() { //Add type filter if provided - $typeFilter = array_search( - $this->getInput('type'), - self::PARAMETERS[$this->queriedContext]['type']['values'] - ); + $typeFilter = $this->getKey('type'); //Build date and filters for making requests $thismonth = date('mY') . $typeFilter; @@ -128,12 +125,7 @@ public function collectData() public function getName() { if (!is_null($this->getInput('type'))) { - $typeFilter = array_search( - $this->getInput('type'), - self::PARAMETERS[$this->queriedContext]['type']['values'] - ); - - return 'Latest ' . $typeFilter . ' - Anime-Ultime Bridge'; + return 'Latest ' . $this->getKey('type') . ' - Anime-Ultime Bridge'; } return parent::getName(); diff --git a/bridges/BrutBridge.php b/bridges/BrutBridge.php index c482c2475ff..aa2a1b4d5a5 100644 --- a/bridges/BrutBridge.php +++ b/bridges/BrutBridge.php @@ -97,13 +97,8 @@ public function getURI() public function getName() { if (!is_null($this->getInput('edition')) && !is_null($this->getInput('category'))) { - $parameters = $this->getParameters(); - - $editionValues = array_flip($parameters[0]['edition']['values']); - $categoryValues = array_flip($parameters[0]['category']['values']); - - return $categoryValues[$this->getInput('category')] . ' - ' . - $editionValues[$this->getInput('edition')] . ' - Brut.'; + return $this->getKey('category') . ' - ' . + $this->getKey('edition') . ' - Brut.'; } return parent::getName(); diff --git a/bridges/GBAtempBridge.php b/bridges/GBAtempBridge.php index 523dc98011e..4aa047992c6 100644 --- a/bridges/GBAtempBridge.php +++ b/bridges/GBAtempBridge.php @@ -146,11 +146,7 @@ private function findItemImage($item, $selector) public function getName() { if (!is_null($this->getInput('type'))) { - $type = array_search( - $this->getInput('type'), - self::PARAMETERS[$this->queriedContext]['type']['values'] - ); - return 'GBAtemp ' . $type . ' Bridge'; + return 'GBAtemp ' . $this->getKey('type') . ' Bridge'; } return parent::getName(); diff --git a/bridges/GithubTrendingBridge.php b/bridges/GithubTrendingBridge.php index 50d7d37d1ea..6db765cfe33 100644 --- a/bridges/GithubTrendingBridge.php +++ b/bridges/GithubTrendingBridge.php @@ -625,8 +625,7 @@ public function collectData() public function getName() { if (!is_null($this->getInput('language'))) { - $language = array_search($this->getInput('language'), self::PARAMETERS['By language']['language']['values']); - return self::NAME . ': ' . $language; + return self::NAME . ': ' . $this->getKey('language'); } return parent::getName(); diff --git a/bridges/InternetArchiveBridge.php b/bridges/InternetArchiveBridge.php index b5701cfad25..0c5826f97ba 100644 --- a/bridges/InternetArchiveBridge.php +++ b/bridges/InternetArchiveBridge.php @@ -138,9 +138,7 @@ public function getURI() public function getName() { if (!is_null($this->getInput('username')) && !is_null($this->getInput('content'))) { - $contentValues = array_flip(self::PARAMETERS['Account']['content']['values']); - - return $contentValues[$this->getInput('content')] . ' - ' + return $this->getKey('content') . ' - ' . $this->processUsername() . ' - Internet Archive'; } diff --git a/bridges/NineGagBridge.php b/bridges/NineGagBridge.php index 61e3975d9ab..6da84d0632c 100644 --- a/bridges/NineGagBridge.php +++ b/bridges/NineGagBridge.php @@ -181,11 +181,11 @@ public function collectData() public function getName() { if ($this->getInput('d')) { - $name = sprintf('%s - %s', '9GAG', $this->getParameterKey('d')); + $name = sprintf('%s - %s', '9GAG', $this->getKey('d')); } elseif ($this->getInput('g')) { - $name = sprintf('%s - %s', '9GAG', $this->getParameterKey('g')); + $name = sprintf('%s - %s', '9GAG', $this->getKey('g')); if ($this->getInput('t')) { - $name = sprintf('%s [%s]', $name, $this->getParameterKey('t')); + $name = sprintf('%s [%s]', $name, $this->getKey('t')); } } if (!empty($name)) { @@ -236,23 +236,6 @@ protected function getPages() return $this->p; } - protected function getParameterKey($input = '') - { - $params = $this->getParameters(); - $tab = 'Sections'; - if ($input === 'd') { - $tab = 'Popular'; - } - if (!isset($params[$tab][$input])) { - return ''; - } - - return array_search( - $this->getInput($input), - $params[$tab][$input]['values'] - ); - } - protected static function getContent($post) { if ($post['type'] === 'Animated') { diff --git a/bridges/NpciBridge.php b/bridges/NpciBridge.php index 342e05624ad..f9fa63af038 100644 --- a/bridges/NpciBridge.php +++ b/bridges/NpciBridge.php @@ -47,13 +47,9 @@ class NpciBridge extends BridgeAbstract public function getName() { - $product = $this->getInput('product'); - if ($product) { - $productNameMap = array_flip(self::PARAMETERS[0]['product']['values']); - $productName = $productNameMap[$product]; - return "NPCI Circulars: $productName"; + if ($this->getInput('product')) { + return 'NPCI Circulars: ' . $this->getKey('product'); } - return 'NPCI Circulars'; } diff --git a/bridges/PepperBridgeAbstract.php b/bridges/PepperBridgeAbstract.php index 3170ef1dc70..9f7aed512bb 100644 --- a/bridges/PepperBridgeAbstract.php +++ b/bridges/PepperBridgeAbstract.php @@ -592,9 +592,7 @@ public function getName() return $this->i8n('bridge-name') . ' - ' . $this->i8n('title-keyword') . ' : ' . $this->getInput('q'); break; case $this->i8n('context-group'): - $values = $this->getParameters()[$this->i8n('context-group')]['group']['values']; - $group = array_search($this->getInput('group'), $values); - return $this->i8n('bridge-name') . ' - ' . $this->i8n('title-group') . ' : ' . $group; + return $this->i8n('bridge-name') . ' - ' . $this->i8n('title-group') . ' : ' . $this->getKey('group'); break; case $this->i8n('context-talk'): return $this->i8n('bridge-name') . ' - ' . $this->i8n('title-talk') . ' : ' . $this->getTalkTitle(); diff --git a/bridges/PixivBridge.php b/bridges/PixivBridge.php index b8f7e36b4b4..4d885cde42a 100644 --- a/bridges/PixivBridge.php +++ b/bridges/PixivBridge.php @@ -76,11 +76,7 @@ public function getName() default: return parent::getName(); } - $mode = array_search( - $this->getInput('mode'), - self::PARAMETERS['global']['mode']['values'] - ); - return "Pixiv ${mode} from ${context} ${query}"; + return 'Pixiv ' . $this->getKey('mode') . " from ${context} ${query}"; } public function getURI() @@ -149,7 +145,6 @@ private function collectWorksArray() public function collectData() { $content = $this->collectWorksArray(); - $content = array_filter($content, function ($v, $k) { return !array_key_exists('isAdContainer', $v); }, ARRAY_FILTER_USE_BOTH); diff --git a/bridges/SplCenterBridge.php b/bridges/SplCenterBridge.php index 396de3b5c0c..a590558d722 100644 --- a/bridges/SplCenterBridge.php +++ b/bridges/SplCenterBridge.php @@ -54,11 +54,7 @@ public function getURI() public function getName() { if (!is_null($this->getInput('content'))) { - $parameters = $this->getParameters(); - - $contentValues = array_flip($parameters[0]['content']['values']); - - return $contentValues[$this->getInput('content')] . ' - Southern Poverty Law Center'; + return $this->getKey('content') . ' - Southern Poverty Law Center'; } return parent::getName(); diff --git a/bridges/TwitScoopBridge.php b/bridges/TwitScoopBridge.php index 8865e06c958..b3ebe8af276 100644 --- a/bridges/TwitScoopBridge.php +++ b/bridges/TwitScoopBridge.php @@ -147,10 +147,7 @@ public function getURI() public function getName() { if (!is_null($this->getInput('country'))) { - $parameters = $this->getParameters(); - $values = array_flip($parameters[0]['country']['values']); - - return $values[$this->getInput('country')] . ' - TwitScoop'; + return $this->getKey('country') . ' - TwitScoop'; } return parent::getName(); diff --git a/bridges/UnogsBridge.php b/bridges/UnogsBridge.php index 021b75eda86..486bac3d4da 100644 --- a/bridges/UnogsBridge.php +++ b/bridges/UnogsBridge.php @@ -76,7 +76,7 @@ public function getName() if ($this->queriedContext == 'Global') { $feedName .= 'Netflix Global - '; } elseif ($this->queriedContext == 'Country') { - $feedName .= 'Netflix ' . $this->getParametersKey('country_code') . ' - '; + $feedName .= 'Netflix ' . $this->getKey('country_code') . ' - '; } if ($this->getInput('feed') == 'expiring') { $feedName .= 'Expiring title'; @@ -88,20 +88,6 @@ public function getName() return $feedName; } - private function getParametersKey($input = '') - { - $params = $this->getParameters(); - $tab = 'Country'; - if (!isset($params[$tab][$input])) { - return ''; - } - - return array_search( - $this->getInput($input), - $params[$tab][$input]['values'] - ); - } - private function getJSON($url) { $header = [ diff --git a/bridges/WebfailBridge.php b/bridges/WebfailBridge.php index e55988dad4e..09e73c2d4ae 100644 --- a/bridges/WebfailBridge.php +++ b/bridges/WebfailBridge.php @@ -48,10 +48,7 @@ public function collectData() { $html = getSimpleHTMLDOM($this->getURI() . $this->getInput('type')); - $type = array_search( - $this->getInput('type'), - self::PARAMETERS[$this->queriedContext]['type']['values'] - ); + $type = $this->getKey('type'); switch (strtolower($type)) { case 'facebook': diff --git a/docs/06_Helper_functions/index.md b/docs/06_Helper_functions/index.md index 33f9eb378ef..a25688485c9 100644 --- a/docs/06_Helper_functions/index.md +++ b/docs/06_Helper_functions/index.md @@ -7,6 +7,35 @@ $this->getInput('your input name here'); `getInput` will either return the value for your parameter or `null` if the parameter is unknown or not specified. +# getKey +The `getKey` function is used to receive the key name to a selected list value given the name of the list, specified in `const PARAMETERS` +Is able to work with multidimensional list arrays. + +```PHP +// Given a multidimensional array like this +const PARAMETERS = [[ + 'country' => [ + 'name' => 'Country', + 'type' => 'list', + 'values' => [ + 'North America' => [ + 'Mexico' => 'mx', + 'United States' => 'us' + ], + 'South America' => [ + 'Uruguay' => 'uy', + 'Venezuela' => 've' + ], + ] + ] +]], +// Provide the list name to the function +$this->getKey('country'); +// if the selected value was "ve", this function will return "Venezuela" +``` + +`getKey` will either return the key name for your parameter or `null` if the parameter is unknown or not specified. + # getContents The `getContents` function uses [cURL](https://secure.php.net/manual/en/book.curl.php) to acquire data from the specified URI while respecting the various settings defined at a global level by RSS-Bridge (i.e., proxy host, user agent, etc.). This function accepts a few parameters: diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index ecbb2529c53..8ebca650e76 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -306,8 +306,16 @@ public function getKey($input) if (!isset($this->inputs[$this->queriedContext][$input]['value'])) { return null; } + if (array_key_exists('global', static::PARAMETERS)) { + if (array_key_exists($input, static::PARAMETERS['global'])) { + $context = 'global'; + } + } + if (!isset($context)) { + $context = $this->queriedContext; + } $needle = $this->inputs[$this->queriedContext][$input]['value']; - foreach (static::PARAMETERS[$this->queriedContext][$input]['values'] as $first_level_key => $first_level_value) { + foreach (static::PARAMETERS[$context][$input]['values'] as $first_level_key => $first_level_value) { if ($needle === (string)$first_level_value) { return $first_level_key; } elseif (is_array($first_level_value)) {