summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Elastic/Indexer/AttachmentAnnotator.php
blob: 63fb7684931fcd1967376560df96502a74bb5be3 (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
<?php

namespace SMW\Elastic\Indexer;

use SMW\DataItemFactory;
use SMW\DIProperty;
use SMW\PropertyAnnotator;
use SMWContainerSemanticData as ContainerSemanticData;
use SMWDIContainer as DIContainer;
use SMWDITime as DITime;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class AttachmentAnnotator implements PropertyAnnotator {

	/**
	 * @var ContainerSemanticData
	 */
	private $containerSemanticData;

	/**
	 * @var []
	 */
	private $doc = [];

	/**
	 * @since 3.0
	 *
	 * @param ContainerSemanticData $containerSemanticData
	 * @param array $doc
	 */
	public function __construct( ContainerSemanticData $containerSemanticData, array $doc ) {
		$this->containerSemanticData = $containerSemanticData;
		$this->doc = $doc;
	}

	/**
	 * @since 3.0
	 *
	 * @return DIProperty
	 */
	public function getProperty() {
		return new DIProperty( '_FILE_ATTCH' );
	}

	/**
	 * @since 3.0
	 *
	 * @return DIContainer
	 */
	public function getContainer() {
		return new DIContainer( $this->containerSemanticData );
	}

	/**
	 * @see PropertyAnnotator::getSemanticData
	 * @since 3.0
	 *
	 * @return SemanticData
	 */
	public function getSemanticData() {
		return $this->containerSemanticData;
	}

	/**
	 * @see PropertyAnnotator::addAnnotation
	 * @since 3.0
	 *
	 * @return PropertyAnnotator
	 */
	public function addAnnotation() {

		$dataItemFactory = new DataItemFactory();

		// @see https://www.elastic.co/guide/en/elasticsearch/plugins/master/using-ingest-attachment.html
		if ( isset( $this->doc['_source']['attachment']['date'] ) ) {
			if ( ( $dataItem = DITime::newFromTimestamp( $this->doc['_source']['attachment']['date'] ) ) instanceof DITime ) {
				$this->containerSemanticData->addPropertyObjectValue(
					$dataItemFactory->newDIProperty( '_CONT_DATE' ),
					$dataItem
				);
			}
		}

		if ( isset( $this->doc['_source']['attachment']['content_type'] ) ) {
			$this->containerSemanticData->addPropertyObjectValue(
				$dataItemFactory->newDIProperty( '_CONT_TYPE' ),
				$dataItemFactory->newDIBlob( $this->doc['_source']['attachment']['content_type'] )
			);
		}

		if ( isset( $this->doc['_source']['attachment']['author'] ) ) {
			$this->containerSemanticData->addPropertyObjectValue(
				$dataItemFactory->newDIProperty( '_CONT_AUTHOR' ),
				$dataItemFactory->newDIBlob( $this->doc['_source']['attachment']['author'] )
			);
		}

		if ( isset( $this->doc['_source']['attachment']['title'] ) ) {
			$this->containerSemanticData->addPropertyObjectValue(
				$dataItemFactory->newDIProperty( '_CONT_TITLE' ),
				$dataItemFactory->newDIBlob( $this->doc['_source']['attachment']['title'] )
			);
		}

		if ( isset( $this->doc['_source']['attachment']['language'] ) ) {
			$this->containerSemanticData->addPropertyObjectValue(
				$dataItemFactory->newDIProperty( '_CONT_LANG' ),
				$dataItemFactory->newDIBlob( $this->doc['_source']['attachment']['language'] )
			);
		}

		if ( isset( $this->doc['_source']['attachment']['content_length'] ) ) {
			$this->containerSemanticData->addPropertyObjectValue(
				$dataItemFactory->newDIProperty( '_CONT_LEN' ),
				$dataItemFactory->newDINumber( intval( $this->doc['_source']['attachment']['content_length'] ) )
			);
		}

		if ( isset( $this->doc['_source']['attachment']['keywords'] ) ) {
			$this->containerSemanticData->addPropertyObjectValue(
				$dataItemFactory->newDIProperty( '_CONT_KEYW' ),
				$dataItemFactory->newDINumber( intval( $this->doc['_source']['attachment']['keywords'] ) )
			);
		}

		return $this;
	}

}