Difference between revisions of "Extension:Flashlets.php"

From Organic Design wiki
(formatting)
Line 9: Line 9:
 
define('FLASHLETS_VERSION','1.0.4, 2007-09-04');
 
define('FLASHLETS_VERSION','1.0.4, 2007-09-04');
  
$wgFlashletsMagic           = "swf";                           # The tag name for embedding a SWF
+
$wgFlashletsMagic   = "swf"; # The tag name for embedding a SWF
$wgFlashletsMTASC           = dirname(__FILE__)."/mtasc/mtasc"; # Command used to launch the MTASC executable
+
$wgFlashletsMTASC   = dirname(__FILE__)."/mtasc/mtasc"; # Command used to launch the MTASC executable
$wgFlashletsCP             = dirname(__FILE__)."/mtasc/std";  # Directory containing the MTASC class headers
+
$wgFlashletsCP   = dirname(__FILE__)."/mtasc/std";  # Directory containing the MTASC class headers
$wgFlashletsPath           = $wgUploadPath;                   # The client-side path to the directory containing the SWF's
+
$wgFlashletsPath = $wgUploadPath; # The client-side path to the directory containing the SWF's
$wgFlashletsDirectory       = $wgUploadDirectory;               # The server-side path to the directory containing the SWF's
+
$wgFlashletsDirectory   = $wgUploadDirectory;   # The server-side path to the directory containing the SWF's
$wgFlashletsExpandTemplates = true;                             # Expand mediawiki templates before compiling
+
$wgFlashletsExpandTemplates = true; # Expand mediawiki templates before compiling
$wgExtensionFunctions[]     = 'wfSetupFlashlets';
+
$wgExtensionFunctions[] = 'wfSetupFlashlets';
  
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
        'name'       => 'Flashlets',
+
'name' => 'Flashlets',
        'description' => 'Actionscript rendering of SWF movies',
+
'description' => 'Actionscript rendering of SWF movies',
        'url'         => 'http://www.mediawiki.org/wiki/Extension:Flashlets',
+
'url' => 'http://www.mediawiki.org/wiki/Extension:Flashlets',
        'author'     => '[http://www.organicdesign.co.nz/nad User:Nad]',
+
'author'   => '[http://www.organicdesign.co.nz/nad User:Nad]',
        'version'     => FLASHLETS_VERSION
+
'version' => FLASHLETS_VERSION
        );
+
);
  
  
Line 29: Line 29:
  
 
var $version = FLASHLETS_VERSION;
 
var $version = FLASHLETS_VERSION;
var $expire  = 100;                   # Number of minutes a generated SWF lives for without being accessed
+
var $expire  = 100;   # Number of minutes a generated SWF lives for without being accessed
var $container;                       # Location if the SWF container needed by the compiler
+
var $container;   # Location if the SWF container needed by the compiler
  
 
# Constructor
 
# Constructor
Line 37: Line 37:
 
$this->container = dirname(__FILE__).'/container.swf';
 
$this->container = dirname(__FILE__).'/container.swf';
 
$wgParser->setHook($wgFlashletsMagic,array($this,'tagHook'));
 
$wgParser->setHook($wgFlashletsMagic,array($this,'tagHook'));
}
+
}
  
 
# Convert the $magic tags to client-side javascript request code from its stored parameters
 
# Convert the $magic tags to client-side javascript request code from its stored parameters
Line 46: Line 46:
 
$width  = isset($argv['width'])  ? intval($argv['width'])  : 300;
 
$width  = isset($argv['width'])  ? intval($argv['width'])  : 300;
 
$height  = isset($argv['height'])  ? intval($argv['height']) : 200;
 
$height  = isset($argv['height'])  ? intval($argv['height']) : 200;
$fps     = isset($argv['fps'])     ? intval($argv['fps'])   : 25;
+
$fps = isset($argv['fps']) ? intval($argv['fps']) : 25;
$bgcolor = isset($argv['bgcolor']) ? $argv['bgcolor']       : '#ffffff';
+
$bgcolor = isset($argv['bgcolor']) ? $argv['bgcolor'] : '#ffffff';
$caption = isset($argv['caption']) ? $argv['caption']       : false;
+
$caption = isset($argv['caption']) ? $argv['caption'] : false;
$ver     = isset($argv['ver'])     ? $argv['ver']           : 6;
+
$ver = isset($argv['ver']) ? $argv['ver'] : 6;
$quality = isset($argv['quality']) ? $argv['quality']       :'high';
+
$quality = isset($argv['quality']) ? $argv['quality'] :'high';
  
                if (isset($argv['src'])) {
+
if (isset($argv['src'])) {
                        $swf = $argv['src'];
+
$swf = $argv['src'];
 
$html = "\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
 
$html = "\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
 
<tr><td><object type=\"application/x-shockwave-flash\" data=\"$swf\" width=\"$width\" height=\"$height\">
 
<tr><td><object type=\"application/x-shockwave-flash\" data=\"$swf\" width=\"$width\" height=\"$height\">
Line 61: Line 61:
 
</object></td></tr>\n";
 
</object></td></tr>\n";
 
if ($caption) $html .= "\n<tr><td align=\"center\"><a href=\"$swf\">$caption</a></td></tr>\n";
 
if ($caption) $html .= "\n<tr><td align=\"center\"><a href=\"$swf\">$caption</a></td></tr>\n";
                        $html .= "</table>";
+
$html .= "</table>";
                        return $html;
+
return $html;
                        }
+
}
  
                if ($wgFlashletsMTASC == '') return "\n<br><b>SWF compiling is disabled!</b>";
+
if ($wgFlashletsMTASC == '') return "\n<br><b>SWF compiling is disabled!</b>";
  
 
/*
 
/*
Line 99: Line 99:
 
$html = shell_exec($sh);
 
$html = shell_exec($sh);
 
unlink("$file.as");
 
unlink("$file.as");
} else $html = "Could not open '$file.as' for writing!";
+
} else $html = "Could not open '$file.as' for writing!";
  
}
+
}
  
 
# If the SWF exists, return the HTMLcode to request and embed the SWF otherwise return error information
 
# If the SWF exists, return the HTMLcode to request and embed the SWF otherwise return error information
Line 113: Line 113:
 
if ($caption) $html .= "\n<tr><td align=\"center\"><a href=\"$wgFlashletsPath/$swf.swf\">$caption</a></td></tr>\n";
 
if ($caption) $html .= "\n<tr><td align=\"center\"><a href=\"$wgFlashletsPath/$swf.swf\">$caption</a></td></tr>\n";
 
$html .= "</table>";
 
$html .= "</table>";
}
+
} else $html = "\n<br><b>Error compiling SWF: </b><pre>$html</pre>\n";
else $html = "\n<br><b>Error compiling SWF: </b><pre>$html</pre>\n";
 
  
 
return $html;
 
return $html;
}
+
}
  
}
+
}
  
 
# Called from $wgExtensionFunctions array when initialising extensions
 
# Called from $wgExtensionFunctions array when initialising extensions
Line 125: Line 124:
 
global $wgFlashlets;
 
global $wgFlashlets;
 
$wgFlashlets = new Flashlets();
 
$wgFlashlets = new Flashlets();
}
+
}
 
 
?>
 

Revision as of 01:38, 18 August 2008

<?php

  1. Flashlets ExtensionTemplate:Php
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.
  1. - Allows ActionScript code to render in the page a SWF using the MTASC (http://www.mtasc.org) SWF compiler
  2. - Version 1.02 (2007-05-05)
  3. - See http://www.mediawiki.org/wiki/Extension:Flashlets for installation and usage details
  4. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  5. - Author: http://www.organicdesign.co.nz/nad

define('FLASHLETS_VERSION','1.0.4, 2007-09-04');

$wgFlashletsMagic = "swf"; # The tag name for embedding a SWF $wgFlashletsMTASC = dirname(__FILE__)."/mtasc/mtasc"; # Command used to launch the MTASC executable $wgFlashletsCP = dirname(__FILE__)."/mtasc/std"; # Directory containing the MTASC class headers $wgFlashletsPath = $wgUploadPath; # The client-side path to the directory containing the SWF's $wgFlashletsDirectory = $wgUploadDirectory; # The server-side path to the directory containing the SWF's $wgFlashletsExpandTemplates = true; # Expand mediawiki templates before compiling $wgExtensionFunctions[] = 'wfSetupFlashlets';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Flashlets', 'description' => 'Actionscript rendering of SWF movies', 'url' => 'http://www.mediawiki.org/wiki/Extension:Flashlets', 'author' => 'User:Nad', 'version' => FLASHLETS_VERSION );


class Flashlets {

var $version = FLASHLETS_VERSION; var $expire = 100; # Number of minutes a generated SWF lives for without being accessed var $container; # Location if the SWF container needed by the compiler

# Constructor function Flashlets() { global $IP,$wgParser,$wgFlashletsMagic; $this->container = dirname(__FILE__).'/container.swf'; $wgParser->setHook($wgFlashletsMagic,array($this,'tagHook')); }

# Convert the $magic tags to client-side javascript request code from its stored parameters function tagHook($actionscript,$argv,&$parser) { global $wgFlashletsMTASC,$wgFlashletsCP,$wgFlashletsDirectory,$wgFlashletsPath,$wgFlashletsExpandTemplates;

# convert args to command-line switches $width = isset($argv['width'])  ? intval($argv['width'])  : 300; $height = isset($argv['height'])  ? intval($argv['height']) : 200; $fps = isset($argv['fps'])  ? intval($argv['fps']) : 25; $bgcolor = isset($argv['bgcolor']) ? $argv['bgcolor'] : '#ffffff'; $caption = isset($argv['caption']) ? $argv['caption'] : false; $ver = isset($argv['ver'])  ? $argv['ver'] : 6; $quality = isset($argv['quality']) ? $argv['quality'] :'high';

if (isset($argv['src'])) { $swf = $argv['src'];

$html = "\n

\n"; if ($caption) $html .= "\n\n"; $html .= "
<object type=\"application/x-shockwave-flash\" data=\"$swf\" width=\"$width\" height=\"$height\">

<param name=\"movie\" value=\"$swf\" /> <param name=\"bgcolor\" value=\"$bgcolor\"/> <param name=\"quality\" value=\"$quality\"/>

</object>
<a href=\"$swf\">$caption</a>

";

return $html; }

if ($wgFlashletsMTASC == ) return "\n
SWF compiling is disabled!";

/* # delete swf's older than cache-expiry $expiry = $now - $this->expire; if ($handle = opendir($wgFlashletsDirectory)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $stat = stat($file); #if ($stat[8] < $expire) unlink($file); } } } closedir($handle);

  • /

# expand templates before compiling if ($wgFlashletsExpandTemplates) $actionscript = $parser->replaceVariables($actionscript);

# create swf filename unique to the actionscript content and parameters $swf = md5("$actionscript$ver$width$height$fps"); $file = "$wgFlashletsDirectory/$swf"; $path = "$wgFlashletsPath/$swf.swf";

# Compile the SWF if it doesn't already exist if (!file_exists("$file.swf")) {

# Write the actionscript to a file for MTASC to compile from if ($handle = fopen("$file.as", 'w+')) { fwrite($handle,$actionscript); fclose($handle); $cont = $this->container; $sh = "$wgFlashletsMTASC -cp $wgFlashletsCP -swf $cont -out $file.swf -main -version $ver -header $width:$height:$fps $file.as 2>&1"; $html = shell_exec($sh); unlink("$file.as"); } else $html = "Could not open '$file.as' for writing!";

}

# If the SWF exists, return the HTMLcode to request and embed the SWF otherwise return error information if (file_exists("$file.swf")) {

$html = "\n

\n"; if ($caption) $html .= "\n\n"; $html .= "
<object type=\"application/x-shockwave-flash\" data=\"$path\" width=\"$width\" height=\"$height\">

<param name=\"movie\" value=\"$path\" /> <param name=\"bgcolor\" value=\"$bgcolor\"/> <param name=\"quality\" value=\"$quality\"/>

</object>
<a href=\"$wgFlashletsPath/$swf.swf\">$caption</a>

"; } else $html = "\n
Error compiling SWF:

$html

\n";

return $html; }

}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupFlashlets() { global $wgFlashlets; $wgFlashlets = new Flashlets(); }