8715d83d38d8bb18adfdd9ce05771b4cede85407 angie Fri Dec 5 09:52:07 2014 -0800 Moved some hgTables code that had been copied into a couple differentplaces into a new lib module, cartTrackDb. hgGenome/import.c has code that is fairly similar but that also needs to filter out custom tracks that were generated by hgGenome. diff --git src/hg/hgTables/mainPage.c src/hg/hgTables/mainPage.c index 25c7ec9..e4d8195 100644 --- src/hg/hgTables/mainPage.c +++ src/hg/hgTables/mainPage.c @@ -1,26 +1,27 @@ /* mainPage - stuff to put up the first table browser page. */ /* Copyright (C) 2014 The Regents of the University of California * See README in this or parent directory for licensing information. */ #include "common.h" #include "linefile.h" #include "hash.h" #include "htmshell.h" #include "cheapcgi.h" #include "cart.h" +#include "cartTrackDb.h" #include "textOut.h" #include "jksql.h" #include "hdb.h" #include "web.h" #include "jsHelper.h" #include "hui.h" #include "hgColors.h" #include "trackDb.h" #include "grp.h" #include "hgTables.h" #include "joiner.h" #include "hubConnect.h" #include "trackHub.h" #include "hgConfig.h" @@ -108,30 +109,31 @@ * to save selection state. */ { jsMakeTrackingRadioButton(hgtaRegionType, "regionType", val, selVal); } struct grp *showGroupField(char *groupVar, char *groupScript, struct sqlConnection *conn, boolean allTablesOk) /* Show group control. Returns selected group. */ { struct grp *group, *groupList = fullGroupList; struct grp *selGroup = findSelectedGroup(groupList, groupVar); hPrintf("group:\n"); hPrintf("\n"); return selGroup; } static void addIfExists(struct hash *hash, struct slName **pList, char *name) /* Add name to tail of list if it exists in hash. */ { if (hashLookup(hash, name)) slNameAddTail(pList, name); } @@ -214,186 +216,82 @@ char *unsplitTableName(char *table) /* Convert chr*_name to name */ { if (startsWith("chr", table)) { char *s = strrchr(table, '_'); if (s != NULL) { table = s + 1; } } return table; } -static char *chopAtFirstDot(char *string) -/* Terminate string at first '.' if found. Return string for convenience. */ -{ -char *ptr = strchr(string, '.'); -if (ptr != NULL) - *ptr = '\0'; -return string; -} - -static void hashAddSlName(struct hash *hash, char *key, char *val) -/* If key is already in hash, add an slName for val to the head of the list; - * otherwise just store key -> slName for val. */ -{ -struct slName *sln = slNameNew(val); -struct hashEl *hel = hashLookup(hash, key); -if (hel == NULL) - hashAdd(hash, key, sln); -else - slAddHead(&(hel->val), sln); -} - -static struct hash *accessControlInit(struct sqlConnection *conn) -/* Return a hash associating restricted table/track names in the given db/conn - * with virtual hosts, or NULL if there is no tableAccessControl table and no - * forbiddenTrackList (see getFullTrackList). */ -{ -struct hash *acHash = NULL; -if (sqlTableExists(conn, "tableAccessControl")) - { - struct sqlResult *sr = NULL; - char **row = NULL; - acHash = newHash(0); - sr = sqlGetResult(conn, "NOSQLINJ select name,host from tableAccessControl"); - while ((row = sqlNextRow(sr)) != NULL) - hashAddSlName(acHash, row[0], chopAtFirstDot(row[1])); - sqlFreeResult(&sr); - } -if (forbiddenTrackList != NULL) - { - if (acHash == NULL) - acHash = newHash(0); - struct trackDb *tdb; - for (tdb = forbiddenTrackList; tdb != NULL; tdb = tdb->next) - { - char *tbOff = cloneString(trackDbSetting(tdb, "tableBrowser")); - if (isEmpty(tbOff)) - errAbort("bug: tdb for %s is in forbiddenTrackList without 'tableBrowser off' setting", - tdb->track); - hashAddSlName(acHash, tdb->table, "-"); - // skip "off" and look for additional table names: - nextWord(&tbOff); - char *tbl; - while ((tbl = nextWord(&tbOff)) != NULL) - hashAddSlName(acHash, tbl, "-"); - } - } -return acHash; -} - -boolean accessControlDenied(char *db, char *table) -/* Return TRUE if table access is restricted to some host(s) other than - * the one we're running on. */ -{ -static char *currentHost = NULL; -struct slName *enabledHosts = NULL; -struct slName *sln = NULL; -static struct hash *dbToAcHash = NULL; - -if (dbToAcHash == NULL) - dbToAcHash = hashNew(0); - -struct hash *acHash = hashFindVal(dbToAcHash, db); -if (acHash == NULL) - { - struct sqlConnection *conn = hAllocConn(db); - acHash = accessControlInit(conn); - hFreeConn(&conn); - hashAdd(dbToAcHash, db, acHash); - } - -if (acHash == NULL) - return FALSE; -enabledHosts = (struct slName *)hashFindVal(acHash, table); -if (enabledHosts == NULL) - return FALSE; -if (currentHost == NULL) - { - currentHost = cloneString(cgiServerName()); - if (currentHost == NULL) - { - warn("accessControl: unable to determine current host"); - return FALSE; - } - else - chopAtFirstDot(currentHost); - } -for (sln = enabledHosts; sln != NULL; sln = sln->next) - { - if (sameString(currentHost, sln->name)) - return FALSE; - } -return TRUE; -} - struct slName *tablesForDb(char *db) /* Find tables associated with database. */ { boolean isGenomeDb = sameString(db, database); struct sqlConnection *conn = hAllocConn(db); struct slName *raw, *rawList = sqlListTables(conn); struct slName *cooked, *cookedList = NULL; struct hash *uniqHash = newHash(0); hFreeConn(&conn); for (raw = rawList; raw != NULL; raw = raw->next) { if (isGenomeDb) { /* Deal with tables split across chromosomes. */ char *root = unsplitTableName(raw->name); - if (accessControlDenied(db, root) || accessControlDenied(db, raw->name)) + if (cartTrackDbIsAccessDenied(db, root) || cartTrackDbIsAccessDenied(db, raw->name)) continue; if (!hashLookup(uniqHash, root)) { hashAdd(uniqHash, root, NULL); cooked = slNameNew(root); slAddHead(&cookedList, cooked); } } else { char dbTable[256]; - if (accessControlDenied(db, raw->name)) + if (cartTrackDbIsAccessDenied(db, raw->name)) continue; safef(dbTable, sizeof(dbTable), "%s.%s", db, raw->name); cooked = slNameNew(dbTable); slAddHead(&cookedList, cooked); } } hashFree(&uniqHash); slFreeList(&rawList); slSort(&cookedList, slNameCmp); return cookedList; } char *showTableField(struct trackDb *track, char *varName, boolean useJoiner) /* Show table control and label. */ { struct slName *name, *nameList = NULL; char *selTable; if (track == NULL) nameList = tablesForDb(findSelDb()); else - nameList = tablesForTrack(track, useJoiner); + nameList = cartTrackDbTablesForTrack(database, track, useJoiner); /* Get currently selected table. If it isn't in our list * then revert to first in list. */ selTable = cartUsualString(cart, varName, nameList->name); if (!slNameInListUseCase(nameList, selTable)) selTable = nameList->name; /* Print out label and drop-down list. */ hPrintf("table: "); hPrintf("