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