1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SFS; |
4
|
|
|
|
5
|
|
|
use Parser; |
6
|
|
|
use SMWQueryProcessor as QueryProcessor; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @license GNU GPL v2+ |
11
|
|
|
* @since 1.3 |
12
|
|
|
* |
13
|
|
|
* @author Jason Zhang |
14
|
|
|
* @author Toni Hermoso Pulido |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
class ApiSemanticFormsSelectRequestProcessor { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Parser |
22
|
|
|
*/ |
23
|
|
|
private $parser; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var boolean |
27
|
|
|
*/ |
28
|
|
|
private $debugFlag = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @since 1.3 |
32
|
|
|
* |
33
|
|
|
* @param Parser $parser |
34
|
|
|
*/ |
35
|
|
|
public function __construct( Parser $parser ) { |
36
|
|
|
$this->parser = $parser; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @since 1.3 |
41
|
|
|
* |
42
|
|
|
* @param boolean $debugFlag |
43
|
|
|
*/ |
44
|
|
|
public function setDebugFlag( $debugFlag ) { |
45
|
|
|
$this->debugFlag = $debugFlag; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @since 1.3 |
50
|
|
|
* |
51
|
|
|
* @param array $parameters |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getJsonDecodedResultValuesForRequestParameters( array $parameters ) { |
56
|
|
|
|
57
|
|
|
if ( !isset( $parameters['query'] ) || !isset( $parameters['sep'] ) ) { |
58
|
|
|
throw new InvalidArgumentException( 'Missing an query parameter' ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->parser->firstCallInit(); |
62
|
|
|
$json = array(); |
|
|
|
|
63
|
|
|
|
64
|
|
|
if ( isset( $parameters['approach'] ) && $parameters['approach'] == 'smw' ) { |
65
|
|
|
$json = $this->doProcessQueryFor( $parameters['query'], $parameters['sep'] ); |
66
|
|
|
} else { |
67
|
|
|
$json = $this->doProcessFunctionFor( $parameters['query'], $parameters['sep'] ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// I have no idea why we first encode and and then decode here?? |
71
|
|
|
|
72
|
|
|
return json_decode( $json ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function doProcessQueryFor( $query, $sep = "," ) { |
76
|
|
|
|
77
|
|
|
$query = str_replace( |
78
|
|
|
array( "<", ">", "sep=;" ), |
79
|
|
|
array( "<", ">", "sep={$sep};" ), |
80
|
|
|
$query |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$params = explode( ";", $query ); |
84
|
|
|
$f = str_replace( ";", "|", $params[0] ); |
85
|
|
|
|
86
|
|
|
$params[0] = $this->parser->replaceVariables( $f ); |
87
|
|
|
|
88
|
|
|
if ( $this->debugFlag ) { |
89
|
|
|
error_log( implode( "|", $params ) ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$values = $this->getFormattedValuesFrom( |
93
|
|
|
$sep, |
94
|
|
|
QueryProcessor::getResultFromFunctionParams( $params, SMW_OUTPUT_WIKI ) |
|
|
|
|
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return json_encode( array( |
98
|
|
|
"values" => $values, |
99
|
|
|
"count" => count( $values ) |
100
|
|
|
) ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function doProcessFunctionFor( $query, $sep = "," ) { |
104
|
|
|
|
105
|
|
|
$query = str_replace( |
106
|
|
|
array( "<", ">", "sep=;" ), |
107
|
|
|
array( "<", ">", "sep={$sep};" ), |
108
|
|
|
$query |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$f = str_replace( ";", "|", $query ); |
112
|
|
|
|
113
|
|
|
if ( $this->debugFlag ) { |
114
|
|
|
error_log( $f ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$values = $this->getFormattedValuesFrom( |
118
|
|
|
$sep, |
119
|
|
|
$this->parser->replaceVariables( $f ) |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return json_encode( array( |
123
|
|
|
"values" => $values, |
124
|
|
|
"count" => count( $values ) |
125
|
|
|
) ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function getFormattedValuesFrom( $sep, $values ) { |
129
|
|
|
|
130
|
|
|
if ( strpos( $values, $sep ) === false ) { |
131
|
|
|
return array( $values ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$values = explode( $sep, $values ); |
135
|
|
|
$values = array_map( "trim", $values ); |
136
|
|
|
$values = array_unique( $values ); |
137
|
|
|
|
138
|
|
|
sort( $values ); |
139
|
|
|
array_unshift( $values, "" ); |
140
|
|
|
|
141
|
|
|
return $values; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.