-
Notifications
You must be signed in to change notification settings - Fork 0
Auto-create 301 redirects when URL profile URLs change #43
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
base: main
Are you sure you want to change the base?
Changes from all commits
6149284
9dd1e89
2a9a51d
d052a53
58d5aa2
bbbe9a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of the Url package. | ||
| * | ||
| * @author (c) Thomas Blum <thomas@addoff.de> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Url; | ||
|
|
||
| class RedirectManager | ||
| { | ||
| /** | ||
| * Creates a 301 redirect from old URL to new URL in yrewrite_redirect table | ||
| * | ||
| * @param string $sourceUrl The old URL to redirect from | ||
| * @param string $targetUrl The new URL to redirect to | ||
| * @param int $domainId The yrewrite domain ID (required, no default) | ||
| * @return bool True if redirect was created successfully | ||
| */ | ||
| public static function createRedirect(string $sourceUrl, string $targetUrl, int $domainId): bool | ||
| { | ||
| if (!\rex_addon::get('yrewrite')->isAvailable()) { | ||
| return false; | ||
| } | ||
|
|
||
| // Validate URLs are non-empty | ||
| if (empty($sourceUrl) || empty($targetUrl)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Don't create redirect if source and target are the same | ||
| if ($sourceUrl === $targetUrl) { | ||
| return false; | ||
| } | ||
|
|
||
| // Remove any existing redirect that would create a loop | ||
| // If the new target URL was previously a source URL, delete it | ||
| self::deleteRedirectBySource($targetUrl); | ||
|
|
||
| // Check if redirect already exists | ||
| $sql = \rex_sql::factory(); | ||
| $existing = $sql->getArray( | ||
| 'SELECT id FROM ' . \rex::getTable('yrewrite_redirect') . | ||
| ' WHERE url_source = ? AND domain_id = ?', | ||
| [$sourceUrl, $domainId] | ||
| ); | ||
|
|
||
| if (count($existing) > 0) { | ||
| // Update existing redirect | ||
| $sql->setTable(\rex::getTable('yrewrite_redirect')); | ||
| $sql->setWhere('id = ?', [$existing[0]['id']]); | ||
| $sql->setValue('url_target', $targetUrl); | ||
| $sql->setValue('status', 301); | ||
| $sql->setValue('is_url_addon', 1); | ||
| try { | ||
| $sql->update(); | ||
| self::clearYrewriteCache(); | ||
| return true; | ||
| } catch (\rex_sql_exception $e) { | ||
| \rex_logger::logException($e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Create new redirect | ||
| $sql = \rex_sql::factory(); | ||
| $sql->setTable(\rex::getTable('yrewrite_redirect')); | ||
| $sql->setValue('domain_id', $domainId); | ||
| $sql->setValue('url_source', $sourceUrl); | ||
| $sql->setValue('url_target', $targetUrl); | ||
| $sql->setValue('status', 301); | ||
| $sql->setValue('type', 'url'); | ||
| $sql->setValue('is_url_addon', 1); | ||
|
|
||
| try { | ||
| $sql->insert(); | ||
| self::clearYrewriteCache(); | ||
| return true; | ||
| } catch (\rex_sql_exception $e) { | ||
| \rex_logger::logException($e); | ||
| return false; | ||
| } | ||
|
Comment on lines
+59
to
+86
|
||
| } | ||
|
|
||
| /** | ||
| * Deletes a redirect by its source URL to prevent loops | ||
| * | ||
| * @param string $sourceUrl The source URL of the redirect to delete | ||
| * @return bool True if redirect was deleted or didn't exist | ||
| */ | ||
| public static function deleteRedirectBySource(string $sourceUrl): bool | ||
| { | ||
| if (!\rex_addon::get('yrewrite')->isAvailable()) { | ||
| return false; | ||
| } | ||
|
|
||
| $sql = \rex_sql::factory(); | ||
| $sql->setTable(\rex::getTable('yrewrite_redirect')); | ||
| $sql->setWhere('url_source = ? AND is_url_addon = 1', [$sourceUrl]); | ||
|
|
||
| try { | ||
| $sql->delete(); | ||
| self::clearYrewriteCache(); | ||
| return true; | ||
| } catch (\rex_sql_exception $e) { | ||
|
AWqxKAWERbXo marked this conversation as resolved.
|
||
| \rex_logger::logException($e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clears the YRewrite redirect cache | ||
| */ | ||
| private static function clearYrewriteCache(): void | ||
| { | ||
| if (class_exists('\rex_yrewrite_forward')) { | ||
| \rex_yrewrite_forward::clearCache(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets all redirects created by the URL addon | ||
| * | ||
| * @return array Array of redirects | ||
| */ | ||
| public static function getUrlAddonRedirects(): array | ||
| { | ||
| if (!\rex_addon::get('yrewrite')->isAvailable()) { | ||
| return []; | ||
| } | ||
|
|
||
| $sql = \rex_sql::factory(); | ||
| return $sql->getArray( | ||
| 'SELECT * FROM ' . \rex::getTable('yrewrite_redirect') . ' WHERE is_url_addon = 1' | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -281,6 +281,22 @@ public static function getOriginAndExpanded(Profile $profile, int $datasetId, in | |
| return $sql->sql->getArray('SELECT * FROM '.\rex::getTable(self::TABLE_NAME).' WHERE `profile_id` = ? AND `data_id` = ? AND `clang_id` = ?', [$profile->getId(), $datasetId, $clangId]); | ||
| } | ||
|
|
||
| /** | ||
| * Get all URLs for a specific profile and dataset (across all languages) | ||
| * | ||
| * @param int $profileId | ||
| * @param int $datasetId | ||
| * | ||
| * @throws \rex_sql_exception | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function getOriginUrls(int $profileId, int $datasetId): array | ||
| { | ||
| $sql = self::factory(); | ||
| return $sql->sql->getArray('SELECT * FROM '.\rex::getTable(self::TABLE_NAME).' WHERE `profile_id` = ? AND `data_id` = ?', [$profileId, $datasetId]); | ||
| } | ||
|
Comment on lines
+284
to
+298
|
||
|
|
||
| /** | ||
| * @param Url $url | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential race condition: Between the check for existing redirects (lines 40-45) and the update/insert operation (lines 48-60 or 64-79), another process could create/modify/delete the same redirect entry. This could lead to unexpected behavior in concurrent scenarios. Consider using database transactions or a single UPSERT query (INSERT ... ON DUPLICATE KEY UPDATE) to make this operation atomic.