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 <snorlaxmonster@users.noreply.github.com>
pull/755/head
SnorlaxMonster 2024-07-18 18:30:34 +09:30 committed by GitHub
parent f4c5ca10d2
commit 2b14da434b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 36 additions and 36 deletions

View File

@ -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;
}