Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}