Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where dropping an invalid file onto JabRef showed no error message. [#8625](https://github.com/JabRef/jabref/issues/8625)
- We fixed JabKit BibTeX conversion losing string definitions and preambles, including when overwriting the input file. [#17409](https://github.com/JabRef/jabref/issues/17409)
- We fixed an issue where the preferences search did not scroll to its first match. [#17012](https://github.com/JabRef/jabref/pull/17012)
- We fixed an issue where "Copy DOI URL" kept tracking parameters like `?utm_source=newsletter`, producing a broken link. [#17689](https://github.com/JabRef/jabref/issues/17689)

### Removed

Expand Down
13 changes: 13 additions & 0 deletions jablib/src/main/java/org/jabref/logic/util/URLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,22 @@ public class URLUtil {
public static final Pattern URL_PATTERN = Pattern.compile(URL_REGEX, Pattern.CASE_INSENSITIVE);

private static final String URL_EXP = "^(https?|ftp)://.+";
// Matches the query part ("?...") and/or the fragment part ("#...") at the end of a URL
private static final Pattern QUERY_AND_FRAGMENT = Pattern.compile("[?#].*$");
// Detect Google search URL
private static final String GOOGLE_SEARCH_EXP = "^https?://(?:www\\.)?google\\.[\\.a-z]+?/url.*";

/// Removes the query (e.g., `?utm_source=newsletter`) and the fragment (e.g., `#section`) from a URL.
///
/// Example: `https://doi.org/10.1371/journal.pone.0000308?utm_source=newsletter` becomes
/// `https://doi.org/10.1371/journal.pone.0000308`
///
/// @param url the URL string
/// @return the URL without query and fragment
public static String removeQueryAndFragment(@NonNull String url) {
return QUERY_AND_FRAGMENT.matcher(url).replaceFirst("");
}

/// Cleans URLs returned by Google search.
/// <h4>Example</h4>
/// ```text
Expand Down
20 changes: 18 additions & 2 deletions jablib/src/main/java/org/jabref/model/entry/identifier/DOI.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public class DOI implements Identifier {
private static final String HTTP_EXP = "https?://[^\\s]+?" + DOI_EXP;
private static final String SHORT_DOI_HTTP_EXP = "https?://[^\\s]+?" + SHORT_DOI_EXP;
// Pattern
private static final Pattern DOI_RESOLVER_PREFIX = Pattern.compile("^https?://(?:www\\.|dx\\.)?doi\\.org/", Pattern.CASE_INSENSITIVE);
private static final Pattern DOI_RESOLVER_PREFIX = Pattern.compile("^https?://(?:www\\.|dx\\.)?doi\\.org(?::[0-9]+)?/", Pattern.CASE_INSENSITIVE);
// Matches doi.org links with optional scheme and port, e.g., "doi.org/", "https://dx.doi.org:443/"
private static final Pattern DOI_RESOLVER_LINK = Pattern.compile("^(?:https?://)?(?:www\\.|dx\\.)?doi\\.org(?::[0-9]+)?/", Pattern.CASE_INSENSITIVE);
private static final Pattern EXACT_DOI_PATT = Pattern.compile("^(?:https?://[^\\s]+?)?" + DOI_EXP + "$", Pattern.CASE_INSENSITIVE);
private static final Pattern FIND_DOI_PATT = Pattern.compile("(?:https?://[^\\s]+?)?" + FIND_DOI_EXP, Pattern.CASE_INSENSITIVE);
// Pattern (short DOI)
Expand Down Expand Up @@ -158,7 +160,7 @@ public DOI(String doi) {
public static Optional<DOI> parse(String doi) {
try {
LatexToUnicodeFormatter formatter = new LatexToUnicodeFormatter();
String cleanedDOI = doi;
String cleanedDOI = removeTrackingParameters(doi);
cleanedDOI = URLDecoder.decode(cleanedDOI, StandardCharsets.UTF_8);
// needs to be handled before LatexToUnicode, because otherwise `^` will be treated as conversion superscript
cleanedDOI = cleanedDOI.replaceAll("\\^", "");
Expand Down Expand Up @@ -225,6 +227,20 @@ public static Optional<LinkSearchTerms> extractLinkSearchTerms(String text) {
}
}

/// Removes the query (e.g., `?utm_source=newsletter`) and the fragment (e.g., `#section`) from a doi.org link (with or without `https://`),
/// because they are not part of the DOI.
///
/// Only DOI resolver URLs (such as `https://doi.org/...`) are changed. Other URLs may carry the DOI inside the query
/// (e.g., `https://www.scitepress.org/Link.aspx?doi=10.5220/0010404301780189`) and are returned unchanged.
/// Call this before URL decoding, because an encoded `%3F` or `%23` is a valid part of a DOI.
private static String removeTrackingParameters(String doi) {
String trimmed = doi.trim();
if (DOI_RESOLVER_LINK.matcher(trimmed).find()) {
return URLUtil.removeQueryAndFragment(trimmed);
}
return doi;
}

/// Determines whether a DOI/Short DOI is valid or not
///
/// @param doi the DOI/Short DOI string
Expand Down
11 changes: 11 additions & 0 deletions jablib/src/test/java/org/jabref/logic/net/URLUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@

class URLUtilTest {

@ParameterizedTest
@CsvSource({
"https://doi.org/10.1371/journal.pone.0000308, https://doi.org/10.1371/journal.pone.0000308?utm_source=newsletter",
"https://doi.org/10.1371/journal.pone.0000308, https://doi.org/10.1371/journal.pone.0000308#abstract",
"https://doi.org/10.1371/journal.pone.0000308, https://doi.org/10.1371/journal.pone.0000308?a=1#b",
"https://doi.org/10.1371/journal.pone.0000308, https://doi.org/10.1371/journal.pone.0000308"
})
void removeQueryAndFragment(String expected, String input) {
assertEquals(expected, URLUtil.removeQueryAndFragment(input));
}

@ParameterizedTest
@CsvSource(
textBlock = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,29 @@ void isShortDoiShouldReturnTrueWhenItIsShortDoi() {
assertTrue(new DOI("10/abcde").isShortDoi());
}

@ParameterizedTest
@CsvSource({
"https://doi.org/10.1371/journal.pone.0000308, https://doi.org/10.1371/journal.pone.0000308?utm_source=newsletter",
"https://doi.org/10.1371/journal.pone.0000308, https://doi.org/10.1371/journal.pone.0000308?utm_source=newsletter&utm_medium=email",
"https://doi.org/10.1371/journal.pone.0000308, https://doi.org/10.1371/journal.pone.0000308#abstract",
"https://doi.org/10.1371/journal.pone.0000308, https://dx.doi.org/10.1371/journal.pone.0000308?utm_source=newsletter",
"https://doi.org/10.1371/journal.pone.0000308, https://doi.org:443/10.1371/journal.pone.0000308?utm_source=newsletter",
"https://doi.org/10/gf4gqc, https://doi.org/10/gf4gqc?utm_source=newsletter",
"https://doi.org/10/bfrhmx, doi.org/bfrhmx?utm_source=x",
"https://doi.org/10/bfrhmx, https://doi.org/bfrhmx?utm_source=x",
"https://doi.org/10/bfrhmx, www.doi.org/bfrhmx?utm_source=x",
// an encoded question mark is part of the DOI and must be kept
"https://doi.org/10.1006/rwei.1999%3F.0001, https://doi.org/10.1006/rwei.1999%3F.0001"
})
void parseRemovesTrackingParametersFromDoiUrl(String expected, String input) {
assertEquals(expected, DOI.parse(input).get().getURIAsASCIIString());
}

@Test
void parseKeepsDoiInQueryOfNonResolverUrl() {
assertEquals("10.5220/0010404301780189", DOI.parse("https://www.scitepress.org/Link.aspx?doi=10.5220/0010404301780189").get().asString());
}

@Test
void noDOIFoundInsideArbitraryText() {
assertEquals(Optional.empty(), DOI.findInText("text without 28282 a doi"));
Expand Down
Loading