Extension:LinkifySVG

From Organic Design wiki

<?php /**

* LinkifySVG extension - Automatically converts URL's or double-square bracket items to links in uploaded SVG's
* 
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:PhpCategory:Extensions created with Template:Extension

* See http://www.mediawiki.org/wiki/Extension:LinkifySVG for installation and usage details
*
* @package MediaWiki
* @subpackage Extensions
* @author User:Nad
* @copyright © 2007 User:Nad
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('LINKIFYSVG_VERSION', '1.0.0, 2009-02-27');

  1. Maximum size in bytes an uploaded SVG can be to get linkificated

$egLinkifySVGMaxSize = 1048576;

$wgExtensionCredits['other'][] = array( 'name' => 'LinkifySVG', 'author' => 'User:Nad', 'description' => 'Automatically converts URL\'s or double-square bracket items to links in uploaded SVG\'s', 'url' => 'http://www.organicdesign.co.nz/Extension:LinkifySVG', 'version' => LINKIFYSVG_VERSION );

  1. Hook into upload before it's processed and modify the temp file

$wgHooks['UploadForm:BeforeProcessing'][] = 'efLinkifySVG'; function efLinkifySVG(&$form) { global $egLinkifySVGMaxSize; if ($form->mFileSize < $egLinkifySVGMaxSize && eregi('\.svg$', $form->mSrcName)) {

   	$svg = file_get_contents($x = $form->mTempPath);
   	if (!preg_match('|<svg[^>]+?xmlns:xlink\s*=|', $svg))
   		$svg = preg_replace('|<svg\s+|', '<svg xmlns:xlink="http://www.w3.org/1999/xlink" ', $svg);
   	$svg = preg_replace_callback('|(<text.+?>\s*<tspan.+?>\s*)(.+?)(\s*</tspan>\s*</text>)|s', 'efLinkifySVGCallback', $svg, -1, $count);
   	if ($count > 0) file_put_contents($form->mTempPath, $svg);

} return true; }

  1. Called for each text element, return wrapped in a link if double-square-brackets around content

function efLinkifySVGCallback($m) { return preg_match("|^\[\[(.+?)\]\]$|", $m[2], $n) ? "<a xlink:href=\"/$n[1]\">$m[1]$n[1]$m[3]</a>" : $m[0]; }