Difference between revisions of "Extension:Livelets.php"
m |
m |
||
Line 29: | Line 29: | ||
# Add the livelets JavaScript functions to the output page | # Add the livelets JavaScript functions to the output page | ||
$wgOut->addScript("<script type='text/javascript'> | $wgOut->addScript("<script type='text/javascript'> | ||
+ | |||
+ | // Keep record of livelets on page | ||
+ | var livelets = new Array(); | ||
// Request an URL | // Request an URL | ||
− | function liveletRequest(livelet) | + | function liveletRequest(id) { |
+ | livelet = document.getElementById(id); | ||
+ | livelets[id] = livelet; | ||
livelet.url = '$wgScript?title=' + livelet.getAttribute('title') + '&action=live' | livelet.url = '$wgScript?title=' + livelet.getAttribute('title') + '&action=live' | ||
livelet.xmlhttp = null | livelet.xmlhttp = null | ||
Line 40: | Line 45: | ||
livelet.xmlhttp.open('GET',livelet.url,true) | livelet.xmlhttp.open('GET',livelet.url,true) | ||
livelet.xmlhttp.send(null) | livelet.xmlhttp.send(null) | ||
− | |||
} | } | ||
else alert('Your browser does not support XMLHTTP!') | else alert('Your browser does not support XMLHTTP!') | ||
Line 47: | Line 51: | ||
// Update the livelet content when loaded | // Update the livelet content when loaded | ||
function stateChange() { | function stateChange() { | ||
− | if ( | + | for (var i = 0; i < livelets.length; i++) { |
− | + | var xmlhttp = livelets[i].xmlhttp; | |
− | + | if (xmlhttp.readyState == 4) { | |
− | alert( | + | if (xmlhttp.status == 200) xmlhttp.livelet.innerHTML = xmlhttp.responseText |
− | + | else alert('Problem retrieving XML data!') | |
+ | xmlhttp.readyState = 0; | ||
} | } | ||
− | |||
} | } | ||
} | } | ||
Line 91: | Line 95: | ||
$title = $args[0]; | $title = $args[0]; | ||
$html = "<div class='livelet' id='$id' title='$title'>livelet#$id=$title</div>"; | $html = "<div class='livelet' id='$id' title='$title'>livelet#$id=$title</div>"; | ||
− | $html .= "<script type='text/javascript'>liveletRequest | + | $html .= "<script type='text/javascript'>liveletRequest('$id')</script>"; |
return $html; | return $html; | ||
} | } |
Revision as of 05:56, 4 April 2007
<?
- Extension:Livelets
- - Allows live articles to be transcluded which update automatically on change in a non-polling, fully event-driven way
- - Version 0.1 (2007-03-30)
- - See http://www.mediawiki.org/wiki/Extension:Livelets for installation and usage details
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: http://www.organicdesign.co.nz/nad
$wgLiveletsMagic = "live"; # the parser-function name for doing live-transclusions $wgExtensionFunctions[] = 'wfSetupLivelets'; $wgHooks['LanguageGetMagic'][] = 'wfLiveletsLanguageGetMagic'; $wgHooks['UnknownAction'][] = 'wfLiveletsActionHandler';
class Livelets {
var $version = '0.1, 2007-03-30'; var $magic; var $args; var $id = 0;
# Constructor function Livelets($magic) { global $wgParser,$wgHooks,$wgOut,$wgScript; $this->magic = $magic; $this->args = array(); $wgParser->setFunctionHook($magic,array($this,'functionHook')); $wgParser->setHook($magic,array($this,'tagHook'));
# Add the livelets JavaScript functions to the output page $wgOut->addScript("<script type='text/javascript'>
// Keep record of livelets on page var livelets = new Array();
// Request an URL function liveletRequest(id) { livelet = document.getElementById(id); livelets[id] = livelet; livelet.url = '$wgScript?title=' + livelet.getAttribute('title') + '&action=live' 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 xmlhttp = livelets[i].xmlhttp; if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) xmlhttp.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) }
</script>");
# Embed livelets.swf into the output page $swf = '/wiki/extensions/livelets/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' id='livelets' width=100 height=100> <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>"); }
# Remove live container content, store its parameters and convert it to $magic tags function functionHook(&$parser) { $magic = $this->magic; $id = 'livelet'.$this->id++; $args = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)=(.+)$/',$arg,$match)) $args[$match[1]] = $match[2]; else $args[] = $arg; } $this->args[$id] = $args; return "<$magic>$id</$magic>"; }
# Convert the $magic tags to client-side javascript request code from its stored parameters function tagHook($id,$argv,&$parser) { global $wgScript; $args = $this->args[$id]; $title = $args[0];
$html = "
";
$html .= "<script type='text/javascript'>liveletRequest('$id')</script>"; return $html; }
}
- Called from $wgExtensionFunctions array when initialising extensions
function wfSetupLivelets() { global $wgLivelets,$wgLiveletsMagic; $wgLivelets = new Livelets($wgLiveletsMagic); }
- 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; }
- Return naked if live action requested
function wfLiveletsActionHandler($action,$title) { global $wgHooks; if ($action == 'live') $wgHooks['ParserAfterTidy'][] = 'wfLiveletsNakedArticle'; return true; }
- Output the naked page and die
function wfLiveletsNakedArticle(&$parser,&$text) { while(@ob_end_clean()); echo($text); die; } ?>