vCardFileExportPrinter::mapField()   F
last analyzed

Complexity

Conditions 53
Paths 54

Size

Total Lines 146

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 65
CRAP Score 291.7927

Importance

Changes 0
Metric Value
dl 0
loc 146
ccs 65
cts 116
cp 0.5603
rs 3.3333
c 0
b 0
f 0
cc 53
nc 54
nop 5
crap 291.7927

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	/**
121
	 * @param QueryResult $res
122 1
	 *
123 1
	 * @return string
124
	 * @throws \MWException
125 1
	 */
126 1
	private function getVCardContent( $res ) {
127
128 1
		$result = '';
129
		$vCards = [];
130 1
131 1
		$row = $res->getNext();
132
		$isPublic = $this->isPublic();
133
134
		while ( $row !== false ) {
135 1
			// Subject of the Result
136
			$subject = $row[0]->getResultSubject();
137
			$title = $subject->getTitle();
138 1
139 1
			// Specifies a value that represents a persistent, globally unique
140
			// identifier associated with the object.
141 1
			$uri = $title->getFullURL();
142 1
143
			// A timestamp for the last time the vCard was updated
144
			$timestamp = WikiPage::factory( $title )->getTimestamp();
145 1
			$text = $title->getText();
146 1
147
			$vCards[] = $this->newVCard( $row, $uri, $text, $timestamp, $isPublic );
148
			$row = $res->getNext();
149 1
		}
150
151
		foreach ( $vCards as $vCard ) {
152 1
			$result .= $vCard->text();
153
		}
154 1
155 1
		return $result;
156 1
	}
157
158
	private function newVCard( $row, $uri, $text, $timestamp, $isPublic ) {
159
160 1
		$vCard = new vCard(
161
			$uri,
162
			$text,
163
			[
164
165
				// something like 'Dr.'
166
				'prefix' => '',
167
168
				// given name
169
				'firstname' => '',
170
171
				// family name
172
				'lastname' => '',
173
174
				// typically the "middle" name (second first name)
175
				'additionalname' => '',
176
177
				// things like "jun." or "sen."
178
				'suffix' => '',
179
180
				// the "formatted name", may be independent from
181
				// first/lastname & co.
182
				'fullname' => '',
183
184
				'tel' => [],
185
				'address' => [],
186
				'email' => [],
187
				// a date
188
				'birthday' => '',
189
190
				// organisational details
191
				'organization' => '',
192
				'department' => '',
193
				'title' => '',
194
				'role' => '',
195
				'category' => '',
196
197
				 // homepage, a legal URL
198
				'url' => '',
199 1
200 1
				// any text
201
				'note' => ''
202 1
			]
203 1
		);
204
205 1
		$tels = [];
206 1
		$emails = [];
207
208
		$addresses['work'] = new Address( 'WORK' );
0 ignored issues
show
Coding Style Comprehensibility introduced by mwjames
$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...
209 1
		$addresses['home'] = new Address( 'HOME' );
210 1
211 1
		foreach ( $row as $field ) {
212
			$this->mapField( $field, $vCard, $tels, $addresses, $emails );
213 1
		}
214 1
215
		$vCard->set( 'tel', $tels );
216 1
		$vCard->set( 'address', $addresses );
217
		$vCard->set( 'email', $emails );
218
219 1
		$vCard->isPublic( $isPublic );
220
		$vCard->setTimestamp( $timestamp );
221 1
222
		return $vCard;
223 1
	}
224 1
225 1
	private function isPublic() {
226
		// heuristic for setting confidentiality level of vCard:
227 1
		global $wgGroupPermissions;
228
229
		if ( ( array_key_exists( '*', $wgGroupPermissions ) ) && ( array_key_exists(
230
				'read',
231
				$wgGroupPermissions['*']
232
			) ) ) {
233 1
			return $wgGroupPermissions['*']['read'];
234
		}
235 1
236
		return true;
237 1
	}
238 1
239 1
	private function mapField( $field, $vCard, &$tels, &$addresses, &$emails ) {
240 1
241 1
		$printRequest = $field->getPrintRequest();
242
243
		switch ( strtolower( $printRequest->getLabel() ) ) {
244 1
			case "name":
245
				$vCard->set( 'fullname', $this->getFieldValue( $field ) );
246
				break;
247 1
			case "prefix":
248
				$vCard->set( 'prefix', $this->getFieldCommaList( $field ) );
249
				break;
250
			case "suffix":
251 1
				$vCard->set( 'suffix', $this->getFieldCommaList( $field ) );
252 1
				break;
253
			case "firstname":
254
				// save only the first
255 1
				$vCard->set( 'firstname', $this->getFieldValue( $field ) );
256
				break;
257
			case "additionalname":
258
			case "extraname":
259 1
				$vCard->set( 'additionalname', $this->getFieldCommaList( $field ) );
260 1
				break;
261 1
			case "lastname":
262 1
				// save only the first
263 1
				$vCard->set( 'lastname', $this->getFieldValue( $field ) );
264 1
				break;
265 1
			case "note":
266 1
				$vCard->set( 'note', $this->getFieldCommaList( $field ) );
267
				break;
268 1
			case "category":
269 1
				$vCard->set( 'category', $this->getFieldCommaList( $field ) );
270
			case "email":
271
				while ( $value = $field->getNextDataValue() ) {
272
					$emails[] = new Email( 'INTERNET', $value->getShortWikiText() );
273
				}
274 1
				break;
275 1
			case "workphone":
276 1
				while ( $value = $field->getNextDataValue() ) {
277
					$tels[] = new Tel( 'WORK', $value->getShortWikiText() );
278 1
				}
279 1
				break;
280 1
			case "cellphone":
281 1
				while ( $value = $field->getNextDataValue() ) {
282
					$tels[] = new Tel( 'CELL', $value->getShortWikiText() );
283 1
				}
284 1
				break;
285
			case "homephone":
286
				while ( $value = $field->getNextDataValue() ) {
287 1
					$tels[] = new Tel( 'HOME', $value->getShortWikiText() );
288
				}
289
				break;
290
			case "organization":
291
				$vCard->set( 'organization', $this->getFieldValue( $field ) );
292 1
				break;
293
			case "workpostofficebox":
294
				if ( ( $workpostofficebox = $this->getFieldValue( $field ) ) !== '' ) {
295
					$addresses['work']->set( 'pobox', $workpostofficebox );
296
				}
297 1
				break;
298
			case "workextendedaddress":
299
				if ( ( $workextendedaddress = $this->getFieldValue( $field ) ) !== '' ) {
300
					$addresses['work']->set( 'ext', $workextendedaddress );
301
				}
302 1
				break;
303
			case "workstreet":
304
				if ( ( $workstreet = $this->getFieldValue( $field ) ) !== '' ) {
305
					$addresses['work']->set( 'street', $workstreet );
306
				}
307 1
				break;
308
			case "worklocality":
309
				if ( ( $worklocality = $this->getFieldValue( $field ) ) !== '' ) {
310
					$addresses['work']->set( 'locality', $worklocality );
311
				}
312 1
				break;
313
			case "workregion":
314
				if ( ( $workregion = $this->getFieldValue( $field ) ) !== '' ) {
315
					$addresses['work']->set( 'region', $workregion );
316
				}
317 1
				break;
318
			case "workpostalcode":
319
				if ( ( $workpostalcode = $this->getFieldValue( $field ) ) !== '' ) {
320
					$addresses['work']->set( 'code', $workpostalcode );
321
				}
322 1
				break;
323
			case "workcountry":
324
				if ( ( $workcountry = $this->getFieldValue( $field ) ) !== '' ) {
325
					$addresses['work']->set( 'country', $workcountry );
326
				}
327 1
				break;
328
			case "homepostofficebox":
329
				if ( ( $homepostofficebox = $this->getFieldValue( $field ) ) !== '' ) {
330
					$addresses['home']->set( 'pobox', $homepostofficebox );
331
				}
332 1
				break;
333 1
			case "homeextendedaddress":
334 1
				if ( ( $homeextendedaddress = $this->getFieldValue( $field ) ) !== '' ) {
335
					$addresses['home']->set( 'ext', $homeextendedaddress );
336 1
				}
337 1
				break;
338 1
			case "homestreet":
339 1
				if ( ( $homestreet = $this->getFieldValue( $field ) ) !== '' ) {
340
					$addresses['home']->set( 'street', $homestreet );
341 1
				}
342 1
				break;
343
			case "homelocality":
344
				if ( ( $homelocality = $this->getFieldValue( $field ) ) !== '' ) {
345
					$addresses['home']->set( 'locality', $homelocality );
346
				}
347 1
				break;
348
			case "homeregion":
349
				if ( ( $homeregion = $this->getFieldValue( $field ) ) !== '' ) {
350
					$addresses['home']->set( 'region', $homeregion );
351
				}
352 1
				break;
353
			case "homepostalcode":
354
				if ( ( $homepostalcode = $this->getFieldValue( $field ) ) !== '' ) {
355
					$addresses['home']->set( 'code', $homepostalcode );
356
				}
357 1
				break;
358 1
			case "homecountry":
359 1
				if ( ( $homecountry = $this->getFieldValue( $field ) ) !== '' ) {
360
					$addresses['home']->set( 'country', $homecountry );
361 1
				}
362 1
				break;
363 1
			case "birthday":
364
				if ( $printRequest->getTypeID() == TimeValue::TYPE_ID ) {
365
					$value = $field->getNextDataValue();
366 1
367 1
					if ( $value !== false ) {
368 1
						$birthday = $value->getISO8601Date();
369 1
						$vCard->set( 'birthday', $birthday );
370
					}
371 1
				}
372 1
				break;
373 1
			case "homepage":
374
				if ( $printRequest->getTypeID() == "_uri" ) {
375
					$value = $field->getNextDataValue();
376 1
377
					if ( $value !== false ) {
378 1
						$url = $value->getWikiValue();
379
						$vCard->set( 'url', $url );
380 1
					}
381
				}
382 1
				break;
383
		}
384 1
	}
385 1
386
	private function getFieldCommaList( $field ) {
387
388 1
		$list = '';
389
390
		while ( $value = $field->getNextDataValue() ) {
391 1
			$list .= ( $list ? ',' : '' ) . $value->getShortWikiText();
392
		}
393 1
394
		return $list;
395 1
	}
396 1
397
	private function getFieldValue( $field ) {
398
399 1
		$v = '';
400
401
		if ( ( $value = $field->getNextDataValue() ) !== false ) {
402
			$v = $value->getShortWikiText();
403
		}
404
405
		return $v;
406
	}
407
408
}
409