From 18011f2fa73880fde52f726800572204471fa1b9 Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 2 Oct 2024 11:25:14 +0200 Subject: [PATCH] feat(routing): add dynamic inbound redirects Introduce a new route to handle redirects for different Wikimedia project domains. The route dynamically identifies the target project and language based on the domain name and redirects accordingly. If the project is not found, it displays a custom error page. Enhances user navigation across Wikimedia subdomains, improving user experience and simplifying URL management. Addresses redirect issues for integration with LibRedirect (https://codeberg.org/LibRedirect/browser_extension/issues/142) --- src/wikimore/app.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/wikimore/app.py b/src/wikimore/app.py index 2eec6d1..15558b3 100644 --- a/src/wikimore/app.py +++ b/src/wikimore/app.py @@ -232,6 +232,41 @@ def search() -> Union[Text, Response]: return render_template("search.html") +@app.route("//") +def inbound_redirect(domain: str, url: str) -> Union[Text, Response, Tuple[Text, int]]: + """Redirects to the appropriate project/language combination given the domain name of a Wikimedia project + + Args: + domain (str): The domain of the Wikimedia project. + url (str): The URL path. + + Returns: + Response: A redirect to the corresponding route + """ + for language, language_projects in app.languages.items(): + for project_name, project_url in language_projects["projects"].items(): + if project_url == f"https://{domain}": + return redirect( + f"{url_for('home')}{project_name}/{language}/{url}" + ) + + for project_name, project_url in app.languages["special"]["projects"].items(): + if project_url == f"https://{domain}": + return redirect( + f"{url_for('home')}/{project_name}/{language}/{url}" + ) + + # TODO / IDEA: Handle non-Wikimedia Mediawiki projects here? + + return ( + render_template( + "article.html", + title="Project does not exist", + content=f"Sorry, the project {domain} does not exist.", + ), + 404, + ) + @app.route("///wiki/") def wiki_article( project: str, lang: str, title: str