r72345 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72344‎ | r72345 | r72346 >
Date:03:06, 4 September 2010
Author:nad
Status:deferred
Tags:
Comment:
Livelets stable now and useful
Modified paths:
  • /trunk/extensions/Livelets (added) (history)
  • /trunk/extensions/Livelets/Livelets.php (added) (history)

Diff [purge]

Index: trunk/extensions/Livelets/Livelets.php
@@ -0,0 +1,153 @@
 2+<?php
 3+/**
 4+ * Extension:Livelets - Allows articles to be transcluded which load after the main page content and can update dynamically with Ajax
 5+ *
 6+ * @package MediaWiki
 7+ * @subpackage Extensions
 8+ * @author Aran Dunkley, Jack Henderson
 9+ * @licence GNU General Public Licence 2.0 or later
 10+ * Started: 2007-10-06
 11+ * 1.0: 2010-08-25
 12+ */
 13+define( 'LIVELETS_VERSION', '1.0.2, 2010-09-04' );
 14+
 15+# the parser-function name for doing live-transclusions
 16+$wgLiveletsMagic = 'live';
 17+
 18+# Default content for livelets while loading
 19+$wgLiveletsDefaultContent = "http://upload.wikimedia.org/wikipedia/commons/d/de/Ajax-loader.gif";
 20+$wgLiveletsDefaultContent = "<div style='widthtext-align:center'><img src='$wgLiveletsDefaultContent'/></div>";
 21+
 22+# Settings for the event-driven live method
 23+$wgLiveletsUseSWF = false; # Set this to true to use SWF to make livelets fully event-driven (no polling for change)
 24+$wgLiveletsSwfBg = '#ffffff'; # The background colour of the embedded SWF
 25+$wgLiveletsPort = '1729'; # The port that Livelets.pl can be reached on (using $wgServer:$wgLiveletsPort)
 26+
 27+$wgExtensionCredits['parserhook'][] = array(
 28+ 'name' => 'Livelets',
 29+ 'author' => '[http://www.organicdesign.co.nz/User:Nad Aran Dunkley], [http://www.organicdesign.co.nz/User:Jack Jack Henderson]',
 30+ 'description' => 'Allows articles to be transcluded which load after the main page content and can update dynamically with Ajax',
 31+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Livelets',
 32+ 'version' => LIVELETS_VERSION
 33+);
 34+
 35+class Livelets {
 36+
 37+ var $container_id = 0;
 38+ var $request_id = 0;
 39+
 40+ function __construct() {
 41+ global $wgHooks, $wgExtensionFunctions;
 42+
 43+ # Activate the parser-function
 44+ $wgHooks['LanguageGetMagic'][] = $this;
 45+
 46+ # Call the setup method at extension setup time
 47+ $wgExtensionFunctions[] = array( $this, 'setup' );
 48+
 49+ # Bypass ajax dispatcher if this is a livelet ajax request
 50+ if( array_key_exists( 'action', $_GET ) && $_GET['action'] == 'ajax' && $_GET['rs'] == 'livelets' ) {
 51+
 52+ # Extract the data from the ajax request
 53+ list( $title, $this->request_id ) = $_GET['rsargs'];
 54+
 55+ # Adjust the request so that it bypasses the ajax dispatcher and executes with action=render
 56+ $_GET = $_REQUEST = array( 'title' => $title, 'action' => 'render' );
 57+
 58+ # Add a hook to replace the wikitext with just the requested livelet's content
 59+ $wgHooks['ArticleAfterFetchContent'][] = $this;
 60+
 61+ }
 62+ }
 63+
 64+
 65+ # Called at extension setup time
 66+ function setup() {
 67+ global $wgOut, $wgParser, $wgLiveletsMagic, $wgLiveletsUseSwf;
 68+
 69+ # Activate the parser-function
 70+ $wgParser->setFunctionHook( $wgLiveletsMagic, array( $this, 'renderContainer' ) );
 71+
 72+ # Embed the SWF if enabled (SWF must be requested from Livelets.pl)
 73+ if ( $wgLiveletsUseSwf ) {
 74+ global $wgServer, $wgLiveletsPort, $wgLiveletsSwfBg;
 75+ $swf = "$wgServer:$wgLiveletsPort/Livelets.swf";
 76+ $wgOut->addHTML("<object type=\"application/x-shockwave-flash\" data=\"$swf\" width=\"1\" height=\"1\">
 77+ <param name=\"movie\" value=\"$swf\" /><param name=\"bgcolor\" value=\"$wgLiveletsSwfBg\"/></object>");
 78+ }
 79+
 80+ }
 81+
 82+
 83+ # Render livelet container
 84+ function renderContainer( &$parser ) {
 85+ global $wgTitle, $wgJsMimeType, $wgLiveletsDefaultContent;
 86+
 87+ # Ensure the livelets are all numbered so they can be referred to by the ajax request
 88+ $id = $this->container_id++;
 89+
 90+ # Render a container with a 'livelet-loading' div that will be replaced
 91+ $loading = "<div class='livelet-loading'></div>";
 92+ $html = "<div class='livelet' id='livelet-$id'>$loading</div>";
 93+
 94+ # Append an ajax call to collect the content for this container
 95+ $title = $wgTitle->getPrefixedText();
 96+ $script = "sajax_do_call('livelets',['$title','$id'],document.getElementById('livelet-$id'))";
 97+ $html .= "<script type='$wgJsMimeType'>$script</script>";
 98+
 99+ return array( $html, 'isHTML' => true, 'noparse' => true );
 100+ }
 101+
 102+
 103+ # This early hook called from the Ajax bypass whereby the parser has yet to process wikitext
 104+ function onArticleAfterFetchContent( &$article, &$content ) {
 105+ global $wgLiveletsMagic;
 106+
 107+ foreach( self::examineBraces( $content ) as $brace ) {
 108+ if ( $brace['NAME'] == "#$wgLiveletsMagic:" && --$this->request_id < 0 ) {
 109+ $len = strlen( $wgLiveletsMagic );
 110+ $content = substr( $content, $brace['OFFSET'] + $len + 4, $brace['LENGTH'] - $len - 6 );
 111+ break;
 112+ }
 113+ }
 114+
 115+ return true;
 116+ }
 117+
 118+
 119+ # Extract recursive braces belonging to templates and parserfunctions
 120+ function examineBraces( &$content ) {
 121+ $braces = array();
 122+ $depths = array();
 123+ $depth = 1;
 124+ $index = 0;
 125+ while( preg_match( '/\\{\\{\\s*([#a-z0-9_]*:?)|\\}\\}/is', $content, $match, PREG_OFFSET_CAPTURE, $index ) ) {
 126+ $index = $match[0][1] + 2;
 127+ if( $match[0][0] == '}}' ) {
 128+ $brace =& $braces[$depths[$depth-1]];
 129+ $brace['LENGTH'] = $match[0][1] - $brace['OFFSET'] + 2;
 130+ $brace['DEPTH'] = $depth--;
 131+ } else {
 132+ $depths[$depth++] = count( $braces );
 133+ $braces[] = array(
 134+ 'NAME' => $match[1][0],
 135+ 'OFFSET' => $match[0][1]
 136+ );
 137+ }
 138+ }
 139+ return $braces;
 140+ }
 141+
 142+
 143+ # Set up magic word
 144+ function onLanguageGetMagic( &$magicWords, $langCode = 0 ) {
 145+ global $wgLiveletsMagic;
 146+ $magicWords[$wgLiveletsMagic] = array( $langCode, $wgLiveletsMagic );
 147+ return true;
 148+ }
 149+}
 150+
 151+# Instantiate a global instance of the extension
 152+$wgLivelets = new Livelets();
 153+
 154+

Status & tagging log