diff --git a/includes/AnnotationHandler.php b/includes/AnnotationHandler.php index 27058f7..2fd3ae3 100644 --- a/includes/AnnotationHandler.php +++ b/includes/AnnotationHandler.php @@ -15,7 +15,7 @@ use Html; use IContextSource; use MediaWiki\Content\ValidationParams; use Status; - +use UploadFromFile; class AnnotationHandler extends CodeContentHandler { private const ANNOT_VERSION = '0.1'; @@ -73,14 +73,7 @@ class AnnotationHandler extends CodeContentHandler { return; } - if (isset($metadata['baseImage'])) { - $imageTitle = Title::makeTitleSafe( NS_FILE, $metadata['baseImage'] ); - $baseImage = MediaWikiServices::getInstance()->getRepoGroup()->findFile($imageTitle); - } else { - $baseImage = false; - } - - $output->setText( self::buildSvg($metadata,$baseImage) ); + $output->setText( self::buildSvg($metadata) ); } /** @@ -90,10 +83,17 @@ class AnnotationHandler extends CodeContentHandler { * and produces the html string for the svg with base image and annotations. * * @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 */ - 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 $elBaseImg = ''; $vbHeight = 100; @@ -123,12 +123,6 @@ class AnnotationHandler extends CodeContentHandler { 'xml:space' => 'preserve', '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'])) { $markers = []; @@ -148,7 +142,7 @@ class AnnotationHandler extends CodeContentHandler { 'cy' => $annot['position']['1'], 'r' => '3.75', '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); array_push($markers,$markCirc); @@ -168,14 +162,45 @@ class AnnotationHandler extends CodeContentHandler { public function validateSave( Content $content, ValidationParams $validationParams ) { self::console_log('Validate Save', true); + $tempDir = sys_get_temp_dir(); $status = Status::newGood(); try { $tomlCheck = toml_decode($content->getText(), true); } catch (TomlError $e) { $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; }