summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/PropertyAnnotators/TranslationPropertyAnnotator.php
blob: 2eb6fbab82099cf5e2e6e49fdad30882435ec767 (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
<?php

namespace SMW\PropertyAnnotators;

use SMW\PropertyAnnotator;
use SMW\DataModel\ContainerSemanticData;
use Title;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class TranslationPropertyAnnotator extends PropertyAnnotatorDecorator {

	/**
	 * @var array|null
	 */
	private $translation;

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

	/**
	 * @since 3.0
	 *
	 * @param PropertyAnnotator $propertyAnnotator
	 * @param array|null $translation
	 */
	public function __construct( PropertyAnnotator $propertyAnnotator, $translation ) {
		parent::__construct( $propertyAnnotator );
		$this->translation = $translation;
	}

	/**
	 * @since 3.0
	 *
	 * @param array $predefinedPropertyList
	 */
	public function setPredefinedPropertyList( array $predefinedPropertyList ) {
		$this->predefinedPropertyList = array_flip( $predefinedPropertyList );
	}

	protected function addPropertyValues() {

		// Expected identifiers, @see https://gerrit.wikimedia.org/r/387548
		if ( !is_array( $this->translation ) || !isset( $this->predefinedPropertyList['_TRANS'] ) ) {
			return;
		}

		$containerSemanticData = null;

		if ( isset( $this->translation['languagecode'] ) ) {
			$languageCode = $this->translation['languagecode'];
			$containerSemanticData = $this->newContainerSemanticData( $languageCode );

			// Translation.Language code
			$containerSemanticData->addPropertyObjectValue(
				$this->dataItemFactory->newDIProperty( '_LCODE' ),
				$this->dataItemFactory->newDIBlob( $languageCode )
			);
		}

		if ( isset( $this->translation['sourcepagetitle'] ) && $this->translation['sourcepagetitle'] instanceof Title ) {
			// Translation.Translation source
			$containerSemanticData->addPropertyObjectValue(
				$this->dataItemFactory->newDIProperty( '_TRANS_SOURCE' ),
				$this->dataItemFactory->newDIWikiPage( $this->translation['sourcepagetitle'] )
			);
		}

		if ( isset( $this->translation['messagegroupid'] ) ) {
			// Translation.Translation group
			$containerSemanticData->addPropertyObjectValue(
				$this->dataItemFactory->newDIProperty( '_TRANS_GROUP' ),
				$this->dataItemFactory->newDIBlob( $this->translation['messagegroupid'] )
			);
		}

		if ( $containerSemanticData !== null ) {
			$this->getSemanticData()->addPropertyObjectValue(
				$this->dataItemFactory->newDIProperty( '_TRANS' ),
				$this->dataItemFactory->newDIContainer( $containerSemanticData )
			);
		}
	}

	private function newContainerSemanticData( $languageCode ) {

		$dataItem = $this->getSemanticData()->getSubject();
		$subobjectName = 'trans.' . $languageCode;

		$subject = $this->dataItemFactory->newDIWikiPage(
			$dataItem->getDBkey(),
			$dataItem->getNamespace(),
			$dataItem->getInterwiki(),
			$subobjectName
		);

		return $this->dataItemFactory->newContainerSemanticData( $subject );
	}

}