Extension:EventCountdown

From MediaWiki.org

Jump to: navigation, search

           

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

Release status: beta

Implementation  Tag
Description Makes it easy to display upcoming events.
Author(s)  Matt Curtis (Razor.bakTalk)
Last Version  1.0 (2006-02-15)
MediaWiki  1.5 and up
License GPLv2
Download see below
Example  <eventcountdown date="10-May-2006"><daysuntil in="days">10-May-2006</daysuntil> until E3 2006</eventcountdown>

check usage (experimental)

EventCountdown extension makes it easy to display upcoming events, possibly showing the number of days until the event. Your event description will be shown up until the day of the event, then it will be automatically hidden.

For example:

<eventcountdown date="10-May-2006"><daysuntil in="days">10-May-2006</daysuntil> until [http://www.e3expo.com E3 2006]</eventcountdown>

shows (on 15th February 2006):

84 days until E3 2006

When the event arrives, the countdown message will no longer be displayed.

Contents

[edit] Installation

Copy the file below into extensions/EventCountdown.php, and add this line to your LocalSettings.php:

require_once("extensions/EventCountdown.php");

[edit] Tags

Both tags use php's strtotime(), so they are quite flexible on the format you use to specify the date.

[edit] <eventcountdown>

The <eventcountdown> tag will show its contents only until the date arrives. On that date, and subsequently, it will show nothing. The contents can be wikitext. For example:

<eventcountdown date="10-May-2006">Get ready for '''E3'''!</eventcountdown>

[edit] <daysuntil>

The <daysuntil> tag is replaced with the number of days until the date. The optional 'in="days"' argument will append "day" or "days" as appropriate.

E3 is <daysuntil in="days">10 May 2005</daysuntil> away.

[edit] ToDo

  • Localization ("day"/"days") - if you know how to do this, please consider updating this page.

[edit] Source code

[edit] extensions/EventCountdown.php

<?php
# EventCountdown extension
# Copyright 2006 Matt Curtis (matt.r.curtis at gmail.com)
#
# Minor edits by Kaolin Fire to get rid of undefined index warnings
#
# License:
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#
# Usage:
#
# The <daysuntil> tag is replaced with the number of days until the date.
# Formatting uses php's strtotime(), so it's quite flexible on the input.
# The optional 'in="days"' argument will append "day" or "days" as
# appropriate.
#
#   E3 is <daysuntil in="days">10 May 2005</daysuntil> away.
#
# The <eventcountdown> tag will show its contents only until the
# date arrives. The contents can be wikitext. For example:
#
#   <eventcountdown date="10-May-2006">Get ready for '''E3'''!</eventcountdown>
#
# They are most useful when combined. For example, to display "x days until
# E3 2006" with a link to E3:
#
#   <eventcountdown date="10-May-2006"><daysuntil in="days">10-May-2006
#     </daysuntil> until [http://www.e3expo.com E3 2006]</eventcountdown>
#
# To activate the extension, include it from your LocalSettings.php
# with: require_once("extensions/EventCountdown.php");

$wgExtensionFunctions[] = "wfEventCountdownExtension";
 
 
function wfEventCountdownExtension() {
        global $wgParser;
        # register the extension with the WikiText parser
        # the first parameter is the name of the new tag.
        # In this case it defines the tag <example> ... </example>
        # the second parameter is the callback function for
        # processing the text between the tags
        $wgParser->setHook( "daysuntil", "runDaysUntil" );
        $wgParser->setHook( "eventcountdown", "runShowEventCountdown" );
}
 
function runDaysUntil( $input, $argv ) {
        $now = time();
        $then = strtotime($input);
 
        $daysUntil = getDaysBetween($now, $then);
        $output = $daysUntil;
        if (!array_key_exists("in",$argv)) return $output;
        switch ($argv["in"]) {
        case "days":
                if ($daysUntil == 1) {
                        $output .= " day";
                }
                else {
                        $output .= " days";
                }
                break;
 
        default:
        }
 
        return $output;
}
 
function runShowEventCountdown( $input, $argv ) {
        $now = time();
        if (!array_key_exists("date",$argv)) return "";
        $then = strtotime($argv["date"]);
        $daysUntil = getDaysBetween($now, $then);
 
        $output = "";
 
        if ($daysUntil > 0) {
                global $wgOut;
                $output = $wgOut->parse($input, false);
        }
 
        return $output;
}
 
function getDaysBetween($date1, $date2) {
        $deltaSeconds = $date2 - $date1;
        $deltaDays = $deltaSeconds / (60 * 60 * 24);
        return ceil($deltaDays);
}