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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
<?php
/**
* Create iCalendar exports
* @file
* @ingroup SemanticResultFormats
*/
/**
* Printer class for creating iCalendar exports
*
* @author Markus Krötzsch
* @author Denny Vrandecic
* @author Jeroen De Dauw
*
* @ingroup SemanticResultFormats
*/
class SRFiCalendar extends SMWExportPrinter {
protected $m_title;
protected $m_description;
protected function handleParameters( array $params, $outputmode ) {
parent::handleParameters( $params, $outputmode );
$this->m_title = trim( $params['title'] );
$this->m_description = trim( $params['description'] );
}
/**
* @see SMWIExportPrinter::getMimeType
*
* @since 1.8
*
* @param SMWQueryResult $queryResult
*
* @return string
*/
public function getMimeType( SMWQueryResult $queryResult ) {
return 'text/calendar';
}
/**
* @see SMWIExportPrinter::getFileName
*
* @since 1.8
*
* @param SMWQueryResult $queryResult
*
* @return string|boolean
*/
public function getFileName( SMWQueryResult $queryResult ) {
if ( $this->m_title != '' ) {
return str_replace( ' ', '_', $this->m_title ) . '.ics';
} else {
return 'iCalendar.ics';
}
}
public function getQueryMode( $context ) {
return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
}
public function getName() {
return wfMessage( 'srf_printername_icalendar' )->text();
}
protected function getResultText( SMWQueryResult $res, $outputmode ) {
return $outputmode == SMW_OUTPUT_FILE ? $this->getIcal( $res ) : $this->getIcalLink( $res, $outputmode );
}
/**
* Returns the query result in iCal.
*
* @since 1.5.2
*
* @param SMWQueryResult $res
*
* @return string
*/
protected function getIcal( SMWQueryResult $res ) {
$result = '';
if ( $this->m_title == '' ) {
global $wgSitename;
$this->m_title = $wgSitename;
}
$result .= "BEGIN:VCALENDAR\r\n";
$result .= "PRODID:-//SMW Project//Semantic Result Formats\r\n";
$result .= "VERSION:2.0\r\n";
$result .= "METHOD:PUBLISH\r\n";
$result .= "X-WR-CALNAME:" . $this->m_title . "\r\n";
if ( $this->m_description !== '' ) {
$result .= "X-WR-CALDESC:" . $this->m_description . "\r\n";
}
// TODO: http://www.kanzaki.com/docs/ical/vtimezone.html
// $result .= "BEGIN:VTIMEZONE\r\n";
// $result .= "TZID:\r\n";
$row = $res->getNext();
while ( $row !== false ) {
$result .= $this->getIcalForItem( $row );
$row = $res->getNext();
}
// $result .= "END:VTIMEZONE\r\n";
$result .= "END:VCALENDAR\r\n";
return $result;
}
/**
* Returns html for a link to a query that returns the iCal.
*
* @since 1.5.2
*
* @param SMWQueryResult $res
* @param $outputmode
*
* @return string
*/
protected function getIcalLink( SMWQueryResult $res, $outputmode ) {
if ( $this->getSearchLabel( $outputmode ) ) {
$label = $this->getSearchLabel( $outputmode );
} else {
$label = wfMessage( 'srf_icalendar_link' )->inContentLanguage()->text();
}
$link = $res->getQueryLink( $label );
$link->setParameter( 'icalendar', 'format' );
if ( $this->m_title !== '' ) {
$link->setParameter( $this->m_title, 'title' );
}
if ( $this->m_description !== '' ) {
$link->setParameter( $this->m_description, 'description' );
}
if ( array_key_exists( 'limit', $this->params ) ) {
$link->setParameter( $this->params['limit'], 'limit' );
} else { // use a reasonable default limit
$link->setParameter( 20, 'limit' );
}
// yes, our code can be viewed as HTML if requested, no more parsing needed
$this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
return $link->getText( $outputmode, $this->mLinker );
}
/**
* Returns the iCal for a single item.
*
* @since 1.5.2
*
* @param array $row
*
* @return string
*/
protected function getIcalForItem( array $row ) {
$result = '';
$wikipage = $row[0]->getResultSubject(); // get the object
$wikipage = SMWDataValueFactory::newDataItemValue( $wikipage, null );
$params = array(
'summary' => $wikipage->getShortWikiText()
);
foreach ( $row as /* SMWResultArray */ $field ) {
// later we may add more things like a generic
// mechanism to add whatever you want :)
// could include funny things like geo, description etc. though
$req = $field->getPrintRequest();
$label = strtolower( $req->getLabel() );
switch ( $label ) {
case 'start': case 'end':
if ( $req->getTypeID() == '_dat' ) {
$dataValue = $field->getNextDataValue();
if ( $dataValue === false ) {
unset( $params[$label] );
}
else {
$params[$label] = $dataValue;
}
}
break;
case 'location': case 'description': case 'summary':
$value = $field->getNextDataValue();
if ( $value !== false ) {
$params[$label] = $value->getShortWikiText();
}
break;
}
}
$title = $wikipage->getTitle();
$article = new Article( $title );
$url = $title->getFullURL();
$result .= "BEGIN:VEVENT\r\n";
$result .= "SUMMARY:" . $params['summary'] . "\r\n";
$result .= "URL:$url\r\n";
$result .= "UID:$url\r\n";
if ( array_key_exists( 'start', $params ) ) $result .= "DTSTART:" . $this->parsedate( $params['start'] ) . "\r\n";
if ( array_key_exists( 'end', $params ) ) $result .= "DTEND:" . $this->parsedate( $params['end'], true ) . "\r\n";
if ( array_key_exists( 'location', $params ) ) $result .= "LOCATION:" . $params['location'] . "\r\n";
if ( array_key_exists( 'description', $params ) ) $result .= "DESCRIPTION:" . $params['description'] . "\r\n";
$t = strtotime( str_replace( 'T', ' ', $article->getTimestamp() ) );
$result .= "DTSTAMP:" . date( "Ymd", $t ) . "T" . date( "His", $t ) . "\r\n";
$result .= "SEQUENCE:" . $title->getLatestRevID() . "\r\n";
$result .= "END:VEVENT\r\n";
return $result;
}
/**
* Extract a date string formatted for iCalendar from a SMWTimeValue object.
*/
static private function parsedate( SMWTimeValue $dv, $isend = false ) {
$year = $dv->getYear();
if ( ( $year > 9999 ) || ( $year < -9998 ) ) return ''; // ISO range is limited to four digits
$year = number_format( $year, 0, '.', '' );
$time = str_replace( ':', '', $dv->getTimeString( false ) );
if ( ( $time == false ) && ( $isend ) ) { // increment by one day, compute date to cover leap years etc.
$dv = SMWDataValueFactory::newTypeIDValue( '_dat', $dv->getWikiValue() . 'T00:00:00-24:00' );
}
$month = $dv->getMonth();
if ( strlen( $month ) == 1 ) $month = '0' . $month;
$day = $dv->getDay();
if ( strlen( $day ) == 1 ) $day = '0' . $day;
$result = $year . $month . $day;
if ( $time != false ) $result .= "T$time";
return $result;
}
/**
* @see SMWResultPrinter::getParamDefinitions
*
* @since 1.8
*
* @param $definitions array of IParamDefinition
*
* @return array of IParamDefinition|array
*/
public function getParamDefinitions( array $definitions ) {
$params = parent::getParamDefinitions( $definitions );
$params['title'] = array(
'default' => '',
'message' => 'srf_paramdesc_icalendartitle',
);
$params['description'] = array(
'default' => '',
'message' => 'srf_paramdesc_icalendardescription',
);
return $params;
}
}
|