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 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( data class FileUsagesUiModel(
val title: String, val title: String,
val link: 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 { fun FileUsage.toUiModel(): FileUsagesUiModel {
// Replace spaces with underscores and URL-encode the title for the link
val encodedTitle = Uri.encode(title.replace(" ", "_"))
return FileUsagesUiModel( return FileUsagesUiModel(
title = title, 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 { 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 // Check for invalid or empty inputs
if (wiki.isEmpty() || title.isEmpty()) { if (wiki.isBlank() || title.isBlank()) {
Timber.w("Invalid GlobalFileUsage: wiki=%s, title=%s", wiki, title) Timber.w("Invalid input: wiki=$wiki, title=$title")
return FileUsagesUiModel(title = title, link = null) return FileUsagesUiModel(title = title, link = null)
} }
// determines the domain // Extract wiki code for prefix (e.g., "en" from "en.wikipedia.org" or "enwiki")
val domain = when { val wikiCode = when {
wiki.contains(".") -> wiki // Already a full domain like "en.wikipedia.org" wiki.contains(".") -> wiki.substringBefore(".") // e.g., "en" from "en.wikipedia.org"
wiki == "commonswiki" -> "commons.wikimedia.org" wiki == "commonswiki" -> "commons"
wiki.endsWith("wiki") -> { wiki.endsWith("wiki") -> wiki.removeSuffix("wiki")
val code = wiki.removeSuffix("wiki") else -> wiki
"$code.wikipedia.org"
}
else -> "$wiki.wikipedia.org" // fallback for codes like "en"
} }
val normalizedTitle = Uri.encode(title.replace(" ", "_")) // Create prefixed title, e.g., "(en) Changi East Depot"
val prefixedTitle = "($wikiCode) $title"
// construct full URL // Determine the domain for the URL
val url = "https://$domain/wiki/$normalizedTitle" val domain = when {
Timber.d("Generated URL for GlobalFileUsage: %s", url) wiki.contains(".") -> wiki // Already a full domain, e.g., "en.wikipedia.org"
wiki == "commonswiki" -> "commons.wikimedia.org"
wiki.endsWith("wiki") -> wiki.removeSuffix("wiki") + ".wikipedia.org"
else -> "$wiki.wikipedia.org" // Fallback for simple codes like "en"
}
return FileUsagesUiModel(title = title, link = url) // Normalize title: replace spaces with underscores and URL-encode
val encodedTitle = Uri.encode(title.replace(" ", "_"))
// Build the full URL
val url = "https://$domain/wiki/$encodedTitle"
Timber.d("Generated URL: $url")
return FileUsagesUiModel(title = prefixedTitle, link = url)
} }