allwiki首页  
天下维客 你可以修改的网络知识库
首页最近更改优秀条目专题展示电脑科技词典软件学习网络知识电脑安全明星时尚天下百科
 

MediaWiki扩展:事件倒计时

天下维客,你可以修改的网络知识库

Jump to: navigation, search

本文取自元维基,欢迎共同翻译、整理与大家共享,促进中文wiki发展^_^

这个扩展可以轻松的使得条目中对于某些即将来临的日期进行倒数。当那个日期到来时,这个扩展将会自动消失。

例如:

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

显示 (on 15th February 2006):

84 days until E3 2006

当日期到达时,这个模板会自动隐藏.

目录

安装

将代码Copy到"Extensions/EventCountdown.php"中,以及在Localsettings.php中添加下列代码。

require_once("extensions/EventCountdown.php");

标签

多数标签使用 php 的 strtotime(),所以他们在你所使用的日期中有很高的可塑性

<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") - I don't know how to do this; if you do, please consider updating this page. --Razor.bak 06:56, 15 February 2006 (UTC)



Source code

extensions/EventCountdown.php

<?php
# EventCountdown extension
# Copyright 2006 Matt Curtis (matt.r.curtis at gmail.com)
#
# 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;
        switch ($argv["in"]) {
        case "days":
                if ($daysUntil == 1) {
                        $output .= " day";
                }
                else {
                        $output .= " days";
                }
                break;

        default:
        }

        return $output;
}

function runShowEventCountdown( $input, $argv ) {
        $now = time();
        $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);
}

?>

--Razor.bak 06:56, 15 February 2006 (UTC)

原文参见: EventCountdown Extension

Personal tools
工具
金银币拍卖 金币拍卖预展  金银币网店 熊猫金银币 生肖金银币