summary refs log tree commit diff
path: root/search.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-12 17:47:58 -0400
committerJune McEnroe <june@causal.agency>2020-07-12 17:47:58 -0400
commitb5d469b648f9bd56bc4d4f9a189631dfbe28d5e1 (patch)
tree943258efcb161f63dff6bdf9a650b1eb08bcb3f5 /search.c
parentRewrite default CSS (diff)
downloadscooper-b5d469b648f9bd56bc4d4f9a189631dfbe28d5e1.tar.gz
scooper-b5d469b648f9bd56bc4d4f9a189631dfbe28d5e1.zip
Add search paging links
Diffstat (limited to 'search.c')
-rw-r--r--search.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/search.c b/search.c
index 7960d6b..2d6e417 100644
--- a/search.c
+++ b/search.c
@@ -64,6 +64,30 @@ enum kcgi_err searchPage(struct kreq *req) {
 		|| khtml_elem(&html, KELEM_TABLE);
 	if (error) return error;
 
+	if (offset) {
+		int64_t prev = offset - eventsLimit;
+		if (prev < 0) prev = 0;
+		char *href = khttp_urlpartx(
+			NULL, NULL, Pages[Search],
+			Keys[Query].name, KATTRX_STRING, scope.query,
+			Keys[Offset].name, KATTRX_INT, prev,
+			(scope.network ? Keys[Network].name : NULL),
+			KATTRX_STRING, scope.network,
+			(scope.context ? Keys[Context].name : NULL),
+			KATTRX_STRING, scope.context,
+			NULL
+		);
+		if (!href) err(EX_OSERR, "khttp_urlpartx");
+		error = 0
+			|| khtml_attr(&html, KELEM_TR, KATTR_CLASS, "page", KATTR__MAX)
+			|| khtml_attr(&html, KELEM_TH, KATTR_COLSPAN, "5", KATTR__MAX)
+			|| khtml_attr(&html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
+			|| khtml_puts(&html, "Earlier results")
+			|| khtml_closeelem(&html, 3);
+		free(href);
+		if (error) return error;
+	}
+
 	sqlite3_reset(stmt.search);
 	dbBindText(stmt.search, ":network", scope.network);
 	dbBindText(stmt.search, ":context", scope.context);
@@ -73,8 +97,9 @@ enum kcgi_err searchPage(struct kreq *req) {
 	dbBindInt(stmt.search, ":offset", offset);
 	dbBindText(stmt.search, ":highlight", "\26");
 
+	int rows;
 	int result;
-	while (SQLITE_ROW == (result = sqlite3_step(stmt.search))) {
+	for (rows = 0; SQLITE_ROW == (result = sqlite3_step(stmt.search)); ++rows) {
 		int i = 0;
 		struct Event event = {0};
 		event.event = sqlite3_column_int64(stmt.search, i++);
@@ -91,5 +116,28 @@ enum kcgi_err searchPage(struct kreq *req) {
 		if (error) return error;
 	}
 	if (result != SQLITE_DONE) errx(EX_SOFTWARE, "%s", sqlite3_errmsg(db));
+
+	if (rows == eventsLimit) {
+		char *href = khttp_urlpartx(
+			NULL, NULL, Pages[Search],
+			Keys[Query].name, KATTRX_STRING, scope.query,
+			Keys[Offset].name, KATTRX_INT, offset + eventsLimit,
+			(scope.network ? Keys[Network].name : NULL),
+			KATTRX_STRING, scope.network,
+			(scope.context ? Keys[Context].name : NULL),
+			KATTRX_STRING, scope.context,
+			NULL
+		);
+		if (!href) err(EX_OSERR, "khttp_urlpartx");
+		error = 0
+			|| khtml_attr(&html, KELEM_TR, KATTR_CLASS, "page", KATTR__MAX)
+			|| khtml_attr(&html, KELEM_TH, KATTR_COLSPAN, "5", KATTR__MAX)
+			|| khtml_attr(&html, KELEM_A, KATTR_HREF, href, KATTR__MAX)
+			|| khtml_puts(&html, "Later results")
+			|| khtml_closeelem(&html, 3);
+		free(href);
+		if (error) return error;
+	}
+
 	return htmlFooter(&html) || khtml_close(&html);
 }