Completed
Push — master ( 1650a0...29e43d )
by mw
06:30
created

vCardFileExportPrinter::getFileName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
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
	public function getMimeType( QueryResult $queryResult ) {
48
		return 'text/x-vcard';
49
	}
50
51
	/**
52
	 * @see FileExportPrinter::getFileName
53
	 *
54
	 * @since 1.8
55
	 *
56
	 * {@inheritDoc}
57
	 */
58 1
	public function getFileName( QueryResult $queryResult ) {
59
60 1
		if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
61 1
			return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.vcf';
62
		}
63
64
		return 'vCard.vcf';
65
	}
66
67
	/**
68
	 * @see FileExportPrinter::getQueryMode
69
	 *
70
	 * @since 1.8
71
	 *
72
	 * {@inheritDoc}
73
	 */
74 1
	public function getQueryMode( $context ) {
75 1
		return ( $context == QueryProcessor::SPECIAL_PAGE ) ? Query::MODE_INSTANCES : Query::MODE_NONE;
76
	}
77
78
	/**
79
	 * @see ResultPrinter::getResultText
80
	 */
81 1
	protected function getResultText( QueryResult $res, $outputMode ) {
82
83
		// Always return a link for when the output mode is not a file request,
84
		// a file request is normally only initiated when resolving the query
85
		// via Special:Ask
86 1
		if ( $outputMode !== SMW_OUTPUT_FILE ) {
87
			return $this->getVCardLink( $res, $outputMode );
88
		}
89
90 1
		return $this->getVCardContent( $res );
91
	}
92
93
	private function getVCardLink( QueryResult $res, $outputMode ) {
94
95
		// Can be viewed as HTML if requested, no more parsing needed
96
		$this->isHTML = $outputMode == SMW_OUTPUT_HTML;
97
98
		if ( $this->getSearchLabel( $outputMode ) ) {
99
			$label = $this->getSearchLabel( $outputMode );
100
		} else {
101
			$label = wfMessage( 'srf_vcard_link' )->inContentLanguage()->text();
102
		}
103
104
		$link = $res->getQueryLink( $label );
105
		$link->setParameter( 'vcard', 'format' );
106
107
		if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
108
			$link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
109
		}
110
111
		if ( array_key_exists( 'limit', $this->params ) ) {
112
			$link->setParameter( $this->params['limit'], 'limit' );
113
		} else { // use a reasonable default limit
114
			$link->setParameter( 20, 'limit' );
115
		}
116
117
		return $link->getText( $outputMode, $this->mLinker );
118
	}
119
120 1
	private function getVCardContent( $res ) {
121
122 1
		$result = '';
123 1
		$vCards = [];
124
125 1
		$row = $res->getNext();
126 1
		$isPublic = $this->isPublic();
127
128 1
		while ( $row !== false ) {
129
			// Subject of the Result
130 1
			$subject = $row[0]->getResultSubject();
131 1
			$title = $subject->getTitle();
132
133
			// Specifies a value that represents a persistent, globally unique
134
			// identifier associated with the object.
135 1
			$uri = $title->getFullURL();
136
137
			// A timestamp for the last time the vCard was updated
138 1
			$timestamp = WikiPage::factory( $title )->getTimestamp();
139 1
			$text = $title->getText();
140
141 1
			$vCards[] = $this->newVCard( $row, $uri, $text, $timestamp, $isPublic );
142 1
			$row = $res->getNext();
143
		}
144
145 1
		foreach ( $vCards as $vCard ) {
146 1
			$result .= $vCard->text();
147
		}
148
149 1
		return $result;
150
	}
151
152 1
	private function newVCard( $row, $uri, $text, $timestamp, $isPublic ) {
153
154 1
		$vCard = new vCard(
155 1
			$uri,
156 1
			$text,
157
			[
158
159
				// something like 'Dr.'
160 1
				'prefix' => '',
161
162
				// given name
163
				'firstname' => '',
164
165
				// family name
166
				'lastname' => '',
167
168
				// typically the "middle" name (second first name)
169
				'additionalname' => '',
170
171
				// things like "jun." or "sen."
172
				'suffix' => '',
173
174
				// the "formatted name", may be independent from
175
				// first/lastname & co.
176
				'fullname' => '',
177
178
				'tel' => [],
179
				'address' => [],
180
				'email' => [],
181
				// a date
182
				'birthday' => '',
183
184
				// organisational details
185
				'organization' => '',
186
				'department' => '',
187
				'title' => '',
188
				'role' => '',
189
				'category' => '',
190
191
				 // homepage, a legal URL
192
				'url' => '',
193
194
				// any text
195
				'note' => ''
196
			]
197
		);
198
199 1
		$tels = [];
200 1
		$emails = [];
201
202 1
		$addresses['work'] = new Address( 'WORK' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$addresses was never initialized. Although not strictly required by PHP, it is generally a good practice to add $addresses = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
203 1
		$addresses['home'] = new Address( 'HOME' );
204
205 1
		foreach ( $row as $field ) {
206 1
			$this->mapField( $field, $vCard, $tels, $addresses, $emails );
207
		}
208
209 1
		$vCard->set( 'tel', $tels );
210 1
		$vCard->set( 'address', $addresses );
211 1
		$vCard->set( 'email', $emails );
212
213 1
		$vCard->isPublic( $isPublic );
214 1
		$vCard->setTimestamp( $timestamp );
215
216 1
		return $vCard;
217
	}
218
219 1
	private function isPublic() {
220
		// heuristic for setting confidentiality level of vCard:
221 1
		global $wgGroupPermissions;
222
223 1
		if ( ( array_key_exists( '*', $wgGroupPermissions ) ) && ( array_key_exists(
224 1
				'read',
225 1
				$wgGroupPermissions['*']
226
			) ) ) {
227 1
			return $wgGroupPermissions['*']['read'];
228
		}
229
230
		return true;
231
	}
232
233 1
	private function mapField( $field, $vCard, &$tels, &$addresses, &$emails ) {
234
235 1
		$printRequest = $field->getPrintRequest();
236
237 1
		switch ( strtolower( $printRequest->getLabel() ) ) {
238 1
			case "name":
239 1
				$vCard->set( 'fullname', $this->getFieldValue( $field ) );
240 1
				break;
241 1
			case "prefix":
242
				$vCard->set( 'prefix', $this->getFieldCommaList( $field ) );
243
				break;
244 1
			case "suffix":
245
				$vCard->set( 'suffix', $this->getFieldCommaList( $field ) );
246
				break;
247 1
			case "firstname":
248
				// save only the first
249
				$vCard->set( 'firstname', $this->getFieldValue( $field ) );
250
				break;
251 1
			case "additionalname":
252 1
			case "extraname":
253
				$vCard->set( 'additionalname', $this->getFieldCommaList( $field ) );
254
				break;
255 1
			case "lastname":
256
				// save only the first
257
				$vCard->set( 'lastname', $this->getFieldValue( $field ) );
258
				break;
259 1
			case "note":
260 1
				$vCard->set( 'note', $this->getFieldCommaList( $field ) );
261 1
				break;
262 1
			case "category":
263 1
				$vCard->set( 'category', $this->getFieldCommaList( $field ) );
264 1
			case "email":
265 1
				while ( $value = $field->getNextDataValue() ) {
266 1
					$emails[] = new Email( 'INTERNET', $value->getShortWikiText() );
267
				}
268 1
				break;
269 1
			case "workphone":
270
				while ( $value = $field->getNextDataValue() ) {
271
					$tels[] = new Tel( 'WORK', $value->getShortWikiText() );
272
				}
273
				break;
274 1
			case "cellphone":
275 1
				while ( $value = $field->getNextDataValue() ) {
276 1
					$tels[] = new Tel( 'CELL', $value->getShortWikiText() );
277
				}
278 1
				break;
279 1
			case "homephone":
280 1
				while ( $value = $field->getNextDataValue() ) {
281 1
					$tels[] = new Tel( 'HOME', $value->getShortWikiText() );
282
				}
283 1
				break;
284 1
			case "organization":
285
				$vCard->set( 'organization', $this->getFieldValue( $field ) );
286
				break;
287 1
			case "workpostofficebox":
288
				if ( ( $workpostofficebox = $this->getFieldValue( $field ) ) !== '' ) {
289
					$addresses['work']->set( 'pobox', $workpostofficebox );
290
				}
291
				break;
292 1
			case "workextendedaddress":
293
				if ( ( $workextendedaddress = $this->getFieldValue( $field ) ) !== '' ) {
294
					$addresses['work']->set( 'ext', $workextendedaddress );
295
				}
296
				break;
297 1
			case "workstreet":
298
				if ( ( $workstreet = $this->getFieldValue( $field ) ) !== '' ) {
299
					$addresses['work']->set( 'street', $workstreet );
300
				}
301
				break;
302 1
			case "worklocality":
303
				if ( ( $worklocality = $this->getFieldValue( $field ) ) !== '' ) {
304
					$addresses['work']->set( 'locality', $worklocality );
305
				}
306
				break;
307 1
			case "workregion":
308
				if ( ( $workregion = $this->getFieldValue( $field ) ) !== '' ) {
309
					$addresses['work']->set( 'region', $workregion );
310
				}
311
				break;
312 1
			case "workpostalcode":
313
				if ( ( $workpostalcode = $this->getFieldValue( $field ) ) !== '' ) {
314
					$addresses['work']->set( 'code', $workpostalcode );
315
				}
316
				break;
317 1
			case "workcountry":
318
				if ( ( $workcountry = $this->getFieldValue( $field ) ) !== '' ) {
319
					$addresses['work']->set( 'country', $workcountry );
320
				}
321
				break;
322 1
			case "homepostofficebox":
323
				if ( ( $homepostofficebox = $this->getFieldValue( $field ) ) !== '' ) {
324
					$addresses['home']->set( 'pobox', $homepostofficebox );
325
				}
326
				break;
327 1
			case "homeextendedaddress":
328
				if ( ( $homeextendedaddress = $this->getFieldValue( $field ) ) !== '' ) {
329
					$addresses['home']->set( 'ext', $homeextendedaddress );
330
				}
331
				break;
332 1
			case "homestreet":
333 1
				if ( ( $homestreet = $this->getFieldValue( $field ) ) !== '' ) {
334 1
					$addresses['home']->set( 'street', $homestreet );
335
				}
336 1
				break;
337 1
			case "homelocality":
338 1
				if ( ( $homelocality = $this->getFieldValue( $field ) ) !== '' ) {
339 1
					$addresses['home']->set( 'locality', $homelocality );
340
				}
341 1
				break;
342 1
			case "homeregion":
343
				if ( ( $homeregion = $this->getFieldValue( $field ) ) !== '' ) {
344
					$addresses['home']->set( 'region', $homeregion );
345
				}
346
				break;
347 1
			case "homepostalcode":
348
				if ( ( $homepostalcode = $this->getFieldValue( $field ) ) !== '' ) {
349
					$addresses['home']->set( 'code', $homepostalcode );
350
				}
351
				break;
352 1
			case "homecountry":
353
				if ( ( $homecountry = $this->getFieldValue( $field ) ) !== '' ) {
354
					$addresses['home']->set( 'country', $homecountry );
355
				}
356
				break;
357 1
			case "birthday":
358 1
				if ( $printRequest->getTypeID() == TimeValue::TYPE_ID ) {
359 1
					$value = $field->getNextDataValue();
360
361 1
					if ( $value !== false ) {
362 1
						$birthday = $value->getISO8601Date();
363 1
						$vCard->set( 'birthday', $birthday );
364
					}
365
				}
366 1
				break;
367 1
			case "homepage":
368 1
				if ( $printRequest->getTypeID() == "_uri" ) {
369 1
					$value = $field->getNextDataValue();
370
371 1
					if ( $value !== false ) {
372 1
						$url = $value->getWikiValue();
373 1
						$vCard->set( 'url', $url );
374
					}
375
				}
376 1
				break;
377
		}
378 1
	}
379
380 1
	private function getFieldCommaList( $field ) {
381
382 1
		$list = '';
383
384 1
		while ( $value = $field->getNextDataValue() ) {
385 1
			$list .= ( $list ? ',' : '' ) . $value->getShortWikiText();
386
		}
387
388 1
		return $list;
389
	}
390
391 1
	private function getFieldValue( $field ) {
392
393 1
		$v = '';
394
395 1
		if ( ( $value = $field->getNextDataValue() ) !== false ) {
396 1
			$v = $value->getShortWikiText();
397
		}
398
399 1
		return $v;
400
	}
401
402
}
403