Extension:Javascript Slideshow/slideshow.php

From MediaWiki.org
Jump to: navigation, search
<?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';
$wgHooks['MakeGlobalVariablesScript'][] = 'wfSlideshowSetGlobalJSVariables';
 
$wgExtensionCredits['other'][] = array(
	'name' => 'Javascript Slideshow',
	'author' => array( 'Chris Reigrut', 'Yaron Koren' ),
	'version' => '0.3',
	'url' => 'https://www.mediawiki.org/wiki/Extension:Javascript_Slideshow',
	'description' => 'Create a slideshow from multiple included div elements'
);
 
$slideshowIP = dirname( __FILE__ );
 
$slideshowResourceTemplate = array(
	'localBasePath' => $slideshowIP,
	'remoteExtPath' => 'Slideshow',
);
 
$wgResourceModules += array(
	'ext.slideshow.main' => $slideshowResourceTemplate + array(
		'scripts' => array(
			'slideshow.js',
		),
	),
);
 
function wfSlideshowExtension() {
	global $wgOut, $wgParser;
 
	$wgOut->addModules( 'ext.slideshow.main' );
	$wgParser->setHook( "slideshow", "renderSlideshowTag" );
	$wgParser->setFunctionHook( "slideshow", "renderSlideshowParserFunction" );
}
 
function wfSlideshowExtensionMagic( &$magicWords, $langCode ) {
	$magicWords['slideshow'] = array( 0, 'slideshow' );
	return 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, $input = '', $options = '' ) {
	$parser->disableCache();
	return renderSlideshow( $input, explode_assoc( '=', ' ', $options ) );
}
 
# The callback function for converting the input text to HTML output
function renderSlideshowTag( $input, $argv, $parser, $frame ) {
	$wikitext = renderSlideshow( $input, $argv );
	$parser->disableCache();
	return $parser->recursiveTagParse( $wikitext, $frame );
}
 
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'] : 'forward';
	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) {
		global $wgSlideshowRefresh, $wgSlideshowSequence, $wgSlideshowTransition;
		$wgSlideshowRefresh = $refresh;
		$wgSlideshowSequence = $sequence;
		$wgSlideshowTransition = $transition;
		$output .= "<div id='$id' class='slideshow'>$wikitext</div> ";
		$output .= "<div id='$id-spacer' class='slideshowspacer'></div>";
	}
 
	return $output;
}
 
// @TODO - this use of global variables means that two slideshows on the
// same page can't have different settings, which is a bug.
function wfSlideshowSetGlobalJSVariables( &$vars ) {
	global $wgSlideshowRefresh, $wgSlideshowSequence, $wgSlideshowTransition;
	$vars['wgSlideshowRefresh'] = $wgSlideshowRefresh;
	$vars['wgSlideshowSequence'] = $wgSlideshowSequence;
	$vars['wgSlideshowTransition'] = $wgSlideshowTransition;
	return true;
}