【问题标题】:Can I query a MediaWiki semantic value (SMW) directly in PHP?我可以直接在 PHP 中查询 MediaWiki 语义值 (SMW) 吗?
【发布时间】:2016-10-17 11:18:36
【问题描述】:

我正在使用 Semantic MediaWiki,并且还在开发另一个自定义扩展。我想直接在 PHP 中查询语义值;即,类似:

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

但是,我似乎找不到任何可能的文档。我知道有一个Ask API,但该文档仅使用 URL 进行查询,而不是直接的 PHP 查询。我也知道我可以通过inline queries 在页面内包含“询问”参考。但是,我想做的是直接在我的自定义扩展的 PHP 中查询语义值。

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

【问题讨论】:

    标签: php mediawiki semantic-mediawiki


    【解决方案1】:

    您也可以使用 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查看更多信息

    【讨论】:

    • 谢谢——这就是我要找的。​​span>
    【解决方案2】:

    通过查看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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多