我可以查询一个MediaWiki语义值(SMW)直接在PHP


Can I query a MediaWiki semantic value (SMW) directly in PHP?

我正在使用Semantic MediaWiki,我也在开发另一个自定义扩展。我想查询语义值直接在PHP;例如:

SemanticMediaWiki::ask('PAGE_NAME', 'FIELD_NAME')

然而,我似乎找不到任何文档证明这是可能的。我知道有一个Ask API,但它只记录使用URL的查询,而不是直接的PHP查询。我还知道可以通过内联查询在页面中包含"ask"引用。然而,我想做的是在我的自定义扩展的PHP中直接查询语义值。

有谁知道我是否可以直接从PHP查询语义值?

您也可以使用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 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;
    }
}