@@ -31,9 +31,11 @@ public function execute() {
3131 // get request parameters
3232 $ requestParams = $ this ->extractRequestParams ();
3333
34+ $ data = json_decode ( $ requestParams ['data ' ], true );
35+
3436 // @see https://datatables.net/reference/option/ajax
35- $ datatableData = json_decode ( $ requestParams [ ' datatable ' ], true ) ;
36- $ settings = json_decode ( $ requestParams ['settings ' ], true ) ;
37+ $ datatableData = $ data [ ' datatableData ' ] ;
38+ $ settings = $ data ['settings ' ];
3739
3840 if ( empty ( $ datatableData ['length ' ] ) ) {
3941 $ datatableData ['length ' ] = $ settings ['defer-each ' ];
@@ -59,15 +61,14 @@ public function execute() {
5961 $ parameters [$ def ->getName ()] = $ def ->getDefault ();
6062 }
6163
62- $ printoutsRaw = json_decode ( $ requestParams ['printouts ' ], true ) ;
64+ $ printoutsRaw = $ data ['printouts ' ];
6365
6466 // add/set specific parameters for this call
6567 $ parameters = array_merge (
6668 $ parameters ,
6769 [
6870 // *** important !!
6971 'format ' => 'datatables ' ,
70-
7172 "apicall " => "apicall " ,
7273 // @see https://datatables.net/manual/server-side
7374 // array length will be sliced client side if greater
@@ -104,10 +105,10 @@ public function execute() {
104105 foreach ( $ printoutsRaw as $ printoutData ) {
105106
106107 // create property from property key
107- if ( $ printoutData [0 ] === SMWPrintRequest::PRINT_PROP ) {
108- $ data = $ dataValueFactory ->newPropertyValueByLabel ( $ printoutData [1 ] );
108+ if ( $ printoutData [0 ] === SMWPrintRequest::PRINT_PROP ) {
109+ $ data_ = $ dataValueFactory ->newPropertyValueByLabel ( $ printoutData [1 ] );
109110 } else {
110- $ data = null ;
111+ $ data_ = null ;
111112 if ( $ hasMainlabel && trim ( $ parameters ['mainlabel ' ] ) === '- ' ) {
112113 continue ;
113114 }
@@ -118,7 +119,7 @@ public function execute() {
118119 $ printouts [] = new SMWPrintRequest (
119120 $ printoutData [0 ], // mode
120121 $ printoutData [1 ], // (canonical) label
121- $ data , // property name
122+ $ data_ , // property name
122123 $ printoutData [3 ], // output format
123124 $ printoutData [4 ] // parameters
124125 );
@@ -127,8 +128,8 @@ public function execute() {
127128
128129 // SMWQueryProcessor::addThisPrintout( $printouts, $parameters );
129130
130- $ printrequests = json_decode ( $ requestParams ['printrequests ' ], true ) ;
131- $ columnDefs = json_decode ( $ requestParams [ ' columndefs ' ], true ) ;
131+ $ printrequests = $ data ['printrequests ' ];
132+ $ columnDefs = $ data [ ' columnDefs ' ] ;
132133
133134 $ getColumnAttribute = function ( $ label , $ attr ) use ( $ columnDefs ) {
134135 foreach ( $ columnDefs as $ value ) {
@@ -171,13 +172,74 @@ public function execute() {
171172 }
172173 }
173174
175+ // @see https://datatables.net/extensions/searchbuilder/customConditions.html
176+ // @see https://datatables.net/reference/option/searchBuilder.depthLimit
177+ if ( !empty ( $ datatableData ['searchBuilder ' ] ) ) {
178+ $ searchBuilder = [];
179+ foreach ( $ datatableData ['searchBuilder ' ]['criteria ' ] as $ criteria ) {
180+ foreach ( $ printoutsRaw as $ key => $ value ) {
181+ // @FIXME $label isn't simply $value[1] ?
182+ $ printrequest = $ printrequests [$ key ];
183+ $ label = ( $ printrequest ['key ' ] !== '' ? $ value [1 ] : '' );
184+ if ( $ label === $ criteria ['data ' ] ) {
185+
186+ // nested condition, skip for now
187+ if ( !array_key_exists ( 'condition ' , $ criteria ) ) {
188+ continue ;
189+ }
190+ $ v = implode ( $ criteria ['value ' ] );
191+ $ str = ( $ label !== '' ? "$ label:: " : '' );
192+ switch ( $ criteria ['condition ' ] ) {
193+ case '= ' :
194+ $ searchBuilder [] = "[[ {$ str }{$ v }]] " ;
195+ break ;
196+ case '!= ' :
197+ $ searchBuilder [] = "[[ {$ str }!~ $ v]] " ;
198+ break ;
199+ case 'starts ' :
200+ $ searchBuilder [] = "[[ {$ str }~ $ v*]] " ;
201+ break ;
202+ case '!starts ' :
203+ $ searchBuilder [] = "[[ {$ str }!~ $ v*]] " ;
204+ break ;
205+ case 'contains ' :
206+ $ searchBuilder [] = "[[ {$ str }~* $ v*]] " ;
207+ break ;
208+ case '!contains ' :
209+ $ searchBuilder [] = "[[ {$ str }!~* $ v*]] " ;
210+ break ;
211+ case 'ends ' :
212+ $ searchBuilder [] = "[[ {$ str }~* $ v]] " ;
213+ break ;
214+ case '!ends ' :
215+ $ searchBuilder [] = "[[ $ str}!~* $ v]] " ;
216+ break ;
217+ // case 'null':
218+ // break;
219+ case '!null ' :
220+ if ( $ label ) {
221+ $ searchBuilder [] = "[[ $ label::+]] " ;
222+ }
223+ break ;
224+
225+ }
226+ }
227+ }
228+ }
229+ if ( $ datatableData ['searchBuilder ' ]['logic ' ] === 'AND ' ) {
230+ $ queryConjunction = array_merge ( $ queryConjunction , $ searchBuilder );
231+ } else if ( $ datatableData ['searchBuilder ' ]['logic ' ] === 'OR ' ) {
232+ $ queryDisjunction = array_merge ( $ queryDisjunction , $ searchBuilder );
233+ }
234+ }
235+
174236 global $ smwgQMaxSize ;
175237
176238 if ( !count ( $ queryDisjunction ) ) {
177239 $ queryDisjunction = ['' ];
178240 }
179241
180- $ query = $ requestParams [ ' query ' ] . implode ( '' , $ queryConjunction );
242+ $ query = $ data [ ' queryString ' ] . implode ( '' , $ queryConjunction );
181243
182244 $ conditions = array_map ( static function ( $ value ) use ( $ query ) {
183245 return $ query . $ value ;
@@ -188,8 +250,6 @@ public function execute() {
188250
189251 $ queryStr = implode ( 'OR ' , $ conditions );
190252
191- // trigger_error('queryStr ' . $queryStr);
192-
193253 $ log ['queryStr ' ] = $ queryStr ;
194254
195255 $ query = SMWQueryProcessor::createQuery (
@@ -205,7 +265,6 @@ public function execute() {
205265 // $smwgQMaxSize = max( $smwgQMaxSize, $size );
206266 // trigger_error('smwgQMaxSize ' . $smwgQMaxSize);
207267
208-
209268 $ applicationFactory = ServicesFactory::getInstance ();
210269 $ queryEngine = $ applicationFactory ->getStore ();
211270 $ results = $ queryEngine ->getQueryResult ( $ query );
@@ -234,6 +293,8 @@ public function execute() {
234293 'data ' => $ res ,
235294 'recordsTotal ' => $ settings ['count ' ],
236295 'recordsFiltered ' => $ count ,
296+ 'cacheKey ' => $ data ['cacheKey ' ],
297+ 'datalength ' => $ datatableData ['length ' ]
237298 ];
238299
239300 if ( $ settings ['displayLog ' ] ) {
@@ -275,44 +336,10 @@ public function getHelpUrls() {
275336 */
276337 protected function getAllowedParams () {
277338 return [
278- 'query ' => [
279- ApiBase::PARAM_TYPE => 'string ' ,
280- ApiBase::PARAM_REQUIRED => true ,
281- ],
282- 'columndefs ' => [
339+ 'data ' => [
283340 ApiBase::PARAM_TYPE => 'string ' ,
284341 ApiBase::PARAM_REQUIRED => true ,
285- ],
286- 'printouts ' => [
287- ApiBase::PARAM_TYPE => 'string ' ,
288- ApiBase::PARAM_REQUIRED => true ,
289- ],
290- 'printrequests ' => [
291- ApiBase::PARAM_TYPE => 'string ' ,
292- ApiBase::PARAM_REQUIRED => true ,
293- ],
294- 'settings ' => [
295- ApiBase::PARAM_TYPE => 'string ' ,
296- ApiBase::PARAM_REQUIRED => true ,
297- ],
298- 'datatable ' => [
299- ApiBase::PARAM_TYPE => 'string ' ,
300- ApiBase::PARAM_REQUIRED => true ,
301- ],
302- ];
303- }
304-
305- /**
306- * Returns an array of parameter descriptions.
307- * Don't call this function directly: use getFinalParamDescription() to
308- * allow hooks to modify descriptions as needed.
309- *
310- * @return array|bool False on no parameter descriptions
311- */
312- protected function getParamDescription () {
313- return [
314- 'query ' => 'Original query ' ,
315- 'printouts ' => 'Printouts used in the original query ' ,
342+ ]
316343 ];
317344 }
318345
0 commit comments