Extension:SLight

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
SLight

Release status: beta

Implementation Tag
Description Extension allows inserting Silverlight content.
Author(s) Josef Martiňák (Jossmarttalk)
Last version 0.2 (May 21 2013)
MediaWiki 1.19+
Database changes no
License GPL
Download This page
Tags
<slight></slight>
Hooks used
ParserFirstCallInit
Check usage and version matrix

Contents

Description [edit]

Extension allows inserting Silverlight content. Silverlight app (XAP extension) can be placed this way:

<slight width="width px" height="height px">wikifilename (with no "File:")</slight>
  • Width is optional, default is 100%.
  • Percentage of height doesn't work in IE for example, so it must be set in pixels.
  • XAP (it's zip actually) file must be uploaded into the wiki due to security permissions.


I know there is already an extension called Extension:Silverlight. But I wasn't able to make it work in my wiki and decided to write my own. I chose a simple code from Microsoft for embedding Silverlight content and it worked. But the were some issues:

  • It's a problem to globally allow using Silverlight content from other servers
  • A height percentage doesn't work well in some browsers (Chrome is ok).

Therefore this simple extension works with following conditions:

  • Silverlight content must be uploaded to wiki
  • The height in pixels must be set

Installation [edit]

  • Create folder extensions/SLight and put in the SLight.php.
  • In LocalSettings.php add this line
require_once( "$IP/extensions/SLight/SLight.php" );


Allow uploading XAP files [edit]

For uploading XAPs we have to allow it.

LocalSettings.php [edit]

  • $wgFileExtensions[] = 'zip';
  • $wgFileExtensions[] = 'xap';

Includes/mime.types [edit]

  • add xap to the end of row starting with application/zip
  • at the end of the file add following rows:
  application/xaml+xml xaml
  application/x-silverlight-app xap


Code [edit]

SLight.php [edit]

<?php
 
####################################################################################################
# Extension allows inserting Silverlight content
# Silverlight app (XAP extension) can be placed this way:
# <slight width="width px" height="height px">wikifilename (with no "File:")</slight>
# Width is optional, default is 100%. 
# Percentage of height doesn't work in IE for example, so it must be set in pixels.
# XAP (it's zip actually) file must be uploaded into the wiki due to security permissions.
# For uploading XAPs we have to allow:
# LocalSettings.php
#       $wgFileExtensions[] = 'zip';
#       $wgFileExtensions[] = 'xap';
# Includes/mime.types
#       add "xap" to the end of row starting with "application/zip"
#       at the end of the file add following rows:
#               application/xaml+xml xaml
#               application/x-silverlight-app xap
####################################################################################################

 
if ( !defined('MEDIAWIKI') ) {
echo <<<EOT
        To install my extension, put the following line in LocalSettings.php:
        require_once( "\$IP/extensions/SLight/SLight.php" );
EOT;
exit( 1 );
}
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'SLight',
        'author' => 'Josef Martinák',
        'url' => "https://www.mediawiki.org/wiki/Extension:SLight",
        'description' => 'Extension allows inserting Silverlight content',
        'version' => '0.2',
);
 
$wgHooks['ParserFirstCallInit'][] = 'registerParserHook';
 
function registerParserHook( $parser ) {
        $parser->setHook( 'slight', "efSlightRender" );
        return true;
}
 
# Callback function for registerParserHook
# @param $input String: user-supplied input, URL of the Silverlight
# @param $args Array: user-supplied arguments, width, height
# @param $parser Parser: instance of Parser
# @return String: HTML
function efSlightRender( $input, $args, $parser ) {
 
        // set attributes
        if( isset( $args["width"] ) && is_numeric( $args["width"] ) ) {
                $width = $args["width"];
        } else {
                $width = "100%";
        }
 
        if ( isset( $args["height"] ) && is_numeric( $args["height"] ) ) {
                $height = $args["height"];
        } else {
                return '<p class="error">Height of the Silverlight application is not set!</p>';
        }
 
        // input can be either URL or wikifile
        if ( isset( $input ) ) {
                $title = Title::newFromText( $input, 6 );
                if ( $title->exists() ) {
                        $url = wfFindFile( $title )->getFullURL();
                        $output = Html::openElement( "div" );
                        $output .= Html::openElement( "object", array(
                                "id" => "SilverlightPlugin1",
                                "width" => "$width",
                                "height" => "$height",
                                "data" => "data:application/x-silverlight-2", 
                                "type" => "application/x-silverlight-2"
                        ) );
                        $output .= Html::rawElement( "param", array(
                                "name" => "source",
                                "value" => "$url"
                        ) );
                        // Display installation image, if SL not installed
                        $output .= Html::openElement( "a", array(
                                "href" => "http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.60310.0",
                                "style" => "text-decoration: none;"
                        ) );
                        $output .= Html::rawElement( "img", array(
                                "src" => "http://go.microsoft.com/fwlink/?LinkId=161376",
                                "alt" => "Get Microsoft Silverlight",
                                "style" => "border-style: none"
                        ) );
                        $output .= Html::closeElement( "a" );
                        $output .= Html::closeElement( "object" );
                        // for cross-browser compatibility - Safari issue
                        $output .= Html::rawElement( "iframe", array(
                                "id" => "_sl_historyFrame",
                                "style" => "visibility: hidden; height: 0px; width: 0px; border: 0px;"
                        ) );
                        $output .= Html::closeElement( "div" );
                        return $output;
                } else {
                        return '<p class="error">There is no file "' . htmlspecialchars( $input ) . '" on this wiki !!!</p>';
                }
        }
}