From 2b14da434b03ff5e429a235e301e87c042c21d7d Mon Sep 17 00:00:00 2001 From: SnorlaxMonster <7100450+SnorlaxMonster@users.noreply.github.com> Date: Thu, 18 Jul 2024 18:30:34 +0930 Subject: [PATCH] Remove reliance on platform to construct URLs (#738) Refactor code that depends on the destination wiki's platform in order to support generic wiki platforms (e.g. Wikidot). Redirection to the Main Page now uses the destination_content_path to send users directly to the destination wiki's main page, rather than using a different URL depending on the destination wiki's software. The only remaining code that depends on the destination wiki's software is the construction of the search path. This code now has a default case that doesn't add any software-specific path to a constructed search URL. This allows any wiki, regardless of the software it runs on, to be added by putting its full search path in the "destination_search_path" property of the redirect definition. As a result of these changes, it is now possible to add redirects to Wikidot wikis, by just defining the full search path in their redirect entry's "destination_search_path". Co-authored-by: SnorlaxMonster --- scripts/common-functions.js | 72 ++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/scripts/common-functions.js b/scripts/common-functions.js index 1ba1cc7..bae54aa 100644 --- a/scripts/common-functions.js +++ b/scripts/common-functions.js @@ -175,53 +175,53 @@ function commonFunctionGetDestinationArticle(matchingSite, article) { return matchingSite['destination_content_prefix'] + article + matchingSite['destination_content_suffix']; } +function encodeArticleTitle(articleTitle) { + // We decode + encode to ensure we don't double-encode, + // in the event a string is already encoded. + // We wrap in a try-catch as decoding can sometimes fail if destination article + // does have special characters (e.g. %) in the title. + try { + return encodeURIComponent(decodeURIComponent(articleTitle)); + } catch { + return encodeURIComponent(articleTitle); + } +} + function commonFunctionGetNewURL(originURL, matchingSite) { // Get article name from the end of the URL; // We can't just take the last part of the path due to subpages; // Instead, we take everything after the wiki's base URL + content path let originArticle = commonFunctionGetOriginArticle(originURL, matchingSite); let destinationArticle = commonFunctionGetDestinationArticle(matchingSite, originArticle); + // Set up URL to redirect user to based on wiki platform let newURL = ''; - if (originArticle) { - // Check if main page - if (decodeURIComponent(originArticle) === matchingSite['origin_main_page']) { - switch (matchingSite['destination_platform']) { - case 'dokuwiki': - destinationArticle = ''; - break; - default: - destinationArticle = matchingSite['destination_main_page'] + matchingSite['destination_content_suffix']; - } - } - // Replace underscores with spaces as that performs better in search - destinationArticle = destinationArticle.replaceAll('_', ' '); - // Encode article - // We decode + encode to ensure we don't double-encode, - // in the event a string is already encoded. - // We wrap in a try-catch as decoding can sometimes fail if destination article - // does have special characters (e.g. %) in the title. - try { - destinationArticle = encodeURIComponent(decodeURIComponent(destinationArticle)); - } catch { - destinationArticle = encodeURIComponent(destinationArticle); - } - - let searchParams = ''; - switch (matchingSite['destination_platform']) { - case 'mediawiki': - searchParams = '?title=Special:Search&search=' + destinationArticle; - break; - case 'dokuwiki': - searchParams = '?do=search&q=' + destinationArticle; - break; - } - newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams; - } else { - newURL = 'https://' + matchingSite["destination_base_url"]; + // If the article is the main page (or missing), redirect to the indie wiki's main page + if ((!originArticle) || (decodeURIComponent(originArticle) === matchingSite['origin_main_page'])) { + const mainPageArticle = encodeArticleTitle(matchingSite['destination_main_page']); + newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_content_path"] + mainPageArticle + matchingSite['destination_content_suffix']; + return newURL; } + // Replace underscores with spaces as that performs better in search + const encodedDestinationArticle = encodeArticleTitle(destinationArticle.replaceAll('_', ' ')); + + let searchParams = ''; + switch (matchingSite['destination_platform']) { + case 'mediawiki': + searchParams = `?title=Special:Search&search=${encodedDestinationArticle}`; + break; + case 'dokuwiki': + searchParams = `?do=search&q=${encodedDestinationArticle}`; + break; + // Otherwise, assume the full search path is defined on "destination_search_path" + default: + searchParams = encodedDestinationArticle; + break; + } + newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams; + return newURL; }