-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMWListProps.classes.php
executable file
·150 lines (112 loc) · 3.18 KB
/
SMWListProps.classes.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
if ( !defined( 'MEDIAWIKI' ) ) {
echo 'Not a valid entry point';
exit( 1 );
}
if ( !defined( 'SMW_VERSION' ) ) {
echo 'This extension requires Semantic MediaWiki to be installed.';
exit( 1 );
}
/**
* This class handles the search for Props.
*/
class SMWListProps {
public static function executeGetListProps( $parser, $frame, $args ) {
//Default current page
$pagetitle = $parser->getTitle();
// Let's get page
if ( isset( $args[0] ) && $args[0]!='' ) {
$title = trim( $frame->expand( $args[0] ) );
$pagetitle = Title::newFromText( $title );
}
if ( ! $pagetitle ) {
return '';
}
$smwpagetitle = SMWDIWikiPage::newFromTitle( $pagetitle );
$data = smwfGetStore()->getSemanticData( $smwpagetitle );
$diProperties = $data->getProperties();
$listprops = array();
// We remove param 0
array_shift($args);
$params = array();
// We parametrize
foreach ( $args as $arg ) {
array_push( $params, trim( $frame->expand( $arg ) ) );
}
// Options
$wikionly = false;
$values = false;
$raw = false;
$sep = ",";
$sepvalues = "-";
$eqvalues = ":";
// If value in arrays
if ( in_array ( 'wikionly' , $params ) ) {
$wikionly = true;
}
if ( in_array ( 'values' , $params ) ) {
$values = true;
}
if ( in_array ( 'raw' , $params ) ) {
$raw = true;
}
// Detect array
$separr = preg_grep( "/^sep\=/", $params );
$sepvaluesarr = preg_grep( "/^sepvalues\=/", $params );
$eqvaluesarr = preg_grep( "/^eqvalues\=/", $params );
// We assume first match
if ( count( $separr ) > 0 ) {
$seppr = $separr[0];
$vals = explode( "=", $seppr, 2 );
if ( ! empty( $vals[1] ) ) {
$sep = $vals[1];
}
}
if ( count( $sepvaluesarr ) > 0 ) {
$sepvaluespr = $sepvaluesarr[0];
$vals = explode( "=", $sepvaluespr, 2 );
if ( ! empty( $vals[1] ) ) {
$sepvalues = $vals[1];
}
}
if ( count( $eqvaluesarr ) > 0 ) {
$eqvaluespr = $eqvaluesarr[0];
$vals = explode( "=", $eqvaluespr, 2 );
if ( ! empty( $vals[1] ) ) {
$eqvalues = $vals[1];
}
}
foreach ( $diProperties as $diProperty ) {
// Check if user defined
if ( $wikionly == true && !$diProperty->isUserDefined() ) {
continue;
}
$label = $diProperty->getLabel();
if ( !empty($label) ) {
if ( $values == true ) {
$result = self::getPropValue( $pagetitle->getFullText(), $label, $raw );
if ( is_array( $result ) ) {
$strarray = implode( $sepvalues, $result );
} else {
$strarray = $result;
}
$label = $label.$eqvalues.$strarray;
}
array_push( $listprops, $label );
}
}
return( implode( $sep, $listprops ) );
}
/** Convenient for getting value **/
static function getPropValue ( $title_text, $query_word, $raw = false ) {
// Ensure proper query
$query_word = str_replace(" ", "_", $query_word);
if ( $raw ) {
$query_word = $query_word."#";
}
// https://semantic-mediawiki.org/wiki/User:Yury_Katkov/programming_examples
$params = array ("[[$title_text]]", "?$query_word", "mainlabel=-", "headers=hide" );
$result = SMWQueryProcessor::getResultFromFunctionParams( $params, SMW_OUTPUT_WIKI );
return $result;
}
}