🚧 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 f1e7da45 authored by Samwilson's avatar Samwilson
Browse files

Add support for image="" attribute

The image must exist on the wiki, and will be rendered full size.

Bug: GH #9
parent cadf758b
<?php
namespace MediaWiki\Extension\Diagrams;
use Exception;
use MediaWiki\MediaWikiServices;
class Dot {
/** @var string Dot source. */
private $src;
/**
* @param string $src The dot source code.
*/
public function __construct( string $src ) {
$this->src = $src;
}
/**
* @return string
* @throws Exception If a forbidden attibute is detected.
*/
public function getSrc(): string {
// Forbidden attributes.
$forbiddenAttributes = [
'imagepath',
'shapefile',
'fontpath'
];
foreach ( $forbiddenAttributes as $forbiddenAttribute ) {
if ( stripos( $this->src, $forbiddenAttribute ) !== false ) {
throw new Exception( 'forbidden-attribute' );
}
}
// Images.
$imagePattern = '|image="([^"]+)"|i';
$out = preg_replace_callback( $imagePattern, [ $this, 'resolveImageAttr' ], $this->src );
return $out;
}
/**
* @param string[] $matches
* @return string
*/
protected function resolveImageAttr( array $matches ): string {
// Strip wrapping quotation marks.
$imageName = trim( $matches[1], '"' );
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $imageName );
if ( !$file || !$file->exists() ) {
return "label=\"Diagrams error: mage not found: $imageName\"";
}
/*
// Create a new tmp file to save the thumbnail to.
// @todo Support generation of image thumbnails, based on size attributes.
$tmpFactory = MediaWikiServices::getInstance()->getTempFSFileFactory();
$tmpFile = $tmpFactory->newTempFSFile( 'ext-diagrams-', $file->getExtension() );
// Render the thumb.
$file->generateAndSaveThumb( $tmpFile, [ 'width' => 1024 ], File::FOR_THIS_USER );
$path = $tmpFile->getPath();
*/
// Output the replacement image attribute with the new path.
$path = $file->getLocalRefPath();
$out = 'image="' . $path . '"';
return $out;
}
}
......@@ -32,7 +32,8 @@ class Hooks {
: 'renderLocally';
if ( $tag === 'graphviz' ) {
// GraphViz.
return $diagrams->$renderMethod( $params['renderer'] ?? 'dot', $input, $params );
$dot = new Dot( $input );
return $diagrams->$renderMethod( $params['renderer'] ?? 'dot', $dot->getSrc(), $params );
} elseif ( $tag === 'mscgen' ) {
// Mscgen.
return $diagrams->$renderMethod( 'mscgen', $input, $params );
......
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