Extension:Countdown

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

Release status: beta

Countdown example.png
Implementation Tag
Description Allows a wiki page to display one or more DHTML countdowns to a specified date
Author(s) Peter Strömberg, Paul Grinberg
Latest version v1.4
MediaWiki 1.5+
License CC BY-SA
Download Copy the Countdown.php code on this page
#Changelog
Example http://nathania.org/wiki/Vlad_Draconis_PenDragon_interview

Translate the Countdown extension if possible

Check usage and version matrix; code metrics
See Extension:EventCountdown for an updated and maintained extension.

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 three 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
  3. finishmessage — the message to display if the page is rendered after the target time

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

After the timer hits zero, the numbers will turn negative (e.g. -1 seconds, -2 seconds, etc.) If finishmessage is set, the next time the page is rendered, whatever text is in that argument will be displayed instead of the timer. 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.

Installation[edit | edit source]

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

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

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

<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; if the page is rendered after that date/time, say "Did you have a nice Christmas?"[edit | edit source]

<div style="text-align: center; color: red; border: dotted green; padding: 10px">
<countdown finishmessage="Did you have a nice Christmas?" 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 | edit source]

<?php
 
$wgExtensionFunctions[] = "wfCountdownExtension";
$wgExtensionCredits['parserhook'][] = array(
                'version'     => '1.4',
                '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) {
        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;
                        case 'finishmessage':
                                $finishMessage = $value;
                                break;
                        default :
                                wfDebug( __METHOD__.": Requested '$key ==> $value'\n" );
                                break;
                }
        }
 
        if ($localDebug) {
                wfDebug( __METHOD__.": input is '$input'\n\n" );
        }
        $dateTime = DateTime::createFromFormat ( 'm/d/Y g:i A O', str_replace ( 'UTC', '',
                $targetTime ) );
        $currentDateTime = new DateTime;
        if ( isset ( $finishMessage ) && $currentDateTime->diff ( $dateTime )->invert ) {
                return $finishMessage;
        }
 
        $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;
 
        $array = array( 'D', 'H', 'M', 'S' );
        foreach ( $array as $element ) {
                $input = str_replace("<$element>",
                        '<span id="' . $name . '_' . $element . '">' . $element . '</span>', $input );
        }
        $counter .= $parser->mStripState->unstripGeneral($parser->recursiveTagParse ( $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;
if ( s < 0 ) s = "0";
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 | edit source]

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.

Troubleshooting[edit | edit source]

If it says something like "D days, H hours, M minutes, and S seconds" instead of "281 days, 13 hours, 12 minutes, and 11 seconds", try purging the cache.

Changelog[edit | edit source]

  • v1.4 - 6 November 2013 - made it so that when the countdown hits zero, it stops instead of counting backward (e.g. -1, -2, etc.)
  • v1.3 - 1 November 2013 - added an optional finishmessage parameter.
  • v1.2 - 24 October 2007 - made the code a little leaner. Fixed a bug in instantiation.
  • v1.1 - 16 October 2007 - incorporated javascript into PHP file for better maintainability. Made the "name" parameter optional
  • v1.0 - 14 October 2007 - added info so that Special:Version displays correctly for this extension
  •  ?? - original version