summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/SemanticResultFormats.parser.php
blob: 3adfdf4bfeaf7ff053751900db24900a4e741c35 (plain)
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
<?php

/**
 * Parser functions for the Semantic Result Formats extension.
 *
 * Two parser functions are defined, both for use by the Calendar format:
 *
 * #calendarstartdate returns the start date for the set of dates being
 *    displayed on the screen, according to the query string.
 *
 * #calendarenddate returns the *day after* the end date for the set of dates
 *    being displayed on the screen, according to the query string.
 *
 * @author David Loomer
 */
class SRFParserFunctions {

	static function registerFunctions( &$parser ) {
		$parser->setFunctionHook( 'calendarstartdate', [ 'SRFParserFunctions', 'runCalendarStartDate' ] );
		$parser->setFunctionHook( 'calendarenddate', [ 'SRFParserFunctions', 'runCalendarEndDate' ] );
		return true;
	}

	static function runCalendarStartDate( &$parser, $calendar_type = 'month', $calendar_start_day = null, $calendar_days = 7, $default_year = null, $default_month = null, $default_day = null ) {
		if ( $calendar_type == '' ) $calendar_type = 'month';
		list( $lower_date, $upper_date, $query_date ) =
			SRFParserFunctions::getBoundaryDates( $calendar_type, $calendar_start_day, $calendar_days, $default_year, $default_month, $default_day );
		return date( "Y", $lower_date ) . '-' . date( "m", $lower_date ) . '-' . date( "d", $lower_date );
	}

	static function runCalendarEndDate( &$parser, $calendar_type = 'month', $calendar_start_day = null, $calendar_days = 7, $default_year = null, $default_month = null, $default_day = null ) {
		if ( $calendar_type == '' ) $calendar_type = 'month';
		list( $lower_date, $upper_date, $query_date ) =
			SRFParserFunctions::getBoundaryDates( $calendar_type, $calendar_start_day, $calendar_days, $default_year, $default_month, $default_day );
		return date( "Y", $upper_date ) . '-' . date( "m", $upper_date ) . '-' . date( "d", $upper_date );
	}

	/**
	 * Obtain both a lower- and upper- bound date to be used for querying.
	 *
	 * @param $calendar_type string Values: 'month' (the default) for monthly
	 *	calendar such as SRF Calendar; others not yet defined.
	 * @param $calendar_start_day int Optionally force the lower bound date to be a certain
	 *	day of the week (0 for Sunday, 6 for Saturday).  If using a $calendar_type
	 *	of 'month' this parameter is ignored, as the start day of week for a monthly
	 *	calendar is currently always set as Sunday.  Ohterwise defaults to either the day
	 *	supplied in the query string, or the current day.
	 * @param $calendar_days int The number of days to display.  Ignored if using a
	 *	$calendar_type of 'month'; otherwise defaults to 7.
	 * @param $default_year int (Optional) Default year if none is specified in
	 *	the query string.  If parameter is not supplied, will fall back to current year.
	 * @param $default_month int (Optional) Default month if none is specified in
	 *	the query string.  If parameter is not supplied, will fall back to current month.
	 * @param $default_day int (Optional) Default day of month if none is specified in
	 *	the query string.  If parameter is not supplied, will fall back to current day of month.
	 * @return array First element contains the lower bound date, second
	 *	element contains the upper bound, third element contains a date indicating
	 *	the year/month/day to be queried.
	 *
	 */
	static function getBoundaryDates( $calendar_type = 'month', $calendar_start_day = null, $calendar_days = 7, $default_year = null, $default_month = null, $default_day = null ) {
 		if ( $calendar_type == 'month' ) $calendar_start_day = 0;

		if ( $default_year == null ) $default_year = date( "Y", time() );
		if ( $default_month == null ) $default_month = date( "n", time() );
		if ( $default_day == null ) $default_day = date( "j", time() );

		global $wgRequest;

		// Set the lower bound based on query string parameters if provided;
		// otherwise fall back to defaults.
		if ( $wgRequest->getCheck( 'year' ) && $wgRequest->getCheck( 'month' ) ) {

			$query_year = $wgRequest->getVal( 'year' );
			if ( is_numeric( $query_year ) && ( intval( $query_year ) == $query_year ) ) {
				$lower_year = $query_year;
			}
			else {
				$lower_year = $default_year;
			}

			$query_month = $wgRequest->getVal( 'month' );
			if ( is_numeric( $query_month ) && ( intval( $query_month ) == $query_month ) && $query_month >= 1 && $query_month <= 12 ) {
				$lower_month = $query_month;
			}
			else {
				$lower_month = $default_month;
			}

			$query_day = $wgRequest->getVal( 'day' );
			if ( $wgRequest->getCheck( 'day' )
				&& is_numeric( $query_day )
				&& ( intval( $query_day ) == $query_day )
				&& $query_day >= 1
				&& $query_day <= 31 ) {

				$lower_day = $query_day;
			} elseif ( $calendar_type != 'month'
				&& (int)$lower_year == (int)$default_year
				&& (int)$lower_month == (int)$default_month ) {

				$lower_day = $default_day;
			}
			else {
				$lower_day = '1';
			}

		} else {
			$lower_year = $default_year;
			$lower_month = $default_month;

			if ( $calendar_type == 'month' ) {
				$lower_day = 1;
			}
			else {
				$lower_day = $default_day;
			}
		}

		$lower_date = mktime( 0, 0, 0, $lower_month, $lower_day, $lower_year );

		// Date to be queried
		$return_date = $lower_date;

		// Set the upper bound based on calendar type or number of days.
		if ( $calendar_type == 'month' ) {
			$upper_year = date( "Y", $lower_date );
			$upper_month = date( "n", $lower_date ) + 1;
			if ( $upper_month == 13 ) {
				$upper_month = 1;
				$upper_year = $upper_year + 1;
			}
			// One second before start of next month.
			$upper_date = mktime( 0, 0, 0, $upper_month, 1, $upper_year ) - 1;
		} else {
			// One second before start of first day outside our range.
			$upper_date = $lower_date + $calendar_days * 86400 - 1;
		}

		// If necessary, adjust bounds to comply with required days of week for each.
		if ( $calendar_type == 'month' || $calendar_start_day >= 0 ) {
			$lower_offset = date( "w", $lower_date ) - $calendar_start_day;

			if ( $lower_offset < 0 ) {
				$lower_offset += 7;
			}

			if ( $calendar_type == 'month' ) {
				$upper_offset = $calendar_start_day + 6 - date( "w", $upper_date );

				if ( $upper_offset > 6 ) {
					$upper_offset -= 7;
				}
			} else {
				$upper_offset = 0 - $lower_offset;
			}

			$lower_date = $lower_date - 86400 * $lower_offset;
			$upper_date = $upper_date + 86400 * $upper_offset;
		}

		// Add a day since users will need to use < operator for upper date.
		$upper_date += 86400;

		return [ $lower_date, $upper_date, $return_date ];
	}

}