gplx/mediawiki/extensions/SemanticMediaWiki/includes/Subobject.php

185 lines
3.4 KiB
PHP

<?php
namespace SMW;
use InvalidArgumentException;
use MediaWiki\Title\Title;
use SMW\DataModel\ContainerSemanticData;
use SMW\Exception\SubSemanticDataException;
use SMWDataValue;
use SMWDIContainer;
/**
* @see http://www.semantic-mediawiki.org/wiki/Help:Subobject
*
* @license GPL-2.0-or-later
* @since 1.9
*
* @author mwjames
*/
class Subobject {
/**
* @var Title
*/
protected $title;
/**
* @var ContainerSemanticData
*/
protected $semanticData;
/**
* @var array
*/
protected $errors = [];
/**
* @since 1.9
*
* @param Title $title
*/
public function __construct( Title $title ) {
$this->title = $title;
}
/**
* Returns the Title object
*
* @since 1.9
*
* @return Title
*/
public function getTitle() {
return $this->title;
}
/**
* @since 2.1
*
* @return DIWikiPage
*/
public function getSubject() {
return $this->getSemanticData()->getSubject();
}
/**
* @since 2.1
*
* @return string
*/
public function getSubobjectId() {
return $this->getSemanticData()->getSubject()->getSubobjectName();
}
/**
* @since 1.9
*
* @return array
*/
public function getErrors() {
return $this->errors;
}
/**
* @since 1.9
*
* @param array|string $error
*/
public function addError( $error ) {
if ( is_string( $error ) ) {
$error = [ md5( $error ) => $error ];
}
// Preserve the keys, avoid using array_merge to avert a possible
// Fatal error: Allowed memory size of ... bytes exhausted ... Subobject.php on line 89
$this->errors += $error;
}
/**
* @since 2.0
*
* @param string $identifier
*
* @return self
* @throws InvalidArgumentException
*/
public function setEmptyContainerForId( $identifier ) {
if ( $identifier === '' ) {
throw new InvalidArgumentException( 'Expected a valid (non-empty) indentifier' );
}
$subWikiPage = new DIWikiPage(
$this->title->getDBkey(),
$this->title->getNamespace(),
$this->title->getInterwiki(),
$identifier
);
$this->semanticData = new ContainerSemanticData( $subWikiPage );
return $this;
}
/**
* @deprecated since 2.0
*/
public function setSemanticData( $identifier ) {
$this->setEmptyContainerForId( $identifier );
}
/**
* Returns semantic data container for a subobject
*
* @since 1.9
*
* @return ContainerSemanticData
*/
public function getSemanticData() {
if ( !( $this->semanticData instanceof ContainerSemanticData ) ) {
throw new SubSemanticDataException( 'The semantic data container is not initialized' );
}
return $this->semanticData;
}
/**
* Returns the property data item for the subobject
*
* @since 1.9
*
* @return DIProperty
*/
public function getProperty() {
return new DIProperty( DIProperty::TYPE_SUBOBJECT );
}
/**
* Returns the container data item for the subobject
*
* @since 1.9
*
* @return SMWDIContainer
*/
public function getContainer() {
return new SMWDIContainer( $this->getSemanticData() );
}
/**
* @since 1.9
*
* @param SMWDataValue $dataValue
*
* @throws SubSemanticDataException
*/
public function addDataValue( SMWDataValue $dataValue ) {
if ( !( $this->semanticData instanceof ContainerSemanticData ) ) {
throw new SubSemanticDataException( 'The semantic data container is not initialized' );
}
$this->semanticData->addDataValue( $dataValue );
$this->addError( $this->semanticData->getErrors() );
}
}