🚧 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.

Unverified Commit 0f69e2cb authored by Samwilson's avatar Samwilson Committed by GitHub
Browse files

Merge pull request #23 from samwilson/plantuml

Add local generation of PlantUML diagrams
parents db9a523a 95e332e0
......@@ -9,5 +9,5 @@
"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:"
"diagrams-error-generic": "Diagrams error (with $1 command):"
}
......@@ -9,5 +9,5 @@
"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-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."
"diagrams-error-generic": "Error message label for errors that don't have a more specific message. $1 is the command name."
}
......@@ -6,6 +6,7 @@ use Html;
use Http;
use LocalRepo;
use MediaWiki\MediaWikiServices;
use MediaWiki\Shell\Result;
use MediaWiki\Shell\Shell;
class Diagrams {
......@@ -55,10 +56,11 @@ class Diagrams {
],
] );
$outputFormats = [
'image' => $params['format'] ?? 'png',
'map' => $commandName === 'mscgen' ? 'ismap' : 'cmapx',
];
$outputFormats = [ 'image' => $params['format'] ?? 'png' ];
if ( $commandName !== 'plantuml' ) {
// Add image map output where it's supported.
$outputFormats['map'] = $commandName === 'mscgen' ? 'ismap' : 'cmapx';
}
$fileName = 'Diagrams ' . md5( $input ) . '.' . $outputFormats['image'];
$graphFile = $diagramsRepo->findFile( $fileName );
......@@ -71,23 +73,26 @@ class Diagrams {
}
$tmpFactory = MediaWikiServices::getInstance()->getTempFSFileFactory();
$tmpGraphSourceFile = $tmpFactory->newTempFSFile( 'diagrams_in' );
$tmpGraphSourceFile = $tmpFactory->newTempFSFile( 'diagrams_in', $commandName );
// Render image and map files.
$mapData = null;
$tmpOutFiles = [];
foreach ( $outputFormats as $outputType => $outputFormat ) {
$tmpOutFiles[$outputType] = $tmpFactory->newTempFSFile( 'diagrams_out_', $outputFormat );
if ( $commandName === 'plantuml' ) {
$input = "@startuml\n$input\n@enduml";
}
file_put_contents( $tmpGraphSourceFile->getPath(), $input );
$cmd = Shell::command(
$result = $this->runCommand(
$commandName,
'-T', $outputFormat,
'-o', $tmpOutFiles[$outputType]->getPath(),
$tmpGraphSourceFile->getPath()
$outputFormat,
$tmpGraphSourceFile->getPath(),
$tmpOutFiles[$outputType]->getPath()
);
$result = $cmd->execute();
if ( $result->getExitCode() !== 0 ) {
return $this->formatError( wfMessage( 'diagrams-error-generic' ) . ' ' . $result->getStderr() );
$errorMessage = $result->getStderr() ?? $result->getStdout();
return $this->formatError( wfMessage( 'diagrams-error-generic', $commandName ) . ' ' . $errorMessage );
}
}
......@@ -95,12 +100,30 @@ class Diagrams {
? $diagramsRepo->storeTemp( $fileName, $tmpOutFiles['image'] )
: $graphFile->publish( $tmpOutFiles['image'] );
$mapData = file_get_contents( $tmpOutFiles['map']->getPath() );
$mapData = isset( $tmpOutFiles['map'] ) ? file_get_contents( $tmpOutFiles['map']->getPath() ) : null;
return !$status->isGood()
? $this->formatError( $status->getHTML() )
: $this->getHtml( $graphFile->getUrl(), $mapData );
}
/**
* @param string $commandName
* @param string $outputFormat
* @param string $inputFilename
* @param string $outputFilename
* @return Result
*/
private function runCommand( $commandName, $outputFormat, $inputFilename, $outputFilename ): Result {
if ( $commandName === 'plantuml' ) {
$cmdArgs = [ "-t$outputFormat", '-output', dirname( $outputFilename ), '-syntax' ];
} else {
$cmdArgs = [ '-T', $outputFormat, '-o', $outputFilename ];
}
$cmd = Shell::command( array_merge( [ $commandName ], $cmdArgs, [ $inputFilename ] ) );
$cmd->restrict( Shell::RESTRICT_DEFAULT | Shell::NO_NETWORK );
return $cmd->execute();
}
/**
* Render graphs via a web service.
* @param string $commandName The command to render the graph with.
......
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