Completed
Pull Request — master (#510)
by mw
10:06 queued 08:43
created

SRFBibTeX::getQueryMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
use SRF\BibTex\Item;
4
use SMWTimeValue as TimeValue;
5
use SMWQueryResult as QueryResult;
6
use SMW\Query\ResultPrinters\FileExportPrinter;
7
8
/**
9
 * Printer class for creating BibTeX exports
10
 *
11
 * For details on availble keys see the README
12
 *
13
 * Example of a book :
14
 *
15
 * @Book{abramowitz1964homf,
16
 *   author =     "Milton Abramowitz and Irene A. Stegun",
17
 *   title =     "Handbook of Mathematical Functions",
18
 *   publisher =     "Dover",
19
 *   year =     1964,
20
 *   address =     "New York",
21
 *   edition =     "ninth Dover printing, tenth GPO printing"
22
 * }
23
 *
24
 * @file
25
 * @ingroup SemanticResultFormats
26
 *
27
 * @author Markus Krötzsch
28
 * @author Denny Vrandecic
29
 * @author Frank Dengler
30
 * @author Steren Giannini
31
 */
32
class SRFBibTeX extends FileExportPrinter {
33
34
	/**
35
	 * @see ResultPrinter::getName
36
	 *
37
	 * {@inheritDoc}
38
	 */
39
	public function getName() {
40
		return wfMessage( 'srf_printername_bibtex' )->text();
41
	}
42
43
	/**
44
	 * @see FileExportPrinter::getMimeType
45
	 *
46
	 * @since 1.8
47
	 *
48
	 * {@inheritDoc}
49
	 */
50
	public function getMimeType( QueryResult $queryResult ) {
51
		return 'text/bibtex';
52
	}
53
54
	/**
55
	 * @see FileExportPrinter::getFileName
56
	 *
57
	 * @since 1.8
58
	 *
59
	 * {@inheritDoc}
60
	 */
61 1
	public function getFileName( QueryResult $queryResult ) {
62
63 1
		if ( $this->params['filename'] !== '' ) {
64
65 1
			if ( strpos( $this->params['filename'], '.bib' ) === false ) {
66
				$this->params['filename'] .= '.bib';
67
			}
68
69 1
			return str_replace( ' ', '_', $this->params['filename'] );
70
		} elseif ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
71
			return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.bib';
72
		}
73
74
		return 'BibTeX.bib';
75
	}
76
77
	/**
78
	 * @see ResultPrinter::getParamDefinitions
79
	 *
80
	 * @since 1.8
81
	 *
82
	 * {@inheritDoc}
83
	 */
84 1
	public function getParamDefinitions( array $definitions ) {
85 1
		$params = parent::getParamDefinitions( $definitions );
86
87 1
		$params['filename'] = [
88
			'message' => 'smw-paramdesc-filename',
89
			'default' => 'bibtex.bib',
90
		];
91
92 1
		return $params;
93
	}
94
95
	/**
96
	 * @since 3.1
97
	 *
98
	 * @param array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
99
	 *
100
	 * @return string
101
	 */
102 1
	public function getFormattedList( $key, array $values ) {
103 1
		return $GLOBALS['wgLang']->listToText( $values );
104
	}
105
106
	/**
107
	 * @see ResultPrinter::getResultText
108
	 *
109
	 * {@inheritDoc}
110
	 */
111 1
	protected function getResultText( QueryResult $res, $outputMode ) {
112
113 1
		if ( $outputMode !== SMW_OUTPUT_FILE ) {
114
			return $this->getBibTexLink( $res, $outputMode );
115
		}
116
117 1
		$items = [];
118
119 1
		while ( $row = $res->getNext() ) {
120 1
			$items[] = $this->newItem( $row )->text();
121
		}
122
123 1
		return implode( "\r\n\r\n", $items );
124
	}
125
126
	private function getBibTexLink( QueryResult $res, $outputMode ) {
127
128
		// Can be viewed as HTML if requested, no more parsing needed
129
		$this->isHTML = $outputMode == SMW_OUTPUT_HTML;
130
131
		$link = $this->getLink(
132
			$res,
133
			$outputMode
134
		);
135
136
		return $link->getText( $outputMode, $this->mLinker );
137
	}
138
139
	/**
140
	 * @since 3.1
141
	 *
142
	 * @param $row array of SMWResultArray
143
	 *
144
	 * @return bibTexItem
145
	 */
146 1
	private function newItem( array /* of SMWResultArray */ $row ) {
147
148 1
		$item = new Item();
149 1
		$item->setFormatterCallback( [ $this, 'getFormattedList' ] );
150
151 1
		foreach ( $row as /* SMWResultArray */ $field ) {
152 1
			$printRequest = $field->getPrintRequest();
153 1
			$values = [];
154
155 1
			$label = strtolower( $printRequest->getLabel() );
156 1
			$dataValue = $field->getNextDataValue();
157
158 1
			if ( $dataValue === false ) {
159 1
				continue;
160
			}
161
162 1
			if ( $label === 'date' && $dataValue instanceof TimeValue ) {
163
				$item->set( 'year', $dataValue->getYear() );
164
				$item->set( 'month', $dataValue->getMonth() );
165 1
			} elseif ( $label === 'author' || $label === 'authors' ) {
166 1
				$values[] = $dataValue->getShortWikiText();
167
168 1
				while ( ( /* SMWDataValue */ $dataValue = $field->getNextDataValue() ) !== false ) {
169 1
					$values[] = $dataValue->getShortWikiText();
170
				}
171
172 1
				$item->set( 'author', $values );
173 1
			} elseif ( $label === 'editor' || $label === 'editors' ) {
174 1
				$values[] = $dataValue->getShortWikiText();
175
176 1
				while ( ( /* SMWDataValue */ $dataValue = $field->getNextDataValue() ) !== false ) {
177 1
					$values[] = $dataValue->getShortWikiText();
178
				}
179
180 1
				$item->set( 'editor', $values );
181
			} else {
182 1
				$item->set( $label, $dataValue->getShortWikiText() );
183
			}
184
		}
185
186 1
		return $item;
187
	}
188
189
}
190