dc4dc74eb9e0b860f56347141d6d3e1ae76bd9ad
braney
  Fri May 30 15:09:18 2014 -0700
clean up some code for opening trackHubs, removed hgHubConnectCgiDestUrlcart variable, added ability to tell CGI's to default to a particular
database supported by a hub

diff --git src/hg/lib/hubConnect.c src/hg/lib/hubConnect.c
index af21aca..8337d6c 100644
--- src/hg/lib/hubConnect.c
+++ src/hg/lib/hubConnect.c
@@ -271,46 +271,42 @@
 return  val;
 }
 
 char *hubConnectSkipHubPrefix(char *trackName)
 /* Given something like "hub_123_myWig" return myWig.  Don't free this, it's not allocated */
 {
 if(!startsWith("hub_", trackName))
     return trackName;
 
 trackName += 4;
 trackName = strchr(trackName, '_');
 assert(trackName != NULL);
 return trackName + 1;
 }
 
-struct trackHub *trackHubFromId(unsigned hubId)
+struct hubConnectStatus *hubFromId(unsigned hubId)
 /* Given a hub ID number, return corresponding trackHub structure. 
  * ErrAbort if there's a problem. */
 {
 struct sqlConnection *conn = hConnectCentral();
 struct hubConnectStatus *status = hubConnectStatusForId(conn, hubId);
 hDisconnectCentral(&conn);
 if (status == NULL)
     errAbort("The hubId %d was not found", hubId);
 if (!isEmpty(status->errorMessage))
     errAbort("%s", status->errorMessage);
-char hubName[16];
-safef(hubName, sizeof(hubName), "hub_%u", hubId);
-struct trackHub *hub = trackHubOpen(status->hubUrl, hubName);
-hubConnectStatusFree(&status);
-return hub;
+return status;
 }
 
 static struct trackDb *findSuperTrack(struct trackDb *tdbList, char *trackName)
 /*  discover any supertracks, and if there are some add them 
  *  to the subtrack list of the supertrack */
 {
 struct trackDb *tdb;
 struct trackDb *p = NULL;
 struct trackDb *next;
 
 for(tdb = tdbList; tdb; tdb = next)
     {
     /* save away the next pointer becuase we may detach this node and
      * add it to its supertrack parent */
     next = tdb->next;
@@ -319,69 +315,68 @@
 	/* found a supertrack with the right name, add this child */
 	p = tdb->parent;
 	slAddHead(&p->subtracks, tdb);
 	}
     }
 
 return p;
 }
 
 
 void hubConnectAddDescription(char *database, struct trackDb *tdb)
 /* Fetch tdb->track's html description (or nearest ancestor's non-empty description)
  * and store in tdb->html. */
 {
 unsigned hubId = hubIdFromTrackName(tdb->track);
-struct trackHub *hub = trackHubFromId(hubId);
-struct trackHubGenome *hubGenome = trackHubFindGenome(hub, database);
-trackHubPolishTrackNames(hub, tdb);
+struct hubConnectStatus *hub = hubFromId(hubId);
+struct trackHubGenome *hubGenome = trackHubFindGenome(hub->trackHub, database);
+trackHubPolishTrackNames(hub->trackHub, tdb);
 trackHubAddDescription(hubGenome->trackDbFile, tdb);
 }
 
 struct trackDb *hubConnectAddHubForTrackAndFindTdb( char *database, 
     char *trackName, struct trackDb **pTdbList, struct hash *trackHash)
 /* Go find hub for trackName (which will begin with hub_), and load the tracks
  * for it, appending to end of list and adding to trackHash.  Return the
  * trackDb associated with trackName. This will also fill in the html fields,
  * but just for that track and it's parents. */ 
 {
 unsigned hubId = hubIdFromTrackName(trackName);
-struct trackHub *hub = trackHubFromId(hubId);
-struct trackHubGenome *hubGenome = trackHubFindGenome(hub, database);
-struct trackDb *tdbList = trackHubTracksForGenome(hub, hubGenome);
+struct hubConnectStatus *hub = hubFromId(hubId);
+struct trackHubGenome *hubGenome = trackHubFindGenome(hub->trackHub, database);
+struct trackDb *tdbList = trackHubTracksForGenome(hub->trackHub, hubGenome);
 tdbList = trackDbLinkUpGenerations(tdbList);
 tdbList = trackDbPolishAfterLinkup(tdbList, database);
-trackHubPolishTrackNames(hub, tdbList);
+trackHubPolishTrackNames(hub->trackHub, tdbList);
 char *fixTrackName = cloneString(trackName);
 trackHubFixName(fixTrackName);
 rAddTrackListToHash(trackHash, tdbList, NULL, FALSE);
 if (pTdbList != NULL)
     *pTdbList = slCat(*pTdbList, tdbList);
 struct trackDb *tdb = hashFindVal(trackHash, fixTrackName);
 if (tdb == NULL) 
     // superTracks aren't in the hash... look in tdbList
     tdb = findSuperTrack(tdbList, fixTrackName);
 
 if (tdb == NULL) 
-    errAbort("Can't find track %s in %s", fixTrackName, hub->url);
+    errAbort("Can't find track %s in %s", fixTrackName, hub->trackHub->url);
 
 /* Add html for track and parents. */
 /* Note: this does NOT add the HTML for supertrack kids */
 struct trackDb *parent;
 for (parent = tdb; parent != NULL; parent = parent->parent)
     trackHubAddDescription(hubGenome->trackDbFile, parent);
-trackHubClose(&hub);
 
 return tdb;
 }
 
 static char *getDbList(struct trackHub *tHub, int *pCount)
 /* calculate dbList for hubStatus table from trackHub */
 {
 struct hashEl *hel;
 struct dyString *dy = newDyString(1024);
 struct hashCookie cookie = hashFirst(tHub->genomeHash);
 int dbCount = 0;
 while ((hel = hashNext(&cookie)) != NULL)
     {
     dbCount++;
     dyStringPrintf(dy,"%s,", hel->name);
@@ -432,31 +427,31 @@
 
     char *thisId = row[0], *thisError = row[1];
 
     if (!isEmpty(thisError))
 	*errorMessage = cloneString(thisError);
 
     id = sqlUnsigned(thisId);
     }
 sqlFreeResult(&sr);
 
 hDisconnectCentral(&conn);
 
 return id;
 }
 
-static void getAndSetHubStatus( struct cart *cart, char *url, 
+static struct hubConnectStatus *getAndSetHubStatus( struct cart *cart, char *url, 
     boolean set)
 /* make sure url is in hubStatus table, fetch the hub to get latest
  * labels and db information.
  * Set the cart variable to turn the hub on if set == TRUE.  
  */
 {
 char *errorMessage = NULL;
 unsigned id;
 
 /* first see if url is in hubStatus table */
 if ((id = getHubId(url, &errorMessage)) == 0)
     {
     /* the url is not in the hubStatus table, add it */
     insertHubUrlInStatus(url);
     if ((id = getHubId(url, &errorMessage)) == 0)
@@ -476,62 +471,73 @@
 struct trackHub *tHub = fetchHub( hub, &errorMessage);
 if (tHub != NULL)
     hub->trackHub = tHub;
 
 /* 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");
     }
 
-hubConnectStatusFree(&hub);
+return hub;
 }
 
 unsigned hubFindOrAddUrlInStatusTable(char *database, 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;
 }
 
-void hubCheckForNew( struct cart *cart)
+static char  *checkForNew( struct cart *cart)
 /* see if the user just typed in a new hub url, return id if so */
 {
+struct hubConnectStatus *hub;
 char *url = cartOptionalString(cart, hgHubDataText);
+char *newDatabase = NULL;
+
+if (url == NULL)
+    return NULL;
 
-if (url != NULL)
-    {
 trimSpaces(url);
-    getAndSetHubStatus( cart, url, TRUE);
+hub = getAndSetHubStatus( cart, url, TRUE);
+cartRemove(cart, hgHubDataText);
+
+char *wantFirstDb = cartOptionalString(cart, hgHubDoFirstDb);
+if (wantFirstDb != NULL)
+    {
+    newDatabase = hub->trackHub->defaultDb;
+    fprintf(stderr, "getting defaultDb %s\n", newDatabase);
     }
+return newDatabase;
 }
 
 unsigned hubResetError(char *url)
 /* clear the error for this url in the hubStatus table,return the id */
 {
 struct sqlConnection *conn = hConnectCentral();
 char query[512];
 
 sqlSafef(query, sizeof(query), "select id from %s where hubUrl = \"%s\"", getHubStatusTableName(), url);
 unsigned id = sqlQuickNum(conn, query);
 
 if (id == 0)
     errAbort("could not find url %s in status table (%s)\n", 
 	url, getHubStatusTableName());
 
@@ -698,25 +704,25 @@
 
 hubTrackDbs = tdbList;
 if (pGroupList != NULL)
     *pGroupList = hubGroups;
 return tdbList;
 }
 
 static struct hubConnectStatus *globalHubList;
 
 struct hubConnectStatus *hubConnectGetHubs()
 /* return the static global to the track data hubs */
 {
 return globalHubList;
 }
 
-struct hubConnectStatus * hubConnectLoadHubs(struct cart *cart)
+char *hubConnectLoadHubs(struct cart *cart)
 /* load the track data hubs.  Set a static global to remember them */
 {
-hubCheckForNew( cart);
+char *newDatabase = checkForNew( cart);
 cartSetString(cart, hgHubConnectRemakeTrackHub, "on");
 struct hubConnectStatus  *hubList =  hubConnectStatusListFromCart(cart);
 globalHubList = hubList;
 
-return hubList;
+return newDatabase;
 }