Difference between revisions of "Extension:Livelets.php"

From Organic Design wiki
(construct request url)
m
Line 21: Line 21:
 
# Constructor
 
# Constructor
 
function Livelets($magic) {
 
function Livelets($magic) {
global $wgParser,$wgHooks,$wgOut;
+
global $wgParser,$wgHooks,$wgOut,$wgScript;
 
$this->magic = $magic;
 
$this->magic = $magic;
 
$this->args  = array();
 
$this->args  = array();
Line 31: Line 31:
 
// Request an URL
 
// Request an URL
 
function liveletRequest(livelet) {
 
function liveletRequest(livelet) {
alert(livelet.innerHTML)
+
livelet.url = '$wgScript?title=' + livelet.getAttribute('title') + '&action=live'
var xmlhttp = livelet.xmlhttp = null
+
alert(livelet.url)
if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest()
+
livelet.xmlhttp = null
else if (window.ActiveXObject) xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
+
if (window.XMLHttpRequest) livelet.xmlhttp = new XMLHttpRequest()
if (xmlhttp != null) {
+
else if (window.ActiveXObject) livelet.xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
 +
if (livelet.xmlhttp != null) {
 
alert('an xmlhttp object has been created');
 
alert('an xmlhttp object has been created');
xmlhttp.onreadystatechange = stateChange
+
livelet.xmlhttp.onreadystatechange = stateChange
xmlhttp.open('GET',livelet.url.value,true)
+
livelet.xmlhttp.open('GET',livelet.url,true)
xmlhttp.send(null)
+
livelet.xmlhttp.send(null)
xmlhttp.livelet = livelet
+
livelet.xmlhttp.livelet = livelet
var url = '$wgScript?title=' + livelet.getAttribute('title') + '&action=live'
+
alert(livelet.url + ' requested')
alert(url + ' requested')
 
 
}
 
}
 
else alert('Your browser does not support XMLHTTP!')
 
else alert('Your browser does not support XMLHTTP!')

Revision as of 02:52, 4 April 2007

<?

  1. Extension:Livelets
  2. - Allows live articles to be transcluded which update automatically on change in a non-polling, fully event-driven way
  3. - Version 0.1 (2007-03-30)
  4. - See http://www.mediawiki.org/wiki/Extension:Livelets for installation and usage details
  5. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  6. - 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')); $wgOut->addScript("<script type='text/javascript'> alert('js is loaded')

// Request an URL function liveletRequest(livelet) { livelet.url = '$wgScript?title=' + livelet.getAttribute('title') + '&action=live' alert(livelet.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) { alert('an xmlhttp object has been created'); livelet.xmlhttp.onreadystatechange = stateChange livelet.xmlhttp.open('GET',livelet.url,true) livelet.xmlhttp.send(null) livelet.xmlhttp.livelet = livelet alert(livelet.url + ' requested') } else alert('Your browser does not support XMLHTTP!') }

// Update the livelet content when loaded function stateChange() { if (this.readyState == 4 && this.status == 200) this.livelet.innerHTML = this.responseText else alert('Problem retrieving XML data!') } </script>"); }

# 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 = "

livelet#$id=$title

";

$html .= "<script type='text/javascript'>liveletRequest(document.getElementById('$id'))</script>"; return $html; }

}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupLivelets() { global $wgLivelets,$wgLiveletsMagic; $wgLivelets = new Livelets($wgLiveletsMagic); }

  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; }

  1. Return naked if live action requested

function wfLiveletsActionHandler($action,$title) { global $wgHooks; if ($action == 'live') $wgHooks['ParserAfterTidy'][] = 'wfLiveletsNakedArticle'; return true; }

  1. Output the naked page and die

function wfLiveletsNakedArticle(&$parser,&$text) { while(@ob_end_clean()); echo($text); die; } ?>