Completed
Push — master ( d3d832...67979d )
by mw
57:02 queued 55:38
created

SMWBibTeXEntry::__construct()   F

Complexity

Conditions 34
Paths > 20000

Size

Total Lines 115

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 56
CRAP Score 48.6038

Importance

Changes 0
Metric Value
dl 0
loc 115
ccs 56
cts 73
cp 0.7671
rs 0
c 0
b 0
f 0
cc 34
nc 1073741824
nop 27
crap 48.6038

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Printer class for creating BibTeX exports
5
 *
6
 * For details on availble keys see the README
7
 *
8
 * Example of a book :
9
 *
10
 * @Book{abramowitz1964homf,
11
 *   author =     "Milton Abramowitz and Irene A. Stegun",
12
 *   title =     "Handbook of Mathematical Functions",
13
 *   publisher =     "Dover",
14
 *   year =     1964,
15
 *   address =     "New York",
16
 *   edition =     "ninth Dover printing, tenth GPO printing"
17
 * }
18
 *
19
 * @file
20
 * @ingroup SemanticResultFormats
21
 *
22
 * @author Markus Krötzsch
23
 * @author Denny Vrandecic
24
 * @author Frank Dengler
25
 * @author Steren Giannini
26
 * @ingroup SemanticResultFormats
27
 */
28
class SRFBibTeX extends SMWExportPrinter {
29
30
	protected $m_title = '';
31
	protected $m_description = '';
32
33
	/**
34
	 * @see SMWIExportPrinter::getMimeType
35
	 *
36
	 * @since 1.8
37
	 *
38
	 * @param SMWQueryResult $queryResult
39
	 *
40
	 * @return string
41
	 */
42
	public function getMimeType( SMWQueryResult $queryResult ) {
43
		return 'text/bibtex';
44
	}
45
46
	/**
47
	 * @see SMWIExportPrinter::getFileName
48
	 *
49
	 * @since 1.8
50
	 *
51
	 * @param SMWQueryResult $queryResult
52
	 *
53
	 * @return string|boolean
54
	 */
55 1
	public function getFileName( SMWQueryResult $queryResult ) {
56 1
		if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
57 1
			return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.bib';
58
		} else {
59
			return 'BibTeX.bib';
60
		}
61
	}
62
63 1
	public function getQueryMode( $context ) {
64 1
		return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
65
	}
66
67
	public function getName() {
68
		return wfMessage( 'srf_printername_bibtex' )->text();
69
	}
70
71 1
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
72 1
		global $wgSitename;
73 1
		$result = '';
74
75 1
		if ( $outputmode == SMW_OUTPUT_FILE ) { // make file
76 1
			if ( $this->m_title == '' ) {
77 1
				$this->m_title = $wgSitename;
78
			}
79
80 1
			$items = [];
81
82 1
			while ( $row = $res->getNext() ) {
83 1
				$items[] = $this->getItemForResultRow( $row )->text();
84
			}
85
86 1
			$result = implode( '', $items );
87
		} else { // just make link to export
88
			if ( $this->getSearchLabel( $outputmode ) ) {
89
				$label = $this->getSearchLabel( $outputmode );
90
			} else {
91
				$label = wfMessage( 'srf_bibtex_link' )->inContentLanguage()->text();
92
			}
93
94
			$link = $res->getQueryLink( $label );
95
			$link->setParameter( 'bibtex', 'format' );
96
97
			if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
98
				$link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
99
			}
100
101
			$result .= $link->getText( $outputmode, $this->mLinker );
102
			$this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed
103
		}
104
105 1
		return $result;
106
	}
107
108
	/**
109
	 * Gets a SMWBibTeXEntry for the row.
110
	 *
111
	 * @since 1.6
112
	 *
113
	 * @param $row array of SMWResultArray
114
	 *
115
	 * @return SMWBibTeXEntry
116
	 */
117 1
	protected function getItemForResultRow( array /* of SMWResultArray */
118
	$row ) {
119 1
		$address = '';
120 1
		$annote = '';
121 1
		$author = '';
122 1
		$booktitle = '';
123 1
		$chapter = '';
124 1
		$crossref = '';
125 1
		$doi = '';
126 1
		$edition = '';
127 1
		$editor = '';
128 1
		$eprint = '';
129 1
		$howpublished = '';
130 1
		$institution = '';
131 1
		$journal = '';
132 1
		$key = '';
133 1
		$month = '';
134 1
		$note = '';
135 1
		$number = '';
136 1
		$organization = '';
137 1
		$pages = '';
138 1
		$publisher = '';
139 1
		$school = '';
140 1
		$series = '';
141 1
		$title = '';
142 1
		$type = '';
143 1
		$url = '';
144 1
		$volume = '';
145 1
		$year = '';
146
147 1
		foreach ( $row as /* SMWResultArray */
148
				  $field ) {
149 1
			$req = $field->getPrintRequest();
150 1
			$label = strtolower( $req->getLabel() );
151 1
			$var = false;
152
153
			switch ( $label ) {
154 1
				case 'type':
155 1
					$var =& $type;
156 1
					break;
157 1
				case 'address':
158 1
					$var =& $address;
159 1
					break;
160 1
				case 'annote':
161
					$var =& $annote;
162
					break;
163 1
				case 'booktitle':
164 1
					$var =& $booktitle;
165 1
					break;
166 1
				case 'chapter':
167
					$var =& $chapter;
168
					break;
169 1
				case 'crossref':
170
					$var =& $crossref;
171
					break;
172 1
				case 'doi':
173
					$var =& $doi;
174
					break;
175 1
				case 'edition':
176 1
					$var =& $edition;
177 1
					break;
178 1
				case 'eprint':
179
					$var =& $eprint;
180
					break;
181 1
				case 'howpublished':
182
					$var =& $howpublished;
183
					break;
184 1
				case 'institution':
185
					$var =& $institution;
186
					break;
187 1
				case 'journal':
188
					$var =& $journal;
189
					break;
190 1
				case 'key':
191
					$var =& $key;
192
					break;
193 1
				case 'note':
194
					$var =& $note;
195
					break;
196 1
				case 'number':
197
					$var =& $number;
198
					break;
199 1
				case 'organization':
200
					$var =& $organization;
201
					break;
202 1
				case 'pages':
203 1
					$var =& $pages;
204 1
					break;
205 1
				case 'publisher':
206 1
					$var =& $publisher;
207 1
					break;
208 1
				case 'school':
209
					$var =& $school;
210
					break;
211 1
				case 'series':
212
					$var =& $series;
213
					break;
214 1
				case 'title':
215 1
					$var =& $title;
216 1
					break;
217 1
				case 'url':
218
					$var =& $url;
219
					break;
220 1
				case 'year':
221 1
					$var =& $year;
222 1
					break;
223 1
				case 'month':
224
					$var =& $month;
225
					break;
226 1
				case 'volume':
227 1
				case 'journal_volume':
228
					$var =& $volume;
229
					break;
230
			}
231
232 1
			if ( $var !== false ) {
233 1
				$dataValue = $field->getNextDataValue();
234
235 1
				if ( $dataValue !== false ) {
236 1
					$var = $dataValue->getShortWikiText();
237
				}
238
239 1
				unset( $var );
240
			} else {
241
				switch ( $label ) {
242 1
					case 'author':
243 1
					case 'authors':
244 1
					case 'editor' :
245 1
					case 'editors':
246 1
						$wikiTexts = [];
247
						while ( ( /* SMWDataValue */
248 1
							$dataValue = $field->getNextDataValue() ) !== false ) {
249 1
							$wikiTexts[] = $dataValue->getShortWikiText();
250
						}
251 1
						$wikiText = $GLOBALS['wgLang']->listToText( $wikiTexts );
252
253 1
						if ( $label == 'author' || $label == 'authors' ) {
254 1
							$author = $wikiText;
255
						} else {
256 1
							$editor = $wikiText;
257
						}
258 1
						break;
259 1
					case 'date':
260
						$dataValue = $field->getNextDataValue();
261
262
						if ( $dataValue !== false && get_class( $dataValue ) == 'SMWTimeValue' ) {
263
							$year = $dataValue->getYear();
264
							$month = $dataValue->getMonth();
265
						}
266 1
						break;
267
				}
268
			}
269
		}
270
271 1
		return new SMWBibTeXEntry(
272 1
			$type,
273 1
			$address,
274 1
			$annote,
275 1
			$author,
276 1
			$booktitle,
277 1
			$chapter,
278 1
			$crossref,
279 1
			$doi,
280 1
			$edition,
281 1
			$editor,
282 1
			$eprint,
283 1
			$howpublished,
284 1
			$institution,
285 1
			$journal,
286 1
			$key,
287 1
			$month,
288 1
			$note,
289 1
			$number,
290 1
			$organization,
291 1
			$pages,
292 1
			$publisher,
293 1
			$school,
294 1
			$series,
295 1
			$title,
296 1
			$url,
297 1
			$volume,
298 1
			$year
299
		);
300
	}
301
}
302
303
/**
304
 * Represents a single entry in an BibTeX
305
 *
306
 * @ingroup SMWQuery
307
 */
308
class SMWBibTeXEntry {
309
310
	private $bibTeXtype;
311
	private $URI;
312
	private $fields = [];
313
314 1
	public function __construct( $type, $address, $annote, $author, $booktitle, $chapter, $crossref, $doi, $edition, $editor, $eprint, $howpublished, $institution, $journal, $key, $month, $note, $number, $organization, $pages, $publisher, $school, $series, $title, $url, $volume, $year ) {
315 1
		if ( $type ) {
316 1
			$this->bibTeXtype = ucfirst( $type );
317
		} else {
318 1
			$this->bibTeXtype = 'Book';
319
		}
320
321 1
		$fields = [];
322
323 1
		if ( $address ) {
324 1
			$fields['address'] = $address;
325
		}
326 1
		if ( $annote ) {
327
			$fields['annote'] = $annote;
328
		}
329 1
		if ( $author ) {
330 1
			$fields['author'] = $author;
331
		}
332 1
		if ( $booktitle ) {
333 1
			$fields['booktitle'] = $booktitle;
334
		}
335 1
		if ( $chapter ) {
336
			$fields['chapter'] = $chapter;
337
		}
338 1
		if ( $crossref ) {
339
			$fields['crossref'] = $crossref;
340
		}
341 1
		if ( $doi ) {
342
			$fields['doi'] = $doi;
343
		}
344 1
		if ( $edition ) {
345 1
			$fields['edition'] = $edition;
346
		}
347 1
		if ( $editor ) {
348 1
			$fields['editor'] = $editor;
349
		}
350 1
		if ( $eprint ) {
351
			$fields['eprint'] = $eprint;
352
		}
353 1
		if ( $howpublished ) {
354
			$fields['howpublished'] = $howpublished;
355
		}
356 1
		if ( $institution ) {
357
			$fields['institution'] = $institution;
358
		}
359 1
		if ( $journal ) {
360
			$fields['journal'] = $journal;
361
		}
362 1
		if ( $key ) {
363
			$fields['key'] = $key;
364
		}
365 1
		if ( $month ) {
366
			$fields['month'] = $month;
367
		}
368 1
		if ( $note ) {
369
			$fields['note'] = $note;
370
		}
371 1
		if ( $number ) {
372
			$fields['number'] = $number;
373
		}
374 1
		if ( $organization ) {
375
			$fields['organization'] = $organization;
376
		}
377 1
		if ( $pages ) {
378 1
			$fields['pages'] = $pages;
379
		}
380 1
		if ( $publisher ) {
381 1
			$fields['publisher'] = $publisher;
382
		}
383 1
		if ( $school ) {
384
			$fields['school'] = $school;
385
		}
386 1
		if ( $series ) {
387
			$fields['series'] = $series;
388
		}
389 1
		if ( $title ) {
390 1
			$fields['title'] = $title;
391
		}
392 1
		if ( $url ) {
393
			$fields['url'] = $url;
394
		}
395 1
		if ( $volume ) {
396
			$fields['volume'] = $volume;
397
		}
398 1
		if ( $year ) {
399 1
			$fields['year'] = $year;
400
		}
401
402 1
		$this->fields = $fields;
403
404
		// generating the URI: author last name + year + first letters of title
405 1
		$URI = '';
406 1
		if ( $author ) {
407 1
			$authors = explode( ',', $author );
408 1
			$authors = explode( wfMessage( 'and' )->text(), $authors[0] );
409 1
			$arrayAuthor = explode( ' ', $authors[0], 2 );
410 1
			$URI .= str_replace( ' ', '', $arrayAuthor[array_key_exists( 1, $arrayAuthor ) ? 1 : 0] );
411
		}
412
413 1
		if ( $year ) {
414 1
			$URI .= $year;
415
		}
416
417 1
		if ( $title ) {
418 1
			foreach ( explode( ' ', $title ) as $titleWord ) {
419 1
				$charsTitleWord = preg_split( '//', $titleWord, -1, PREG_SPLIT_NO_EMPTY );
420
421 1
				if ( !empty( $charsTitleWord ) ) {
422 1
					$URI .= $charsTitleWord[0];
423
				}
424
			}
425
		}
426
427 1
		$this->URI = strtolower( $URI );
428 1
	}
429
430
	/**
431
	 * Creates the BibTeX output for a single item.
432
	 */
433 1
	public function text() {
434 1
		$text = '@' . $this->bibTeXtype . '{' . $this->URI . ",\r\n";
435
436 1
		foreach ( $this->fields as $key => $value ) {
437 1
			$text .= '  ' . $key . ' = "' . $value . '", ' . "\r\n";
438
		}
439
440 1
		$text .= "}\r\n\r\n";
441
442 1
		return $text;
443
	}
444
}
445