06d7be056190c14b85e71bc12523f18ea6815b5e
markd
  Mon Dec 7 00:50:29 2020 -0800
BLAT mmap index support merge with master

diff --git src/hg/lib/cart.c src/hg/lib/cart.c
index 812a774..f9cd742 100644
--- src/hg/lib/cart.c
+++ src/hg/lib/cart.c
@@ -241,30 +241,47 @@
     cdb = cartDbLoadWhere(conn, table, where->string);
     dyStringFree(&where);
     if (looksCorrupted(cdb))
        {
        /* Can't use warn here -- it interrupts the HTML header, causing an
 	* err500 (and nothing useful in error_log) instead of a warning. */
        fprintf(stderr,
 	       "%s id=%u looks corrupted -- starting over with new %s id.\n",
 	       table, id, table);
        cdb = NULL;
        }
     return cdb;
     }
 }
 
+static char *getDefaultCart(struct sqlConnection *conn)
+/* Get the default cart if any. */
+{
+char *contents = "";
+char *table = defaultCartTable();
+if (sqlTableExists(conn, table))
+    {
+    char buffer[16 * 1024];
+    char query[1024];
+
+    sqlSafef(query, sizeof query, "select contents from %s", table);
+    sqlQuickQuery(conn, query, buffer, sizeof buffer);
+    contents = cloneString(buffer);
+    }
+return contents;
+}
+
 struct cartDb *loadDb(struct sqlConnection *conn, char *table, char *secureId, boolean *found)
 /* Load bits from database and save in hash. */
 {
 struct cartDb *cdb;
 boolean result = TRUE;
 
 cdb = cartDbLoadFromId(conn, table, secureId);
 if (!cdb)
     {
     result = FALSE;
     struct dyString *query = dyStringNew(256);
     sqlDyStringPrintf(query, "INSERT %s VALUES(0,'',0,now(),now(),0", table);
     char *sessionKey = "";
     if (cartDbHasSessionKey(conn, table)) 
 	{
@@ -1315,30 +1332,35 @@
 struct sqlConnection *conn = cartDefaultConnector();
 char *ex;
 boolean userIdFound = FALSE, sessionIdFound = FALSE;
 
 AllocVar(cart);
 cart->hash = newHash(12);
 cart->exclude = newHash(7);
 cart->userId = userId;
 cart->sessionId = sessionId;
 cart->userInfo = loadDb(conn, userDbTable(), userId, &userIdFound);
 cart->sessionInfo = loadDb(conn, sessionDbTable(), sessionId, &sessionIdFound);
 if (sessionIdFound)
     cartParseOverHash(cart, cart->sessionInfo->contents);
 else if (userIdFound)
     cartParseOverHash(cart, cart->userInfo->contents);
+else
+    {
+    char *defaultCartContents = getDefaultCart(conn);
+    cartParseOverHash(cart, defaultCartContents);
+    }
 char when[1024];
 safef(when, sizeof(when), "open %s %s", userId, sessionId);
 cartTrace(cart, when, conn);
 
 loadCgiOverHash(cart, oldVars);
 
 // I think this is the place to justify old and new values
 cartJustify(cart, oldVars);
 
 #ifndef GBROWSE
 /* If some CGI other than hgSession been passed hgSession loading instructions,
  * apply those to cart before we do anything else.  (If this is hgSession,
  * let it handle the settings so it can display feedback to the user.) */
 boolean didSessionLoad = FALSE;
 if (! (cgiScriptName() && endsWith(cgiScriptName(), "hgSession")))
@@ -2141,31 +2163,32 @@
 
 static char *getSessionId()
 /* Get session id if any from CGI. */
 {
 return cgiOptionalString("hgsid");
 }
 
 static void clearDbContents(struct sqlConnection *conn, char *table, char * secureId)
 /* Clear out contents field of row in table that matches id. */
 {
 if (!secureId)
     return;
 struct dyString *query = dyStringNew(256);
 char *sessionKey = NULL;	    
 unsigned int id = cartDbParseId(secureId, &sessionKey);
-sqlDyStringPrintf(query, "update %s set contents='' where id=%u", table, id);
+char *defaultCartContents = getDefaultCart(conn);
+sqlDyStringPrintf(query, "update %s set contents='%s' where id=%u", table, defaultCartContents, id);
 if (cartDbUseSessionKey())
     {
     if (!sessionKey)
 	sessionKey = "";
     sqlDyStringPrintf(query, " and sessionKey='%s'", sessionKey);
     }
 sqlUpdate(conn, query->string);
 dyStringFree(&query);
 
 
 }
 
 void cartResetInDb(char *cookieName)
 /* Clear cart in database. */
 {