@@ -70,6 +70,92 @@ public function getLog(): array {
7070 return $ this ->searchPanesLog ;
7171 }
7272
73+ private function parseQuerySegment ( $ querySegment ) {
74+ /*
75+ parse QuerySegment in this form:
76+ (
77+ [type] => 1
78+ [depth] =>
79+ [fingerprint] =>
80+ [null] =>
81+ [not] =>
82+ [joinType] =>
83+ [joinTable] => smw_object_ids
84+ [joinfield] => t0.smw_id
85+ [indexField] =>
86+ [from] => INNER JOIN (`smw_fpt_mdat` AS t2 INNER JOIN `smw_di_blob` AS t3 ON t2.s_id=t3.s_id) ON t0.smw_id=t2.s_id
87+ [fromTables] => Array
88+ (
89+ [nestedt2] => Array
90+ (
91+ [t3] => smw_di_blob
92+ [t2] => smw_fpt_mdat
93+ )
94+
95+ )
96+
97+ [joinConditions] => Array
98+ (
99+ [t3] => Array
100+ (
101+ [0] => INNER JOIN
102+ [1] => t2.s_id=t3.s_id
103+ )
104+
105+ [nestedt2] => Array
106+ (
107+ [0] => INNER JOIN
108+ [1] => t0.smw_id=t2.s_id
109+ )
110+
111+ )
112+
113+ [where] => ((t3.p_id=519))
114+ [sortIndexField] =>
115+ [components] => Array
116+ (
117+ )
118+
119+ [alias] => t0
120+ [sortfields] => Array
121+ (
122+ [] => t0.smw_sort
123+ )
124+
125+ [queryNumber] => 0
126+ )
127+ */
128+ $ tables = [
129+ $ querySegment ['alias ' ] => $ querySegment ['joinTable ' ]
130+ ];
131+ $ joins = [];
132+ $ conds = $ querySegment ['where ' ] ?: [];
133+
134+ if ( !empty ( $ querySegment ['joinConditions ' ] ) ) {
135+ foreach ( $ querySegment ['joinConditions ' ] as $ alias => $ joinInfo ) {
136+ $ aliasFixed = preg_replace ( '/^nested/ ' , '' , $ alias );
137+ $ joins [$ aliasFixed ] = [ $ joinInfo [0 ], [ $ joinInfo [1 ] ] ];
138+ }
139+ }
140+
141+ if ( !empty ( $ querySegment ['fromTables ' ] ) ) {
142+ foreach ( $ querySegment ['fromTables ' ] as $ nestedAlias => $ nested ) {
143+ if ( is_array ( $ nested ) ) {
144+ foreach ( $ nested as $ alias => $ table ) {
145+ $ aliasFixed = preg_replace ( '/^nested/ ' , '' , $ alias );
146+ $ tables [$ aliasFixed ] = $ table ;
147+ }
148+ } else {
149+ $ tables [$ nestedAlias ] = $ nested ;
150+ }
151+ }
152+ }
153+
154+ ksort ( $ tables );
155+
156+ return [ $ tables , $ joins , $ conds ];
157+ }
158+
73159 private function getPanesOptions (
74160 PrintRequest $ printRequest ,
75161 string $ canonicalLabel ,
@@ -126,21 +212,29 @@ private function getPanesOptions(
126212
127213 $ qobj = $ querySegmentList [$ rootid ];
128214
215+ [ $ tables , $ joins , $ conds ] = $ this ->parseQuerySegment ( (array )$ qobj );
216+
129217 $ property = new DIProperty ( DIProperty::newFromUserLabel ( $ printRequest ->getCanonicalLabel () ) );
130218 $ propTypeid = $ property ->findPropertyValueType ();
131219
132220 if ( $ isCategory ) {
133-
134221 // data-length without the GROUP BY clause
135- $ sql_options = [ 'LIMIT ' => 1 ];
222+ $ sql_options_ = [ 'LIMIT ' => 1 ];
223+
224+ $ tables_ = $ tables ;
225+ $ tables_ ['insts ' ] = 'smw_fpt_inst ' ;
226+ $ fields_ = [ 'count ' => 'COUNT(*) ' ];
227+ $ conds_ = $ conds ;
228+ $ joins_ = $ joins ;
229+ $ joins_ ['insts ' ] = [ 'JOIN ' , [ "$ qobj ->alias .smw_id = insts.s_id " ] ];
136230
137231 $ dataLength = (int )$ this ->connection ->selectField (
138- $ this ->connection ->tableName ( $ qobj ->joinTable ) . " AS $ qobj ->alias " . $ qobj ->from
139- . ' JOIN ' . $ this ->connection ->tableName ( 'smw_fpt_inst ' ) . " AS insts ON $ qobj ->alias .smw_id = insts.s_id " ,
140- "COUNT(*) AS count " ,
141- $ qobj ->where ,
232+ $ tables_ ,
233+ $ fields_ ,
234+ $ conds_ ,
142235 __METHOD__ ,
143- $ sql_options
236+ $ sql_options_ ,
237+ $ joins_
144238 );
145239
146240 if ( !$ dataLength ) {
@@ -149,7 +243,7 @@ private function getPanesOptions(
149243
150244 $ groupBy = "i.smw_id " ;
151245 $ orderBy = "count DESC, $ groupBy ASC " ;
152- $ sql_options = [
246+ $ sql_options_ = [
153247 'GROUP BY ' => $ groupBy ,
154248 // $this->query->getOption( 'count' ),
155249 'LIMIT ' => $ dataLength ,
@@ -167,15 +261,22 @@ private function getPanesOptions(
167261 HAVING COUNT(i.smw_id) >= 1 ORDER BY COUNT(i.smw_id) DESC
168262 */
169263
264+ $ tables_ = $ tables ;
265+ $ tables_ ['insts ' ] = 'smw_fpt_inst ' ;
266+ $ tables_ [SQLStore::ID_TABLE ] = 'i ' ;
267+ $ joins_ = $ joins ;
268+ $ joins_ ['insts ' ] = [ 'JOIN ' , [ "$ qobj ->alias .smw_id = insts.s_id " ] ];
269+ $ joins_ ['i ' ] = [ 'JOIN ' , [ 'i.smw_id = insts.o_id ' ] ];
270+ $ conds_ = $ conds ;
271+ $ fields_ = "COUNT( $ groupBy) AS count, i.smw_id, i.smw_title, i.smw_namespace, i.smw_iw, i.smw_sort, i.smw_subobject " ;
272+
170273 $ res = $ this ->connection ->select (
171- $ this ->connection ->tableName ( $ qobj ->joinTable ) . " AS $ qobj ->alias " . $ qobj ->from
172- // @see https://github.com/SemanticMediaWiki/SemanticDrilldown/blob/master/includes/Sql/SqlProvider.php
173- . ' JOIN ' . $ this ->connection ->tableName ( 'smw_fpt_inst ' ) . " AS insts ON $ qobj ->alias .smw_id = insts.s_id "
174- . ' JOIN ' . $ this ->connection ->tableName ( SQLStore::ID_TABLE ) . " AS i ON i.smw_id = insts.o_id " ,
175- "COUNT( $ groupBy) AS count, i.smw_id, i.smw_title, i.smw_namespace, i.smw_iw, i.smw_sort, i.smw_subobject " ,
176- $ qobj ->where ,
274+ $ tables_ ,
275+ $ fields_ ,
276+ $ conds_ ,
177277 __METHOD__ ,
178- $ sql_options
278+ $ sql_options_ ,
279+ $ joins_
179280 );
180281
181282 $ isIdField = true ;
@@ -203,25 +304,31 @@ private function getPanesOptions(
203304 }
204305
205306 // data-length without the GROUP BY clause
206- $ sql_options = [ 'LIMIT ' => 1 ];
307+ $ sql_options_ = [ 'LIMIT ' => 1 ];
207308
208309 // SELECT COUNT(*) as count FROM `smw_object_ids` AS t0
209310 // INNER JOIN (`smw_fpt_mdat` AS t2 INNER JOIN `smw_di_wikipage` AS t3 ON t2.s_id=t3.s_id) ON t0.smw_id=t2.s_id
210311 // WHERE ((t3.p_id=517)) LIMIT 500
211312
313+ $ tables_ = $ tables ;
314+ $ fields_ = [ 'count ' => 'COUNT(*) ' ];
315+ $ conds_ = $ conds ;
316+ $ joins_ = $ joins ;
317+
212318 $ dataLength = (int )$ this ->connection ->selectField (
213- $ this -> connection -> tableName ( $ qobj -> joinTable ) . " AS $ qobj -> alias " . $ qobj -> from ,
214- " COUNT(*) as count " ,
215- $ qobj -> where ,
319+ $ tables_ ,
320+ $ fields_ ,
321+ $ conds_ ,
216322 __METHOD__ ,
217- $ sql_options
323+ $ sql_options_ ,
324+ $ joins_
218325 );
219326
220327 if ( !$ dataLength ) {
221328 return [];
222329 }
223330
224- [ $ diType , $ isIdField , $ fields , $ groupBy , $ orderBy ] = $ this ->fetchValuesByGroup ( $ property , $ p_alias , $ propTypeid );
331+ [ $ diType , $ isIdField , $ fields_ , $ groupBy , $ orderBy ] = $ this ->fetchValuesByGroup ( $ property , $ p_alias , $ propTypeid );
225332
226333 /*
227334 ---GENERATED FROM DATATABLES
@@ -231,7 +338,7 @@ private function getPanesOptions(
231338 SELECT i.smw_id,i.smw_title,i.smw_namespace,i.smw_iw,i.smw_subobject,i.smw_hash,i.smw_sort,COUNT( p.o_id ) as count FROM `smw_object_ids` `o` INNER JOIN `smw_di_wikipage` `p` ON ((p.s_id=o.smw_id)) JOIN `smw_object_ids` `i` ON ((p.o_id=i.smw_id)) WHERE o.smw_hash IN ('1_-_A','1_-_Ab','1_-_Abc','10_-_Abcd','11_-_Abc') AND (o.smw_iw!=':smw') AND (o.smw_iw!=':smw-delete') AND p.p_id = 517 GROUP BY p.o_id, i.smw_id ORDER BY count DESC, i.smw_sort ASC
232339 */
233340
234- $ sql_options = [
341+ $ sql_options_ = [
235342 'GROUP BY ' => $ groupBy ,
236343 // the following implies that if the user sets a threshold
237344 // close or equal to 1, and there are too many unique values,
@@ -243,18 +350,27 @@ private function getPanesOptions(
243350 ];
244351
245352 // @see QueryEngine
353+ $ tables_ = $ tables ;
354+ $ joins_ = $ joins ;
355+ $ conds_ = $ conds ;
356+
357+ if ( $ isIdField ) {
358+ $ tables_ ['i ' ] = SQLStore::ID_TABLE ;
359+ $ joins_ ['i ' ] = [ 'JOIN ' , "$ p_alias.o_id = i.smw_id " ];
360+ $ conds_ .= !empty ( $ conds_ ) ? ' AND ' : '' ;
361+ $ conds_ .= ' i.smw_iw != ' . $ this ->connection ->addQuotes ( SMW_SQL3_SMWIW_OUTDATED );
362+ $ conds_ .= ' AND i.smw_iw != ' . $ this ->connection ->addQuotes ( SMW_SQL3_SMWDELETEIW );
363+ }
364+
365+ // perform the select
246366 $ res = $ this ->connection ->select (
247- $ this ->connection ->tableName ( $ qobj ->joinTable ) . " AS $ qobj ->alias " . $ qobj ->from
248- . ( !$ isIdField ? ''
249- : " JOIN " . $ this ->connection ->tableName ( SQLStore::ID_TABLE ) . " AS `i` ON ( $ p_alias.o_id = i.smw_id) " ),
250- implode ( ', ' , $ fields ),
251- $ qobj ->where . ( !$ isIdField ? '' : ( !empty ( $ qobj ->where ) ? ' AND ' : '' )
252- . ' i.smw_iw!= ' . $ this ->connection ->addQuotes ( SMW_SQL3_SMWIW_OUTDATED )
253- . ' AND i.smw_iw!= ' . $ this ->connection ->addQuotes ( SMW_SQL3_SMWDELETEIW ) ),
367+ $ tables_ ,
368+ $ fields_ ,
369+ $ conds_ ,
254370 __METHOD__ ,
255- $ sql_options
371+ $ sql_options_ ,
372+ $ joins_
256373 );
257-
258374 }
259375
260376 // verify uniqueRatio
@@ -605,9 +721,9 @@ private function searchPanesMainlabel( PrintRequest $printRequest, array $search
605721 'threshold ' => $ threshold ,
606722 ];
607723
608- if ( $ threshold < 1 ) {
609- return [];
610- }
724+ // if ( $threshold < 1 ) {
725+ // return [];
726+ // }
611727
612728 $ query = $ this ->datatables ->query ;
613729 $ queryDescription = $ query ->getDescription ();
@@ -628,7 +744,9 @@ private function searchPanesMainlabel( PrintRequest $printRequest, array $search
628744
629745 $ qobj = $ querySegmentList [$ rootid ];
630746
631- $ sql_options = [
747+ [ $ tables , $ joins , $ conds ] = $ this ->parseQuerySegment ( (array )$ qobj );
748+
749+ $ sql_options_ = [
632750 // *** should we set a limit here ?
633751 // it makes sense to show the pane for
634752 // mainlabel only when page titles are grouped
@@ -639,21 +757,28 @@ private function searchPanesMainlabel( PrintRequest $printRequest, array $search
639757
640758 // Selecting those is required in standard SQL (but MySQL does not require it).
641759 $ sortfields = implode ( ', ' , $ qobj ->sortfields );
642- $ sortfields = $ sortfields ? ', ' . $ sortfields : '' ;
643760
644761 // @see QueryEngine
762+ $ tables_ = $ tables ;
763+ $ fields_ = [];
764+ $ fields_ ['id ' ] = "$ qobj ->alias .smw_id " ;
765+ $ fields_ ['t ' ] = "$ qobj ->alias .smw_title " ;
766+ $ fields_ ['ns ' ] = "$ qobj ->alias .smw_namespace " ;
767+ $ fields_ ['iw ' ] = "$ qobj ->alias .smw_iw " ;
768+ $ fields_ ['so ' ] = "$ qobj ->alias .smw_subobject " ;
769+ $ fields_ ['sortkey ' ] = "$ qobj ->alias .smw_sortkey " ;
770+ $ fields_ [] = $ sortfields ;
771+
772+ $ conds_ = $ conds ;
773+ $ joins_ = $ joins ;
774+
645775 $ res = $ this ->connection ->select (
646- $ this ->connection ->tableName ( $ qobj ->joinTable ) . " AS $ qobj ->alias " . $ qobj ->from ,
647- "$ qobj ->alias .smw_id AS id, " .
648- "$ qobj ->alias .smw_title AS t, " .
649- "$ qobj ->alias .smw_namespace AS ns, " .
650- "$ qobj ->alias .smw_iw AS iw, " .
651- "$ qobj ->alias .smw_subobject AS so, " .
652- "$ qobj ->alias .smw_sortkey AS sortkey " .
653- "$ sortfields " ,
654- $ qobj ->where ,
776+ $ tables_ ,
777+ $ fields_ ,
778+ $ conds_ ,
655779 __METHOD__ ,
656- $ sql_options
780+ $ sql_options_ ,
781+ $ joins_
657782 );
658783
659784 $ diHandler = $ this ->datatables ->store ->getDataItemHandlerForDIType (
0 commit comments