通过查看语义标题扩展的方式,我能够编写一个函数来完成我需要的操作:
/**
* Given a wiki page DB key and a Semantic MediaWiki property name, get
* the value for that page.
*
* Remarks: Assumes that the property is of type "string" or "blob", and that
* there is only one value for that page/property combination.
*
* @param string $dbKey The MediaWiki DB key for the page (i.e., "Test_Page")
* @param string $propertyLabel The property label used to set the Semantic MediaWiki property
* @return string The property value, or NULL if none exists
*/
static function getSemanticProperty($dbKey, $propertyLabel) {
// Use Semantic MediaWiki code to properly retrieve the value
$page = SMWDIWikiPage::newFromTitle( Title::newFromDBkey($dbKey) );
$store = \SMW\StoreFactory::getStore();
$data = $store->getSemanticData( $page );
$property = SMWDIProperty::newFromUserLabel( $propertyLabel );
$values = $data->getPropertyValues( $property );
if (count($values) > 0) {
$value = array_shift( $values );
if ( $value->getDIType() == SMWDataItem::TYPE_STRING ||
$value->getDIType() == SMWDataItem::TYPE_BLOB ) {
return $value->getString();
}
} else {
return null;
}
}