I am using Semantic MediaWiki and am also developing another custom extension. I would like to query semantic values directly in PHP; ie, something like:
SemanticMediaWiki::ask('PAGE_NAME', 'FIELD_NAME')
However, I cannot seem to find any documentation of this being possible. I know there is an Ask API , but this documents only querying using a URL, not a direct PHP query. I also know that I can include "ask" references inside pages via inline queries . However, what I want to do is query semantic values directly inside the PHP of my custom extension.
Does anyone know whether I can directly query a semantic value from PHP?
You can also use https://github.com/vedmaka/SemanticQueryInterface - it is a wrapper around SMW internal API which allows you to do things like that:
$results = $sqi->condition("My property", "My value")->toArray();
See more at https://www.mediawiki.org/wiki/User:Vedmaka/Semantic_Query_Interface
By looking at the way the Semantic Title extension does it, I was able to write a function to do what I needed:
/**
* 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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.