Difference between revisions of "Extension:Livelets.php"

From Organic Design wiki
(0.2.5 - use MW ajax here too)
Line 6: Line 6:
 
# - Author: http://www.organicdesign.co.nz/nad
 
# - Author: http://www.organicdesign.co.nz/nad
  
define('LIVELETS_VERSION', '0.2.4, 2007-08-08');
+
define('LIVELETS_VERSION', '0.2.5, 2007-10-05');
  
 
$wgLiveletsMagic  = 'live';    # the parser-function name for doing live-transclusions
 
$wgLiveletsMagic  = 'live';    # the parser-function name for doing live-transclusions
$wgLiveletsMoo    = '';        # Set this to the relative path of Mootools.js unless loaded elsewhere
 
  
 
$wgLiveletsUseSWF = false;    # Set this to true to use SWF to make livelets fully event-driven (no polling for change)
 
$wgLiveletsUseSWF = false;    # Set this to true to use SWF to make livelets fully event-driven (no polling for change)
Line 36: Line 35:
 
# Constructor
 
# Constructor
 
function Livelets() {
 
function Livelets() {
global $IP,$wgOut,$wgParser,$wgServer,$wgLiveletsBaseUrl,$wgLiveletsMagic,$wgLiveletsMoo,$wgLiveletsPath,
+
global $IP,$wgOut,$wgParser,$wgServer,$wgLiveletsMagic,$wgLiveletsUseSwf,$wgLiveletsSwfBg,$wgLiveletsPort;
$wgLiveletsUseSwf,$wgLiveletsSwfBg,$wgLiveletsPort;
 
  
 
# Activate the #live parser-function
 
# Activate the #live parser-function
 
$wgParser->setFunctionHook($wgLiveletsMagic,array($this,'functionHook'));
 
$wgParser->setFunctionHook($wgLiveletsMagic,array($this,'functionHook'));
 
# Include Livelets.js
 
$wgOut->addScript("<script type=\"text/javascript\" src=\"$wgLiveletsBaseUrl/Livelets.js\"></script>");
 
 
# Include Mootools.js is set
 
if ($wgLiveletsMoo) $wgOut->addScript("<script type=\"text/javascript\" src=\"$wgLiveletsMoo\"></script>");
 
  
 
# Embed the SWF if enabled (SWF must be requested from Livelets.pl)
 
# Embed the SWF if enabled (SWF must be requested from Livelets.pl)
Line 58: Line 50:
 
# Render livelet container
 
# Render livelet container
 
function functionHook(&$parser) {
 
function functionHook(&$parser) {
global $wgScript,$wgLiveletsPath,$wgLiveletsDefaultContent;
+
global $wgScript,$wgTitle,$wgLiveletsPath,$wgLiveletsDefaultContent;
  
 
# Process parameters
 
# Process parameters
$args = '';
 
 
$argv = array();
 
$argv = array();
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
Line 67: Line 58:
 
else $argv['title'] = trim($arg);
 
else $argv['title'] = trim($arg);
 
}
 
}
if (isset($argv['update'])) { $update = $argv['update']; unset($argv['update']); }
+
$title  = $argv['title'];
if (isset($argv['type']))  { $type = $argv['type']; unset($argv['type']); }
+
$update = isset($argv['update']) ? $argv['update'] : 0;
foreach ($argv as $k => $v) $args .= " $k=\"$v\"";
+
unset($argv['update']);
 
+
if (isset($argv['type'])) $type = $argv['type'];
# Determine URL
+
unset($argv['type']);
if (isset($argv['title'])) {
 
$title = Title::newFromText($argv['title']);
 
if (is_object($title)) $argv['url'] = $title->getLocalURL('action=render&pagename='.$wgTitle->getPrefixedText());
 
}
 
  
 
# Render container
 
# Render container
 +
$args = '';
 +
foreach ($argv as $k => $v) $args .= " $k=\"$v\"";
 
$id = 'livelet'.$this->id++;
 
$id = 'livelet'.$this->id++;
$content = isset($argv['url']) ? $wgLiveletsDefaultContent : "Error: No URL";
+
$html = "<div$args id='$id'>$wgLiveletsDefaultContent</div>";
$html = "<div$args id='$id'>$content</div>";
+
if ($update >= 0) $html .= "<script type='text/javascript'>x = sajax_do_call('wfSimpleFormsAjax',['title=$title'],document.getElementById('$id'))</script>";
if (isset($update)) $html .= "<script type='text/javascript'>updateLivelet('$id')</script>";
 
  
 
return array($html,'isHTML' => true,'noparse' => true);
 
return array($html,'isHTML' => true,'noparse' => true);
Line 90: Line 78:
 
# Called from $wgExtensionFunctions array when initialising extensions
 
# Called from $wgExtensionFunctions array when initialising extensions
 
function wfSetupLivelets() {
 
function wfSetupLivelets() {
global $wgLivelets,$wgScriptPath,$wgLiveletsPath;
+
global $wgLivelets;
 
$wgLivelets = new Livelets();
 
$wgLivelets = new Livelets();
if ($wgLiveletsPath == '') $wgLiveletsPath = "$wgScriptPath/extensions/livelets";
 
 
}
 
}
  

Revision as of 09:59, 5 October 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.

Scripts and articles related to Extension:Livelets, see also Wiklets

  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.5, 2007-10-05');

$wgLiveletsMagic = 'live'; # the parser-function name for doing live-transclusions

$wgLiveletsUseSWF = false; # Set this to true to use SWF to make livelets fully event-driven (no polling for change) $wgLiveletsSwfBg = '#ffffff'; # The background colour of the embedded SWF $wgLiveletsPort = '1729'; # The port that Livelets.pl can be reached on (using $wgServer:$wgLiveletsPort)

$wgLiveletsPath = dirname(__FILE__); $wgLiveletsBaseUrl = preg_replace('|^.+(?=/extensions)|',$wgScriptPath,$wgLiveletsPath);

$wgLiveletsDefaultContent = "

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

";

$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 $IP,$wgOut,$wgParser,$wgServer,$wgLiveletsMagic,$wgLiveletsUseSwf,$wgLiveletsSwfBg,$wgLiveletsPort;

# Activate the #live parser-function $wgParser->setFunctionHook($wgLiveletsMagic,array($this,'functionHook'));

# Embed the SWF if enabled (SWF must be requested from Livelets.pl) if ($wgLiveletsUseSwf) { $swf = "$wgServer:$wgLiveletsPort/Livelets.swf"; $wgOut->addHTML("<object type=\"application/x-shockwave-flash\" data=\"$swf\" width=\"1\" height=\"1\"> <param name=\"movie\" value=\"$swf\" /><param name=\"bgcolor\" value=\"$wgLiveletsSwfBg\"/></object>"); } }

# Render livelet container function functionHook(&$parser) { global $wgScript,$wgTitle,$wgLiveletsPath,$wgLiveletsDefaultContent;

# Process parameters $argv = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^([a-z0-9_]+?)\\s*=(.+)$/is',$arg,$match)) $argv[trim($match[1])] = trim($match[2]); else $argv['title'] = trim($arg); } $title = $argv['title']; $update = isset($argv['update']) ? $argv['update'] : 0; unset($argv['update']); if (isset($argv['type'])) $type = $argv['type']; unset($argv['type']);

# Render container $args = ; foreach ($argv as $k => $v) $args .= " $k=\"$v\""; $id = 'livelet'.$this->id++;

$html = "<div$args id='$id'>$wgLiveletsDefaultContent";

if ($update >= 0) $html .= "<script type='text/javascript'>x = sajax_do_call('wfSimpleFormsAjax',['title=$title'],document.getElementById('$id'))</script>";

return array($html,'isHTML' => true,'noparse' => true); }

}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupLivelets() { global $wgLivelets; $wgLivelets = new 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; } ?>