Completed
Push — master ( c0e2ef...2173f1 )
by Karsten
07:19
created

formats/bibtex/SRF_BibTeX.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	public function getFileName( SMWQueryResult $queryResult ) {
56
		if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
57
			return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.bib';
58
		} else {
59
			return 'BibTeX.bib';
60
		}
61
	}
62
63
	public function getQueryMode( $context ) {
64
		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
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
72
		global $wgSitename;
73
		$result = '';
74
75
		if ( $outputmode == SMW_OUTPUT_FILE ) { // make file
76
			if ( $this->m_title == '' ) {
77
				$this->m_title = $wgSitename;
78
			}
79
80
			$items = [];
81
82
			while ( $row = $res->getNext() ) {
83
				$items[] = $this->getItemForResultRow( $row )->text();
84
			}
85
86
			$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
		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
	protected function getItemForResultRow( array /* of SMWResultArray */
0 ignored issues
show
getItemForResultRow uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
118
	$row ) {
119
		$address = '';
120
		$annote = '';
121
		$author = '';
122
		$booktitle = '';
123
		$chapter = '';
124
		$crossref = '';
125
		$doi = '';
126
		$edition = '';
127
		$editor = '';
128
		$eprint = '';
129
		$howpublished = '';
130
		$institution = '';
131
		$journal = '';
132
		$key = '';
133
		$month = '';
134
		$note = '';
135
		$number = '';
136
		$organization = '';
137
		$pages = '';
138
		$publisher = '';
139
		$school = '';
140
		$series = '';
141
		$title = '';
142
		$type = '';
143
		$url = '';
144
		$volume = '';
145
		$year = '';
146
147
		foreach ( $row as /* SMWResultArray */
148
				  $field ) {
149
			$req = $field->getPrintRequest();
150
			$label = strtolower( $req->getLabel() );
151
			$var = false;
152
153
			switch ( $label ) {
154
				case 'type':
155
					$var =& $type;
156
					break;
157
				case 'address':
158
					$var =& $address;
159
					break;
160
				case 'annote':
161
					$var =& $annote;
162
					break;
163
				case 'booktitle':
164
					$var =& $booktitle;
165
					break;
166
				case 'chapter':
167
					$var =& $chapter;
168
					break;
169
				case 'crossref':
170
					$var =& $crossref;
171
					break;
172
				case 'doi':
173
					$var =& $doi;
174
					break;
175
				case 'edition':
176
					$var =& $edition;
177
					break;
178
				case 'eprint':
179
					$var =& $eprint;
180
					break;
181
				case 'howpublished':
182
					$var =& $howpublished;
183
					break;
184
				case 'institution':
185
					$var =& $institution;
186
					break;
187
				case 'journal':
188
					$var =& $journal;
189
					break;
190
				case 'key':
191
					$var =& $key;
192
					break;
193
				case 'note':
194
					$var =& $note;
195
					break;
196
				case 'number':
197
					$var =& $number;
198
					break;
199
				case 'organization':
200
					$var =& $organization;
201
					break;
202
				case 'pages':
203
					$var =& $pages;
204
					break;
205
				case 'publisher':
206
					$var =& $publisher;
207
					break;
208
				case 'school':
209
					$var =& $school;
210
					break;
211
				case 'series':
212
					$var =& $series;
213
					break;
214
				case 'title':
215
					$var =& $title;
216
					break;
217
				case 'url':
218
					$var =& $url;
219
					break;
220
				case 'year':
221
					$var =& $year;
222
					break;
223
				case 'month':
224
					$var =& $month;
225
					break;
226
				case 'volume':
227
				case 'journal_volume':
228
					$var =& $volume;
229
					break;
230
			}
231
232
			if ( $var !== false ) {
233
				$dataValue = $field->getNextDataValue();
234
235
				if ( $dataValue !== false ) {
236
					$var = $dataValue->getShortWikiText();
237
				}
238
239
				unset( $var );
240
			} else {
241
				switch ( $label ) {
242
					case 'author':
243
					case 'authors':
244
					case 'editor' :
245
					case 'editors':
246
						$wikiTexts = [];
247
						while ( ( /* SMWDataValue */
248
							$dataValue = $field->getNextDataValue() ) !== false ) {
249
							$wikiTexts[] = $dataValue->getShortWikiText();
250
						}
251
						$wikiText = $GLOBALS['wgLang']->listToText( $wikiTexts );
252
253
						if ( $label == 'author' || $label == 'authors' ) {
254
							$author = $wikiText;
255
						} else {
256
							$editor = $wikiText;
257
						}
258
						break;
259
					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
						break;
267
				}
268
			}
269
		}
270
271
		return new SMWBibTeXEntry(
272
			$type,
273
			$address,
274
			$annote,
275
			$author,
276
			$booktitle,
277
			$chapter,
278
			$crossref,
279
			$doi,
280
			$edition,
281
			$editor,
282
			$eprint,
283
			$howpublished,
284
			$institution,
285
			$journal,
286
			$key,
287
			$month,
288
			$note,
289
			$number,
290
			$organization,
291
			$pages,
292
			$publisher,
293
			$school,
294
			$series,
295
			$title,
296
			$url,
297
			$volume,
298
			$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
	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
		if ( $type ) {
316
			$this->bibTeXtype = ucfirst( $type );
317
		} else {
318
			$this->bibTeXtype = 'Book';
319
		}
320
321
		$fields = [];
322
323
		if ( $address ) {
324
			$fields['address'] = $address;
325
		}
326
		if ( $annote ) {
327
			$fields['annote'] = $annote;
328
		}
329
		if ( $author ) {
330
			$fields['author'] = $author;
331
		}
332
		if ( $booktitle ) {
333
			$fields['booktitle'] = $booktitle;
334
		}
335
		if ( $chapter ) {
336
			$fields['chapter'] = $chapter;
337
		}
338
		if ( $crossref ) {
339
			$fields['crossref'] = $crossref;
340
		}
341
		if ( $doi ) {
342
			$fields['doi'] = $doi;
343
		}
344
		if ( $edition ) {
345
			$fields['edition'] = $edition;
346
		}
347
		if ( $editor ) {
348
			$fields['editor'] = $editor;
349
		}
350
		if ( $eprint ) {
351
			$fields['eprint'] = $eprint;
352
		}
353
		if ( $howpublished ) {
354
			$fields['howpublished'] = $howpublished;
355
		}
356
		if ( $institution ) {
357
			$fields['institution'] = $institution;
358
		}
359
		if ( $journal ) {
360
			$fields['journal'] = $journal;
361
		}
362
		if ( $key ) {
363
			$fields['key'] = $key;
364
		}
365
		if ( $month ) {
366
			$fields['month'] = $month;
367
		}
368
		if ( $note ) {
369
			$fields['note'] = $note;
370
		}
371
		if ( $number ) {
372
			$fields['number'] = $number;
373
		}
374
		if ( $organization ) {
375
			$fields['organization'] = $organization;
376
		}
377
		if ( $pages ) {
378
			$fields['pages'] = $pages;
379
		}
380
		if ( $publisher ) {
381
			$fields['publisher'] = $publisher;
382
		}
383
		if ( $school ) {
384
			$fields['school'] = $school;
385
		}
386
		if ( $series ) {
387
			$fields['series'] = $series;
388
		}
389
		if ( $title ) {
390
			$fields['title'] = $title;
391
		}
392
		if ( $url ) {
393
			$fields['url'] = $url;
394
		}
395
		if ( $volume ) {
396
			$fields['volume'] = $volume;
397
		}
398
		if ( $year ) {
399
			$fields['year'] = $year;
400
		}
401
402
		$this->fields = $fields;
403
404
		// generating the URI: author last name + year + first letters of title
405
		$URI = '';
406
		if ( $author ) {
407
			$authors = explode( ',', $author );
408
			$authors = explode( wfMessage( 'and' )->text(), $authors[0] );
409
			$arrayAuthor = explode( ' ', $authors[0], 2 );
410
			$URI .= str_replace( ' ', '', $arrayAuthor[array_key_exists( 1, $arrayAuthor ) ? 1 : 0] );
411
		}
412
413
		if ( $year ) {
414
			$URI .= $year;
415
		}
416
417
		if ( $title ) {
418
			foreach ( explode( ' ', $title ) as $titleWord ) {
419
				$charsTitleWord = preg_split( '//', $titleWord, -1, PREG_SPLIT_NO_EMPTY );
420
421
				if ( !empty( $charsTitleWord ) ) {
422
					$URI .= $charsTitleWord[0];
423
				}
424
			}
425
		}
426
427
		$this->URI = strtolower( $URI );
428
	}
429
430
	/**
431
	 * Creates the BibTeX output for a single item.
432
	 */
433
	public function text() {
434
		$text = '@' . $this->bibTeXtype . '{' . $this->URI . ",\r\n";
435
436
		foreach ( $this->fields as $key => $value ) {
437
			$text .= '  ' . $key . ' = "' . $value . '", ' . "\r\n";
438
		}
439
440
		$text .= "}\r\n\r\n";
441
442
		return $text;
443
	}
444
}
445