diff options
author | Alex Legler <alex@a3li.li> | 2014-12-23 17:49:26 +0100 |
---|---|---|
committer | Alex Legler <alex@a3li.li> | 2014-12-23 17:49:26 +0100 |
commit | e352fff59842ca14fbfd81ee1c4a64297bb598c5 (patch) | |
tree | 153f268484aa5cc41cacf912bdce8c4847df222d /SemanticMediaWiki/includes/datavalues/SMW_DV_PropertyList.php | |
download | extensions-e352fff59842ca14fbfd81ee1c4a64297bb598c5.tar.gz extensions-e352fff59842ca14fbfd81ee1c4a64297bb598c5.tar.bz2 extensions-e352fff59842ca14fbfd81ee1c4a64297bb598c5.zip |
Add initial set of additional extensions
Diffstat (limited to 'SemanticMediaWiki/includes/datavalues/SMW_DV_PropertyList.php')
-rw-r--r-- | SemanticMediaWiki/includes/datavalues/SMW_DV_PropertyList.php | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/SemanticMediaWiki/includes/datavalues/SMW_DV_PropertyList.php b/SemanticMediaWiki/includes/datavalues/SMW_DV_PropertyList.php new file mode 100644 index 00000000..afc03ab6 --- /dev/null +++ b/SemanticMediaWiki/includes/datavalues/SMW_DV_PropertyList.php @@ -0,0 +1,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(); + } + } +} |