Added the wiki prefix to titles in GlobalFileUsage for issue #6416 (#6417)

This commit is contained in:
VoidRaven 2025-09-04 16:17:53 +05:30 committed by GitHub
parent e2c8f85a5b
commit a59bf64677
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,45 +4,65 @@ import android.net.Uri
import timber.log.Timber
/**
* shows where file is being used on Commons and other wikis.
* Data model for displaying file usage information in the UI, including the title and link to the page.
*/
data class FileUsagesUiModel(
val title: String,
val link: String?
)
/**
* Converts a FileUsage object to a UI model for Commons file usages.
* Creates a link to the file's page on Commons.
*/
fun FileUsage.toUiModel(): FileUsagesUiModel {
// Replace spaces with underscores and URL-encode the title for the link
val encodedTitle = Uri.encode(title.replace(" ", "_"))
return FileUsagesUiModel(
title = title,
link = "https://commons.wikimedia.org/wiki/${Uri.encode(title.replace(" ", "_"))}"
link = "https://commons.wikimedia.org/wiki/$encodedTitle"
)
}
/**
* Converts a GlobalFileUsage object to a UI model for file usages on other wikis.
* Generates a link to the page and prefixes the title with the wiki code (e.g., "(en) Title").
*/
fun GlobalFileUsage.toUiModel(): FileUsagesUiModel {
Timber.d("GlobalFileUsage: wiki=%s, title=%s", wiki, title)
// Log input values for debugging
Timber.d("Converting GlobalFileUsage: wiki=$wiki, title=$title")
// handles the empty or invalid wiki/title
if (wiki.isEmpty() || title.isEmpty()) {
Timber.w("Invalid GlobalFileUsage: wiki=%s, title=%s", wiki, title)
// Check for invalid or empty inputs
if (wiki.isBlank() || title.isBlank()) {
Timber.w("Invalid input: wiki=$wiki, title=$title")
return FileUsagesUiModel(title = title, link = null)
}
// determines the domain
// Extract wiki code for prefix (e.g., "en" from "en.wikipedia.org" or "enwiki")
val wikiCode = when {
wiki.contains(".") -> wiki.substringBefore(".") // e.g., "en" from "en.wikipedia.org"
wiki == "commonswiki" -> "commons"
wiki.endsWith("wiki") -> wiki.removeSuffix("wiki")
else -> wiki
}
// Create prefixed title, e.g., "(en) Changi East Depot"
val prefixedTitle = "($wikiCode) $title"
// Determine the domain for the URL
val domain = when {
wiki.contains(".") -> wiki // Already a full domain like "en.wikipedia.org"
wiki.contains(".") -> wiki // Already a full domain, e.g., "en.wikipedia.org"
wiki == "commonswiki" -> "commons.wikimedia.org"
wiki.endsWith("wiki") -> {
val code = wiki.removeSuffix("wiki")
"$code.wikipedia.org"
}
else -> "$wiki.wikipedia.org" // fallback for codes like "en"
wiki.endsWith("wiki") -> wiki.removeSuffix("wiki") + ".wikipedia.org"
else -> "$wiki.wikipedia.org" // Fallback for simple codes like "en"
}
val normalizedTitle = Uri.encode(title.replace(" ", "_"))
// Normalize title: replace spaces with underscores and URL-encode
val encodedTitle = Uri.encode(title.replace(" ", "_"))
// construct full URL
val url = "https://$domain/wiki/$normalizedTitle"
Timber.d("Generated URL for GlobalFileUsage: %s", url)
// Build the full URL
val url = "https://$domain/wiki/$encodedTitle"
Timber.d("Generated URL: $url")
return FileUsagesUiModel(title = title, link = url)
return FileUsagesUiModel(title = prefixedTitle, link = url)
}