Completed
Push — master ( ee88cb...822860 )
by mw
07:03
created

vCardFileExportPrinter::getQueryMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
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 vCardFileExportPrinter 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 1
	public function getQueryMode( $context ) {
74 1
		return ( $context == QueryProcessor::SPECIAL_PAGE ) ? Query::MODE_INSTANCES : Query::MODE_NONE;
75
	}
76
77
	/**
78
	 * @see ResultPrinter::getResultText
79
	 */
80 1
	protected function getResultText( QueryResult $res, $outputmode ) {
81
82 1
		$result = '';
83
84 1
		if ( $outputmode == SMW_OUTPUT_FILE ) { // make vCard file
85 1
			$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 );
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 1
		return $result;
114
	}
115
116 1
	private function getVCardContent( $res ) {
117
118 1
		$result = '';
119 1
		$vCards = [];
120
121 1
		$row = $res->getNext();
122 1
		$isPublic = $this->isPublic();
123
124 1
		while ( $row !== false ) {
125
			 // Subject of the Result
126 1
			$subject = $row[0]->getResultSubject();
127 1
			$title = $subject->getTitle();
128
129
			// Specifies a value that represents a persistent, globally unique
130
			// identifier associated with the object.
131 1
			$uri = $title->getFullURL();
132
133
			// A timestamp for the last time the vCard was updated
134 1
			$timestamp = WikiPage::factory( $title )->getTimestamp();
135 1
			$text = $title->getText();
136
137 1
			$vCards[] = $this->newVCard( $row, $uri, $text, $timestamp, $isPublic );
138 1
			$row = $res->getNext();
139
		}
140
141 1
		foreach ( $vCards as $vCard ) {
142 1
			$result .= $vCard->text();
143
		}
144
145 1
		return $result;
146
	}
147
148 1
	private function newVCard( $row, $uri, $text, $timestamp, $isPublic ) {
149
150
		// name
151 1
		$prefix = ''; // something like 'Dr.'
152 1
		$firstname = ''; // given name
153 1
		$additionalname = ''; // typically the "middle" name (second first name)
154 1
		$lastname = ''; // family name
155 1
		$suffix = ''; // things like "jun." or "sen."
156 1
		$fullname = ''; // the "formatted name", may be independent from first/lastname & co.
157
		// contacts
158 1
		$emails = [];
159 1
		$tels = [];
160 1
		$addresses = [];
161
		// organisational details:
162 1
		$organization = ''; // any string
163 1
		$jobtitle = '';
164 1
		$role = '';
165 1
		$department = '';
166
		// other stuff
167 1
		$category = '';
168 1
		$birthday = ''; // a date
169 1
		$url = ''; // homepage, a legal URL
170 1
		$note = ''; // any text
171 1
		$workaddress = false;
172 1
		$homeaddress = false;
173
174 1
		$workpostofficebox = '';
175 1
		$workextendedaddress = '';
176 1
		$workstreet = '';
177 1
		$worklocality = '';
178 1
		$workregion = '';
179 1
		$workpostalcode = '';
180 1
		$workcountry = '';
181
182 1
		$homepostofficebox = '';
183 1
		$homeextendedaddress = '';
184 1
		$homestreet = '';
185 1
		$homelocality = '';
186 1
		$homeregion = '';
187 1
		$homepostalcode = '';
188 1
		$homecountry = '';
189
190 1
		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 1
			$req = $field->getPrintRequest();
195
196 1
			switch( strtolower( $req->getLabel() ) ) {
197 1
				case "name":
198 1
					$fullname = $this->getFieldValue( $field );
199 1
				break;
200 1
				case "prefix":
201
					$prefix = $this->getFieldCommaList( $field );
202
				break;
203 1
				case "suffix":
204
					$suffix = $this->getFieldCommaList( $field );
205
				break;
206 1
				case "firstname":
207
					// save only the first
208
					$firstname = $this->getFieldValue( $field );
209
				break;
210 1
				case "extraname":
211
					$additionalname = $this->getFieldCommaList( $field );
212
				break;
213 1
				case "lastname":
214
					// save only the first
215
					$lastname = $this->getFieldValue( $field );
216
				break;
217 1
				case "note":
218 1
					$note = $this->getFieldCommaList( $field );
219 1
				break;
220 1
				case "category":
221 1
					$category = $this->getFieldCommaList( $field );
222 1
				case "email":
223 1
					while( $value = $field->getNextDataValue() ) {
224 1
						$emails[] = new Email( 'INTERNET', $value->getShortWikiText() );
225
					}
226 1
				break;
227 1
				case "workphone":
228
					while( $value = $field->getNextDataValue() ) {
229
						$tels[] = new Tel( 'WORK', $value->getShortWikiText() );
230
					}
231
				break;
232 1
				case "cellphone":
233 1
					while( $value = $field->getNextDataValue() ) {
234 1
						$tels[] = new Tel( 'CELL', $value->getShortWikiText() );
235
					}
236 1
				break;
237 1
				case "homephone":
238 1
					while( $value = $field->getNextDataValue() ) {
239 1
						$tels[] = new Tel( 'HOME', $value->getShortWikiText() );
240
					}
241 1
				break;
242 1
				case "organization":
243
					$organization = $this->getFieldValue( $field );
244
				break;
245 1
				case "workpostofficebox":
246
					if ( ( $workpostofficebox = $this->getFieldValue( $field ) ) !== '' ) {
247
						$workaddress = true;
248
					}
249
				break;
250 1
				case "workextendedaddress":
251
					if ( ( $workextendedaddress = $this->getFieldValue( $field ) ) !== '' ) {
252
						$workaddress = true;
253
					}
254
				break;
255 1
				case "workstreet":
256
					if ( ( $workstreet = $this->getFieldValue( $field ) ) !== '' ) {
257
						$workaddress = true;
258
					}
259
				break;
260 1
				case "worklocality":
261
					if ( ( $worklocality = $this->getFieldValue( $field ) ) !== '' ) {
262
						$workaddress = true;
263
					}
264
				break;
265 1
				case "workregion":
266
					if ( ( $workregion = $this->getFieldValue( $field ) ) !== '' ) {
267
						$workaddress = true;
268
					}
269
				break;
270 1
				case "workpostalcode":
271
					if ( ( $workpostalcode = $this->getFieldValue( $field ) ) !== '' ) {
272
						$workaddress = true;
273
					}
274
				break;
275 1
				case "workcountry":
276
					if ( ( $workcountry = $this->getFieldValue( $field ) ) !== '' ) {
277
						$workaddress = true;
278
					}
279
				break;
280 1
				case "homepostofficebox":
281
					if ( ( $homepostofficebox = $this->getFieldValue( $field ) ) !== '' ) {
282
						$homeaddress = true;
283
					}
284
				break;
285 1
				case "homeextendedaddress":
286
					if ( ( $homeextendedaddress = $this->getFieldValue( $field ) ) !== '' ) {
287
						$homeaddress = true;
288
					}
289
				break;
290 1
				case "homestreet":
291 1
					if ( ( $homestreet = $this->getFieldValue( $field ) ) !== '' ) {
292 1
						$homeaddress = true;
293
					}
294 1
				break;
295 1
				case "homelocality":
296 1
					if ( ( $homelocality = $this->getFieldValue( $field ) ) !== '' ) {
297 1
						$homeaddress = true;
298
					}
299 1
				break;
300 1
				case "homeregion":
301
					if ( ( $homeregion = $this->getFieldValue( $field ) ) !== '' ) {
302
						$homeaddress = true;
303
					}
304
				break;
305 1
				case "homepostalcode":
306
					if ( ( $homepostalcode = $this->getFieldValue( $field ) ) !== '' ) {
307
						$homeaddress = true;
308
					}
309
				break;
310 1
				case "homecountry":
311
					if ( ( $homecountry = $this->getFieldValue( $field ) ) !== '' ) {
312
						$homeaddress = true;
313
					}
314
				break;
315 1
				case "birthday":
316 1
					if ( $req->getTypeID() == TimeValue::TYPE_ID )  {
317 1
						$value = $field->getNextDataValue();
318 1
						if ( $value !== false ) {
319 1
							$birthday =  $value->getISO8601Date();
320
						}
321
					}
322 1
				break;
323 1
				case "homepage":
324 1
					if ( $req->getTypeID() == "_uri" )  {
325 1
						$value = $field->getNextDataValue();
326 1
						if ( $value !== false ) {
327 1
							$url = $value->getWikiValue();
328
						}
329
					}
330 1
				break;
331
			}
332
		}
333
334 1
		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 1
		if ( $homeaddress ) {
350 1
			$addresses[] = new Address (
351 1
				'HOME',
352
				[
353 1
					'pobox' => $homepostofficebox,
354 1
					'ext' => $homeextendedaddress,
355 1
					'street' => $homestreet,
356 1
					'locality' => $homelocality,
357 1
					'region' => $homeregion,
358 1
					'code' => $homepostalcode,
359 1
					'country' => $homecountry
360
				]
361
			);
362
		}
363
364 1
		$vCard = new vCard(
365 1
			$uri,
366 1
			$text,
367
			[
368 1
				'prefix' => $prefix,
369 1
				'firstname' => $firstname,
370 1
				'lastname' => $lastname,
371 1
				'additionalname' => $additionalname,
372 1
				'suffix' => $suffix,
373 1
				'fullname' => $fullname,
374 1
				'tel' => $tels,
375 1
				'address' => $addresses,
376 1
				'email' => $emails,
377 1
				'birthday' => $birthday,
378 1
				'title' => $jobtitle,
379 1
				'role' => $role,
380 1
				'organization' => $organization,
381 1
				'department' => $department,
382 1
				'category' => $category,
383 1
				'url' => $url,
384 1
				'note' => $note
385
			]
386
		);
387
388 1
		$vCard->isPublic( $isPublic );
389 1
		$vCard->setTimestamp( $timestamp );
390
391 1
		return $vCard;
392
	}
393
394 1
	private function getFieldCommaList( $field ) {
395
396 1
		$list = '';
397
398 1
		while( $value = $field->getNextDataValue() ) {
399 1
			$list .= ( $list ? ',': '' ) . $value->getShortWikiText();
400
		}
401
402 1
		return $list;
403
	}
404
405 1
	private function getFieldValue( $field ) {
406
407 1
		$v = '';
408
409 1
		if ( ( $value = $field->getNextDataValue() ) !== false ) {
410 1
			$v = $value->getShortWikiText();
411
		}
412
413 1
		return $v;
414
	}
415
416 1
	private function isPublic() {
417
		// heuristic for setting confidentiality level of vCard:
418 1
		global $wgGroupPermissions;
419
420 1
		if ( ( array_key_exists( '*', $wgGroupPermissions ) ) && ( array_key_exists( 'read', $wgGroupPermissions['*'] ) ) ) {
421 1
			return $wgGroupPermissions['*']['read'];
422
		}
423
424
		return true;
425
	}
426
427
}
428