Completed
Push — master ( cb17e1...aaf96d )
by Stephan
08:23 queued 06:10
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
 * Printer class for creating BibTeX exports
4
 *
5
 * For details on availble keys see the README
6
 *
7
 * Example of a book :
8
 *
9
 * @Book{abramowitz1964homf,
10
 *   author =	 "Milton Abramowitz and Irene A. Stegun",
11
 *   title = 	 "Handbook of Mathematical Functions",
12
 *   publisher = 	 "Dover",
13
 *   year = 	 1964,
14
 *   address =	 "New York",
15
 *   edition =	 "ninth Dover printing, tenth GPO printing"
16
 * }
17
 * @file
18
 * @ingroup SemanticResultFormats
19
 *
20
 * @author Markus Krötzsch
21
 * @author Denny Vrandecic
22
 * @author Frank Dengler
23
 * @author Steren Giannini
24
 * @ingroup SemanticResultFormats
25
 */
26
class SRFBibTeX extends SMWExportPrinter {
27
	protected $m_title = '';
28
	protected $m_description = '';
29
30
	/**
31
	 * @see SMWIExportPrinter::getMimeType
32
	 *
33
	 * @since 1.8
34
	 *
35
	 * @param SMWQueryResult $queryResult
36
	 *
37
	 * @return string
38
	 */
39
	public function getMimeType( SMWQueryResult $queryResult ) {
40
		return 'text/bibtex';
41
	}
42
43
	/**
44
	 * @see SMWIExportPrinter::getFileName
45
	 *
46
	 * @since 1.8
47
	 *
48
	 * @param SMWQueryResult $queryResult
49
	 *
50
	 * @return string|boolean
51
	 */
52
	public function getFileName( SMWQueryResult $queryResult ) {
53
		if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
54
			return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.bib';
55
		} else {
56
			return 'BibTeX.bib';
57
		}
58
	}
59
60
	public function getQueryMode( $context ) {
61
		return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES:SMWQuery::MODE_NONE;
62
	}
63
64
	public function getName() {
65
		return wfMessage( 'srf_printername_bibtex' )->text();
66
	}
67
68
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
69
		global $wgSitename;
70
		$result = '';
71
		
72
		if ( $outputmode == SMW_OUTPUT_FILE ) { // make file
73
			if ( $this->m_title == '' ) {
74
				$this->m_title = $wgSitename;
75
			}
76
			
77
			$items = [];
78
			
79
			while ( $row = $res->getNext() ) {
80
				$items[] = $this->getItemForResultRow( $row )->text();
81
			}
82
			
83
			$result = implode( '', $items );
84
		} else { // just make link to export
85
			if ( $this->getSearchLabel( $outputmode ) ) {
86
				$label = $this->getSearchLabel( $outputmode );
87
			} else {
88
				$label = wfMessage( 'srf_bibtex_link' )->inContentLanguage()->text();
89
			}
90
			
91
			$link = $res->getQueryLink( $label );
0 ignored issues
show
Deprecated Code introduced by
The method SMWQueryResult::getQueryLink() has been deprecated with message: since SMW 1.8

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
92
			$link->setParameter( 'bibtex', 'format' );
93
			
94
			if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
95
				$link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
96
			}
97
			
98
			$result .= $link->getText( $outputmode, $this->mLinker );
99
			$this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed
100
		}
101
		
102
		return $result;
103
	}
104
105
	/**
106
	 * Gets a SMWBibTeXEntry for the row.
107
	 *
108
	 * @since 1.6
109
	 *
110
	 * @param $row array of SMWResultArray
111
	 *
112
	 * @return SMWBibTeXEntry
113
	 */
114
	protected function getItemForResultRow( array /* of SMWResultArray */ $row ) {
115
		$address = '';
116
		$annote = '';
117
		$author = '';
118
		$booktitle = '';
119
		$chapter = '';
120
		$crossref = '';
121
		$doi = '';
122
		$edition = '';
123
		$editor = '';
124
		$eprint = '';
125
		$howpublished = '';
126
		$institution = '';
127
		$journal = '';
128
		$key = '';
129
		$month = '';
130
		$note = '';
131
		$number = '';
132
		$organization = '';
133
		$pages = '';
134
		$publisher = '';
135
		$school = '';
136
		$series = '';
137
		$title = '';
138
		$type = '';
139
		$url = '';
140
		$volume = '';
141
		$year = '';
142
		
143
		foreach ( $row as /* SMWResultArray */ $field ) {
144
			$req = $field->getPrintRequest();
145
			$label = strtolower( $req->getLabel() );
146
			$var = false;
147
			
148
			switch ( $label ) {
149
				case 'type': $var =& $type; break;
150
				case 'address': $var =& $address; break;
151
				case 'annote': $var =& $annote; break;
152
				case 'booktitle': $var =& $booktitle; break;
153
				case 'chapter': $var =& $chapter; break;
154
				case 'crossref': $var =& $crossref; break;
155
				case 'doi': $var =& $doi; break;
156
				case 'edition': $var =& $edition; break;
157
				case 'eprint': $var =& $eprint; break;
158
				case 'howpublished': $var =& $howpublished; break;
159
				case 'institution': $var =& $institution; break;
160
				case 'journal': $var =& $journal; break;
161
				case 'key': $var =& $key; break;
162
				case 'note': $var =& $note; break;
163
				case 'number': $var =& $number; break;
164
				case 'organization': $var =& $organization; break;
165
				case 'pages': $var =& $pages; break;
166
				case 'publisher': $var =& $publisher; break;
167
				case 'school': $var =& $school; break;
168
				case 'series': $var =& $series; break;
169
				case 'title': $var =& $title; break;
170
				case 'url': $var =& $url; break;
171
				case 'year': $var =& $year; break;
172
				case 'month': $var =& $month; break;
173
				case 'volume': case 'journal_volume': $var =& $volume; break;
174
			}
175
			
176
			if ( $var !== false ) {
177
				$dataValue = $field->getNextDataValue();
178
				
179
				if ( $dataValue !== false ) {
180
					$var = $dataValue->getShortWikiText();
181
				}
182
				
183
				unset( $var );
184
			}
185
			else {
186
				switch ( $label ) {
187
					case 'author': case 'authors': case 'editor' : case 'editors':
188
						$wikiTexts = [];
189
						while ( ( /* SMWDataValue */ $dataValue = $field->getNextDataValue() ) !== false ) {
190
							$wikiTexts[] = $dataValue->getShortWikiText();
191
						}
192
						$wikiText = $GLOBALS['wgLang']->listToText( $wikiTexts );
193
						
194
						if ( $label == 'author' || $label == 'authors' ) {
195
							$author = $wikiText;
196
						} else {
197
							$editor = $wikiText;
198
						}
199
						break;
200
					case 'date':
201
						$dataValue = $field->getNextDataValue();
202
						
203
						if ( $dataValue !== false && get_class( $dataValue ) == 'SMWTimeValue' ) {
204
							$year = $dataValue->getYear();
205
							$month = $dataValue->getMonth();
206
						}
207
						break;
208
				}
209
			}
210
		}
211
212
		return new SMWBibTeXEntry( $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 );
213
	}
214
}
215
216
/**
217
 * Represents a single entry in an BibTeX
218
 * @ingroup SMWQuery
219
 */
220
class SMWBibTeXEntry {
221
	private $bibTeXtype;
222
	private $URI;
223
	private $fields = [];
224
225
	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 ) {
226
		if ( $type ) $this->bibTeXtype = ucfirst( $type ); else $this->bibTeXtype = 'Book';
227
228
		$fields = [];
229
230
		if ( $address ) $fields['address'] = $address;
231
		if ( $annote ) $fields['annote'] = $annote;
232
		if ( $author ) $fields['author'] = $author;
233
		if ( $booktitle ) $fields['booktitle'] = $booktitle;
234
		if ( $chapter ) $fields['chapter'] = $chapter;
235
		if ( $crossref ) $fields['crossref'] = $crossref;
236
		if ( $doi ) $fields['doi'] = $doi;
237
		if ( $edition ) $fields['edition'] = $edition;
238
		if ( $editor ) $fields['editor'] = $editor;
239
		if ( $eprint ) $fields['eprint'] = $eprint;
240
		if ( $howpublished ) $fields['howpublished'] = $howpublished;
241
		if ( $institution ) $fields['institution'] = $institution;
242
		if ( $journal ) $fields['journal'] = $journal;
243
		if ( $key ) $fields['key'] = $key;
244
		if ( $month ) $fields['month'] = $month;
245
		if ( $note ) $fields['note'] = $note;
246
		if ( $number ) $fields['number'] = $number;
247
		if ( $organization ) $fields['organization'] = $organization;
248
		if ( $pages ) $fields['pages'] = $pages;
249
		if ( $publisher ) $fields['publisher'] = $publisher;
250
		if ( $school ) $fields['school'] = $school;
251
		if ( $series ) $fields['series'] = $series;
252
		if ( $title ) $fields['title'] = $title;
253
		if ( $url ) $fields['url'] = $url;
254
		if ( $volume ) $fields['volume'] = $volume;
255
		if ( $year ) $fields['year'] = $year;
256
257
		$this->fields = $fields;
258
259
		// generating the URI: author last name + year + first letters of title
260
		$URI = '';
261
		if ( $author ) {
262
			$authors = explode( ',', $author );
263
			$authors = explode( wfMessage( 'and' )->text(), $authors[0] );
264
			$arrayAuthor = explode( ' ', $authors[0], 2 );
265
			$URI .= str_replace( ' ', '', $arrayAuthor[array_key_exists( 1, $arrayAuthor ) ? 1 : 0] );
266
		}
267
		
268
		if ( $year ) {
269
			$URI .= $year;
270
		}
271
		
272
		if ( $title ) {
273
			foreach ( explode( ' ', $title ) as $titleWord ) {
274
				$charsTitleWord = preg_split( '//', $titleWord, - 1, PREG_SPLIT_NO_EMPTY );
275
				
276
				if ( !empty( $charsTitleWord ) ) {
277
					$URI .= $charsTitleWord[0];
278
				}
279
			}
280
		}
281
		
282
		$this->URI = strtolower( $URI );
283
	}
284
285
286
	/**
287
	 * Creates the BibTeX output for a single item.
288
	 */
289
	public function text() {
290
		$text  = '@' . $this->bibTeXtype . '{' . $this->URI . ",\r\n";
291
		
292
		foreach ( $this->fields as $key => $value ) {
293
			$text .= '  ' . $key . ' = "' . $value . '", ' . "\r\n";
294
		}
295
		
296
		$text .= "}\r\n\r\n";
297
298
		return $text;
299
	}
300
}
301