Extension:Countdown

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

Release status: unmaintained

Implementation Parser extension
Description Allows a wiki page to display one or more DHTML countdowns to a specified date
Author(s) Peter Strömberg, Paul Grinberg
Last version v1.2
MediaWiki 1.5+ (even works with 1.19!)
License CC-BY-SA
Download Copy the Countdown.php code on this page
Example http://halowiki.net/p/Help:Countdown_Extension
Check usage and version matrix

Countdown is a MediaWiki 1.5, and up, extension that allows a wiki page to display one or more "live" countdowns (using DHTML) to a specified date. It is designed to allow the countdown text to be formatted using wiki syntax. Once it is installed you invoke it via the countdown tag which accepts two arguments;

  1. time - this is the target time to count down to.
  2. name (optional) - used when there are more than one countdown on a page

Withing the countdown opening and closing tags you'll have four extra markups you can use to place the countdown components:

  • <D> - days to target time
  • <H> - hours to target time
  • <M> - minutes to target time
  • <S> - seconds to target time

See below for some usage examples.

Caveats:

  • This plugin seems to be broken. Enabling this plugin in LocalSettings.php according to [the documentation for Extensions in the manual] causes an empty, white page to load instead of your wiki. Don't bother trying to use it unless you are an expert with how MediaWiki works and are capable of fixing whatever is wrong.
  • The "time" attribute, while pretty flexible, must be written in some standard parseable form (parseable by javascript). Use the examples below and modify if you're not familiar with what formats that work.

-- PEZ 20:32, 24 November 2005 (UTC)

Contents

Installation [edit]

Place the following code at the bottom of LocalSettings.php:

require_once("$IP/extensions/Countdown/Countdown.php");


Demo [edit]

See http://halowiki.net/p/Help:Countdown_Extension for some examples on a site where the extension is installed.

Example: Countdown to New Year 2015 (Eastern Standard Time, UTC minus 5 hours) [edit]

<countdown time="12/31/2015 5:00 AM UTC-0500">
* '''Days:''' <D>
* '''Hours:''' <H>
* '''Minutes:''' <M>
* '''Seconds:''' <S>
</countdown>

Example: Countdown to 2015 Christmas Eve in Sweden [edit]

<div style="text-align: center; color: red; border: dotted green; padding: 10px">
<countdown time="12/24/2015 7:00 PM UTC+0100">
Santa will arrive in '''<D>''' days, '''<H>''' hours, '''<M>''' minutes and '''<S>''' seconds. Have you been a good boy/girl?
</countdown>
</div>

<countdown time="12/24/2015 7:00 PM UTC+0100"> Santa will arrive in <D> days, <H> hours, <M> minutes. Have you been a good boy/girl? </countdown>

Countdown.php [edit]

<?php
 
$wgExtensionFunctions[] = "wfCountdownExtension";
$wgExtensionCredits['parserhook'][] = array(
                'version'     => '1.2',
                'name'        => 'Countdown',
                'author'      => array('Peter Strömberg', 'Paul Grinberg'),
                'email'       => 'pez@pezius.com, gri6507 at yahoo dot com',
                'url'         => 'http://www.mediawiki.org/wiki/Extension:Countdown',
                'description' => 'adds <tt>&lt;countdown&gt;</tt> tags',
                );
 
function wfCountdownExtension() {
        global $wgParser;
        $wgParser->setHook( "countdown", "renderCountdown" );
}
 
function renderCountdown($input, $argv, &$parser) {
#function renderCountdown($input, $argv, $parser) { # If you're on php5
        global $wgCountdown;
        $localDebug = 0;
 
        $name = uniqid('countdown');
        $targetTime = "12/24/2007 7:00 PM UTC-0500";
 
        foreach ($argv as $key => $value) {
                switch ($key) {
                        case 'name':
                                $name = $value;
                                break;
                        case 'time':
                                $targetTime = $value;
                                break;
                        default :
                                wfDebug( __METHOD__.": Requested '$key ==> $value'\n" );
                                break;
                }
        }
 
        if ($localDebug) {
                wfDebug( __METHOD__.": input is '$input'\n\n" );
        }
 
        $counter  = <<<END
<!-- CountDown Instance -->
<script type="text/javascript">
var $name = new countdown("$name");
$name.Name = "$name";
$name.TargetDate = "$targetTime";
</script>
<script language="javascript">$name.Setup()</script>
END;
 
        $counter .= $parser->mStripState->unstripGeneral($parser->recursiveTagParse(ereg_replace('<([DHMS])>',
                    '<span id="' . $name . '_\1">\1</span>',$input)));
 
        $javascript  = <<<END
<!-- Script for CountDown -->
<script type="text/javascript">
/*      Author:         Robert Hashemian (http://www.hashemian.com/)
Modified by:    Munsifali Rashid (http://www.munit.co.uk/)
Modified by:    Peter Strömberg (http://halowiki.net/wiki/User:PEZ) */
function countdown(obj) {
this.obj                = obj;
this.Name               = "clock";
this.TargetDate         = "12/31/2020 5:00 AM UTC+0100";
this.CountActive        = true;
this.Calcage            = cd_Calcage;
this.CountBack          = cd_CountBack;
this.Setup              = cd_Setup;
}
function cd_Calcage(secs, num1, num2) {
s = ((Math.floor(secs/num1))%num2).toString();
if (s.length < 2) s = "0" + s;
return (s);
}
function cd_CountBack(secs) {
try { document.getElementById(this.Name + "_D").innerHTML = this.Calcage(secs,86400,100000); } catch(e) {};
try { document.getElementById(this.Name + "_H").innerHTML = this.Calcage(secs,3600,24); } catch(e) {};
try { document.getElementById(this.Name + "_M").innerHTML = this.Calcage(secs,60,60); } catch(e) {};
try { document.getElementById(this.Name + "_S").innerHTML = this.Calcage(secs,1,60); } catch(e) {};
if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
}
function cd_Setup() {
var ddiff       = new Date((new Date(this.TargetDate)) - (new Date()));
this.CountBack(Math.floor(ddiff.valueOf() / 1000));
}
</script>
END;
        $output = '';
        if ($wgCountdown != 1) {
                $output .= $javascript;
                $wgCountdown = 1;
        }
        $output .= $counter;
 
        return $output;
}

Notes [edit]

The JavaScript code is based on Robert Hashemian's/Mun Rashid's countdown.js. I have made some adaptations to better support the flexible embedding in a wiki page that I want.

Revisions [edit]

  • v1.2 - October 24, 2007 - made the code a little leaner. Fixed a bug in instantiation.
  • v1.1 - October 16, 2007 - incorporated javascript into PHP file for better maintainability. Made the "name" parameter optional
  • v1.0 - October 14, 2007 - added info so that Special:Version displays correctly for this extension
  •  ?? - original version