summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticResultFormats/src/BibTex/Item.php
blob: 7ff631efccd4adf53b877e90d7d341a8079a65c1 (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
<?php

namespace SRF\BibTex;

use SMWDataValue as DataValue;

/**
 * @see http://www.semantic-mediawiki.org/wiki/BibTex
 *
 * @license GNU GPL v2+
 * @since 3.1
 *
 * @author mwjames
 */
class Item {

	/**
	 * @see https://en.wikipedia.org/wiki/BibTeX
	 *
	 * @var string
	 */
	private $type = '';

	/**
	 * @var []
	 */
	protected $fields = [
		'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' => ''
	];

	/**
	 * @var callable
	 */
	private $formatterCallback;

	/**
	 * @since 3.1
	 */
	public function __construct() {
		$this->type = 'Book';
	}

	/**
	 * @since 3.1
	 *
	 * @param callable $compoundLabelCallback
	 */
	public function setFormatterCallback( callable $formatterCallback ) {
		$this->formatterCallback = $formatterCallback;
	}

	/**
	 * @since 3.1
	 *
	 * @param $key
	 * @param string $text
	 *
	 * @return string
	 */
	public function replace( $key, $text ) {

		if ( $key === 'uri' ) {
			$text = str_replace(
				[ "Ä", "ä", "Ö", "ö", "Ü", "ü", "ß" ],
				[ 'Ae', 'ae', 'Oe', 'oe', 'Ue', 'ue', 'ss' ],
				$text
			);
			$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text );
		}

		return $text;
	}

	/**
	 * @since 3.1
	 *
	 * @param $key
	 * @param mixed $value
	 */
	public function set( $key, $value ) {

		$key = strtolower( $key );

		if ( $key === 'type' ) {
			$this->type = ucfirst( $value );
		}

		if ( isset( $this->fields[$key] ) ) {
			$this->fields[$key] = $value;
		}
	}

	/**
	 * @since 3.1
	 *
	 * @return string
	 */
	public function text() {

		$formatterCallback = $this->formatterCallback;

		$text = '@' . $this->type . '{' . $this->buildURI() . ",\r\n";

		foreach ( $this->fields as $key => $value ) {

			if ( ( $key === 'author' || $key === 'editor' ) && is_array( $value ) ) {
				if ( is_callable( $formatterCallback ) ) {
					$value = $formatterCallback( $key, $value );
				} else {
					$value = implode( ', ', $value );
				}
			}

			if ( $value === '' ) {
				continue;
			}

			$text .= '  ' . $key . ' = "' . $value . '", ' . "\r\n";
		}

		$text .= "}";

		return $text;
	}

	/**
	 * Consist of `author last name` + `year` + `first word of title`
	 *
	 * @return string
	 */
	protected function buildURI() {

		$uri = '';

		if ( isset( $this->fields['author'] ) ) {
			foreach ( $this->fields['author'] as $key => $author ) {
				$elements = explode( ' ', $author );
				$uri .= array_pop( $elements );
				break;
			}
		}

		if ( isset( $this->fields['year'] ) ) {
			$uri .= $this->fields['year'];
		}

		if ( isset( $this->fields['title'] ) ) {
			foreach ( explode( ' ', $this->fields['title'] ) as $titleWord ) {
				$charsTitleWord = preg_split( '//', $titleWord, -1, PREG_SPLIT_NO_EMPTY );

				if ( !empty( $charsTitleWord ) ) {
					$uri .= $charsTitleWord[0];
				}
			}
		}

		return strtolower( $this->replace( 'uri', $uri ) );
	}

}