SemanticCite icon indicating copy to clipboard operation
SemanticCite copied to clipboard

Special:FindCitableMetadata: DOI lookups fail with "The response returned with an empty result" (redirect body prepended to JSON)

Open aidanfindlater opened this issue 1 month ago • 0 comments

Environment

  • Semantic Cite 5.0.0
  • Semantic MediaWiki 7.3.1
  • MediaWiki 1.46.2
  • PHP 8.4.27 (fpm-fcgi)
  • MariaDB 11.8.6

Description

Every DOI search on Special:FindCitableMetadata fails with "The response returned with an empty result". PubMed searches work. Outbound HTTP from the server works, and the DOI itself is valid.

Steps to reproduce

  1. Open Special:FindCitableMetadata/doi/10.1053/j.gastro.2023.07.018
  2. Observe the error above.
  3. Open the same URL with ?format=raw appended.

Actual result

The raw output begins with the HTML body of doi.org's 302 response, and the CrossRef JSON follows immediately after it in the same string:

<html><head><title>Handle Redirect</title></head>
<body><a href="https://api.crossref.org/v1/works/10.1053%2Fj.gastro.2023.07.018/transform">https://api.crossref.org/v1/works/10.1053%2Fj.gastro.2023.07.018/transform</a></body></html>{"indexed":{"date-parts":[[2026,10,30]], ... }

The JSON portion is complete and valid.

The HTTP debug log shows the expected chain:

[http] GET https://dx.doi.org/10.1053/j.gastro.2023.07.018 HTTP/1.1 - 302 NULL
[http] GET https://api.crossref.org/v1/works/10.1053%2Fj.gastro.2023.07.018/transform HTTP/1.1 - 200 NULL

Expected result

The response contains only the final CrossRef JSON and the metadata is returned.

Analysis

CrossRefFilteredHttpResponseParser::doFilterResponseById() passes the whole response string to json_decode(). Because the string starts with HTML, json_decode() returns null and the parser reports onoi-remi-response-empty.

The extra HTML appears to come from MediaWikiHttpRequest::doRequest(), which sets followRedirects => true on the HttpRequestFactory request and returns $request->getContent(). The docblock says this class replaced the former onoi/http-request CachedCurlRequest in 7.0. With plain cURL, -L discards intermediate redirect bodies, and curl -L with Accept: application/vnd.citationstyles.csl+json against https://dx.doi.org/<doi> returns clean JSON on the same server. I have not confirmed whether the concatenation originates in this class's usage or in MediaWiki core's redirect handling.

Every DOI lookup is affected, since all of them go through the doi.org redirect. Providers that do not redirect (PubMed) are unaffected.

A secondary effect: because no error is flagged, the unusable response is cached, so repeating the search returns "The search response was fetched from cache" until the cache lifetime expires.

Workaround

Strip everything before the first { before decoding, in CrossRefFilteredHttpResponseParser::doFilterResponseById():

$response = $this->requestResponseFor( $doi );
$start = strpos( $response, '{' );
if ( $start !== false && $start > 0 ) {
	$response = substr( $response, $start );
}
$json = json_decode( $response, true );

This restores DOI lookups, but it is a parser-level workaround for a transport-level problem.

aidanfindlater avatar Nov 03 '26 18:11 aidanfindlater