forked from wikimedia/mediawiki-extensions-intersection
-
Notifications
You must be signed in to change notification settings - Fork 13
/
EventCalendar.php
223 lines (180 loc) · 7.64 KB
/
EventCalendar.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*
Yet Another Simple Event Calendar
https://github.com/improper/mediawiki-extensions-yasec
Outputs a tabular calendar filled with events automatically generated
from page titles in a certain namespace. Based on the intersection extension.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
http://www.gnu.org/copyleft/gpl.html
*/
if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This is not a valid entry point to MediaWiki.' );
}
// Extension credits that will show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name' => 'EventCalendar',
'version' => '0.2.1',
'description' => 'Yet Another Simple Event Calendar',
'url' => 'https://github.com/improper/mediawiki-extensions-yasec',
'author' => 'Steffen Beyer and others',
);
// JavaScript and CSS resources
$wgResourceModules['ext.yasec'] = array(
// JavaScript and CSS styles. To combine multiple files, just list them as an array.
'scripts' => array(
'fullcalendar/lib/moment.min.js',
'fullcalendar/fullcalendar/fullcalendar.min.js',
'ext.yasec.core.js',
),
'styles' => array(
'fullcalendar/fullcalendar/fullcalendar.css',
'ext.yasec.css',
),
// When your module is loaded, these messages will be available through mw.msg().
// E.g. in JavaScript you can access them with mw.message( 'myextension-hello-world' ).text()
// 'messages' => array( 'myextension-hello-world', 'myextension-goodbye-world' ),
// If your scripts need code from other modules, list their identifiers as dependencies
// and ResourceLoader will make sure they're loaded before you.
// You don't need to manually list 'mediawiki' or 'jquery', which are always loaded.
'dependencies' => array( 'jquery.ui.datepicker' ),
// You need to declare the base path of the file paths in 'scripts' and 'styles'
'localBasePath' => __DIR__ . '/resources',
// ... and the base from the browser as well. For extensions this is made easy,
// you can use the 'remoteExtPath' property to declare it relative to where the wiki
// has $wgExtensionAssetsPath configured:
'remoteExtPath' => 'yasec/resources'
);
// Configuration variables (change in LocalSettings.php)
# How long to cache pages using EventCalendar in seconds. Default to 1 day.
# Set to false to use the normal amount of page caching (most efficient),
# set to 0 to disable cache altogether (inefficient, but results will never
# be outdated)
$wgECMaxCacheTime = 60*60*24; // How long to cache pages in seconds
$wgHooks['ParserFirstCallInit'][] = 'wfEventCalendar';
/**
* Set up the <EventCalendar> tag.
*
* @param $parser Object: instance of Parser
* @return Boolean: true
*/
function wfEventCalendar( &$parser ) {
$parser->setHook( 'EventCalendar', 'renderEventCalendar' );
return true;
}
// The callback function for converting the input text to HTML output
function renderEventCalendar( $input, $args, $mwParser ) {
// config variables
global $wgECMaxCacheTime;
global $wgContLang;
global $wgECCounter; // instantiation counter
$wgECCounter += 1;
$mwParser->getOutput()->addModules( 'ext.yasec' );
if ( $wgECMaxCacheTime !== false ) {
$mwParser->getOutput()->updateCacheExpiry( $wgECMaxCacheTime );
}
// defaults
$aspectRatio = 1.6;
$namespaceIndex = 0;
$parameters = explode( "\n", $input );
foreach ( $parameters as $parameter ) {
$paramField = explode( '=', $parameter, 2 );
if( count( $paramField ) < 2 ) {
continue;
}
$type = trim( $paramField[0] );
$arg = trim( $paramField[1] );
switch ( $type ) {
case 'aspectratio':
$aspectRatio = floatval( $arg );
break;
case 'namespace':
$ns = $wgContLang->getNsIndex( $arg );
if ( $ns != null ) {
$namespaceIndex = $ns;
}
break;
} // end main switch()
} // end foreach()
// build the SQL query
$dbr = wfGetDB( DB_SLAVE );
$tables = array( 'page' );
$fields = array( 'page_title' );
$where = array();
$options = array();
$where['page_namespace'] = $namespaceIndex;
$title_pattern = '^[0-9]{4}/[0-9]{2}/[0-9]{2}_[[:alnum:]]';
if ( ! $dbr instanceof DatabaseSqlite ) {
if ( $dbr instanceof DatabasePostgres ) {
$regexp_op = '~';
} else {
$regexp_op = 'REGEXP';
}
$where[] = "page_title " . $regexp_op . " '" . $title_pattern . "'";
}
$options['ORDER BY'] = 'page_title DESC';
$options['LIMIT'] = 5000; // should limit output volume to about 300 KiB
// assuming 60 bytes per entry
// process the query
$res = $dbr->select( $tables, $fields, $where, __METHOD__, $options );
$eventmap = array();
foreach ( $res as $row ) {
if ( $dbr instanceof DatabaseSqlite ) {
if( ! preg_match("@" . $title_pattern . "@", $row->page_title)) {
continue; // Ignoring page titles that don't follow the
// pattern of event pages
}
}
$date = str_replace( '/', '-', substr( $row->page_title, 0, 10 ));
$title = str_replace( '_', ' ', substr( $row->page_title, 11 ));
$url = Title::makeTitle( $namespaceIndex, $row->page_title )->getLinkURL();
if ( !array_key_exists( $title, $eventmap )) {
$eventmap[$title] = array();
}
// minimal interval is one day
$tempdate = date_create( $date );
date_add( $tempdate, date_interval_create_from_date_string( '1 day' ));
$enddate = date_format( $tempdate, 'Y-m-d' );
// look for events with same name on consecutive days
$last = array_pop( $eventmap[$title] );
if ( $last !== NULL ) {
if ( $last['start'] == $enddate ) {
// conflate multi-day event
$enddate = $last['end'];
} else {
// no match, keep last event
$eventmap[$title][] = $last;
}
}
$eventmap[$title][] = array(
'title' => $title,
'start' => $date,
'end' => $enddate,
'url' => $url,
);
}
// concatenate all events to single list
$events = array();
foreach ( $eventmap as $entries ) {
$events = array_merge( $events, $entries );
}
// calendar container and data array
$output = "<div id=\"eventcalendar-{$wgECCounter}\"></div>\n" .
"<script>\n" .
"if ( typeof window.eventCalendarAspectRatio !== 'object' ) { window.eventCalendarAspectRatio = []; }\n" .
"window.eventCalendarAspectRatio.push( {$aspectRatio} );\n" .
"if ( typeof window.eventCalendarData !== 'object' ) { window.eventCalendarData = []; }\n" .
"window.eventCalendarData.push( " . json_encode( $events ) . " );\n" .
"</script>\n";
return array( $output, 'markerType' => 'nowiki' );
}