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

namespace SMW\PropertyAnnotators;

use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\PageInfo;
use SMW\PropertyAnnotator;
use SMW\PropertyRegistry;
use SMWDataItem as DataItem;
use SMWDIBlob as DIBlob;
use SMWDIBoolean as DIBoolean;
use SMWDITime as DITime;

/**
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class PredefinedPropertyAnnotator extends PropertyAnnotatorDecorator {

	/**
	 * @var PageInfo
	 */
	private $pageInfo;

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

	/**
	 * @since 1.9
	 *
	 * @param PropertyAnnotator $propertyAnnotator
	 * @param PageInfo $pageInfo
	 */
	public function __construct( PropertyAnnotator $propertyAnnotator, PageInfo $pageInfo ) {
		parent::__construct( $propertyAnnotator );
		$this->pageInfo = $pageInfo;
	}

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

	protected function addPropertyValues() {

		$cachedProperties = [];

		foreach ( $this->predefinedPropertyList as $propertyId ) {

			if ( $this->isRegisteredPropertyId( $propertyId, $cachedProperties ) ) {
				continue;
			}

			$propertyDI = new DIProperty( $propertyId );

			if ( $this->getSemanticData()->getPropertyValues( $propertyDI ) !== [] ) {
				$cachedProperties[$propertyId] = true;
				continue;
			}

			$dataItem = $this->createDataItemByPropertyId( $propertyId );

			if ( $dataItem instanceof DataItem ) {
				$cachedProperties[$propertyId] = true;
				$this->getSemanticData()->addPropertyObjectValue( $propertyDI, $dataItem );
			}
		}
	}

	protected function isRegisteredPropertyId( $propertyId, $cachedProperties ) {
		return ( PropertyRegistry::getInstance()->getPropertyValueTypeById( $propertyId ) === '' ) ||
			array_key_exists( $propertyId, $cachedProperties );
	}

	protected function createDataItemByPropertyId( $propertyId ) {

		$dataItem = null;

		switch ( $propertyId ) {
			case DIProperty::TYPE_MODIFICATION_DATE :
				$dataItem = DITime::newFromTimestamp( $this->pageInfo->getModificationDate() );
				break;
			case DIProperty::TYPE_CREATION_DATE :
				$dataItem = DITime::newFromTimestamp( $this->pageInfo->getCreationDate() );
				break;
			case DIProperty::TYPE_NEW_PAGE :
				$dataItem = new DIBoolean( $this->pageInfo->isNewPage() );
				break;
			case DIProperty::TYPE_LAST_EDITOR :
				$dataItem = $this->pageInfo->getLastEditor() ? DIWikiPage::newFromTitle( $this->pageInfo->getLastEditor() ) : null;
				break;
			case DIProperty::TYPE_MEDIA : // @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength
				$dataItem = $this->pageInfo->isFilePage() && $this->pageInfo->getMediaType() !== '' && $this->pageInfo->getMediaType() !== null ? new DIBlob( $this->pageInfo->getMediaType() ) : null;
				// @codingStandardsIgnoreEnd
				break;
			case DIProperty::TYPE_MIME : // @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength
				$dataItem = $this->pageInfo->isFilePage() && $this->pageInfo->getMimeType() !== '' && $this->pageInfo->getMimeType() !== null  ? new DIBlob( $this->pageInfo->getMimeType() ) : null;
				// @codingStandardsIgnoreEnd
				break;
		}

		return $dataItem;
	}

}