Skip to content

Commit 35df6c9

Browse files
roed314claude
andcommitted
Hide ui.-prefixed knowls from the public knowl index (LMFDB#3721)
Knowls whose id starts with "ui." are user-interface helper texts (search-box help bubbles, sort-order and statistics-extent explanations, section landing-page intros) rather than context-free definitions, and should not clutter the public knowl browse/search index. In lmfdb/knowledge/main.py index(), when the visitor is not logged in, drop knowls whose id starts with "ui." from the search results before categories are counted, so the "ui" category never appears publicly; also reset an explicit ?category=ui request to the empty category so no empty "ui" bucket is seeded. Logged-in editors still see everything. Verified with a logged-out flask test client: new lmfdb/knowledge/ test_knowledge.py patches knowldb.search to inject a synthetic ui.* knowl and asserts it (and its category) are hidden when logged out and shown when authenticated; the real /knowledge/ index still returns 200 with no "ui" category. pyflakes clean; existing test_dynamic_knowls tests still pass. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 5ef81bd commit 35df6c9

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

lmfdb/knowledge/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,15 @@ def index():
871871
else:
872872
flash_error("Unexpected error %s occurred during knowl search", str(e))
873873
all_knowls = []
874+
# Knowls whose id starts with "ui." (e.g. search-box help bubbles, sort-order
875+
# or statistics-extent explanations) are user-interface helper texts rather
876+
# than context-free definitions. They should not clutter the public knowl
877+
# browse/search index, so we hide them -- and their "ui" category -- from
878+
# visitors who are not logged in. See LMFDB issue #3721.
879+
if not current_user.is_authenticated:
880+
all_knowls = [k for k in all_knowls if not k["id"].startswith("ui.")]
881+
if cur_cat == "ui":
882+
cur_cat = ""
874883
categories = Counter()
875884
if cur_cat:
876885
# Always include the current category

lmfdb/knowledge/test_knowledge.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Tests for the knowledge (knowl) blueprint.
2+
3+
from lmfdb.tests import LmfdbTest
4+
from lmfdb.knowledge import main as knowl_main
5+
6+
7+
class KnowlUIVisibilityTest(LmfdbTest):
8+
"""
9+
Issue #3721: knowls whose id starts with ``ui.`` are user-interface helper
10+
texts (search-box help, sort-order and statistics-extent explanations, ...)
11+
rather than context-free definitions. They must be hidden from the public
12+
(logged-out) knowl browse/search index, together with their ``ui`` category.
13+
"""
14+
15+
# Stand-in for a knowldb.search() response: one ui. helper and one ordinary
16+
# (context-free) math-definition knowl.
17+
_fake_knowls = [
18+
{"id": "ui.demo_helper", "title": "Demo UI helper"},
19+
{"id": "ec.q.conductor", "title": "Conductor of an elliptic curve"},
20+
]
21+
22+
def _get_index(self):
23+
"""Render /knowledge/ with knowldb.search patched to return _fake_knowls."""
24+
knowl_main.knowldb.search = lambda *args, **kwargs: [dict(k) for k in self._fake_knowls]
25+
try:
26+
return self.tc.get("/knowledge/", follow_redirects=True).get_data(as_text=True)
27+
finally:
28+
# Remove the instance override, restoring the real bound method.
29+
del knowl_main.knowldb.search
30+
31+
def test_index_page_loads(self):
32+
# Regression: the public knowl index still renders.
33+
page = self.tc.get("/knowledge/", follow_redirects=True).get_data(as_text=True)
34+
assert "Knowledge database" in page
35+
36+
def test_ui_knowl_hidden_when_logged_out(self):
37+
page = self._get_index()
38+
# The ui. helper and its "ui" category are hidden from a logged-out user ...
39+
assert "ui.demo_helper" not in page
40+
assert "ui(1)" not in page
41+
# ... while ordinary knowls and their categories remain visible.
42+
assert "ec.q.conductor" in page
43+
assert "ec(1)" in page
44+
45+
def test_ui_category_query_hidden_when_logged_out(self):
46+
# Explicitly requesting ?category=ui must not surface an empty "ui" category.
47+
knowl_main.knowldb.search = lambda *args, **kwargs: [dict(k) for k in self._fake_knowls]
48+
try:
49+
page = self.tc.get("/knowledge/?category=ui", follow_redirects=True).get_data(as_text=True)
50+
finally:
51+
del knowl_main.knowldb.search
52+
assert "ui.demo_helper" not in page
53+
assert "ui(0)" not in page
54+
assert "ui(1)" not in page
55+
56+
def test_ui_knowl_visible_when_logged_in(self):
57+
# When authenticated, the filter is skipped and ui. knowls are listed.
58+
class _FakeUser:
59+
is_authenticated = True
60+
61+
def is_admin(self):
62+
return False
63+
64+
orig_user = knowl_main.current_user
65+
knowl_main.current_user = _FakeUser()
66+
knowl_main.knowldb.search = lambda *args, **kwargs: [dict(k) for k in self._fake_knowls]
67+
try:
68+
page = self.tc.get("/knowledge/", follow_redirects=True).get_data(as_text=True)
69+
finally:
70+
knowl_main.current_user = orig_user
71+
del knowl_main.knowldb.search
72+
assert "ui.demo_helper" in page
73+
assert "ec.q.conductor" in page

0 commit comments

Comments
 (0)