summaryrefslogtreecommitdiff
blob: afc03ab6c4c619e4f6a7ae03de3cac789d044e87 (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
/**
 * @file
 * @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 = array();
		$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->addError( wfMessage( 'smw_wrong_namespace', $propertyNamespace )->inContentLanguage()->text() );
				}
			}

			$propertyName = smwfNormalTitleText( $propertyName );

			try {
				$diProperty = SMWDIProperty::newFromUserLabel( $propertyName );
			} catch ( SMWDataItemException $e ) {
				$diProperty = new SMWDIProperty( 'Error' );
				$this->addError( wfMessage( 'smw_noproperty', $propertyName )->inContentLanguage()->text() );
			}

			$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 ) {
			$this->m_dataitem = $dataItem;
			$this->m_diProperties = array();

			foreach ( explode( ';', $dataItem->getString() ) as $propertyKey ) {
				try {
					$this->m_diProperties[] = new SMWDIProperty( $propertyKey );
				} catch ( SMWDataItemException $e ) {
					$this->m_diProperties[] = new SMWDIProperty( 'Error' );
					$this->addError( wfMessage( 'smw_parseerror' )->inContentLanguage()->text() );
				}
			}

			$this->m_caption = false;

			return true;
		} else {
			return false;
		}
	}

	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()->newDataItemValue( $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();
		}
	}
}