a019cec925f0e902a6eb2ddeb781dd549c7e9406
chmalee
  Fri Mar 5 15:54:25 2021 -0800
Add public track hub results to track search, refs #26179

diff --git src/hg/lib/hubConnect.c src/hg/lib/hubConnect.c
index 1791171..dd3cc70 100644
--- src/hg/lib/hubConnect.c
+++ src/hg/lib/hubConnect.c
@@ -11,30 +11,31 @@
 #include "linefile.h"
 #include "hash.h"
 #include "dystring.h"
 #include "sqlNum.h"
 #include "jksql.h"
 #include "hdb.h"
 #include "net.h"
 #include "trackHub.h"
 #include "hubConnect.h"
 #include "hui.h"
 #include "errCatch.h"
 #include "obscure.h"
 #include "hgConfig.h"
 #include "grp.h"
 #include "udc.h"
+#include "hubPublic.h"
 
 boolean isHubTrack(char *trackName)
 /* Return TRUE if it's a hub track. */
 {
 return startsWith(hubTrackPrefix, trackName);
 }
 
 static char *hubStatusTableName = NULL;
 static char *_hubPublicTableName = NULL;
 
 static char *getHubStatusTableName()
 /* return the hubStatus table name from the environment, 
  * or hg.conf, or use the default.  Cache the result */
 {
 if (hubStatusTableName == NULL)
@@ -530,32 +531,31 @@
 
 /* update the status table with the lastest label and database information */
 hubUpdateStatus( errorMessage, hub);
 
 /* if we're turning on the hub, set the cart variable */
 if (set)
     {
     char hubName[32];
     safef(hubName, sizeof(hubName), "%s%u", hgHubConnectHubVarPrefix, id);
     cartSetString(cart, hubName, "1");
     }
 
 return hub;
 }
 
-unsigned hubFindOrAddUrlInStatusTable(char *database, struct cart *cart,
-    char *url, char **errorMessage)
+unsigned hubFindOrAddUrlInStatusTable(struct cart *cart, char *url, char **errorMessage)
 /* find this url in the status table, and return its id and errorMessage (if an errorMessage exists) */
 {
 int id = 0;
 
 *errorMessage = NULL;
 
 if ((id = getHubId(url, errorMessage)) > 0)
     return id;
 
 getAndSetHubStatus( cart, url, FALSE);
 
 if ((id = getHubId(url, errorMessage)) == 0)
     errAbort("inserted new hubUrl %s, but cannot find it", url);
 
 return id;
@@ -996,15 +996,66 @@
 
 return newDatabase;
 }
 
 char *hubNameFromUrl(char *hubUrl)
 /* Given the URL for a hub, return its hub_# name. */
 {
 char query[PATH_LEN*4];
 sqlSafef(query, sizeof(query), "select concat('hub_', id) from %s where hubUrl = '%s'",
          getHubStatusTableName(), hubUrl);
 struct sqlConnection *conn = hConnectCentral();
 char *name = sqlQuickString(conn, query);
 hDisconnectCentral(&conn);
 return name;
 }
+
+void addPublicHubsToHubStatus(struct cart *cart, struct sqlConnection *conn, char *publicTable, char  *statusTable)
+/* Add urls in the hubPublic table to the hubStatus table if they aren't there already */
+{
+char query[1024];
+sqlSafef(query, sizeof(query),
+        "select %s.hubUrl from %s left join %s on %s.hubUrl = %s.hubUrl where %s.hubUrl is NULL",
+        publicTable, publicTable, statusTable, publicTable, statusTable, statusTable);
+struct sqlResult *sr = sqlGetResult(conn, query);
+char **row;
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    char *errorMessage = NULL;
+    char *url = row[0];
+
+    // add this url to the hubStatus table
+    hubFindOrAddUrlInStatusTable(cart, url, &errorMessage);
+    }
+}
+
+struct hash *buildPublicLookupHash(struct sqlConnection *conn, char *publicTable, char *statusTable,
+        struct hash **pHash)
+/* Return a hash linking hub URLs to struct hubEntries.  Also make pHash point to a hash that just stores
+ * the names of the public hubs (for use later when determining if hubs were added by the user) */
+{
+struct hash *hubLookup = newHash(5);
+struct hash *publicLookup = newHash(5);
+char query[512];
+bool hasDescription = sqlColumnExists(conn, publicTable, "descriptionUrl");
+if (hasDescription)
+    sqlSafef(query, sizeof(query), "select p.hubUrl,p.shortLabel,p.longLabel,p.dbList,"
+            "s.errorMessage,s.id,p.descriptionUrl from %s p,%s s where p.hubUrl = s.hubUrl",
+	    publicTable, statusTable);
+else
+    sqlSafef(query, sizeof(query), "select p.hubUrl,p.shortLabel,p.longLabel,p.dbList,"
+            "s.errorMessage,s.id from %s p,%s s where p.hubUrl = s.hubUrl",
+	    publicTable, statusTable);
+
+struct sqlResult *sr = sqlGetResult(conn, query);
+char **row;
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    struct hubEntry *hubInfo = hubEntryTextLoad(row, hasDescription);
+    hubInfo->tableHasDescriptionField = hasDescription;
+    hashAddUnique(hubLookup, hubInfo->hubUrl, hubInfo);
+    hashStore(publicLookup, hubInfo->hubUrl);
+    }
+sqlFreeResult(&sr);
+*pHash = publicLookup;
+return hubLookup;
+}