forked from openSUSE/wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventCountdown.php
110 lines (98 loc) · 3.67 KB
/
EventCountdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?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";
$wgExtensionCredits['parserhook'][] = array(
'name' => 'EventCountdown',
'description' => 'Makes it easy to display upcoming events.',
'version' => '1.0',
'author' => 'Matt Curtis',
'url' => 'http://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);
}