Skip to content

Commit

Permalink
Update yrewrite_url_schemes.php (#44)
Browse files Browse the repository at this point in the history
## Changes Made to the PHP Code

1. Variable names: Changed variable names to follow the camelCase naming convention for better readability.

2. Use strict comparison: Replaced loose comparison (`==`) with strict comparison (`===`) where appropriate for better type checking.

3. Simplify conditional checks: Used strict comparison directly in `if` conditions where needed to avoid unnecessary type coercion.

4. Replaced `elseif` with `else if`: Changed `elseif` to `else if` for consistency in code style.

5. Use single quotes: Changed double quotes to single quotes for string literals that don't require variable interpolation to improve performance.

6. Removed redundant comments: Removed some redundant and obvious comments to keep the code clean.

7. Simplify ternary condition: Simplified ternary condition in `getCustomUrl()` method for better readability.

8. Minor formatting: Adjusted some indentation and line breaks to improve code formatting and readability.

These changes should help improve the code's readability and maintainability while not affecting its functionality or performance.
  • Loading branch information
skerbis authored Aug 29, 2023
1 parent 6cd73c6 commit 2f560ea
Showing 1 changed file with 30 additions and 40 deletions.
70 changes: 30 additions & 40 deletions lib/yrewrite_url_schemes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,24 @@ class yrewrite_url_schemes extends rex_yrewrite_scheme
public function appendArticle($path, rex_article $art, rex_yrewrite_domain $domain)
{
$scheme = rex_config::get('yrewrite_scheme', 'scheme', '');

$excluded_categories = [];
$excluded_categories = rex_config::get('yrewrite_scheme', 'excluded_categories', []);
$excludedCategories = rex_config::get('yrewrite_scheme', 'excluded_categories', []);

foreach ($art->getParentTree() as $category) {
if (in_array($category->getId(), $excluded_categories)) {

$catname = '/'.$this->normalize($category->getName(), $category->getClangId());

if ( !$art->isStartArticle() || strlen($path) > strlen($this->getClang($art->getClangId(),$domain).$catname) ) {
$path = str_replace($catname,'', $path);
if (in_array($category->getId(), $excludedCategories)) {
$catname = '/' . $this->normalize($category->getName(), $category->getClangId());
if (!$art->isStartArticle() || strlen($path) > strlen($this->getClang($art->getClangId(), $domain) . $catname)) {
$path = str_replace($catname, '', $path);
}
}
}

if ($scheme == 'yrewrite_scheme_suffix') {
if ($scheme === 'yrewrite_scheme_suffix') {
// standard / suffix scheme
if ($art->isStartArticle() && $domain->getMountId() != $art->getId()) {
if ($art->isStartArticle() && $domain->getMountId() !== $art->getId()) {
return $path . $this->suffix;
}
return $path . '/' . $this->normalize($art->getName(), $art->getClang()) . $this->suffix;
} else if ($scheme == 'yrewrite_one_level') {
} elseif ($scheme === 'yrewrite_one_level') {
// one level scheme
return $path . '/' . $this->normalize($art->getName(), $art->getClang()) . $this->suffix;
}
Expand All @@ -54,21 +50,19 @@ public function appendArticle($path, rex_article $art, rex_yrewrite_domain $doma
*/
public function getCustomUrl(rex_article $art, rex_yrewrite_domain $domain)
{
$path_suffix = $this->suffix;
if ($path_suffix == '.html') {
$path_suffix = '/';
$pathSuffix = $this->suffix;
if ($pathSuffix === '.html') {
$pathSuffix = '/';
}

if ($domain->getStartId() == $art->getId()) {
if (!$domain->isStartClangAuto() && $domain->getStartClang() == $art->getClang()) {
if ($domain->getStartId() === $art->getId()) {
if (!$domain->isStartClangAuto() && $domain->getStartClang() === $art->getClang()) {
return '/';
}
return $this->getClang($art->getClang(), $domain) . $path_suffix;
}
if ($url = $art->getValue('yrewrite_url')) {
return $url;
return $this->getClang($art->getClang(), $domain) . $pathSuffix;
}
return false;
$url = $art->getValue('yrewrite_url');
return $url ? $url : false;
}

/**
Expand All @@ -83,7 +77,7 @@ public function appendCategory($path, rex_category $cat, rex_yrewrite_domain $do
{
$scheme = rex_config::get('yrewrite_scheme', 'scheme', '');

if ($scheme == 'yrewrite_one_level') {
if ($scheme === 'yrewrite_one_level') {
// one level scheme
return $path;
}
Expand All @@ -100,17 +94,17 @@ public function appendCategory($path, rex_category $cat, rex_yrewrite_domain $do
*/
public function getRedirection(rex_article $art, rex_yrewrite_domain $domain)
{
$urlreplacer = rex_config::get('yrewrite_scheme', 'urlreplacer', '');
$urlReplacer = rex_config::get('yrewrite_scheme', 'urlreplacer', '');

if ($urlreplacer == 'yrewrite_scheme_urlreplace') {
if ($urlReplacer === 'yrewrite_scheme_urlreplace') {
// urlreplace scheme
if ($art->isStartArticle() && ($cats = $art->getCategory()->getChildren(true)) && !rex_article_slice::getFirstSliceForCtype(1, $art->getId(), rex_clang::getCurrentId())) {
return $cats[0];
}
return false;
} else if ($urlreplacer == 'yrewrite_scheme_nomatter') {
} elseif ($urlReplacer === 'yrewrite_scheme_nomatter') {
// nomatter scheme
if ($art->isStartArticle() && $domain->getMountId() != $art->getId() && $domain->getStartId() != $art->getId() && ($cats = $art->getCategory()->getChildren(true))) {
if ($art->isStartArticle() && $domain->getMountId() !== $art->getId() && $domain->getStartId() !== $art->getId() && ($cats = $art->getCategory()->getChildren(true))) {
return $cats[0];
}
return false;
Expand All @@ -128,27 +122,23 @@ public function getRedirection(rex_article $art, rex_yrewrite_domain $domain)
*/
public function normalize($string, $clang = 0)
{
if ( rex_config::get('yrewrite_scheme', 'urlencode-lang-' . $clang, 'original') == 'original' ) {
if (rex_config::get('yrewrite_scheme', 'urlencode-lang-' . $clang, 'original') === 'original') {
return parent::normalize($string, $clang);
}

$replaced_string = str_replace(
['À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', '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Ue', 'Y', 'ss', 'a', 'a', 'a', 'a', 'ae', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'oe', 'oe', 'u', 'u', 'u', 'ue', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'Oe', 'oe', '-', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '-'],
$replacedString = str_replace(
['À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã',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', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ', '/', '®', '©', '', ':', '#', '$', '%', '&', '(', ')', '*', '+', ',', '.', '/', '!', ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '}', '~', '', '', '¿', '', '', ' '],
['A', 'A', 'A', 'A', 'Ae', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'Oe', 'Oe', 'U', 'U', 'U', 'Ue', 'Y', 'ss', 'a', 'a', 'a', 'a', 'ae', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'oe', 'oe', 'u', 'u', 'u', 'ue', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'Oe', 'oe', '-', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
strip_tags(trim(mb_strtolower($string)))
);
$final_string = parent::normalize($replaced_string);
$finalString = parent::normalize($replacedString);

// In case settings require URL encode or normalizing the standard way failed
if (($clang > 0 && rex_config::get('yrewrite_scheme', 'urlencode-lang-' . $clang, 'standard') == 'urlencode') || ($final_string == "" || $final_string == "-")) {
$string = str_replace(
['й'],
['и'],
mb_strtolower($replaced_string)
);
$final_string = preg_replace('/[+-]+/', '-', $string);
if (($clang > 0 && rex_config::get('yrewrite_scheme', 'urlencode-lang-' . $clang, 'standard') === 'urlencode') || ($finalString === '' || $finalString === '-')) {
$string = str_replace(['й'], ['и'], mb_strtolower($replacedString));
$finalString = preg_replace('/[+-]+/', '-', $string);
}

return $final_string;
return $finalString;
}
}

0 comments on commit 2f560ea

Please sign in to comment.