2016-10-17 31 views
1

我正在使用Semantic MediaWiki,並且還在開發另一個自定義擴展。我想直接在PHP中查詢語義值;即類似的東西:我可以直接在PHP中查詢MediaWiki語義值(SMW)嗎?

但是,我似乎無法找到任何可能的文件。我知道有一個Ask API,但是這個文檔只使用URL進行查詢,而不是直接的PHP查詢。我也知道我可以通過inline queries在頁面內包含「詢問」引用。但是,我想要做的是直接在我的自定義擴展的PHP內查詢語義值。

有誰知道我是否可以直接從PHP查詢語義值?

回答

0

通過查看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; 
    } 
} 
相關問題