@@ -32,52 +32,25 @@ public function execute() {
32
32
$ this ->userProfile = $ this ->getUserProfile ();
33
33
34
34
$ result = null ;
35
- $ item = null ;
36
35
37
- // The default community search is cached in Maintenance.php
38
- if (empty ($ query )) {
39
- // $item = \core\Cache::LoadItem(\core\Cache::CommunityKey($query, $startIndex, $limit));
40
- }
41
-
42
- // Disable cache, for testing
43
- $ item = null ;
44
-
45
- if (!is_null ($ item )) {
46
- $ result = $ item ;
47
- } else {
48
- $ result = $ this ->searchCommunity ($ query , $ startIndex , $ limit , $ type );
49
- }
36
+ $ result = $ this ->searchCommunity ($ query , $ startIndex , $ limit , $ type );
50
37
51
38
return new \core \Result ($ result );
52
39
}
53
40
54
41
public function searchCommunity ($ query , $ startIndex , $ limit , $ type ) {
55
42
// Build the search query.
56
- $ whereStatements = array () ;
43
+ $ whereStatements = [] ;
57
44
$ searchSqlParams = [];
58
45
59
46
// Search everything using the query.
60
47
if (!empty ($ query )) {
61
- $ whereStatements [] = " p.name LIKE ? " ;
62
- $ whereStatements [] = " p.description LIKE ? " ;
63
- $ whereStatements [] = " p.author LIKE ? " ;
64
-
65
- $ preparedQuery = "% {$ query }% " ;
66
- $ searchSqlParams [] = ["s " , $ preparedQuery ];
67
- $ searchSqlParams [] = ["s " , $ preparedQuery ];
68
- $ searchSqlParams [] = ["s " , $ preparedQuery ];
48
+ $ whereStatements [] = "MATCH(`name`, `description`, `pattern`, `replace`, `author`) AGAINST(? IN NATURAL LANGUAGE MODE) " ;
49
+ $ searchSqlParams [] = ["s " , $ query ];
69
50
}
70
51
71
52
// Do the actual search.
72
- $ q = "SELECT p.*, urJoin.rating AS userRating, fJoin.patternId as favorite
73
- FROM patterns p
74
- LEFT JOIN userRatings urJoin ON urJoin.patternId = p.id AND urJoin.userId = ?
75
- LEFT JOIN favorites as fJoin ON fJoin.userId = ? AND fJoin.patternId=p.id
76
- WHERE p.visibility='public'
77
- " ;
78
-
79
- $ searchSqlParams [] = ["s " , $ this ->userProfile ->userId ];
80
- $ searchSqlParams [] = ["s " , $ this ->userProfile ->userId ];
53
+ $ q = "SELECT p.* FROM patterns p WHERE p.visibility='public' " ;
81
54
82
55
if (!is_null ($ type )) {
83
56
$ typeArray = quoteStringArray ($ type );
@@ -94,6 +67,37 @@ public function searchCommunity($query, $startIndex, $limit, $type) {
94
67
95
68
$ result = $ this ->db ->execute ($ q , $ searchSqlParams );
96
69
70
+ // Inject userRating and favorite
71
+ $ patternIds = quoteStringArray (array_map (function ($ pattern ) {
72
+ return idx ($ pattern , 'id ' );
73
+ }, $ result ));
74
+
75
+ $ userId = $ this ->userProfile ->userId ;
76
+
77
+ $ userRatings = $ this ->db ->execute ("SELECT rating, patternId FROM userRatings WHERE patternId IN ( $ patternIds) AND userId=? " , [
78
+ ['s ' , $ userId ]
79
+ ]);
80
+
81
+ $ userFavorites = $ this ->db ->execute ("SELECT patternId FROM favorites WHERE patternId IN ( $ patternIds) AND userId=? " , [
82
+ ['s ' , $ userId ]
83
+ ]);
84
+
85
+ function injectIntoResults ($ result , $ sourceList , $ sourceKey , $ destKey )
86
+ {
87
+ for ($ i = 0 ; $ i < count ($ sourceList ); $ i ++) {
88
+ $ sourceValue = $ sourceList [$ i ];
89
+ for ($ j = 0 ; $ j < count ($ result ); $ j ++) {
90
+ if (idx ($ result [$ j ], 'id ' ) === $ sourceValue ->patternId ) {
91
+ $ result [$ i ]->{$ destKey } = idx ($ result [$ j ], $ sourceKey , true );
92
+ break ;
93
+ }
94
+ }
95
+ }
96
+ }
97
+
98
+ injectIntoResults ($ result , $ userRatings , 'rating ' , 'userRating ' );
99
+ injectIntoResults ($ result , $ userFavorites , null , 'favorite ' );
100
+
97
101
$ json = createPatternSet ($ result );
98
102
99
103
return $ json ;
@@ -106,10 +110,10 @@ function getFlavorValues() {
106
110
public function getSchema () {
107
111
$ flavorValues = $ this ->getFlavorValues ();
108
112
return array (
109
- "query " => array ("type " => self ::STRING , "required " => true ),
110
- "startIndex " => array ("type " => self ::NUMBER , "required " => false , "default " => 0 ),
111
- "limit " => array ("type " => self ::NUMBER , "required " => false , "default " => 100 ),
112
- "flavor " => array ("type " => self ::ENUM_ARRAY , "values " => $ flavorValues , "default " => $ flavorValues , "required " => false )
113
+ "query " => array ("type " => self ::STRING , "required " => true ),
114
+ "startIndex " => array ("type " => self ::NUMBER , "required " => false , "default " => 0 ),
115
+ "limit " => array ("type " => self ::NUMBER , "required " => false , "default " => 100 ),
116
+ "flavor " => array ("type " => self ::ENUM_ARRAY , "values " => $ flavorValues , "default " => $ flavorValues , "required " => false )
113
117
);
114
118
}
115
119
}
0 commit comments