Extension:Javascript Slideshow/slideshow.php

From mediawiki.org
Revision as of 02:36, 3 September 2008 by Cmreigrut (talk | contribs) (Version 0.2.0)
<?php
# This extension will show the contents of a single <DIV> inside of it, and will
# rotate through the remaining DIVs as specified
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/Slideshow/slideshow.php");

if( !defined( 'MEDIAWIKI' ) ) {
        die();
}

$wgExtensionFunctions[] = "wfSlideshowExtension";
$wgHooks['LanguageGetMagic'][]  = 'wfSlideshowExtensionMagic';

$wgExtensionCredits['other'][] = array(
        'name' => 'Javascript Slideshow',
        'author' => 'Chris Reigrut',
        'version' => '0.2.0',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Javascript_Slideshow',
        'description' => 'Create a slideshow from multiple included DIV elements'
);

function wfSlideshowExtension() {
    global $wgParser;
    # register the extension with the WikiText parser
    $wgParser->setHook( "slideshow", "renderSlideshowTag" );
    $wgParser->setFunctionHook( "slideshow", "renderSlideshowParserFunction" );
}

function wfSlideshowExtensionMagic( &$magicWords, $langCode ) {
        # Add the magic word
        # The first array element is case sensitive, in this case it is not case sensitive
        # All remaining elements are synonyms for our parser function
        $magicWords['slideshow'] = array( 0, 'slideshow' );
        # unless we return true, other parser functions extensions won't get loaded.
        return true;
}

$SS_SCRIPT_INCLUDED = false;
function addScript($parser) {
	global $SS_SCRIPT_INCLUDED, $wgScriptPath;
	
	if (!$SS_SCRIPT_INCLUDED) {
		$parser->mOutput->addHeadItem("<script src=\"{$wgScriptPath}/extensions/Slideshow/slideshow.js\"></script>\n");
		$parser->mOutput->addHeadItem("<script src=\"{$wgScriptPath}/extensions/Slideshow/scriptaculous/prototype.js\"></script>\n");
		$parser->mOutput->addHeadItem("<script src=\"{$wgScriptPath}/extensions/Slideshow/scriptaculous/scriptaculous.js\"></script>\n");
		$SS_SCRIPT_INCLUDED = true;
	}
}

function explode_assoc($glue1, $glue2, $array) {
	$array2 = explode($glue2, $array);
	foreach($array2 as  $val) {
		$pos = strpos($val,$glue1);
		$key = substr($val,0,$pos);
		$array3[$key] = trim(substr($val,$pos+1,strlen($val)));
	}
	return $array3;
}

function renderSlideshowParserFunction( &$parser, $wikitext='', $options='') {
	//$parser->disableCache();
	addScript($parser);
	return renderSlideshow($wikitext, explode_assoc('=', ' ', $options));
}

# The callback function for converting the input text to HTML output
function renderSlideshowTag( $input, $argv, &$parser ) {
	$wikitext = renderSlideshow($input, $argv);
	# Parse the wikitext with a local parser and return it
	$localParser = new Parser();
	//$localParser->disableCache();
	addScript($parser);
	return $localParser->parse($wikitext, $parser->mTitle, $parser->mOptions, false)->getText();
}

function renderSlideshow($wikitext, $options) {
	$isValid = true;
	$validSequences = array('forward', 'backward', 'random');
	$validTransitions = array('cut', 'fade', 'blindDown');
	
	$output = '';
	
	$id = (isset($options['id'])) ? $options['id'] : 'slideshow_' . rand(); 
	$refresh = (isset($options['refresh'])) ? $options['refresh'] : '1000'; 

	$sequence = (isset($options['sequence'])) ? $options['sequence'] : 'random';
	if (!in_array($sequence, $validSequences) ) {
		$output .=  "Invalid sequence $sequence (may be one of: " . implode(',', $validSequences) . "). ";
		$isValid = false;
	}
	
	$transition = (isset($options['transition'])) ? $options['transition'] : 'cut'; 
	if (!in_array($transition, $validTransitions) ) {
		$output .=  "Invalid transition $transition (may be one of: " . implode(',', $validTransitions) . "). ";
		$isValid = false;
	}
	
	if ($isValid) {
		//$output .= "Slideshow ({$id} {$refresh} {$sequence} {$transition}):";
		$output .= "<div id='$id' class='slideshow'>$wikitext</div> ";
		
		$script = <<<ENDSCRIPT
	
	<script type="text/javascript">
/*<![CDATA[*/
		startSlideshow('$id', $refresh, '$sequence', '$transition');
/*]]>*/
	</script>
ENDSCRIPT;

		$output .= encode($script);
	}
		
	return $output;
}

# Process the page after tidy runs, decoding all encoded portions
if (!function_exists('processEncodedOutput')) {
    $wgHooks['ParserAfterTidy'][] = 'processEncodedOutput';
    function processEncodedOutput( &$out, &$text ) {
 
        # Find all hidden content and restore to normal
        $text = decode($text);
 
        return true;
    }


	# Encode the given string so that it will no longer be processed by the wikitext engine
	function encode($text) {
		return '-- ENCODED_CONTENT '.base64_encode($text).' --';
	}

	# Decode all of the encoded portions of the page
	function decode($text) {
		return preg_replace(
            	'/-- ENCODED_CONTENT ([0-9a-zA-Z\\+\/]+=*) --/esm',
            	'base64_decode("$1")',
            	$text
        	);
	}
}
?>