Difference between revisions of "Extension:Livelets.php"

From Organic Design wiki
m
(1.2.0 changing this over to the mooer too)
Line 1: Line 1:
 
<?
 
<?
# Extension:Livelets{{#Security:*|dev}}{{#Security:view|*}}{{php}}
+
# Extension:Livelets{{php}}{{Category:Extensions|Livelets}}
# - Allows live articles to be transcluded which update automatically on change in a non-polling, fully event-driven way
+
# - Allows articles to be transcluded which load after the main page content and can update dynamically with Ajax
 
# - See http://www.mediawiki.org/wiki/Extension:Livelets for installation and usage details
 
# - See http://www.mediawiki.org/wiki/Extension:Livelets for installation and usage details
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Author: http://www.organicdesign.co.nz/nad
 
# - Author: http://www.organicdesign.co.nz/nad
  
define('LIVELETS_VERSION', '0.1.1, 2007-05-12');
+
define('LIVELETS_VERSION', '0.2.0, 2007-06-19');
  
$wgLiveletsMagic               = "live"; # the parser-function name for doing live-transclusions
+
$wgLiveletsMagic = "live"; # the parser-function name for doing live-transclusions
 +
$wgLiveletsMoo  = "";    # the relative URL to mootools ajax.js script (don't set this if it's already been included by another extension)
 +
 
 +
$wgLiveletsSWF                = dirname(__FILE__).'/Livelets.swf';
 
$wgExtensionFunctions[]        = 'wfSetupLivelets';
 
$wgExtensionFunctions[]        = 'wfSetupLivelets';
 
$wgHooks['LanguageGetMagic'][] = 'wfLiveletsLanguageGetMagic';
 
$wgHooks['LanguageGetMagic'][] = 'wfLiveletsLanguageGetMagic';
Line 15: Line 18:
 
'name'        => 'Livelets',
 
'name'        => 'Livelets',
 
'author'      => '[http://www.organicdesign.co.nz/User:Nad User:Nad]',
 
'author'      => '[http://www.organicdesign.co.nz/User:Nad User:Nad]',
'description' => 'dynamic non-polling updates of articles',
+
'description' => 'Allows articles to be transcluded which load after the main page content and can update dynamically with Ajax',
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:Livelets',
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:Livelets',
 
'version'    => LIVELETS_VERSION
 
'version'    => LIVELETS_VERSION
Line 27: Line 30:
 
# Constructor
 
# Constructor
 
function Livelets() {
 
function Livelets() {
global $wgParser,$wgLiveletsMagic;
+
global $wgParser,$wgLiveletsMagic,$wgLiveletsMoo;
 
$wgParser->setFunctionHook($wgLiveletsMagic,array($this,'functionHook'));
 
$wgParser->setFunctionHook($wgLiveletsMagic,array($this,'functionHook'));
 +
if ($wgLiveletsMoo) $wgOut->addScript("<script type=\"text/javascript\" src=\"$wgLiveletsMoo\"></script>");
 
}
 
}
  
Line 34: Line 38:
 
function functionHook(&$parser,$title) {
 
function functionHook(&$parser,$title) {
 
global $wgScript,$wgLiveletsPath;
 
global $wgScript,$wgLiveletsPath;
$this->addJSandSWF();
 
 
$id    = 'livelet'.$this->id++; # this should be a GUID
 
$id    = 'livelet'.$this->id++; # this should be a GUID
 +
unset($argv['type']);
 +
$element = 'a';
 +
$options = "{data:'action=render',update:$('$update')}";
 +
if ($content == '') $content = $link;
 +
if (preg_match('/^\\w/',$link)) {
 +
$title = Title::newFromText($link);
 +
$argv['class'] = $title->exists() ? 'ajax' : 'new ajax';
 +
$link = $title->getLocalURL();
 +
}
 +
$argv['href'] = "javascript:var a=new Ajax('$link',$options).request()";
 +
 
$url  = Title::newFromText($title)->getFullURL('action=render');
 
$url  = Title::newFromText($title)->getFullURL('action=render');
 
$html  = "<div class='livelet' id='$id' url='$url'><div style='text-align:center'><img src='$wgLiveletsPath/wait.gif'/></div></div>";
 
$html  = "<div class='livelet' id='$id' url='$url'><div style='text-align:center'><img src='$wgLiveletsPath/wait.gif'/></div></div>";
Line 45: Line 59:
 
global $wgOut,$wgLiveletsPath,$wgAddJavaScriptFunctions;
 
global $wgOut,$wgLiveletsPath,$wgAddJavaScriptFunctions;
  
# Add the livelets JavaScript functions to the output page
+
# JS SWF recv: function livelets_DoFScommand(cmd,args) { alert(args) }
$wgAddJavaScriptFunctions['Livelets'] = "
 
 
 
// Keep record of livelets on page
 
var livelets = new Array();
 
 
 
// Request an URL
 
function liveletRequest(id) {
 
livelet = document.getElementById(id)
 
livelets.push(livelet);
 
livelet.id = id
 
livelet.url = livelet.getAttribute(url);
 
livelet.xmlhttp = null
 
if (window.XMLHttpRequest) livelet.xmlhttp = new XMLHttpRequest()
 
else if (window.ActiveXObject) livelet.xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
 
if (livelet.xmlhttp != null) {
 
livelet.xmlhttp.onreadystatechange = stateChange
 
livelet.xmlhttp.open('GET',livelet.url,true)
 
livelet.xmlhttp.send(null)
 
}
 
else alert('Your browser does not support XMLHTTP!')
 
}
 
 
 
// Update the livelet content when loaded
 
function stateChange() {
 
for (var i = 0; i < livelets.length; i++) {
 
var livelet = livelets[i];
 
var xmlhttp = livelet.xmlhttp
 
if (xmlhttp.readyState == 4) {
 
if (xmlhttp.status == 200) livelet.innerHTML = xmlhttp.responseText
 
else alert('Problem retrieving XML data!')
 
xmlhttp.readyState = 0;
 
}
 
}
 
}
 
 
 
// Trap fscommands from the SWF movie
 
function livelets_DoFScommand(cmd,args) {
 
alert(args)
 
}";
 
  
 
# Embed livelets.swf into the output page
 
# Embed livelets.swf into the output page
 
# NOTE: this actually needs to be served by the WikiDaemon, not the PHP
 
# NOTE: this actually needs to be served by the WikiDaemon, not the PHP
$swf = "$wgLiveletsPath/livelets.swf";
+
#$swf = "$wgLiveletsPath/livelets.swf";
$wgOut->addHTML("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0'
+
#$wgOut->addHTML("<object type=\"application/x-shockwave-flash\" data=\"$swf\" width=\"width\" height=\"$height\">
id='livelets' width=100 height=100>
+
# <param name=\"movie\" value=\"$swf\" /><param name=\"bgcolor\" value=\"#ffffff\"/></object>");
<param name='movie' value='$swf'>  
 
<embed swliveconnect='true' name='livelets' src='$swf' width=100 height=100 type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'>
 
</embed></object>");
 
 
}
 
}
  
Line 111: Line 83:
 
return true;
 
return true;
 
}
 
}
 
{{AddJavaScript|Livelets}}
 
 
?>
 
?>

Revision as of 09:11, 19 June 2007

<?

  1. Extension:LiveletsTemplate: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 articles to be transcluded which load after the main page content and can update dynamically with Ajax
  2. - See http://www.mediawiki.org/wiki/Extension:Livelets for installation and usage details
  3. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  4. - Author: http://www.organicdesign.co.nz/nad

define('LIVELETS_VERSION', '0.2.0, 2007-06-19');

$wgLiveletsMagic = "live"; # the parser-function name for doing live-transclusions $wgLiveletsMoo = ""; # the relative URL to mootools ajax.js script (don't set this if it's already been included by another extension)

$wgLiveletsSWF = dirname(__FILE__).'/Livelets.swf'; $wgExtensionFunctions[] = 'wfSetupLivelets'; $wgHooks['LanguageGetMagic'][] = 'wfLiveletsLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Livelets', 'author' => 'User:Nad', 'description' => 'Allows articles to be transcluded which load after the main page content and can update dynamically with Ajax', 'url' => 'http://www.mediawiki.org/wiki/Extension:Livelets', 'version' => LIVELETS_VERSION );

class Livelets {

var $version = LIVELETS_VERSION; var $id = 0;

# Constructor function Livelets() { global $wgParser,$wgLiveletsMagic,$wgLiveletsMoo; $wgParser->setFunctionHook($wgLiveletsMagic,array($this,'functionHook')); if ($wgLiveletsMoo) $wgOut->addScript("<script type=\"text/javascript\" src=\"$wgLiveletsMoo\"></script>"); }

# Remove live container content function functionHook(&$parser,$title) { global $wgScript,$wgLiveletsPath; $id = 'livelet'.$this->id++; # this should be a GUID unset($argv['type']); $element = 'a'; $options = "{data:'action=render',update:$('$update')}"; if ($content == ) $content = $link; if (preg_match('/^\\w/',$link)) { $title = Title::newFromText($link); $argv['class'] = $title->exists() ? 'ajax' : 'new ajax'; $link = $title->getLocalURL(); } $argv['href'] = "javascript:var a=new Ajax('$link',$options).request()";

$url = Title::newFromText($title)->getFullURL('action=render');

$html = "

<img src='$wgLiveletsPath/wait.gif'/>

";

$html .= "<script type='text/javascript'>liveletRequest('$id')</script>"; return array($html,isHTML => true); }

function addJSandSWF() { global $wgOut,$wgLiveletsPath,$wgAddJavaScriptFunctions;

# JS SWF recv: function livelets_DoFScommand(cmd,args) { alert(args) }

# Embed livelets.swf into the output page # NOTE: this actually needs to be served by the WikiDaemon, not the PHP #$swf = "$wgLiveletsPath/livelets.swf"; #$wgOut->addHTML("<object type=\"application/x-shockwave-flash\" data=\"$swf\" width=\"width\" height=\"$height\"> # <param name=\"movie\" value=\"$swf\" /><param name=\"bgcolor\" value=\"#ffffff\"/></object>"); }

}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupLivelets() { global $wgLivelets,$wgScriptPath,$wgLiveletsPath; $wgLivelets = new Livelets(); if ($wgLiveletsPath == ) $wgLiveletsPath = "$wgScriptPath/extensions/livelets"; }

  1. Needed in MediaWiki >1.8.0 for magic word hooks to work properly

function wfLiveletsLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgLiveletsMagic; $magicWords[$wgLiveletsMagic] = array(0,$wgLiveletsMagic); return true; } ?>