Extension:Countdown

From MediaWiki.org

Jump to: navigation, search

           

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Countdown

Release status: stable

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+
License No license specified
Download Copy the Countdown.php code on this page
Example  http://halowiki.net/p/Help:Countdown_Extension

check usage (experimental)

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:

  • 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

[edit] Demo

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

[edit] Example: Countdown to New Year 2010

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

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

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

[edit] Countdown.php

<?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->unstrip($parser->recursiveTagParse(ereg_replace('<([DHMS])>','<span id="' . $name . '_\1">\1</span>',$input)), $parser->mStripState) . "\n";
 
        $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;
}

[edit] Notes

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.

[edit] Revisions

  • 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