Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

873
LINES

< > BotCompany Repo | #1024288 // agi.blue source [adding slices, dev.]

JavaX module (desktop) [tags: butter use-pretranspiled] - homepage

Download Jar. Libraryless. Click here for Pure Java version (17406L/126K).

!7

// See #1023660 for the older 99 lines version

// phasing out
concept PhysicalSlice {
  new Ref<Page> slicePage; // page that describes this slice
}

// in main DB
concept Slice {
  GlobalID globalID = aGlobalIDObject();
  S caseID;
  S name;
  
  S defaultCaseID() {
    ret uniqueFileNameUsingMD5_80_v2(name + " " + globalID);
  }
}

// in any DB
concept Page {
  new Ref<PhysicalSlice> slice; // where are we stored
  S globalID = aGlobalID();
  S url, q;
}

abstract concept AbstractEntry {
  new Ref<Page> page;
  int count;
  S key;
  S ip;
  new Ref<Signer> signer;
}

concept Entry > AbstractEntry {
  S globalID = aGlobalID(); // TODO: migrate to object
  S value;
}

concept MultiLineEntry > AbstractEntry {
  GlobalID globalID = aGlobalIDObject();
  S value;
}

concept BlobEntry > AbstractEntry {
  GlobalID globalID = aGlobalIDObject();
  long length;
  S md5;
}

// in main DB?
concept Signer {
  S globalID = aGlobalID();
  S publicKey;
  bool trusted;
  S approvedBy;
}

// in main DB
concept Session {
  S cookie;
  S selectedSlice; // global ID
  Page slicePage; // phasing out
}

static SS searchTypeToText = litmap(
  leven := "Leven",
  literal := "Literal",
  scored := "Scored");

static int displayLength = 140;
static int sideDisplayLength = 50; // when in side bar
static int searchResultsToShow = 50;
sS sideSearchType = 'literal;

static int lines;
sbool asyncSearch = false;
sbool allowMultipleCasesInValues = true;
sbool showSliceSelector = true;

static ConceptsLoadedOnDemand fan;
static Map<S, LoadedSlice> loadedSlices = syncMap();

sclass LoadedSlice {
  S caseID;
  Concepts cc;
  Slice sliceConcept;
  
  ConceptFieldIndexDesc idx_latestEntries, idx_latestCreatedPages, idx_latestChangedPages;
  
  *(S *caseID, Concepts *cc) {
    indexThings();
  }
  
  Page pageFromQ(S q) {
    ret empty(q) ? null : uniqCI_sync(cc, Page, +q);
  }

  void indexThings() {
    indexConceptFieldsCI(cc, Page, 'url, Page, 'q, Entry, 'key, Entry, 'value);
    indexConceptFields(cc, Signer, 'publicKey);
    indexConceptFields(cc, Session, 'cookie);
    idx_latestCreatedPages = new ConceptFieldIndexDesc(cc, Page, 'created);
    idx_latestChangedPages = new ConceptFieldIndexDesc(cc, Page, '_modified);
    idx_latestEntries = new ConceptFieldIndexDesc(cc, Entry, 'created);
  }
  
  bool isMainSlice() { ret empty(caseID); }
}

p {
  fan = new ConceptsLoadedOnDemand;
  fan.onCaseLoaded(voidfunc(S caseID, Concepts concepts) {
    print("onCaseLoaded " + quote(caseID));
    loadedSlices.put(caseID, new LoadedSlice(caseID, concepts));
  });
  mainConcepts = fan.get("");
  indexConceptFields(Slice, 'globalID, Slice, 'caseID);
  cset(uniq_returnIfNew Slice(caseID := ""), name := "main slice");

  // legacy conversions!
  deleteConcepts(Slice, globalID := GlobalID('wftlawbagrwprywn));
  //moveSlicelessPagesToTheWildSlice();

  // Approve this machine's key
  PKIKeyPair machineKey = agiBot_trustedKeyForMachine();
  if (machineKey != null) {
    print("Approving this machine's key: " + machineKey.publicKey);
    cset(uniq_sync(Signer, publicKey := machineKey.publicKey), trusted := true, approvedBy := "local");
  }
  
  lines = countLines(mySource());
}

// Serve page

set flag NoNanoHTTPD. html {
  ret new Request().serve(uri, params);
}

sclass Request {
  S cookie;
  Session session;
  S uri;
  SS params;
  Concepts cc;
  LoadedSlice slice;
  
  // should also work for standalone
  bool isHomeDomain() {
    S domain = domain();
    ret eqic(domain, "www.agi.blue") || !ewic(domain, ".agi.blue");
  }
  
  O serve(S uri, SS params) {
    this.uri = dropMultipleSlashes(uri);
    this.params = params;
    new Matches m;
    
    print(uri + " ? " + unnull(subBot_query()));
    
    // get cookie & session
    
    cookie = cookieFromUser();
    if (cookie != null)
      session = uniq_sync(Session, +cookie);
    else
      session = unlistedWithValues(Session);
      
    // check if user wants to change slices
      
    S selectSlice = params.get('slice);
    if (isGlobalID(selectSlice))
      cset(session, selectedSlice := selectSlice);

    // get case ID
    
    S caseID = "";
    Slice sliceConcept = sliceConceptForGlobalID(session.selectedSlice);
    print("Selected slice: " + session.selectedSlice + ", obj? " + (sliceConcept != null));
    if (sliceConcept != null) caseID = sliceConcept.caseID;
    print("caseID: " + caseID);
    
    // load slice
    
    slice = assertNotNull(loadSlice(caseID));
    cc = slice.cc;
    slice.sliceConcept = sliceConcept;
      
    // Check for special URIs

    if (swic(uri, "/bot/")) ret serveBot();
    
    // eleu appends a slash to the URI if it's a single identifier, so we drop it again
    S uri2 = dropTrailingSlash(uri);
    
    if (eqic(uri2, "/search")) ret serveScoredSearch();
    if (eqic(uri2, "/literalSearch")) ret serveLiteralSearch();
    if (eqic(uri2, "/levenSearch")) ret serveLevenSearch();
    
    if (eqic(uri2, "/query")) ret serveQueryPage();
    if (eqic(uri2, "/createSlice")) ret serveCreateSlicePage();
    
    S q = params.get('q);
    S domain = or2(params.get('domain), domain());
    
    S raw = firstKeyWithValue("", params); // agi.blue?something
    if (nempty(raw) && empty(q)) q = raw;
    /*if (nempty(q)) {
      domain = makeAGIDomain(q);
      if (l(domain) > maximumDomainPartLength()) // escape with "domain="
        ret hrefresh(agiBlueURL() + hquery(+domain, key := "read as", value := q));
      ret hrefresh("http://" + domain + (eq(q, domain) ? "" : "/" +  hquery(key := "read as", value := q)));
      //uri = "/"; replaceMapWithParams(params, key := "read as", value := q);
    }*/
    S url = domain + dropTrailingSlash(uri);
    
    // domain to query
    //if (empty(q)) q = url;
    if (empty(q)) q = agiBlue_urlToQuery(url);
    
    Page page, bool newPage = unpair uniqCI2_sync(cc, Page, +q);
    if (newPage) dbLog("New page", +q);
    //printStructs(+params, +raw, +q);
  
    S top = hcomment("cookie: " + takeFirst(4, session.cookie))
      + hSilentComputatorWithFlag("agi.blue: " + q)
      + p(ahref(agiBlueURL(),
        //hsnippetimg(#1101682, width := 565/5, height := 800/5, title := "Robot by Peerro @ DeviantArt")
        hsnippetimg(#1101778, width := 96, height := 96, title := "agi.blue - a database for everything")
      ))
      + p(small(
          b(agiBlueNameHTML())
        + (agiBlue_isOriginal() ? "" : " " + targetBlank("http://agi.blue", "[original]"))
        + " | " + targetBlank(progLink(), "source code") + " of this web site (" + nLines(lines) + ") | " + targetBlank("https://gitter.im/agi-blue/community", "sponsor https?") + " | by " + targetBlank("https://BotCompany.de", "BC") + " | " + targetBlank("http://fiverr.tinybrain.de/", "Fiverr") + " | " + targetBlank("https://discordapp.com/invite/SEAjPqk", "Discord") + " | " + targetBlank("https://www.youtube.com/watch?v=b6jtRdV3Ev8", "Video")
        + " | " + targetBlank("http://code.botcompany.de/1024233", "Notes")
        + " | " + ahref(agiBlueURL() + "/query", "Query")
      ));
    
    if (empty(params.get('q)) && empty(raw) && isHomeDomain()) {
      L<Page> pages = sortedByFieldDesc _modified(list(cc, Page)); // TODO: use index
      int start = parseInt(params.get("start")), step = 100;
      S nav = pageNav2("/", l(pages), start, step, "start");
      ret hhtml2(hhead_title("agi.blue Overview") // SERVE HOME PAGE
        + hbody(hfullcenterAndTopLeft(top
          + hform(b("GIVE ME INPUT:") + "<br><br>" + htextinput('q, autofocus := true) + " " + hsubmit("Ask"))
          + h1(sliceAsHTML() + " has " + nPages(countConcepts(cc, Page)) + " and " + nEntries(countConcepts(cc, Entry)))
          + p(nav)
          + p_nemptyLines(map(pageToHTMLLink(), subList(pages, start, start+step))),
          sliceSelector()
        )));
    }
        
    S key = trim(params.get('key)), value = trim(params.get('value));
    L<Entry> entries = GetEntriesAndPost().go(page, params).entries;
    
    Set<S> get = asCISet(nempties(subBot_paramsAsMultiMap().get('get)));
    //S get = params.get('get);
    if (nempty(get))
      ret serveJSON(collect value(llNotNulls(firstThat(entries, e -> get.contains(e.key)))));
    
    S key2 = key, value2 = value; if (nempty(key) && nempty(value)) key2 = value2 = ""; // input reset
  
    bool withHidden = eq(params.get('withHidden), "1");
    new Set<Int> hide;
    if (!withHidden) for (Entry e : entries) if (eqic(e.key, 'hide) && isSquareBracketedInt(e.value)) addAll(hide, e.count, parseInt(unSquareBracket(e.value)));
    new MultiMap<Int, S> mmMeta;
    for (Entry e : entries) if (isSquareBracketedInt(e.key)) mmMeta.put(parseInt(unSquareBracket(e.key)), e.value);
    new MultiMap<S> mm;
    for (Entry e : entries) mm.put(e.key, e.value);
    
    //S name = or2(/* ouch */ last(mm.get("read as")), /* end ouch */ unpackAGIDomain(page.url), page.url);
    S name = page.q;
    
    // Find references
    
    L<Entry> refs = concatLists(conceptsWhereIC Entry(value := name),
      conceptsWhereIC Entry(key := name));
    Set<Page> refPages = asSet(ccollect page(refs));
    refPages.remove(page); // don't list current page as reference
    
    // Search in page names (depending on default search type)
    
    L<Page> searchResults;
    if (eq(sideSearchType, 'leven))
      searchResults = levenSearch(page.q, max := 50);
    else if (eq(sideSearchType, 'literal))
      searchResults = literalSearch(page.q, max := 50);
    else
      searchResults = (L<Page>) dm_call('agiBlueSearch, 'search, page.q, maxResult := searchResultsToShow+1, agiBlueBotID := programID());
    searchResults.remove(page);

    S mainContents =
        top + h1(ahref_unstyled("http://" + url + hquery(+q), htmlEncode2(shorten(displayLength, name))) + (newPage ? " [huh????]" : ""))
      + p_nemptyLines(map(entries, func(Entry e) -> S {
        !withHidden && (hide.contains(e.count) || eqic(e.key, "read as") && eqic(e.value, name)) ? ""
        : "[" + e.count + "] " +
          renderThing(e.key, false) + ": " +
          b(renderThing(e.value, cic(mmMeta.get(e.count), "is a URL")))
        }))
      + hpostform(h3("Add an entry")
        + "Key: " + hinputfield(key := key2) + " Value: " + hinputfield(value := value2) + "<br><br>" + hsubmit("Add")
        )
        
      + p(ahref(agiBlueURL() + "/literalSearch" + hquery(q := page.q), "[literal search]", title := "Search pages with a name containing this page's name literally")
       + " " +
       ahref(agiBlueURL() + "/levenSearch" + hquery(q := page.q), "[leven search 1]", title := "Search pages with a Levenshtein similarity of 1 containing this page's name literally")
       + " " +
       ahref(agiBlueURL() + "/search" + hquery(q := page.q), "[scored search]", title := "Search pages with ScoredSearch"));
        
    S sideContents =
      hform(b("GIVE ME INPUT:") + " "
        + htextinput('q) + " "
        + hsubmit("Ask", onclick := "document.getElementById('newInputForm').target = '';") + " "
        + hsubmit("+Tab", title := "Ask and show result in a new tab", onclick := "document.getElementById('newInputForm').target = '_blank';"),
        id := 'newInputForm)
      
      + h3("References (" + l(refPages) + ")")
      
      + p_nemptyLines_showFirst(10, map(pageToHTMLLink(displayLength := sideDisplayLength), refPages))
      
      + h3(searchTypeToText.get(sideSearchType) + " search results (" + (l(searchResults) >= searchResultsToShow ? searchResultsToShow + "+" : str(l(searchResults))) + ")")
      
      + p_nemptyLines_showFirst(searchResultsToShow, map(pageToHTMLLink(displayLength := sideDisplayLength), searchResults))
      
      + hdiv("", id := 'extraStuff);
      
    // TODO: sync search delivery with WebSocket creation
    if (asyncSearch)
      doLater(6.0, r { dm_call('agiBlueSearch, 'searchAndPost, page.q, agiBlueBotID := programID()) });
      
    // serve a concept page
    ret hhtml2(hhead_title(pageDisplayName(page)) + hbody(
      tag('table,
        tr(
          td(sliceSelector(), valign := 'top)
        + td(sideContents, align := 'right, valign := 'top, rowspan := 2))
      + tr(td(mainContents, align := 'center, valign := 'top)),
      width := "100%", height := "100%")));
  }
  
  O servePagesToBot(Iterable<Page> pages) {
    ret serveListToBot(map(pageToMap(wrapMapAsParams(params)), pages));
  }

  O serveListToBot(Collection l) {
    if (nempty(params.get('max)))
      l = takeFirst(parseInt(params.get('max)), l);
    ret serveJSON(l);
  }

  // uri starts with "/bot/"
  O serveBot() {
    S q = params.get('q);
    
    if (eqic(uri, "/bot/hello")) ret serveJSON("hello");
    if (eqic(uri, "/bot/hasPage")) ret serveJSON(hasPage(q));
    if (eqic(uri, "/bot/randomPageContaining")) {
      assertNempty(q);
      ret servePageToBot(random(filter(list(cc, Page), p -> cic(p.q, q))), params);
    }
    if (eqic(uri, "/bot/allPages"))
      ret servePagesToBot(list(cc, Page));
    if (eqic(uri, "/bot/allPagesStartingWith")) {
      assertNempty(q);
      ret servePagesToBot(filter(list(cc, Page), p -> swic(p.q, q)));
    }
    if (eqic(uri, "/bot/allPagesEndingWith")) {
      assertNempty(q);
      ret servePagesToBot(filter(list(cc, Page), p -> ewic(p.q, q)));
    }
    if (eqic(uri, "/bot/allPagesContaining")) {
      assertNempty(q);
      ret servePagesToBot(filter(list(cc, Page), p -> cic(p.q, q)));
    }
    if (eqic(uri, "/bot/allPagesContainingRegexp")) {
      assertNempty(q);
      Pattern pat = regexpIC(q);
      ret servePagesToBot(filter(list(cc, Page), p -> regexpFindIC(pat, p.q)));
    }
    
    if (eqicOneOf(uri, "/bot/postSigned", "/bot/makePhysicalSlice", "/bot/approveTrustRequest")) {
      S text = rtrim(params.get('text));
      S key = getSignerKey(text);
      if (empty(key)) ret subBot_serve500("Please include your public key");
      if (!isSignedWithKey(text, key)) ret subBot_serve500("Signature didn't verify");
      text = dropLastTwoLines(text); // drop signer + sig line
      
      Signer signer = uniq_sync Signer(publicKey := key);
      
      if (eqic(uri, "/bot/makePhysicalSlice")) {
        if (!signer.trusted) ret subBot_serve500("Untrusted signer");
        Page page = findPageFromParams(jsonDecodeMap(text));
        if (page == null) ret subBot_serve500("Page not found");
        ret serveJSON(uniq2_sync(PhysicalSlice, slicePage := page).b ? "Slice made" : "Slice exists");
      }
    
      if (eqic(uri, "/bot/postSigned")) {
        new L out;
        for (S line : tlft(text)) {
          SS map = jsonDecodeMap(line);
          new GetEntriesAndPost x;
          x.signer = signer;
          Page page = findOrMakePageFromParams(map);
          if (page == null) continue with out.add("Invalid page reference");
          x.go(page, map);
          out.add(x.newEntry ? "Saved" : x.entry != null ? "Entry exists" : "Need key and value");
        }
        ret serveJSON(out);
      }
      
      if (eqic(uri, "/bot/approveTrustRequest")) {
        if (!signer.trusted) ret subBot_serve500("Untrusted signer");
        Signer toApprove = conceptWhere Signer(publicKey := trim(text));
        if (toApprove == null) ret subBot_serve500("Signer to approve not found");
        cset(toApprove, trusted := true, approvedBy := signer.globalID);
        ret serveJSON("Approved: " + trim(text));
      }
      
      ret subBot_serve500("CONFUSION");
    }
    
    if (eqic(uri, "/bot/post")) {
      new GetEntriesAndPost x;
      x.go(pageFromQ(q), params);
      ret serveJSON(x.newEntry ? "Saved" : x.entry != null ? "Entry exists" : "Need key and value");
    }
    
    if (eqic(uri, "/bot/entriesOnPage"))
      ret serveJSON(map(entriesOnPage(findPageFromParams(params)), entryToMap(false)));
  
    if (eqic(uri, "/bot/lookup")) {
      S key = params.get('key);
      if (empty(key)) ret serveJSON("Need key");
      S value = getValue(findPageFromParams(params), key);
      ret serveJSON(empty(value) ? "" : litmap(+value));
    }
  
    if (eqic(uri, "/bot/latestEntries"))
      ret serveJSON(map(takeFirst(10, slice.idx_latestEntries.objectIterator()), entryToMap(true)));
    if (eqic(uri, "/bot/latestPages"))
      ret serveJSON(map(takeFirst(10, slice.idx_latestCreatedPages.objectIterator()), pageToMap()));
    if (eqic(uri, "/bot/latestChangedPages"))
      ret serveJSON(map(takeFirst(10, slice.idx_latestChangedPages.objectIterator()), pageToMap()));
      
    if (eqic(uri, "/bot/totalPageCount"))
      ret serveJSON(countConcepts(Page));
    if (eqic(uri, "/bot/pageWithoutPhysicalSliceCount"))
      ret serveJSON(countConceptsWhere(Page, slice := null));
    if (eqic(uri, "/bot/physicalSliceCount"))
      ret serveJSON(countConcepts(PhysicalSlice));
    if (eqic(uri, "/bot/trustedSignersCount"))
      ret serveJSON(countConcepts(Signer, trusted := true));
      
    if (eqic(uri, "/bot/valueSearch")) {
      S value = params.get('value);
      L<Entry> entries = conceptsWhereIC Entry(+value);
      ret serveJSON(map(takeFirst(100, entries), entryToMap(true)));
    }
      
    if (eqic(uri, "/bot/keyAndValueSearch")) {
      S key = params.get('key), value = params.get('value);
      Cl<Page> pages = pagesForKeyAndValue(key, value);
      ret servePagesToBot(pages);
    }
    
    if (eqic(uri, "/bot/keyValuePairsByPopularity")) {
      L<PairS> pairs = map(list(Entry), e -> pair(e.key, e.value));
      LPair<S, Int> pairs2 = multiSetTopPairs(ciMultiSet(map pairToUglyStringForCIComparison(pairs)));
      ret serveJSON(map(pairs2, p -> {
        S key, value = unpair pairFromUglyString(p.a);
        ret litorderedmap(n := p.b, +key, +value);
      }));
    }
    
    if (eqic(uri, "/bot/allKeys"))
      ret serveListToBot(distinctCIFieldValuesOfConcepts Entry('key));
      
    if (eqic(uri, "/bot/allKeysByPopularity"))
      ret serveListToBot(mapMultiSetByPopularity(distinctCIFieldValuesOfConcepts_multiSet Entry('key), (key, n) -> litorderedmap(+n, +key)));
      
    if (eqic(uri, "/bot/dbSize"))
      ret serveJSON(l(conceptsFile()));
      
    if (eqic(uri, "/bot/query")) {
      S query = params.get('query);
      L<ALQLLine> lines = agiBlue_parseQueryScript(query);
      
      SS vars = ciMap();
      for (ALQLLine line : lines) {
        if (line cast ALQLReturn)
          ret serveJSON(ll(getOrKeep(vars, line.var)));
        else if (line cast ALQLTriple) {
          T3S t = line.triple;
          t = tripleMap(t, s -> getOrKeep(vars, s));
          Cl<Page> pages;
          S var;
          if (isDollarVar(t.c)) {
            var = t.c;
            if (isDollarVar(t.a)) {
              if (isDollarVar(t.b)) todo(t);
              Entry e = random(conceptsWhereCI Entry(key := t.b));
              if (e == null) ret serveJSON("No results for " + var);
              pages = pagesForKeyAndValue(t.b, t.c);
              vars.put(t.a, e.page->q);
              vars.put(t.c, e.value);
              continue;
            } else if (isDollarVar(t.b)) {
              Page page = findPageFromQ(t.a);
              Entry e = random(findBackRefs(page, Entry));
              if (e == null) ret serveJSON("No results for " + var);
              vars.put(t.b, e.key);
              vars.put(t.c, e.value);
              continue;
            } else {
              S val = getValue(t.a, t.b);
              if (val == null) ret serveJSON("No results for " + var);
              pages = ll(pageFromQ(val));
            }
          } else if (isDollarVar(t.b)) {
            var = t.b;
            if (isDollarVar(t.c)) todo(t);
            if (isDollarVar(t.a)) {
              L<Entry> entries = conceptsWhereCI Entry(value := t.c);
              if (empty(entries)) ret serveJSON("No results for " + var);
              Entry e = random(entries);
              vars.put(t.a, e.page->q);
              vars.put(t.b, e.key);
              continue;
            } else {
              Cl<S> keys = keysForPageAndValue(t.a, t.c);
              if (empty(keys)) ret serveJSON("No results for " + var);
              pages = map(f<S, Page> pageFromQ, keys);
            }
          } else {
            var = t.a;
            if (!isDollarVar(t.a)) todo(t);
            if (isDollarVar(t.b)) todo(t);
            if (isDollarVar(t.c)) todo(t);
            pages = pagesForKeyAndValue(t.b, t.c);
          }
          if (empty(pages)) ret serveJSON("No results for " + var);
          vars.put(var, random(pages).q);
        } else
          fail("Can't interpret: " + line);
      }
      
      ret serveJSON("No return statement");
    }

    if (eqic(uri, "/bot/createSlice")) {
      Slice slice = createSlice(assertNempty(params.get('name)));
      ret serveJSON(litorderedmap(id := str(slice.globalID), name := slice.name));
    }
    
    // end of serveBot()
  
    ret subBot_serve404();
  }
  
  O serveLiteralSearch() {
    S q = params.get('q);
    L<Page> searchResults = literalSearch(q);
    ret serveSearchResults("literal search" , q, searchResultsToShow, searchResults);
  }
  
  L<Page> literalSearch(S q, O... _) {
    int searchResultsToShow = optPar max(_, 100);
    
    // quick search in random order
    //L<Page> searchResults = takeFirst(searchResultsToShow+1, filterIterator(iterator(list(cc, Page)), p -> cic(p.q, q));
    
    // full search, order by length
    ret takeFirst(searchResultsToShow+1, pagesSortedByLength(filter(list(cc, Page), p -> cic(p.q, q))));
  }
  
  O serveScoredSearch() {
    S q = params.get('q);
    L<Page> searchResults = (L<Page>) dm_call('agiBlueSearch, 'search, q);
    ret serveSearchResults("scored search" , q, searchResultsToShow, searchResults);
  }
  
  O serveLevenSearch() {
    S q = params.get('q);
    L<Page> searchResults = levenSearch(q);
    ret serveSearchResults("leven search with distance 1" , q, searchResultsToShow, searchResults);
  }
  
  L<Page> levenSearch(S q, O... _) {
    int searchResultsToShow = optPar max(_, 100);
    int maxEditDistance = 1;
    
    new Map<Page, Int> map;
    for (Page p) {
      int distance = leven_limitedIC(q, p.q, maxEditDistance+1);
      if (distance <= maxEditDistance) map.put(p, distance);
    }
    ret takeFirst(searchResultsToShow+1, keysSortedByValue(map));
  }
  
  O serveSearchResults(S searchType, S q, int searchResultsToShow, Collection<Page> searchResults) {
    S title = "agi.blue " + searchType + " for " + htmlEncode2(quote(q)) + " (" + (l(searchResults) >= searchResultsToShow ? searchResultsToShow + "+ results" : nResults(l(searchResults))) + ")";
    
    ret hhtml2(hhead_title(htmldecode_dropAllTags(title))
      + hbody(hfullcenter(//top +
        h3(title)
      + p_nemptyLines_showFirst(searchResultsToShow, map(pageToHTMLLink(), searchResults)))));
  }
  
  O serveQueryPage() {
    S query = params.get('query);
    if (query == null) query = loadSnippet(#1024258);
    S title = agiBlueNameHTML() + " | Execute a query script (" + targetBlank("http://code.botcompany.de/1024274", "ALQL") + ")";
    ret hhtml2(hhead_title(htmldecode_dropAllTags(title))
      + hbody(hfullcenter(
        h3(title)
      + form(
        htextarea(query, name := 'query, cols := 80, rows := 10, autofocus := true)
        + "<br><br>"
        + hsubmit("Execute"), action := "/bot/query")
      )));    
  }
  
  O serveCreateSlicePage() {
    S sliceName = trim(params.get('sliceName));
    if (eq(params.get('doIt), "1") && nempty(sliceName)) {
      // TODO: check for existing name
      Slice slice = createSlice(sliceName);
      ret hrefresh(agiBlueURL() + hquery(slice := slice.globalID));
    }
    
    S title = agiBlueNameHTML() + " | Create slice";
    ret hhtml2(hhead_title(htmldecode_dropAllTags(title))
      + hbody(hfullcenter(
        h3(title)
      + form(
          hhidden(doIt := 1)
        + "Slice name: " + htextinput(+sliceName, autofocus := true)
        + "<br><br>"
        + hsubmit("Create slice"))
      )));    
  }
  
  Page findPageFromParams(Map map) {
    S q = getString q(map);
    ret empty(q) ? null : findPageFromQ(q);
  }

  Page findOrMakePageFromParams(Map map) {
    ret pageFromQ(getString q(map));
  }
  
  Page findPageFromQ(S q) {
    ret conceptWhereCI(cc, Page, +q);
  }
  
  Page pageFromQ(S q) {
    ret slice.pageFromQ(q);
  }
  
  F1<Page, S> pageToHTMLLink(O... _) {
    optPar int displayLength = main.displayLength;
    ret func(Page p) -> S {
      S name = pageDisplayName(p);
      ret ahref(agiBlueURL() + hquery(q := p.q),
        htmlEncode2(shorten(displayLength, name)),
        title := name);
    };
  }
  
  // DB functions

  bool hasPage(S q) { ret hasConceptWhereIC(Page, +q); }
  S getValue(Page page, S key) {
    ret page == null || empty(key) ? null : getString value(highestByField count(objectsWhereIC(findBackRefs(page, Entry), +key)));
  }
  S getValue(S page, S key) {
    ret getValue(findPageFromQ(page), key);
  }
  S pageDisplayName(Page page) {
    /*S name = getValue(page, "read as");
    bool unnaturalName = nempty(name) && !eq(makeAGIDomain(name), page.url);
    ret unnaturalName ? name + " " + squareBracketed(page.url) : or2(name, unpackAGIDomainOpt(page.url));*/
    ret page.q;
  }

  Set<Page> pagesForKeyAndValue(S key, S value) {
    L<Entry> entries = conceptsWhereIC Entry(+value, +key);
    ret asSet(ccollect page(entries));
  }
  
  Cl<S> keysForPageAndValue(S q, S value) {
    Page page = findPageFromQ(q);
    if (page == null) null;
    ret collect key(conceptsWhereIC Entry(+page, +value));
  }
  
  S sliceSelector() {
    ret !showSliceSelector ? "" : hform(
        "Select reality slice: "
      + hselect(availableSlices(), session.selectedSlice, name := 'slice)
      /*+ " " + hsubmit("Go")*/
      + " &nbsp; " + ahref(agiBlueURL() + "/createSlice", "Create slice...")
      , onchange := "this.form.submit()");
  }
  
  S sliceAsHTML() {
    if (slice.isMainSlice()) ret htmlEncode2(agiBlueName() + "'s main slice");
    if (slice.sliceConcept == null) ret "Slice ???";
    ret htmlEncode2("Slice " + quote(slice.sliceConcept.name));
  }

} // end of Request

svoid dbLog(O... params) {
  logStructure(programFile("db.log"), ll(params));
}

static IF1<Page, Map> pageToMap(O... _) {
  optPar bool withEntries;
  
  bool nameOnly = eqOneOf(optPar nameOnly(_), "1", true);
  if (nameOnly) ret (IF1<Page, Map>) p -> litmap(q := p.q);
  
  ret (IF1<Page, Map>) p -> {
    L<Entry> entries = findBackRefs(p, Entry);
    ret litorderedmap(
      q := p.q,
      nEntries := l(entries),
      created := p.created,
      modified := p._modified,
      entries := !withEntries ? null : map(entries, entryToMap(false)));
  };
}

static IF1<Entry, Map> entryToMap(bool withPage) {
  ret (IF1<Entry, Map>) e -> litorderedmap(
    created := e.created,
    i := e.count,
    key := e.key,
    value := e.value,
    q := withPage ? e.page->q : null,
    signer := getString globalID(e.signer!));
}

sO servePageToBot(Page page, SS params) {
  if (page == null) ret serveJSON(null);
  params = asCIMap(params);
  Map map = pageToMap(withEntries := valueIs1 withEntries(params)).get(page);
  ret serveJSON(map);
}

sclass GetEntriesAndPost {
  L<Entry> entries;
  Entry entry;
  bool newEntry;
  Signer signer;
  
  GetEntriesAndPost go(Page page, SS params) {
    S key = trim(params.get('key)), value = trim(params.get('value));
    print("GetEntriesAndPost: " + quote(page.q) + ", " + quote(key) + " := " + quote(value));
    withDBLock {
      entries = findBackRefs(page, Entry);
      if (nempty(key) && nempty(value)) {
        S ip = subBot_clientIP();
        entry = firstThat(e -> eqic(e.key, key) && eq_icIf(!allowMultipleCasesInValues, e.value, value) && eq(e.ip, ip), entries);
        if (entry == null) {
          print("SAVING");
          Entry e = cnew Entry(+page, +key, +value, +ip, count := l(entries) + 1, +signer);
          page.change(); // bump modification date
          entry = e;
          newEntry = true;
          entries.add(entry);
          dbLog("New entry", page := page.q, globalID := e.globalID, count := e.count, +key, +value);
        }
      }
    }

    sortByFieldInPlace created(entries);
    numberEntriesInConceptField count(entries);
    this;
  }
}

static Collection<S> backSearch(S key, S value) {
  // we query the index for the value field because that yields fewer results
  ret map(conceptsWhereCI Entry(value := "a slice of reality", key := "is"), e -> e.page->q);
}

static SS availableSlices() {
  ret mapToOrderedMap(s -> pair(str(s.globalID), s.name + " [ID: " + s.globalID + "]"), list(Slice));
}

static PhysicalSlice getOrMakePhysicalSlice(Concepts cc, Page p) {
  PhysicalSlice slice = first(findBackRefs PhysicalSlice(p));
  if (slice == null)
    slice = uniq_sync(cc, PhysicalSlice, slicePage := p);
  ret slice;
}

static PhysicalSlice theWildSlice() {
  ret getOrMakePhysicalSlice(db_mainConcepts(), loadedSlices.get("").pageFromQ("the wild slice"));
}

/*svoid moveSlicelessPagesToTheWildSlice() {
  PhysicalSlice slice = theWildSlice();
  for (Page p : conceptsWhere Page(slice := null))  {
    print("Moving to wild slice: " + p.q);
    cset_sync(p, +slice);
  }
}*/

sS renderThing(S s, bool forceURLDisplay) {
  ret forceURLDisplay || isURL(s) || isAGIDomain(s)
    ? ahref(fixAGILink(absoluteURL(s)), htmlencode2(shorten(displayLength, s)))
    : ahref(agiBlue_linkForPhrase(s), htmlencode2(shorten(displayLength, s)));
}

static L<Entry> entriesOnPage(Page p) {
  ret p == null ? null : sortedByField count(findBackRefs(p, Entry));
}

static L<Page> pagesSortedByLength(L<Page> l) {
  ret sortedByCalculatedField(l, p -> l(p.q));
}

sS hhtml2(S contents) {
  ret hhtml(hAddToHead_fast(contents, 
    hIncludeGoogleFont("Source Sans Pro")
    + hmobilefix()
    + hstylesheet("body { font-family: Source Sans Pro }")));
}

sbool agiBlue_isOriginal() {
  ret amProgram(#1023558);
}

sS agiBlueURL() {
  ret agiBlue_isOriginal() ? "http://agi.blue" :  "/" + psI(programID()) + "/raw";
}

sS agiBlueName() {
  ret agiBlue_isOriginal() ? "agi.blue" : "agi.blue clone " + programID();
}

svoid cleanMeUp {
  cleanUp(fan);
}

static LoadedSlice loadSlice(S caseID) {
  caseID = unnull(caseID);
  fan.get(caseID);
  ret loadedSlices.get(caseID);
}

static Slice sliceConceptForGlobalID(S globalID) {
  ret conceptWhere Slice(globalID := toGlobalIDObj(globalID));
}

static Slice sliceConceptForCaseID(S caseID) {
  ret conceptWhere Slice(+caseID);
}

sS agiBlueNameHTML() {
  ret ahref(agiBlueURL(), htmlEncode2(agiBlueName()));
}

static Slice createSlice(S name) {
  Slice slice = cnew Slice(+name);
  cset(slice, caseID := slice.defaultCaseID());
  ret slice;
}

Author comment

Began life as a copy of #1024284

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1024288
Snippet name: agi.blue source [adding slices, dev.]
Eternal ID of this version: #1024288/60
Text MD5: 94034d028c6e722e81b59c7de83e40f5
Transpilation MD5: e9bf0b87da09144e9d08544afe548ed5
Author: stefan
Category: javax / agi.blue
Type: JavaX module (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-07-30 14:07:14
Source code size: 31456 bytes / 873 lines
Pitched / IR pitched: No / No
Views / Downloads: 286 / 1819
Version history: 59 change(s)
Referenced in: [show references]