summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_PropertyList.php
blob: 7c255aaaedfa6c0215489feae540b4460e53ea5d (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
<?php

/**
 * @ingroup SMWDataValues
 */

/**
 * This datavalue implements special processing suitable for defining the list
 * of properties that is required for SMWRecordValue objects. The input is a
 * plain semicolon-separated list of property names, optionally with the
 * namespace prefix.
 *
 * @author Markus Krötzsch
 * @ingroup SMWDataValues
 */
class SMWPropertyListValue extends SMWDataValue {
	/**
	 * List of properte data items that are stored.
	 * @var array of SMWDIProperty
	 */
	protected $m_diProperties;

	protected function parseUserValue( $value ) {
		global $wgContLang;

		$this->m_diProperties = [];
		$stringValue = '';
		$valueList = preg_split( '/[\s]*;[\s]*/u', trim( $value ) );
		foreach ( $valueList as $propertyName ) {
			$propertyNameParts = explode( ':', $propertyName, 2 );
			if ( count( $propertyNameParts ) > 1 ) {
				$namespace = smwfNormalTitleText( $propertyNameParts[0] );
				$propertyName = $propertyNameParts[1];
				$propertyNamespace = $wgContLang->getNsText( SMW_NS_PROPERTY );
				if ( $namespace != $propertyNamespace ) {
					$this->addErrorMsg( [ 'smw_wrong_namespace', $propertyNamespace ] );
				}
			}

			$propertyName = smwfNormalTitleText( $propertyName );

			try {
				$diProperty = SMW\DIProperty::newFromUserLabel( $propertyName );
			} catch ( SMWDataItemException $e ) {
				$diProperty = new SMW\DIProperty( 'Error' );
				$this->addErrorMsg( [ 'smw_noproperty', $propertyName ] );
			}

			$this->m_diProperties[] = $diProperty;
			$stringValue .= ( $stringValue ? ';' : '' ) . $diProperty->getKey();
		}

		$this->m_dataitem = new SMWDIBlob( $stringValue );
	}

	/**
	 * @see SMWDataValue::loadDataItem()
	 *
	 * @param $dataitem SMWDataItem
	 *
	 * @return boolean
	 */
	protected function loadDataItem( SMWDataItem $dataItem ) {

		if ( !$dataItem instanceof SMWDIBlob ) {
			return false;
		}

		$this->m_dataitem = $dataItem;
		$this->m_diProperties = [];

		foreach ( explode( ';', $dataItem->getString() ) as $propertyKey ) {
			$property = null;

			try {
				$property = new SMW\DIProperty( $propertyKey );
			} catch ( SMWDataItemException $e ) {
				$property = new SMW\DIProperty( 'Error' );
				$this->addErrorMsg( [ 'smw-datavalue-propertylist-invalid-property-key', $dataItem->getString(), $propertyKey ] );
			}

			if ( $property instanceof SMWDIProperty ) {
				 // Find a possible redirect
				$this->m_diProperties[] = $property->getRedirectTarget();
			}
		}

		$this->m_caption = false;

		return true;
	}

	public function getShortWikiText( $linked = null ) {
		return ( $this->m_caption !== false ) ?  $this->m_caption : $this->makeOutputText( 2, $linked );
	}

	public function getShortHTMLText( $linker = null ) {
		return ( $this->m_caption !== false ) ? $this->m_caption : $this->makeOutputText( 3, $linker );
	}

	public function getLongWikiText( $linked = null ) {
		return $this->makeOutputText( 2, $linked );
	}

	public function getLongHTMLText( $linker = null ) {
		return $this->makeOutputText( 3, $linker );
	}

	public function getWikiValue() {
		return $this->makeOutputText( 4 );
	}

	public function getPropertyDataItems() {
		return $this->m_diProperties;
	}

////// Internal helper functions

	protected function makeOutputText( $type, $linker = null ) {
		if ( !$this->isValid() ) {
			return ( ( $type == 0 ) || ( $type == 1 ) ) ? '' : $this->getErrorText();
		}
		$result = '';
		$sep = ( $type == 4 ) ? '; ' : ', ';
		foreach ( $this->m_diProperties as $diProperty ) {
			if ( $result !== '' ) {
				$result .= $sep;
			}
			$propertyValue = \SMW\DataValueFactory::getInstance()->newDataValueByItem( $diProperty, null );
			$result .= $this->makeValueOutputText( $type, $propertyValue, $linker );
		}
		return $result;
	}

	protected function makeValueOutputText( $type, $propertyValue, $linker ) {
		switch ( $type ) {
			case 0:
			return $propertyValue->getShortWikiText( $linker );
			case 1:
			return $propertyValue->getShortHTMLText( $linker );
			case 2:
			return $propertyValue->getLongWikiText( $linker );
			case 3:
			return $propertyValue->getLongHTMLText( $linker );
			case 4:
			return $propertyValue->getWikiValue();
		}
	}
}