我可以直接在 PHP 中查询 MediaWiki 语义值 (SMW) 吗?
Can I query a MediaWiki semantic value (SMW) directly in PHP?
我正在使用 Semantic MediaWiki,并且还在开发另一个自定义扩展。我想直接在PHP中查询语义值;即,类似于:
SemanticMediaWiki::ask('PAGE_NAME', 'FIELD_NAME')
但是,我似乎找不到任何可能的文档。我知道有一个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。但是,我想做的是直接在自定义扩展的 PHP 中查询语义值。
有谁知道我是否可以直接从 PHP 查询语义值?
通过查看 Semantic Title 扩展的实现方式,我能够编写一个函数来执行我需要的操作:
/**
* 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;
}
}
您还可以使用 https://github.com/vedmaka/SemanticQueryInterface - 它是 SMW 内部的包装器 API,它允许您执行类似的操作:
$results = $sqi->condition("My property", "My value")->toArray();
在 https://www.mediawiki.org/wiki/User:Vedmaka/Semantic_Query_Interface
查看更多
我正在使用 Semantic MediaWiki,并且还在开发另一个自定义扩展。我想直接在PHP中查询语义值;即,类似于:
SemanticMediaWiki::ask('PAGE_NAME', 'FIELD_NAME')
但是,我似乎找不到任何可能的文档。我知道有一个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。但是,我想做的是直接在自定义扩展的 PHP 中查询语义值。
有谁知道我是否可以直接从 PHP 查询语义值?
通过查看 Semantic Title 扩展的实现方式,我能够编写一个函数来执行我需要的操作:
/**
* 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;
}
}
您还可以使用 https://github.com/vedmaka/SemanticQueryInterface - 它是 SMW 内部的包装器 API,它允许您执行类似的操作:
$results = $sqi->condition("My property", "My value")->toArray();
在 https://www.mediawiki.org/wiki/User:Vedmaka/Semantic_Query_Interface
查看更多