/
SMWUtil.php
109 lines (99 loc) · 2.62 KB
/
SMWUtil.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
*
* Utility methods interfacing with Semantic Mediawiki.
* @author jason
*
*/
class SMWUtil
{
/**
*
* Load all semantic properties of a page as a hash
* @param String $pagename
* @param Integer $namespace
* @param Boolean $normalizeTitle
* @return an associative array with the property name as key and property value as value.
*/
public static function &loadSemanticProperties($pagename, $namespace=NS_MAIN, $normalizeTitle=true)
{
//-----normalize title
$data=null;
if ($normalizeTitle)
{
$title=MWUtil::normalizePageTitle($pagename, false);
$di=SMWDIWikiPage::newFromTitle( $title );
$data = smwfGetStore()->getSemanticData($di);
} else
{
$di=new SMWDIWikiPage( $pagename, $namespace,'');
$data = smwfGetStore()->getSemanticData($di);
}
$valuehash=array();
$diProperties = $data->getProperties();
foreach ( $diProperties as $diProperty ) {
$dvProperty = SMWDataValueFactory::newDataItemValue( $diProperty, null );
$name=null;
if ( $dvProperty->isVisible() ) {
$name = $diProperty->getLabel();
} elseif ( $diProperty->getKey() == '_INST' ) {
$name = 'Categories' ;
} else {
continue; // skip this line
}
if (!$name)
{
continue;
}
$values = $data->getPropertyValues( $diProperty );
$vs=array();
foreach ( $values as $di )
{
$dv = SMWDataValueFactory::newDataItemValue( $di, $diProperty );
$vs[]=$dv->getWikiValue();
}
if (count($vs)==1)
{
$valuehash[$name]=$vs[0];
} else
{
$valuehash[$name]=$vs;
}
}
#error_log(print_r($valuehash, true));
return $valuehash;
}
/**
*
* Retrieve all internal objects and their properties.
* @param String $pagename
* @param String $internalproperty
* @return an array. Each element is an associative array and represents an internal object.
*
* E.G: {{#ptest:Chross|Is a parameter in an application}}
*
*/
public static function getSemanticInternalObjects($pagename, $internalproperty, $namespace=NS_MAIN)
{
$params=array("[[{$internalproperty}::{$pagename}]]", "format=list", "link=none", "headers=hide", "sep=;","limit=5000");
$result=SMWQueryProcessor::getResultFromFunctionParams($params,SMW_OUTPUT_WIKI);
$result=trim($result);
$sios=array();
if ($result)
{
$sios=explode(";", $result);
}
$ret=array();
foreach ($sios as $sio)
{
#remove namespace prefix.
$sio=preg_replace("/^[^:]+:/", "", $sio);
$sio=str_replace(" ", "_", $sio);
#remove fragment to -
#$sio=str_replace("#", "-23", $sio);
array_push($ret, self::loadSemanticProperties($sio, $namespace, true));
}
return $ret;
}
}
?>