1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SRF\vCard; |
4
|
|
|
|
5
|
|
|
use SMWExportPrinter as FileExportPrinter; |
6
|
|
|
use SMWQuery as Query; |
7
|
|
|
use SMWQueryProcessor as QueryProcessor; |
8
|
|
|
use SMWQueryResult as QueryResult; |
9
|
|
|
use SMWTimeValue as TimeValue; |
10
|
|
|
use WikiPage; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Printer class for creating vCard exports |
14
|
|
|
* |
15
|
|
|
* @see http://www.semantic-mediawiki.org/wiki/vCard |
16
|
|
|
* @see https://tools.ietf.org/html/rfc6350 |
17
|
|
|
* @see https://www.w3.org/2002/12/cal/vcard-notes.html |
18
|
|
|
* |
19
|
|
|
* @license GNU GPL v2+ |
20
|
|
|
* @since 1.5 |
21
|
|
|
* |
22
|
|
|
* @author Markus Krötzsch |
23
|
|
|
* @author Denny Vrandecic |
24
|
|
|
* @author Frank Dengler |
25
|
|
|
* @author mwjames |
26
|
|
|
*/ |
27
|
|
|
class vCardFileExportPrinter extends FileExportPrinter { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @see FileExportPrinter::getName |
31
|
|
|
* |
32
|
|
|
* @since 1.8 |
33
|
|
|
* |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
|
|
public function getName() { |
37
|
|
|
return wfMessage( 'srf_printername_vcard' )->text(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @see FileExportPrinter::getMimeType |
42
|
|
|
* |
43
|
|
|
* @since 1.8 |
44
|
|
|
* |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
1 |
|
public function getMimeType( QueryResult $queryResult ) { |
48
|
1 |
|
return 'text/x-vcard'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @see FileExportPrinter::getFileName |
53
|
|
|
* |
54
|
|
|
* @since 1.8 |
55
|
|
|
* |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
4 |
|
public function getFileName( QueryResult $queryResult ) { |
59
|
|
|
|
60
|
4 |
|
if ( $this->params['filename'] !== '' ) { |
61
|
|
|
|
62
|
3 |
|
if ( strpos( $this->params['filename'], '.vcf' ) === false ) { |
63
|
1 |
|
$this->params['filename'] .= '.vcf'; |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
return str_replace( ' ', '_', $this->params['filename'] ); |
67
|
1 |
|
} elseif ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) { |
68
|
|
|
return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.vcf'; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return 'vCard.vcf'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see FileExportPrinter::getQueryMode |
76
|
|
|
* |
77
|
|
|
* @since 1.8 |
78
|
|
|
* |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
1 |
|
public function getQueryMode( $context ) { |
82
|
1 |
|
return ( $context == QueryProcessor::SPECIAL_PAGE ) ? Query::MODE_INSTANCES : Query::MODE_NONE; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @see ResultPrinter::getParamDefinitions |
87
|
|
|
* |
88
|
|
|
* @since 1.8 |
89
|
|
|
* |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function getParamDefinitions( array $definitions ) { |
93
|
1 |
|
$params = parent::getParamDefinitions( $definitions ); |
94
|
|
|
|
95
|
1 |
|
$params['filename'] = [ |
96
|
|
|
'message' => 'smw-paramdesc-filename', |
97
|
|
|
'default' => 'vCard.vcf', |
98
|
|
|
]; |
99
|
|
|
|
100
|
1 |
|
return $params; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @see ResultPrinter::getResultText |
105
|
|
|
*/ |
106
|
1 |
|
protected function getResultText( QueryResult $res, $outputMode ) { |
107
|
|
|
|
108
|
|
|
// Always return a link for when the output mode is not a file request, |
109
|
|
|
// a file request is normally only initiated when resolving the query |
110
|
|
|
// via Special:Ask |
111
|
1 |
|
if ( $outputMode !== SMW_OUTPUT_FILE ) { |
112
|
|
|
return $this->getVCardLink( $res, $outputMode ); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return $this->getVCardContent( $res ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function getVCardLink( QueryResult $res, $outputMode ) { |
119
|
|
|
|
120
|
|
|
// Can be viewed as HTML if requested, no more parsing needed |
121
|
|
|
$this->isHTML = $outputMode == SMW_OUTPUT_HTML; |
122
|
|
|
|
123
|
|
|
if ( $this->getSearchLabel( $outputMode ) ) { |
124
|
|
|
$label = $this->getSearchLabel( $outputMode ); |
125
|
|
|
} else { |
126
|
|
|
$label = wfMessage( 'srf_vcard_link' )->inContentLanguage()->text(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$link = $res->getQueryLink( $label ); |
130
|
|
|
$link->setParameter( 'vcard', 'format' ); |
131
|
|
|
|
132
|
|
|
if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) { |
133
|
|
|
$link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ( array_key_exists( 'limit', $this->params ) ) { |
137
|
|
|
$link->setParameter( $this->params['limit'], 'limit' ); |
138
|
|
|
} else { // use a reasonable default limit |
139
|
|
|
$link->setParameter( 20, 'limit' ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $link->getText( $outputMode, $this->mLinker ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param QueryResult $res |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
* @throws \MWException |
150
|
|
|
*/ |
151
|
1 |
|
private function getVCardContent( $res ) { |
152
|
|
|
|
153
|
1 |
|
$result = ''; |
154
|
1 |
|
$vCards = []; |
155
|
|
|
|
156
|
1 |
|
$row = $res->getNext(); |
157
|
1 |
|
$isPublic = $this->isPublic(); |
158
|
|
|
|
159
|
1 |
|
while ( $row !== false ) { |
160
|
|
|
// Subject of the Result |
161
|
1 |
|
$subject = $row[0]->getResultSubject(); |
162
|
1 |
|
$title = $subject->getTitle(); |
163
|
|
|
|
164
|
|
|
// Specifies a value that represents a persistent, globally unique |
165
|
|
|
// identifier associated with the object. |
166
|
1 |
|
$uri = $title->getFullURL(); |
167
|
|
|
|
168
|
|
|
// A timestamp for the last time the vCard was updated |
169
|
1 |
|
$timestamp = WikiPage::factory( $title )->getTimestamp(); |
170
|
1 |
|
$text = $title->getText(); |
171
|
|
|
|
172
|
1 |
|
$vCards[] = $this->newVCard( $row, $uri, $text, $timestamp, $isPublic ); |
173
|
1 |
|
$row = $res->getNext(); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
foreach ( $vCards as $vCard ) { |
177
|
1 |
|
$result .= $vCard->text(); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
return $result; |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
private function newVCard( $row, $uri, $text, $timestamp, $isPublic ) { |
184
|
|
|
|
185
|
1 |
|
$vCard = new vCard( |
186
|
1 |
|
$uri, |
187
|
1 |
|
$text, |
188
|
|
|
[ |
189
|
|
|
|
190
|
|
|
// something like 'Dr.' |
191
|
1 |
|
'prefix' => '', |
192
|
|
|
|
193
|
|
|
// given name |
194
|
|
|
'firstname' => '', |
195
|
|
|
|
196
|
|
|
// family name |
197
|
|
|
'lastname' => '', |
198
|
|
|
|
199
|
|
|
// typically the "middle" name (second first name) |
200
|
|
|
'additionalname' => '', |
201
|
|
|
|
202
|
|
|
// things like "jun." or "sen." |
203
|
|
|
'suffix' => '', |
204
|
|
|
|
205
|
|
|
// the "formatted name", may be independent from |
206
|
|
|
// first/lastname & co. |
207
|
|
|
'fullname' => '', |
208
|
|
|
|
209
|
|
|
'tel' => [], |
210
|
|
|
'address' => [], |
211
|
|
|
'email' => [], |
212
|
|
|
// a date |
213
|
|
|
'birthday' => '', |
214
|
|
|
|
215
|
|
|
// organisational details |
216
|
|
|
'organization' => '', |
217
|
|
|
'department' => '', |
218
|
|
|
'title' => '', |
219
|
|
|
'role' => '', |
220
|
|
|
'category' => '', |
221
|
|
|
|
222
|
|
|
// homepage, a legal URL |
223
|
|
|
'url' => '', |
224
|
|
|
|
225
|
|
|
// any text |
226
|
|
|
'note' => '' |
227
|
|
|
] |
228
|
|
|
); |
229
|
|
|
|
230
|
1 |
|
$tels = []; |
231
|
1 |
|
$emails = []; |
232
|
|
|
|
233
|
1 |
|
$addresses['work'] = new Address( 'WORK' ); |
|
|
|
|
234
|
1 |
|
$addresses['home'] = new Address( 'HOME' ); |
235
|
|
|
|
236
|
1 |
|
foreach ( $row as $field ) { |
237
|
1 |
|
$this->mapField( $field, $vCard, $tels, $addresses, $emails ); |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
$vCard->set( 'tel', $tels ); |
241
|
1 |
|
$vCard->set( 'address', $addresses ); |
242
|
1 |
|
$vCard->set( 'email', $emails ); |
243
|
|
|
|
244
|
1 |
|
$vCard->isPublic( $isPublic ); |
245
|
1 |
|
$vCard->setTimestamp( $timestamp ); |
246
|
|
|
|
247
|
1 |
|
return $vCard; |
248
|
|
|
} |
249
|
|
|
|
250
|
1 |
|
private function isPublic() { |
251
|
|
|
// heuristic for setting confidentiality level of vCard: |
252
|
1 |
|
global $wgGroupPermissions; |
253
|
|
|
|
254
|
1 |
|
if ( ( array_key_exists( '*', $wgGroupPermissions ) ) && ( array_key_exists( |
255
|
1 |
|
'read', |
256
|
1 |
|
$wgGroupPermissions['*'] |
257
|
|
|
) ) ) { |
258
|
1 |
|
return $wgGroupPermissions['*']['read']; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return true; |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
private function mapField( $field, $vCard, &$tels, &$addresses, &$emails ) { |
265
|
|
|
|
266
|
1 |
|
$printRequest = $field->getPrintRequest(); |
267
|
|
|
|
268
|
1 |
|
switch ( strtolower( $printRequest->getLabel() ) ) { |
269
|
1 |
|
case "name": |
270
|
1 |
|
$vCard->set( 'fullname', $this->getFieldValue( $field ) ); |
271
|
1 |
|
break; |
272
|
1 |
|
case "prefix": |
273
|
|
|
$vCard->set( 'prefix', $this->getFieldCommaList( $field ) ); |
274
|
|
|
break; |
275
|
1 |
|
case "suffix": |
276
|
|
|
$vCard->set( 'suffix', $this->getFieldCommaList( $field ) ); |
277
|
|
|
break; |
278
|
1 |
|
case "firstname": |
279
|
|
|
// save only the first |
280
|
|
|
$vCard->set( 'firstname', $this->getFieldValue( $field ) ); |
281
|
|
|
break; |
282
|
1 |
|
case "additionalname": |
283
|
1 |
|
case "extraname": |
284
|
|
|
$vCard->set( 'additionalname', $this->getFieldCommaList( $field ) ); |
285
|
|
|
break; |
286
|
1 |
|
case "lastname": |
287
|
|
|
// save only the first |
288
|
|
|
$vCard->set( 'lastname', $this->getFieldValue( $field ) ); |
289
|
|
|
break; |
290
|
1 |
|
case "note": |
291
|
1 |
|
$vCard->set( 'note', $this->getFieldCommaList( $field ) ); |
292
|
1 |
|
break; |
293
|
1 |
|
case "category": |
294
|
1 |
|
$vCard->set( 'category', $this->getFieldCommaList( $field ) ); |
295
|
1 |
|
case "email": |
296
|
1 |
|
while ( $value = $field->getNextDataValue() ) { |
297
|
1 |
|
$emails[] = new Email( 'INTERNET', $value->getShortWikiText() ); |
298
|
|
|
} |
299
|
1 |
|
break; |
300
|
1 |
|
case "workphone": |
301
|
|
|
while ( $value = $field->getNextDataValue() ) { |
302
|
|
|
$tels[] = new Tel( 'WORK', $value->getShortWikiText() ); |
303
|
|
|
} |
304
|
|
|
break; |
305
|
1 |
|
case "cellphone": |
306
|
1 |
|
while ( $value = $field->getNextDataValue() ) { |
307
|
1 |
|
$tels[] = new Tel( 'CELL', $value->getShortWikiText() ); |
308
|
|
|
} |
309
|
1 |
|
break; |
310
|
1 |
|
case "homephone": |
311
|
1 |
|
while ( $value = $field->getNextDataValue() ) { |
312
|
1 |
|
$tels[] = new Tel( 'HOME', $value->getShortWikiText() ); |
313
|
|
|
} |
314
|
1 |
|
break; |
315
|
1 |
|
case "organization": |
316
|
|
|
$vCard->set( 'organization', $this->getFieldValue( $field ) ); |
317
|
|
|
break; |
318
|
1 |
|
case "workpostofficebox": |
319
|
|
|
if ( ( $workpostofficebox = $this->getFieldValue( $field ) ) !== '' ) { |
320
|
|
|
$addresses['work']->set( 'pobox', $workpostofficebox ); |
321
|
|
|
} |
322
|
|
|
break; |
323
|
1 |
|
case "workextendedaddress": |
324
|
|
|
if ( ( $workextendedaddress = $this->getFieldValue( $field ) ) !== '' ) { |
325
|
|
|
$addresses['work']->set( 'ext', $workextendedaddress ); |
326
|
|
|
} |
327
|
|
|
break; |
328
|
1 |
|
case "workstreet": |
329
|
|
|
if ( ( $workstreet = $this->getFieldValue( $field ) ) !== '' ) { |
330
|
|
|
$addresses['work']->set( 'street', $workstreet ); |
331
|
|
|
} |
332
|
|
|
break; |
333
|
1 |
|
case "worklocality": |
334
|
|
|
if ( ( $worklocality = $this->getFieldValue( $field ) ) !== '' ) { |
335
|
|
|
$addresses['work']->set( 'locality', $worklocality ); |
336
|
|
|
} |
337
|
|
|
break; |
338
|
1 |
|
case "workregion": |
339
|
|
|
if ( ( $workregion = $this->getFieldValue( $field ) ) !== '' ) { |
340
|
|
|
$addresses['work']->set( 'region', $workregion ); |
341
|
|
|
} |
342
|
|
|
break; |
343
|
1 |
|
case "workpostalcode": |
344
|
|
|
if ( ( $workpostalcode = $this->getFieldValue( $field ) ) !== '' ) { |
345
|
|
|
$addresses['work']->set( 'code', $workpostalcode ); |
346
|
|
|
} |
347
|
|
|
break; |
348
|
1 |
|
case "workcountry": |
349
|
|
|
if ( ( $workcountry = $this->getFieldValue( $field ) ) !== '' ) { |
350
|
|
|
$addresses['work']->set( 'country', $workcountry ); |
351
|
|
|
} |
352
|
|
|
break; |
353
|
1 |
|
case "homepostofficebox": |
354
|
|
|
if ( ( $homepostofficebox = $this->getFieldValue( $field ) ) !== '' ) { |
355
|
|
|
$addresses['home']->set( 'pobox', $homepostofficebox ); |
356
|
|
|
} |
357
|
|
|
break; |
358
|
1 |
|
case "homeextendedaddress": |
359
|
|
|
if ( ( $homeextendedaddress = $this->getFieldValue( $field ) ) !== '' ) { |
360
|
|
|
$addresses['home']->set( 'ext', $homeextendedaddress ); |
361
|
|
|
} |
362
|
|
|
break; |
363
|
1 |
|
case "homestreet": |
364
|
1 |
|
if ( ( $homestreet = $this->getFieldValue( $field ) ) !== '' ) { |
365
|
1 |
|
$addresses['home']->set( 'street', $homestreet ); |
366
|
|
|
} |
367
|
1 |
|
break; |
368
|
1 |
|
case "homelocality": |
369
|
1 |
|
if ( ( $homelocality = $this->getFieldValue( $field ) ) !== '' ) { |
370
|
1 |
|
$addresses['home']->set( 'locality', $homelocality ); |
371
|
|
|
} |
372
|
1 |
|
break; |
373
|
1 |
|
case "homeregion": |
374
|
|
|
if ( ( $homeregion = $this->getFieldValue( $field ) ) !== '' ) { |
375
|
|
|
$addresses['home']->set( 'region', $homeregion ); |
376
|
|
|
} |
377
|
|
|
break; |
378
|
1 |
|
case "homepostalcode": |
379
|
|
|
if ( ( $homepostalcode = $this->getFieldValue( $field ) ) !== '' ) { |
380
|
|
|
$addresses['home']->set( 'code', $homepostalcode ); |
381
|
|
|
} |
382
|
|
|
break; |
383
|
1 |
|
case "homecountry": |
384
|
|
|
if ( ( $homecountry = $this->getFieldValue( $field ) ) !== '' ) { |
385
|
|
|
$addresses['home']->set( 'country', $homecountry ); |
386
|
|
|
} |
387
|
|
|
break; |
388
|
1 |
|
case "birthday": |
389
|
1 |
|
if ( $printRequest->getTypeID() == TimeValue::TYPE_ID ) { |
390
|
1 |
|
$value = $field->getNextDataValue(); |
391
|
|
|
|
392
|
1 |
|
if ( $value !== false ) { |
393
|
1 |
|
$birthday = $value->getISO8601Date(); |
394
|
1 |
|
$vCard->set( 'birthday', $birthday ); |
395
|
|
|
} |
396
|
|
|
} |
397
|
1 |
|
break; |
398
|
1 |
|
case "homepage": |
399
|
1 |
|
if ( $printRequest->getTypeID() == "_uri" ) { |
400
|
1 |
|
$value = $field->getNextDataValue(); |
401
|
|
|
|
402
|
1 |
|
if ( $value !== false ) { |
403
|
1 |
|
$url = $value->getWikiValue(); |
404
|
1 |
|
$vCard->set( 'url', $url ); |
405
|
|
|
} |
406
|
|
|
} |
407
|
1 |
|
break; |
408
|
|
|
} |
409
|
1 |
|
} |
410
|
|
|
|
411
|
1 |
|
private function getFieldCommaList( $field ) { |
412
|
|
|
|
413
|
1 |
|
$list = ''; |
414
|
|
|
|
415
|
1 |
|
while ( $value = $field->getNextDataValue() ) { |
416
|
1 |
|
$list .= ( $list ? ',' : '' ) . $value->getShortWikiText(); |
417
|
|
|
} |
418
|
|
|
|
419
|
1 |
|
return $list; |
420
|
|
|
} |
421
|
|
|
|
422
|
1 |
|
private function getFieldValue( $field ) { |
423
|
|
|
|
424
|
1 |
|
$v = ''; |
425
|
|
|
|
426
|
1 |
|
if ( ( $value = $field->getNextDataValue() ) !== false ) { |
427
|
1 |
|
$v = $value->getShortWikiText(); |
428
|
|
|
} |
429
|
|
|
|
430
|
1 |
|
return $v; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
} |
434
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.