mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Fix: URL generation for GlobalFileUsage in FileUsagesUiModel.kt for issue #6307
This commit is contained in:
parent
6dcce45c59
commit
2da54b053b
1 changed files with 35 additions and 5 deletions
|
|
@ -1,7 +1,10 @@
|
||||||
package fr.free.nrw.commons.fileusages
|
package fr.free.nrw.commons.fileusages
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show where file is being used on Commons and oher wikis.
|
* shows where file is being used on Commons and other wikis.
|
||||||
*/
|
*/
|
||||||
data class FileUsagesUiModel(
|
data class FileUsagesUiModel(
|
||||||
val title: String,
|
val title: String,
|
||||||
|
|
@ -9,10 +12,37 @@ data class FileUsagesUiModel(
|
||||||
)
|
)
|
||||||
|
|
||||||
fun FileUsage.toUiModel(): FileUsagesUiModel {
|
fun FileUsage.toUiModel(): FileUsagesUiModel {
|
||||||
return FileUsagesUiModel(title = title, link = "https://commons.wikimedia.org/wiki/$title")
|
return FileUsagesUiModel(
|
||||||
|
title = title,
|
||||||
|
link = "https://commons.wikimedia.org/wiki/${Uri.encode(title.replace(" ", "_"))}"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun GlobalFileUsage.toUiModel(): FileUsagesUiModel {
|
fun GlobalFileUsage.toUiModel(): FileUsagesUiModel {
|
||||||
// link is associated with sub items under wiki group (which is not used ATM)
|
Timber.d("GlobalFileUsage: wiki=%s, title=%s", wiki, title)
|
||||||
return FileUsagesUiModel(title = wiki, link = null)
|
|
||||||
}
|
// handles the empty or invalid wiki/title
|
||||||
|
if (wiki.isEmpty() || title.isEmpty()) {
|
||||||
|
Timber.w("Invalid GlobalFileUsage: wiki=%s, title=%s", wiki, title)
|
||||||
|
return FileUsagesUiModel(title = title, link = null)
|
||||||
|
}
|
||||||
|
|
||||||
|
// determines the domain
|
||||||
|
val domain = when {
|
||||||
|
wiki.contains(".") -> wiki // Already a full domain like "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"
|
||||||
|
}
|
||||||
|
|
||||||
|
val normalizedTitle = Uri.encode(title.replace(" ", "_"))
|
||||||
|
|
||||||
|
// construct full URL
|
||||||
|
val url = "https://$domain/wiki/$normalizedTitle"
|
||||||
|
Timber.d("Generated URL for GlobalFileUsage: %s", url)
|
||||||
|
|
||||||
|
return FileUsagesUiModel(title = title, link = url)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue