🚧 This instance is under construction; expect occasional downtime. Runners available in /repos. Questions? Ask in #wikimedia-gitlab on libera.chat, or under GitLab on Phabricator.

Commit dab2eb95 authored by Samwilson's avatar Samwilson
Browse files

Make the service handle non-PNG formats

Add the user-supplied format to the request to the web service,
and show an error if it's not returned for display.

Bug: GH#13
parent 64e3bbe9
......@@ -8,5 +8,6 @@
"diagrams-desc": "Render Graphviz, Mscgen, and PlantUML diagrams in wiki pages.",
"diagrams-error-no-response": "Diagrams error: no response received from remote service.",
"diagrams-error-returned-0": "Diagrams service error:",
"diagrams-error-bad-format": "Diagrams service error: format '$1' was not returned.",
"diagrams-error-generic": "Diagrams error:"
}
......@@ -6,7 +6,8 @@
},
"diagrams-extensionname": "{{name}}",
"diagrams-desc": "{{desc|name=Diagrams|url=https://www.mediawiki.org/wiki/Extension:Diagrams}}",
"diagrams-error-generic": "Error message label for errors that don't have a more specific message.",
"diagrams-error-no-response": "Error message displayed when no response is received from the web service.",
"diagrams-error-returned-0": "Error message label for errors returned by the web service."
"diagrams-error-returned-0": "Error message label for errors returned by the web service.",
"diagrams-error-bad-format": "Error message label for when the web service doesn't return the requested format.",
"diagrams-error-generic": "Error message label for errors that don't have a more specific message."
}
......@@ -102,23 +102,24 @@ class Diagrams {
}
/**
* The main rendering method, handling all types.
* @param string $generator
* @param string $input
* @param string|null $type
* @return string
* Render graphs via a web service.
* @param string $commandName The command to render the graph with.
* @param string $input The graph source.
* @param array $params Parameter to the wikitext tag (caption, format, etc.).
* @return string HTML to display the image and image map.
*/
public function renderWithService( $generator, $input, $type = null ) {
public function renderWithService( string $commandName, string $input, array $params ) {
$baseUrl = MediaWikiServices::getInstance()->getMainConfig()->get( 'DiagramsServiceUrl' );
$url = trim( $baseUrl, '/' ) . '/render';
$params = [
$format = isset( $params['format'] ) && $params['format'] ? $params['format'] : 'png';
$requestParams = [
'postData' => http_build_query( [
'generator' => $generator,
'types' => array_filter( [ 'png', $type ] ),
'generator' => $commandName,
'types' => array_filter( [ $format ] ),
'source' => $input,
] ),
];
$result = Http::request( 'POST', $url, $params, __METHOD__ );
$result = Http::request( 'POST', $url, $requestParams, __METHOD__ );
if ( $result === false ) {
return static::formatError( wfMessage( 'diagrams-error-no-response' ) );
}
......@@ -130,9 +131,13 @@ class Diagrams {
}
return static::formatError( $error );
}
// Make sure the requested format was returned.
if ( !isset( $response->diagrams->$format->url ) ) {
return static::formatError( wfMessage( 'diagrams-error-bad-format', $format ) );
}
$cmapx = $response->diagrams->cmapx->contents ?? null;
$ismapUrl = $response->diagrams->ismap->url ?? null;
return $this->getHtml( $response->diagrams->png->url, $cmapx, $ismapUrl );
return $this->getHtml( $response->diagrams->$format->url, $cmapx, $ismapUrl );
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment