Wikimedia Developer Support

Instant commons fix for Graphviz extension



I would like to fix the behavior of the Graphviz extension regarding the handling of instant commons files as asked at

I found the following relevant source code lines in Version 2.1.0:

GraphViz_body.php lines 797 ff:

$imageFile = UploadLocalFile::getUploadedFile( $imageName );
		if ( $imageFile ) {
			$result = 'image="' . $imageFile->getLocalRefPath() . '"';
			wfDebug( __METHOD__ . ": replacing: $imageName with: $result\n" );
			return $result;
		} else {
			wfDebug( __METHOD__ . ": removing invalid imageName: $imageName\n" );
			return 'image=""';

and UploadLocalFile.php lines 202 ff:

 * Check if the given file has been uploaded to the wiki.
 * @param[in] string $filename is the name of the file to check.
 * @return File: file, or null on failure
static function getUploadedFile( $fileName ) {
	$upload = new UploadFromLocalFile;
	$upload->initializePathInfo( $fileName, "", 0, false );
	$title = $upload->getTitle();
	$file = wfFindFile( $title );
	return $file;

the issue seems to be that wfFindFile will not find the File in the instant commons case.
I donot understand how InstantCommons works in detail. It seems there is no local cached copy available of an instant commons file and therefore the above call fails. So what would be the best approach to achieve that the above code will return the proper image URL in the instant commons case?


A local copy might or might not be available, depending on configuration (as the $wgUseInstantCommons page explains that flag is really just a shorthand for the more complex foreign repo setting). Usually it’s not wanted - Commons hosts hundred-megapixel multi-gigabyte originals, if you want to display a ten kilopixel thumbnail of such a file in your wiki page, fetching the original would not be fun.
Of course that assumes that Commons (o, in general, the foreign API repo) can do the thumbnailing for you.

wfFindFile does work with remote image repos. UploadLocalFile shouldn’t, since it’s about locally uploaded files. What seems to be happening here is that the GraphViz extension takes a wikitext definition of a graph, generates an image file from it, and uses the code meant for file uploads to store that file. I don’t see how InstantCommons is related to all that.


This is about using the image attribute for nodes in a graph isn’t it? e.g.

digraph G {
  test [ image="File:Some_file_on_Commons.jpg" ]

I think one problem with supporting InstantCommons for these is that there doesn’t seem to be a way to supply the wanted image size in the node definition, i.e. it wants to use the given image at its full size (and in the case of SVG and Postscript images it expects them to have a size defined internally). Which actually might make things easier, in that we don’t have to worry about resizing, and can just copy the full image over (and of course, if someone tries to do this with a massive file, things will likely not work well).


It is possible to embed images within a graph using the image attribute of the graphviz language. For example, if you were making an org chart you could use photos of people in the org as nodes in the graph. The rationale behind the restriction that only uploaded files may be embedded in graphs (documented here: is that otherwise a user could potentially access and display (within a graph) any image on the file system where the wiki is running.

That said, without knowing much about Commons, it seems reasonable to allow the user editing the graph to specify a file that exists at Commons for inclusion in a local graph.


Well, resizing means specifying an extra parameter to the imageinfo API, so I’d be a lot less worried about that than getting some crazy huge original :slight_smile:

If Graphviz can take an URL parameter, just pass $file->transform(...)->getUrl() to it (except with error handling and a null check in the middle). Otherwise, you’ll have to refactor a bunch of stuff in ForeignAPIRepo::getThumbUrlFromCache.


Maybe GraphViz could enforce a maximum size of the requested image? It’s not that likely that people will want 1000px images in their graph nodes is it? @WolfgangFahl do you have an example of the graph you’re trying to make?

#7 has an incomplete example.