Extension:LinkifySVG
<?php /**
* LinkifySVG extension - Automatically converts URL's or double-square bracket items to links in uploaded SVG's *
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');
- 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 );
- 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; }
- 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]; }