User:Jiangxin/Improved Freemind Extension

This improved freemind extension is based on AlexLane's. Orignal extension page is here.

Usage

edit

You can use any internal/external link wiki support as mindmap extension parameter. Example:

  • <mindmap width="100%" height="500px">[http://www.worldhello.net/doc/wiki.mm Johnson's Wiki tutorial]</mindmap>
  • <mindmap>New upload mm file: [[Media:Wiki.mm]]</mindmap>

Improvement of this extension

edit

I like this idea embedding mindmap into wiki's page. This extension works but needs some enhancement.

  1. Various of links no matter internal or external free link can be used;
  2. Add parameters, width, height, bgcolor, to custom display;
  3. Default using flash (visorFreemind.swf) as web interface; --Jiangxin 21:36, 16 April 2006 (UTC)

My patch is here


<?php
# Freemind mindmap WikiMedia extension
# Example: 
#     <mindmap width="100%" height="500px" type="flash">[http://www.worldhello.net/doc/wiki.mm Johnson's Wiki tutorial]</mindmap>, or
#     <mindmap type="java">New upload mm file: [[Media:Wiki.mm]]</mindmap>
#     <mindmap>{{SERVER}}/doc/Wiki.mm</mindmap>


$wgExtensionFunctions[] = "wfFreemindExtension";

function wfFreemindExtension() {
	global $wgParser;
	# Defines the tag <mindmap> ... </mindmap>
	# The second parameter is the callback function for
	# processing the text between the tags
	$wgParser->setHook( "mindmap", "renderMindmap" );
	wfRawHtmlTag('mindmap');
}


# The callback function for converting the input text to HTML output
function renderMindmap( $input, $params = array() ) {
	global $wgServer, $wgScriptPath, $wgTitle, $wgUrlProtocols, $wgUser;

	$localParser = new Parser();
	$parserOptions = ParserOptions::newFromUser( $wgUser );
	$mm_width  = isset($params['width'])  ? $params['width']  : "100%";
	$mm_height = isset($params['height']) ? $params['height'] : "100%";
	$mm_bgcolor = isset($params['bgcolor']) ? $params['bgcolor'] : "#ffffff";

	$html = $localParser->parse($input,$wgTitle,$parserOptions);
	$html->mText = preg_replace('/^<p>[\s]*(.*?)[\s]*<\/p>$/is', '\1' , $html->mText);
	$url = preg_replace('/^.*<a[\s]+href=*"(.*?)".*$/is', '\1' , $html->mText);
	if ( is_array($wgUrlProtocols) )
	{
		$protocols = implode("|", $wgUrlProtocols);
		$protocols = str_replace("/", "\\/", $protocols);
	}
	else
	{
		$protocols = $wgUrlProtocols;
	}
	
	if ( !preg_match( '/^(?:' . $protocols . ')/', $url ) ) 
	{
		if ( strpos($url, '/') === 0 ) {
			$url = "$wgServer".$url;
		}
		else 
		{
			$url = "$wgServer". dirname($_SERVER['REQUEST_URI']) . '/' . $url;
		}
	}

	if( !isset( $params['type'] ) || strcasecmp($params['type'],"flash") ==0 )
	{
		$output = "
			<script type=\"text/javascript\" src=\"$wgScriptPath/extensions/freemind/flashobject.js\"></script>
			<style type=\"text/css\">
				#flashcontent {
					height: 100%;
				}
			</style>
			<div id=\"flashcontent\">
				 Flash plugin or Javascript are turned off.
				 Activate both  and reload to view the mindmap
			</div>
			
			<script type=\"text/javascript\">
				// <![CDATA[
				var fo = new FlashObject(\"$wgScriptPath/extensions/freemind/visorFreemind.swf\", \"$wgScriptPath/extensions/freemind/visorFreeMind\", \"$mm_width\", \"$mm_height\", 6, \"$mm_bgcolor\");
				fo.addParam(\"quality\", \"high\");
				fo.addParam(\"bgcolor\", \"$mm_bgcolor\");
				fo.addVariable(\"openUrl\", \"_blank\");
				fo.addVariable(\"initLoadFile\", \"$url\");
				fo.addVariable(\"startCollapsedToLevel\",\"5\");
				fo.write(\"flashcontent\");
				// ]]>
			</script>
			";
	}
	else if ( strcasecmp($params['type'],"java") ==0 )
	{
		$output = "
			<script type=\"text/javascript\">
			<!--
			    if(!navigator.javaEnabled()) {
			        document.write('<div class=\"freeminderror\"> Please install a <a href=\"http://www.java.com\">Java Runtime Environment<\/a> on your computer.</div>');
			    }
			//-->
			</script>
			<div>
			  <APPLET CODE=\"freemind.main.FreeMindApplet.class\"
			    ARCHIVE=\"$wgScriptPath/extensions/freemind/freemindbrowser.jar\" WIDTH=\"$mm_width\" HEIGHT=\"$mm_height\">
			    <PARAM NAME=\"type\" VALUE=\"application/x-java-applet;version=1.4\">
			    <PARAM NAME=\"scriptable\" VALUE=\"false\">
			    <PARAM NAME=\"modes\" VALUE=\"freemind.modes.browsemode.BrowseMode\">
			    <PARAM NAME=\"browsemode_initial_map\" VALUE=\"$url\">
			    <PARAM NAME=\"initial_mode\" VALUE=\"Browse\">
			    <PARAM NAME=\"selection_method\" VALUE=\"selection_method_direct\">
			  </APPLET>
			</div>
			";
	}
	else
	{
		$output = MindmapHelp($url);
	}

	$output .= "<div>$html->mText</div>";
	return $output;
}


function MindmapHelp($link) {
	return "<div style='border: solid red 1px'>
<p><b>Syntex error or wrong link</b>: <i>$link</i>.</p><br>
<p><b>Syntex: </b>
<blockquote><b><tt>&lt;mindmap <i>width=\"x%\"</i> <i>height=\"x%\"</i> <i>type=\"[flash|java]\"</i>&gt;<i>Link_To_MM_File</i>&lt;/mindmap&gt;</tt></b></blockquote><br>
<b>Example</b>
<blockquote>
<ul>
<li>&lt;mindmap width=\"100%\" height=\"500px\" type=\"flash\"&gt;[http://www.worldhello.net/doc/wiki.mm Johnson's Wiki tutorial]&lt;/mindmap&gt;
<li>&lt;mindmap type=\"java\"&gt;New upload mm file: [[Media:Wiki.mm]]&lt;/mindmap&gt;
<li>&lt;mindmap&gt;{{SERVER}}/doc/Wiki.mm&lt;/mindmap&gt;
</ul></blockquote>
</div>\n";
}

?>

-- JiangXin 20:20, 8 November 2005 (UTC)


Output of the freemind extension is raw html. If already installed this patch, try this.


$ cvs diff -u freemind.php
Index: freemind.php
===================================================================
RCS file: /user/jiangxin/project/wiki/mediawiki/src/extensions/freemind/freemind
.php,v
retrieving revision 1.4
diff -u -r1.4 freemind.php
--- freemind.php        8 Nov 2005 20:27:57 -0000       1.4
+++ freemind.php        15 Nov 2005 01:55:39 -0000
@@ -13,6 +13,7 @@
        # The second parameter is the callback function for
        # processing the text between the tags
        $wgParser->setHook( "mindmap", "renderMindmap" );
+       wfRawHtmlTag('mindmap');
 }

--Jiangxin 02:00, 15 November 2005 (UTC)