summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Specials/Browse/GroupFormatter.php
blob: 0a7885b178bd461f1d09de32b11918746b0485bf (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php

namespace SMW\MediaWiki\Specials\Browse;

use Html;
use SMW\DIWikiPage;
use SMW\Message;
use SMW\PropertySpecificationLookup;
use SMWDataItem as DataItem;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since   3.0
 *
 * @author mwjames
 */
class GroupFormatter {

	/**
	 * Identifies a group label
	 */
	const MESSAGE_GROUP_LABEL = 'smw-property-group-label-';

	/**
	 * Identifies a group label
	 */
	const MESSAGE_GROUP_DESCRIPTION = 'smw-property-group-description-';

	/**
	 * @var PropertySpecificationLookup
	 */
	private $propertySpecificationLookup;

	/**
	 * @var boolean
	 */
	private $showGroup = true;

	/**
	 * @var string
	 */
	private $lastGroup = '';

	/**
	 * @var array
	 */
	private $groupLinks = [];

	/**
	 * @since 3.0
	 *
	 * @param PropertySpecificationLookup $propertySpecificationLookup
	 */
	public function __construct( PropertySpecificationLookup $propertySpecificationLookup ) {
		$this->propertySpecificationLookup = $propertySpecificationLookup;
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $showGroup
	 */
	public function showGroup( $showGroup ) {
		$this->showGroup = $showGroup;
	}

	/**
	 * @since 3.0
	 *
	 * @return boolean
	 */
	public function isLastGroup( $group ) {
		return $this->lastGroup === $group;
	}

	/**
	 * @since 3.0
	 *
	 * @return boolean
	 */
	public function hasGroups() {
		return $this->groupLinks !== [];
	}

	/**
	 * @since 3.0
	 *
	 * @param array &$properties
	 */
	public function findGroupMembership( array &$properties ) {

		$groupedProperties = [];
		$this->groupLinks = [];

		foreach ( $properties as $key => $property ) {

			$group = $this->findGroup( $property );

			if ( !isset( $groupedProperties[$group] ) ) {
				$groupedProperties[$group] = [];
			}

			$groupedProperties[$group][] = $property;
		}

		ksort( $groupedProperties, SORT_NATURAL | SORT_FLAG_CASE );
		$properties = $groupedProperties;

		$keys = array_keys( $groupedProperties );
		$this->lastGroup = end( $keys );
	}

	/**
	 * @since 3.0
	 *
	 * @param string $group
	 *
	 * @return string
	 */
	public function getGroupLink( $group ) {

		if ( !isset( $this->groupLinks[$group] ) || $this->groupLinks[$group] === '' ) {
			return $group;
		}

		return Html::rawElement(
			'span',
			[
				'class' => 'group-link'
			],
			$this->groupLinks[$group]
		);
	}

	/**
	 * @since 3.0
	 *
	 * @param string $id
	 * @param DIWikiPage $dataItem
	 *
	 * @return string
	 */
	public function getMessageClassLink( $id, DIWikiPage $dataItem ) {

		$gr = str_replace( '_', ' ', $dataItem->getDBKey() );
		$key = mb_strtolower( str_replace( ' ', '-', $gr ) );

		return Html::rawElement(
			'a',
			[
				'href' => DIWikiPage::newFromText( $id . $key, NS_MEDIAWIKI )->getTitle()->getFullURL(),
				'class' => !Message::exists( $id . $key ) ? 'new' : ''
			],
			$id . $key
		);
	}

	private function findGroup( $property ) {

		if ( $this->showGroup === false ) {
			return '';
		}

		$group = null;

		// Special handling for a `Category` property instance that itself cannot
		// be annotated with a `Is property group` therefor use the fixed
		// `smw-category-group` message to point to a group
		if ( $property->getKey() === '_INST' && Message::exists( 'smw-category-group' ) ) {
			$gr = Message::get( 'smw-category-group' );
		} elseif( ( $group = $this->propertySpecificationLookup->getPropertyGroup( $property ) ) instanceof DataItem ) {
			$gr = str_replace( '_', ' ', $group->getDBKey() );
		} else {
			return '';
		}

		$desc = '';
		$link = '';

		// Convention key to allow a category to transtable using the
		// `smw-group-...` as key and transforms a group `Foo bar` to
		// `smw-group-foo-bar`
		$key = mb_strtolower( str_replace( ' ', '-', $gr ) );

		if ( Message::exists( self::MESSAGE_GROUP_LABEL . $key ) ) {
			$gr = Message::get(
				self::MESSAGE_GROUP_LABEL . $key,
				Message::TEXT,
				Message::USER_LANGUAGE
			);
		}

		if ( Message::exists( self::MESSAGE_GROUP_DESCRIPTION . $key ) ) {
			$desc = Message::get(
				self::MESSAGE_GROUP_DESCRIPTION . $key,
				Message::TEXT,
				Message::USER_LANGUAGE
			);
		}

		if ( $group instanceof DataItem ) {
			$link = Html::rawElement(
				'a',
				[
					'href' => $group->getTitle()->getFullURL()
				],
				$gr
			);
		}

		if ( $desc !== '' ) {
			$link = Html::rawElement(
				'span',
				[
					'class' => 'smw-highlighter smwttinline',
					'data-state' => 'inline'
				],
				$link . Html::rawElement(
					'span',
					[
						'class' => 'smwttcontent'
					],
					$desc
				)
			);
		}

		if ( !isset( $this->groupLinks[$gr] ) || $this->groupLinks[$gr] === '' ) {
			$this->groupLinks[$gr] = $link;
		}

		return $gr;
	}

}