summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/src/BibTex/BibTexFileExportPrinter.php
blob: aa2bc0115e519f11fc09db6885b36866af8d3b19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php

namespace SRF\BibTex;

use SMWTimeValue as TimeValue;
use SMWQueryResult as QueryResult;
use SMW\Query\ResultPrinters\FileExportPrinter;

/**
 * Printer class for creating BibTeX exports
 *
 * For details on availble keys see the README
 *
 * Example of a book :
 *
 * @Book{abramowitz1964homf,
 *   author =     "Milton Abramowitz and Irene A. Stegun",
 *   title =     "Handbook of Mathematical Functions",
 *   publisher =     "Dover",
 *   year =     1964,
 *   address =     "New York",
 *   edition =     "ninth Dover printing, tenth GPO printing"
 * }
 *
 * @license GNU GPL v2+
 * @since 1.5
 *
 * @author Markus Krötzsch
 * @author Denny Vrandecic
 * @author Frank Dengler
 * @author Steren Giannini
 */
class BibTexFileExportPrinter extends FileExportPrinter {

	/**
	 * @see ResultPrinter::getName
	 *
	 * {@inheritDoc}
	 */
	public function getName() {
		return wfMessage( 'srf_printername_bibtex' )->text();
	}

	/**
	 * @see FileExportPrinter::getMimeType
	 *
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getMimeType( QueryResult $queryResult ) {
		return 'text/bibtex';
	}

	/**
	 * @see FileExportPrinter::getFileName
	 *
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getFileName( QueryResult $queryResult ) {

		if ( $this->params['filename'] !== '' ) {

			if ( strpos( $this->params['filename'], '.bib' ) === false ) {
				$this->params['filename'] .= '.bib';
			}

			return str_replace( ' ', '_', $this->params['filename'] );
		} elseif ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
			return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.bib';
		}

		return 'BibTeX.bib';
	}

	/**
	 * @see ResultPrinter::getParamDefinitions
	 *
	 * @since 1.8
	 *
	 * {@inheritDoc}
	 */
	public function getParamDefinitions( array $definitions ) {
		$params = parent::getParamDefinitions( $definitions );

		$params['filename'] = [
			'message' => 'smw-paramdesc-filename',
			'default' => 'bibtex.bib',
		];

		return $params;
	}

	/**
	 * @since 3.1
	 *
	 * @param array $list
	 *
	 * @return string
	 */
	public function getFormattedList( $key, array $values ) {
		return $GLOBALS['wgLang']->listToText( $values );
	}

	/**
	 * @see ResultPrinter::getResultText
	 *
	 * {@inheritDoc}
	 */
	protected function getResultText( QueryResult $res, $outputMode ) {

		if ( $outputMode !== SMW_OUTPUT_FILE ) {
			return $this->getBibTexLink( $res, $outputMode );
		}

		$items = [];

		while ( $row = $res->getNext() ) {
			$items[] = $this->newItem( $row )->text();
		}

		return implode( "\r\n\r\n", $items );
	}

	private function getBibTexLink( QueryResult $res, $outputMode ) {

		// Can be viewed as HTML if requested, no more parsing needed
		$this->isHTML = $outputMode == SMW_OUTPUT_HTML;

		$link = $this->getLink(
			$res,
			$outputMode
		);

		return $link->getText( $outputMode, $this->mLinker );
	}

	/**
	 * @since 3.1
	 *
	 * @param $row array of SMWResultArray
	 *
	 * @return bibTexItem
	 */
	private function newItem( array /* of SMWResultArray */ $row ) {

		$item = new Item();
		$item->setFormatterCallback( [ $this, 'getFormattedList' ] );

		foreach ( $row as /* SMWResultArray */ $field ) {
			$printRequest = $field->getPrintRequest();
			$values = [];

			$label = strtolower( $printRequest->getLabel() );
			$dataValue = $field->getNextDataValue();

			if ( $dataValue === false ) {
				continue;
			}

			if ( $label === 'date' && $dataValue instanceof TimeValue ) {
				$item->set( 'year', $dataValue->getYear() );
				$item->set( 'month', $dataValue->getMonth() );
			} elseif ( $label === 'author' || $label === 'authors' ) {
				$values[] = $dataValue->getShortWikiText();

				while ( ( /* SMWDataValue */ $dataValue = $field->getNextDataValue() ) !== false ) {
					$values[] = $dataValue->getShortWikiText();
				}

				$item->set( 'author', $values );
			} elseif ( $label === 'editor' || $label === 'editors' ) {
				$values[] = $dataValue->getShortWikiText();

				while ( ( /* SMWDataValue */ $dataValue = $field->getNextDataValue() ) !== false ) {
					$values[] = $dataValue->getShortWikiText();
				}

				$item->set( 'editor', $values );
			} else {
				$item->set( $label, $dataValue->getShortWikiText() );
			}
		}

		return $item;
	}

}