Export svg as File page on save

Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
2026-05-21 21:27:07 -07:00
parent ca9656c56b
commit db5689adbc

View File

@@ -15,7 +15,7 @@ use Html;
use IContextSource; use IContextSource;
use MediaWiki\Content\ValidationParams; use MediaWiki\Content\ValidationParams;
use Status; use Status;
use UploadFromFile;
class AnnotationHandler extends CodeContentHandler { class AnnotationHandler extends CodeContentHandler {
private const ANNOT_VERSION = '0.1'; private const ANNOT_VERSION = '0.1';
@@ -73,14 +73,7 @@ class AnnotationHandler extends CodeContentHandler {
return; return;
} }
if (isset($metadata['baseImage'])) { $output->setText( self::buildSvg($metadata) );
$imageTitle = Title::makeTitleSafe( NS_FILE, $metadata['baseImage'] );
$baseImage = MediaWikiServices::getInstance()->getRepoGroup()->findFile($imageTitle);
} else {
$baseImage = false;
}
$output->setText( self::buildSvg($metadata,$baseImage) );
} }
/** /**
@@ -90,10 +83,17 @@ class AnnotationHandler extends CodeContentHandler {
* and produces the html string for the svg with base image and annotations. * and produces the html string for the svg with base image and annotations.
* *
* @param string $metadata The metadata object parsed from the text * @param string $metadata The metadata object parsed from the text
* @param File $baseImage The full url pointing to the base image to annotate
* @return string Html string of the complete svg * @return string Html string of the complete svg
*/ */
private function buildSvg($metadata, $baseImage) { private function buildSvg($metadata) {
//Check if the image to annotate has been set
if (isset($metadata['baseImage'])) {
$imageTitle = Title::makeTitleSafe( NS_FILE, $metadata['baseImage'] );
$baseImage = MediaWikiServices::getInstance()->getRepoGroup()->findFile($imageTitle);
} else {
$baseImage = false;
}
//Gather basic data //Gather basic data
$elBaseImg = ''; $elBaseImg = '';
$vbHeight = 100; $vbHeight = 100;
@@ -123,12 +123,6 @@ class AnnotationHandler extends CodeContentHandler {
'xml:space' => 'preserve', 'xml:space' => 'preserve',
'xmlns' => 'http://www.w3.org/2000/svg' 'xmlns' => 'http://www.w3.org/2000/svg'
); );
if ($this->parameters['width'] > 0) {
$attrSvg['width'] = $this->parameters['width'];
}
elseif ($this->parameters['height'] > 0) {
$attrSvg['height'] = $this->parameters['height'];
}
if (isset($metadata['annotation'])) { if (isset($metadata['annotation'])) {
$markers = []; $markers = [];
@@ -148,7 +142,7 @@ class AnnotationHandler extends CodeContentHandler {
'cy' => $annot['position']['1'], 'cy' => $annot['position']['1'],
'r' => '3.75', 'r' => '3.75',
'style' => 'fill-opacity: .35; stroke-linejoin: bevel; stroke-width: .3793; stroke: #ff0000;', 'style' => 'fill-opacity: .35; stroke-linejoin: bevel; stroke-width: .3793; stroke: #ff0000;',
'fill' => (isset($annot['light']) && $annot['light']) ? 'white' : 'balck' 'fill' => (isset($annot['light']) && $annot['light']) ? 'white' : 'black'
); );
$markCirc = Html::rawElement('circle',$attrMarkerCirc); $markCirc = Html::rawElement('circle',$attrMarkerCirc);
array_push($markers,$markCirc); array_push($markers,$markCirc);
@@ -168,14 +162,45 @@ class AnnotationHandler extends CodeContentHandler {
public function validateSave( Content $content, ValidationParams $validationParams ) { public function validateSave( Content $content, ValidationParams $validationParams ) {
self::console_log('Validate Save', true); self::console_log('Validate Save', true);
$tempDir = sys_get_temp_dir();
$status = Status::newGood(); $status = Status::newGood();
try { try {
$tomlCheck = toml_decode($content->getText(), true); $tomlCheck = toml_decode($content->getText(), true);
} catch (TomlError $e) { } catch (TomlError $e) {
$status->fatal( 'content-failed-to-parse', 'SVG', "", $e->getMessage() ); $status->fatal( 'content-failed-to-parse', 'SVG', "", $e->getMessage() );
return;
} }
$svgText = self::buildSvg($tomlCheck);
$pageTitle = $validationParams->getPageIdentity()->__toString();
$tmpFileFactory = MediaWikiServices::getInstance()->getTempFSFileFactory();
$tmpFile = $tmpFileFactory->newTempFSFile( 'annotSvg_', 'svg' );
//$tmpFile = TempFSFile::factory( $pageTitle, 'svg' );
if ( !$tmpFile ) {
$status->fatal( 'svg-error-create-temp', 'Failed to create temp SVG' );
return;
}
$tmpPath = $tmpFile->getPath();
self::console_log($tmpPath, true);
$result = file_put_contents( $tmpPath, $svgText );
if ( $result === false ) {
$status->fatal( 'svg-error-write-temp', 'Failed to write SVG text' );
return;
}
$repoGroup = MediaWikiServices::getInstance()->getRepoGroup();
$localRepo = $repoGroup->getLocalRepo();
$targetTitleText = 'File:' . substr($pageTitle, 11) . '.svg';
$targetTitle = Title::newFromText($targetTitleText);
$newImage = new UploadFromFile();
$newImage->initializePathInfo( 'File:' . substr($pageTitle, 11) . '.svg', $tmpPath, filesize( $tmpPath ), true );
$newImage->performUpload('Generate annotation svg','Annotation image automatically generated from [[' . $pageTitle . ']]', false, \User::newSystemUser( 'MediaWiki default' ));
return $status; return $status;
} }