Completed
Push — master ( 311968...fbc650 )
by mw
18:59
created

vCardResultPrinter::getFieldValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace SRF\vCard;
4
5
use SMWQueryResult  as QueryResult;
6
use SMWQueryProcessor as QueryProcessor;
7
use SMWQuery as Query;
8
use SMWExportPrinter as FileExportPrinter;
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
 */
26
class vCardResultPrinter extends FileExportPrinter {
27
28
	/**
29
	 * @see FileExportPrinter::getName
30
	 *
31
	 * @since 1.8
32
	 *
33
	 * {@inheritDoc}
34
	 */
35
	public function getName() {
36
		return wfMessage( 'srf_printername_vcard' )->text();
37
	}
38
39
	/**
40
	 * @see FileExportPrinter::getMimeType
41
	 *
42
	 * @since 1.8
43
	 *
44
	 * {@inheritDoc}
45
	 */
46
	public function getMimeType( QueryResult $queryResult ) {
47
		return 'text/x-vcard';
48
	}
49
50
	/**
51
	 * @see FileExportPrinter::getFileName
52
	 *
53
	 * @since 1.8
54
	 *
55
	 * {@inheritDoc}
56
	 */
57
	public function getFileName( QueryResult $queryResult ) {
58
59
		if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
60
			return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.vcf';
61
		}
62
63
		return 'vCard.vcf';
64
	}
65
66
	/**
67
	 * @see FileExportPrinter::getQueryMode
68
	 *
69
	 * @since 1.8
70
	 *
71
	 * {@inheritDoc}
72
	 */
73
	public function getQueryMode( $context ) {
74
		return ( $context == QueryProcessor::SPECIAL_PAGE ) ? Query::MODE_INSTANCES : Query::MODE_NONE;
75
	}
76
77
	/**
78
	 * @see ResultPrinter::getResultText
79
	 */
80
	protected function getResultText( QueryResult $res, $outputmode ) {
81
82
		$result = '';
83
84
		if ( $outputmode == SMW_OUTPUT_FILE ) { // make vCard file
85
			$result = $this->getVCardContent( $res );
86
		} else { // just make link to vcard
87
88
			if ( $this->getSearchLabel( $outputmode ) ) {
89
				$label = $this->getSearchLabel( $outputmode );
90
			} else {
91
				$label = wfMessage( 'srf_vcard_link' )->inContentLanguage()->text();
92
			}
93
94
			$link = $res->getQueryLink( $label );
0 ignored issues
show
Deprecated Code introduced by
The method SMWQueryResult::getQueryLink() has been deprecated with message: since SMW 1.8

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
95
			$link->setParameter( 'vcard', 'format' );
96
97
			if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
98
				$link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
99
			}
100
101
			if ( array_key_exists( 'limit', $this->m_params ) ) {
102
				$link->setParameter( $this->m_params['limit'], 'limit' );
103
			} else { // use a reasonable default limit
104
				$link->setParameter( 20, 'limit' );
105
			}
106
107
			$result .= $link->getText( $outputmode, $this->mLinker );
108
109
			// yes, our code can be viewed as HTML if requested, no more parsing needed
110
			$this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
111
		}
112
113
		return $result;
114
	}
115
116
	private function getVCardContent( $res ) {
117
118
		$result = '';
119
		$vCards = [];
120
121
		$row = $res->getNext();
122
		$isPublic = $this->isPublic();
123
124
		while ( $row !== false ) {
125
			 // Subject of the Result
126
			$subject = $row[0]->getResultSubject();
127
			$title = $subject->getTitle();
128
129
			// Specifies a value that represents a persistent, globally unique
130
			// identifier associated with the object.
131
			$uri = $title->getFullURL();
132
133
			// A timestamp for the last time the vCard was updated
134
			$timestamp = WikiPage::factory( $title )->getTimestamp();
135
			$text = $title->getText();
136
137
			$vCards[] = $this->newVCard( $row, $uri, $text, $timestamp, $isPublic );
138
			$row = $res->getNext();
139
		}
140
141
		foreach ( $vCards as $vCard ) {
142
			$result .= $vCard->text();
143
		}
144
145
		return $result;
146
	}
147
148
	private function newVCard( $row, $uri, $text, $timestamp, $isPublic ) {
149
150
		// name
151
		$prefix = ''; // something like 'Dr.'
152
		$firstname = ''; // given name
153
		$additionalname = ''; // typically the "middle" name (second first name)
154
		$lastname = ''; // family name
155
		$suffix = ''; // things like "jun." or "sen."
156
		$fullname = ''; // the "formatted name", may be independent from first/lastname & co.
157
		// contacts
158
		$emails = [];
159
		$tels = [];
160
		$addresses = [];
161
		// organisational details:
162
		$organization = ''; // any string
163
		$jobtitle = '';
164
		$role = '';
165
		$department = '';
166
		// other stuff
167
		$category = '';
168
		$birthday = ''; // a date
169
		$url = ''; // homepage, a legal URL
170
		$note = ''; // any text
171
		$workaddress = false;
172
		$homeaddress = false;
173
174
		$workpostofficebox = '';
175
		$workextendedaddress = '';
176
		$workstreet = '';
177
		$worklocality = '';
178
		$workregion = '';
179
		$workpostalcode = '';
180
		$workcountry = '';
181
182
		$homepostofficebox = '';
183
		$homeextendedaddress = '';
184
		$homestreet = '';
185
		$homelocality = '';
186
		$homeregion = '';
187
		$homepostalcode = '';
188
		$homecountry = '';
189
190
		foreach ( $row as $field ) {
191
			// later we may add more things like a generic
192
			// mechanism to add non-standard vCard properties as well
193
			// (could include funny things like geo, description etc.)
194
			$req = $field->getPrintRequest();
195
196
			switch( strtolower( $req->getLabel() ) ) {
197
				case "name":
198
					$fullname = $this->getFieldValue( $field );
199
				break;
200
				case "prefix":
201
					$prefix = $this->getFieldCommaList( $field );
202
				break;
203
				case "suffix":
204
					$suffix = $this->getFieldCommaList( $field );
205
				break;
206
				case "firstname":
207
					// save only the first
208
					$firstname = $this->getFieldValue( $field );
209
				break;
210
				case "extraname":
211
					$additionalname = $this->getFieldCommaList( $field );
212
				break;
213
				case "lastname":
214
					// save only the first
215
					$lastname = $this->getFieldValue( $field );
216
				break;
217
				case "note":
218
					$note = $this->getFieldCommaList( $field );
219
				break;
220
				case "category":
221
					$category = $this->getFieldCommaList( $field );
222
				case "email":
223
					while( $value = $field->getNextDataValue() ) {
224
						$emails[] = new Email( 'INTERNET', $value->getShortWikiText() );
225
					}
226
				break;
227
				case "workphone":
228
					while( $value = $field->getNextDataValue() ) {
229
						$tels[] = new Tel( 'WORK', $value->getShortWikiText() );
230
					}
231
				break;
232
				case "cellphone":
233
					while( $value = $field->getNextDataValue() ) {
234
						$tels[] = new Tel( 'CELL', $value->getShortWikiText() );
235
					}
236
				break;
237
				case "homephone":
238
					while( $value = $field->getNextDataValue() ) {
239
						$tels[] = new Tel( 'HOME', $value->getShortWikiText() );
240
					}
241
				break;
242
				case "organization":
243
					$organization = $this->getFieldValue( $field );
244
				break;
245
				case "workpostofficebox":
246
					if ( ( $workpostofficebox = $this->getFieldValue( $field ) ) !== '' ) {
247
						$workaddress = true;
248
					}
249
				break;
250
				case "workextendedaddress":
251
					if ( ( $workextendedaddress = $this->getFieldValue( $field ) ) !== '' ) {
252
						$workaddress = true;
253
					}
254
				break;
255
				case "workstreet":
256
					if ( ( $workstreet = $this->getFieldValue( $field ) ) !== '' ) {
257
						$workaddress = true;
258
					}
259
				break;
260
				case "worklocality":
261
					if ( ( $worklocality = $this->getFieldValue( $field ) ) !== '' ) {
262
						$workaddress = true;
263
					}
264
				break;
265
				case "workregion":
266
					if ( ( $workregion = $this->getFieldValue( $field ) ) !== '' ) {
267
						$workaddress = true;
268
					}
269
				break;
270
				case "workpostalcode":
271
					if ( ( $workpostalcode = $this->getFieldValue( $field ) ) !== '' ) {
272
						$workaddress = true;
273
					}
274
				break;
275
				case "workcountry":
276
					if ( ( $workcountry = $this->getFieldValue( $field ) ) !== '' ) {
277
						$workaddress = true;
278
					}
279
				break;
280
				case "homepostofficebox":
281
					if ( ( $homepostofficebox = $this->getFieldValue( $field ) ) !== '' ) {
282
						$homeaddress = true;
283
					}
284
				break;
285
				case "homeextendedaddress":
286
					if ( ( $homeextendedaddress = $this->getFieldValue( $field ) ) !== '' ) {
287
						$homeaddress = true;
288
					}
289
				break;
290
				case "homestreet":
291
					if ( ( $homestreet = $this->getFieldValue( $field ) ) !== '' ) {
292
						$homeaddress = true;
293
					}
294
				break;
295
				case "homelocality":
296
					if ( ( $homelocality = $this->getFieldValue( $field ) ) !== '' ) {
297
						$homeaddress = true;
298
					}
299
				break;
300
				case "homeregion":
301
					if ( ( $homeregion = $this->getFieldValue( $field ) ) !== '' ) {
302
						$homeaddress = true;
303
					}
304
				break;
305
				case "homepostalcode":
306
					if ( ( $homepostalcode = $this->getFieldValue( $field ) ) !== '' ) {
307
						$homeaddress = true;
308
					}
309
				break;
310
				case "homecountry":
311
					if ( ( $homecountry = $this->getFieldValue( $field ) ) !== '' ) {
312
						$homeaddress = true;
313
					}
314
				break;
315
				case "birthday":
316
					if ( $req->getTypeID() == TimeValue::TYPE_ID )  {
317
						$value = $field->getNextDataValue();
318
						if ( $value !== false ) {
319
							$birthday =  $value->getISO8601Date();
320
						}
321
					}
322
				break;
323
				case "homepage":
324
					if ( $req->getTypeID() == "_uri" )  {
325
						$value = $field->getNextDataValue();
326
						if ( $value !== false ) {
327
							$url = $value->getWikiValue();
328
						}
329
					}
330
				break;
331
			}
332
		}
333
334
		if ( $workaddress ) {
335
			$addresses[] = new Address (
336
				'WORK',
337
				[
338
					'pobox' => $workpostofficebox,
339
					'ext' => $workextendedaddress,
340
					'street' => $workstreet,
341
					'locality' => $worklocality,
342
					'region' => $workregion,
343
					'code' => $workpostalcode,
344
					'country' => $workcountry
345
				]
346
			);
347
		}
348
349
		if ( $homeaddress ) {
350
			$addresses[] = new Address (
351
				'HOME',
352
				[
353
					'pobox' => $homepostofficebox,
354
					'ext' => $homeextendedaddress,
355
					'street' => $homestreet,
356
					'locality' => $homelocality,
357
					'region' => $homeregion,
358
					'code' => $homepostalcode,
359
					'country' => $homecountry
360
				]
361
			);
362
		}
363
364
		$vCard = new vCard(
365
			$uri,
366
			$text,
367
			[
368
				'prefix' => $prefix,
369
				'firstname' => $firstname,
370
				'lastname' => $lastname,
371
				'additionalname' => $additionalname,
372
				'suffix' => $suffix,
373
				'fullname' => $fullname,
374
				'tel' => $tels,
375
				'address' => $addresses,
376
				'email' => $emails,
377
				'birthday' => $birthday,
378
				'title' => $jobtitle,
379
				'role' => $role,
380
				'organization' => $organization,
381
				'department' => $department,
382
				'category' => $category,
383
				'url' => $url,
384
				'note' => $note
385
			]
386
		);
387
388
		$vCard->isPublic( $isPublic );
389
		$vCard->setTimestamp( $timestamp );
390
391
		return $vCard;
392
	}
393
394
	private function getFieldCommaList( $field ) {
395
396
		$list = '';
397
398
		while( $value = $field->getNextDataValue() ) {
399
			$list .= ( $list ? ',': '' ) . $value->getShortWikiText();
400
		}
401
402
		return $list;
403
	}
404
405
	private function getFieldValue( $field ) {
406
407
		$v = '';
408
409
		if ( ( $value = $field->getNextDataValue() ) !== false ) {
410
			$v = $value->getShortWikiText();
411
		}
412
413
		return $v;
414
	}
415
416
	private function isPublic() {
417
		// heuristic for setting confidentiality level of vCard:
418
		global $wgGroupPermissions;
419
420
		if ( ( array_key_exists( '*', $wgGroupPermissions ) ) && ( array_key_exists( 'read', $wgGroupPermissions['*'] ) ) ) {
421
			return $wgGroupPermissions['*']['read'];
422
		}
423
424
		return true;
425
	}
426
427
}
428