BibTexFileExportPrinter::getMimeType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SRF\BibTex;
4
5
use SMWTimeValue as TimeValue;
6
use SMWQueryResult as QueryResult;
7
use SMW\Query\ResultPrinters\FileExportPrinter;
8
9
/**
10
 * Printer class for creating BibTeX exports
11
 *
12
 * For details on availble keys see the README
13
 *
14
 * Example of a book :
15
 *
16
 * @Book{abramowitz1964homf,
17
 *   author =     "Milton Abramowitz and Irene A. Stegun",
18
 *   title =     "Handbook of Mathematical Functions",
19
 *   publisher =     "Dover",
20
 *   year =     1964,
21
 *   address =     "New York",
22
 *   edition =     "ninth Dover printing, tenth GPO printing"
23
 * }
24
 *
25
 * @license GNU GPL v2+
26
 * @since 1.5
27
 *
28
 * @author Markus Krötzsch
29
 * @author Denny Vrandecic
30
 * @author Frank Dengler
31
 * @author Steren Giannini
32
 */
33
class BibTexFileExportPrinter extends FileExportPrinter {
34
35
	/**
36
	 * @see ResultPrinter::getName
37
	 *
38
	 * {@inheritDoc}
39
	 */
40
	public function getName() {
41
		return wfMessage( 'srf_printername_bibtex' )->text();
42
	}
43
44
	/**
45
	 * @see FileExportPrinter::getMimeType
46
	 *
47
	 * @since 1.8
48
	 *
49
	 * {@inheritDoc}
50
	 */
51 2
	public function getMimeType( QueryResult $queryResult ) {
52 2
		return 'text/bibtex';
53
	}
54
55
	/**
56
	 * @see FileExportPrinter::getFileName
57
	 *
58
	 * @since 1.8
59
	 *
60
	 * {@inheritDoc}
61
	 */
62 6
	public function getFileName( QueryResult $queryResult ) {
63
64 6
		if ( $this->params['filename'] !== '' ) {
65
66 4
			if ( strpos( $this->params['filename'], '.bib' ) === false ) {
67 1
				$this->params['filename'] .= '.bib';
68
			}
69
70 4
			return str_replace( ' ', '_', $this->params['filename'] );
71 2
		} elseif ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
72 1
			return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.bib';
73
		}
74
75 1
		return 'BibTeX.bib';
76
	}
77
78
	/**
79
	 * @see ResultPrinter::getParamDefinitions
80
	 *
81
	 * @since 1.8
82
	 *
83
	 * {@inheritDoc}
84
	 */
85 1
	public function getParamDefinitions( array $definitions ) {
86 1
		$params = parent::getParamDefinitions( $definitions );
87
88 1
		$params['filename'] = [
89
			'message' => 'smw-paramdesc-filename',
90
			'default' => 'bibtex.bib',
91
		];
92
93 1
		return $params;
94
	}
95
96
	/**
97
	 * @since 3.1
98
	 *
99
	 * @param array $list
0 ignored issues
show
Bug introduced by mwjames
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...
100
	 *
101
	 * @return string
102
	 */
103 1
	public function getFormattedList( $key, array $values ) {
104 1
		return $GLOBALS['wgLang']->listToText( $values );
105
	}
106
107
	/**
108
	 * @see ResultPrinter::getResultText
109
	 *
110
	 * {@inheritDoc}
111
	 */
112 2
	protected function getResultText( QueryResult $res, $outputMode ) {
113
114 2
		if ( $outputMode !== SMW_OUTPUT_FILE ) {
115 1
			return $this->getBibTexLink( $res, $outputMode );
116
		}
117
118 1
		$items = [];
119
120 1
		while ( $row = $res->getNext() ) {
121 1
			$items[] = $this->newItem( $row )->text();
122
		}
123
124 1
		return implode( "\r\n\r\n", $items );
125
	}
126
127 1
	private function getBibTexLink( QueryResult $res, $outputMode ) {
128
129
		// Can be viewed as HTML if requested, no more parsing needed
130 1
		$this->isHTML = $outputMode == SMW_OUTPUT_HTML;
131
132 1
		$link = $this->getLink(
133 1
			$res,
134
			$outputMode
135
		);
136
137 1
		return $link->getText( $outputMode, $this->mLinker );
138
	}
139
140
	/**
141
	 * @since 3.1
142
	 *
143
	 * @param $row array of SMWResultArray
144
	 *
145
	 * @return bibTexItem
146
	 */
147 1
	private function newItem( array /* of SMWResultArray */ $row ) {
148
149 1
		$item = new Item();
150 1
		$item->setFormatterCallback( [ $this, 'getFormattedList' ] );
151
152 1
		foreach ( $row as /* SMWResultArray */ $field ) {
153 1
			$printRequest = $field->getPrintRequest();
154 1
			$values = [];
155
156 1
			$label = strtolower( $printRequest->getLabel() );
157 1
			$dataValue = $field->getNextDataValue();
158
159 1
			if ( $dataValue === false ) {
160 1
				continue;
161
			}
162
163 1
			if ( $label === 'date' && $dataValue instanceof TimeValue ) {
164
				$item->set( 'year', $dataValue->getYear() );
165
				$item->set( 'month', $dataValue->getMonth() );
166 1
			} elseif ( $label === 'author' || $label === 'authors' ) {
167 1
				$values[] = $dataValue->getShortWikiText();
168
169 1
				while ( ( /* SMWDataValue */ $dataValue = $field->getNextDataValue() ) !== false ) {
170 1
					$values[] = $dataValue->getShortWikiText();
171
				}
172
173 1
				$item->set( 'author', $values );
174 1
			} elseif ( $label === 'editor' || $label === 'editors' ) {
175 1
				$values[] = $dataValue->getShortWikiText();
176
177 1
				while ( ( /* SMWDataValue */ $dataValue = $field->getNextDataValue() ) !== false ) {
178 1
					$values[] = $dataValue->getShortWikiText();
179
				}
180
181 1
				$item->set( 'editor', $values );
182
			} else {
183 1
				$item->set( $label, $dataValue->getShortWikiText() );
184
			}
185
		}
186
187 1
		return $item;
188
	}
189
190
}
191