summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Exporter/ResourceBuilders/SortPropertyValueResourceBuilder.php
blob: 3a91d0fb60cf981822ed89c832142f65979f54d1 (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
<?php

namespace SMW\Exporter\ResourceBuilders;

use SMW\DIProperty;
use SMW\MediaWiki\Collator;
use SMWDataItem as DataItem;
use SMWDIBlob as DIBlob;
use SMWExpData as ExpData;
use SMWExpLiteral as ExpLiteral;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class SortPropertyValueResourceBuilder extends PredefinedPropertyValueResourceBuilder {

	/**
	 * @var boolean
	 */
	private $enabledCollationField = false;

	/**
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function isResourceBuilderFor( DIProperty $property ) {
		return $property->getKey() === '_SKEY';
	}


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

	/**
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function addResourceValue( ExpData $expData, DIProperty $property, DataItem $dataItem ) {

		if ( !$dataItem instanceof DIBlob ) {
			$dataItem = new DIBlob( $dataItem->getSortKey() );
		}

		parent::addResourceValue( $expData, $property, $dataItem );

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

		$sort = Collator::singleton()->armor(
			Collator::singleton()->getSortKey( $dataItem->getSortKey() )
		);

		$expData->addPropertyObjectValue(
			$this->exporter->getSpecialNsResource( 'swivt', 'sort' ),
			new ExpLiteral(
				$sort,
				'http://www.w3.org/2001/XMLSchema#string'
			)
		);
	}

}