Jump to content

Extension:EventCountdown

From mediawiki.org
Revision as of 03:37, 10 November 2016 by BrentLaabs (talk | contribs) (since this extension is unmaintained, suggest an easy and common alternative)
MediaWiki extensions manual
EventCountdown
Release status: unmaintained
Implementation Tag
Description Allows to display countdowns for upcoming events
Author(s) Matt Curtis (Razor.baktalk)
Latest version 1.0 (2006-02-15)
MediaWiki 1.5+
Database changes No
License GNU General Public License 2.0 or later
Download see below

  • <eventcountdown>
  • <daysuntil>

The 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.

Installation

  • Copy the code into a file and place the file(s) in a directory called EventCountdown in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    require_once "$IP/extensions/EventCountdown/EventCountdown.php";
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Usage

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.

Tags

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

<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>

<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.

ToDo

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

Source code

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");

if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}

$wgExtensionFunctions[] = "wfEventCountdownExtension";

$wgExtensionCredits['parserhook'][] = array(
        'name' => 'EventCountdown',
        'description' => 'Allows to display countdowns for upcoming events',
        'version' => '1.0',
        'author' => 'Matt Curtis',
        'url' => 'https://www.mediawiki.org/wiki/Extension:EventCountdown',
);

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);
}

See also