Check-in [1f8c4959f1]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Load properly comments
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | scgi
Files: files | file ages | folders
SHA1:1f8c4959f11bdd2022e78a8d563a011126bdeb5e
User & Date: bapt 2015-04-23 16:02:10
Context
2015-04-23
17:08
Use poll which is portable check-in: 7c87881fd0 user: bapt tags: scgi
16:02
Load properly comments check-in: 1f8c4959f1 user: bapt tags: scgi
16:01
if string is empty consider it as false check-in: bffa15ec7c user: bapt tags: scgi
Changes

Changes to web/cblogweb.c.

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78







79
80
81
82
83




84
85
86
87
88
89
90


91
92
93
94
95
96
97
...
100
101
102
103
104
105
106

107
108
109
110
111
112
113
114
115
116





















117
118
119
120


121
122
123
124
125
126
127
...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
...
519
520
521
522
523
524
525

526
527
528
529
530
531
532
533
534
535
536
537
	const char *timefmt;
	const char *tagname;
	int64_t start;
	int64_t end;
};

bool
add_posts(ucl_object_t *out, sqlite3_stmt *stmt, sqlite3 *sqlite)
{
	char *filename = NULL;
	int icol;
	sqlite3_stmt *stmt2;
	ucl_object_t *posts, *post, *tags, *o;
	bool ret = false;

	if (sqlite3_prepare_v2(sqlite,
	    "SELECT tag FROM tags, tags_posts, posts WHERE link=?1 AND posts.id=post_id and tag_id=tags.id;",
	    -1, &stmt2, NULL) != SQLITE_OK) {
		syslog(LOG_ERR, "%s", sqlite3_errmsg(sqlite));
		return (ret);
	}








	posts = ucl_object_typed_new(UCL_ARRAY);
	while (sqlite3_step(stmt) == SQLITE_ROW) {
		ret = true;
		sqlite3_reset(stmt2);




		post = ucl_object_typed_new(UCL_OBJECT);
		for (icol = 0; icol < sqlite3_column_count(stmt); icol++) {
			switch (sqlite3_column_type(stmt, icol)) {
			case SQLITE_TEXT:
				if (strcmp(sqlite3_column_name(stmt, icol), "filename") == 0) {
					filename = (char*)sqlite3_column_text(stmt, icol);
					sqlite3_bind_text(stmt2, 1, filename, -1, SQLITE_STATIC);


				}
				ucl_object_insert_key(post,
				    ucl_object_fromstring_common((const char *)sqlite3_column_text(stmt, icol), 0, UCL_STRING_TRIM),
				    sqlite3_column_name(stmt, icol),
				    0, true);
				break;
			case SQLITE_INTEGER:
................................................................................
				    sqlite3_column_name(stmt, icol),
				    0, true);
				break;
			default:
				break;
			}
		}

		tags = ucl_object_typed_new(UCL_ARRAY);
		while (sqlite3_step(stmt2) == SQLITE_ROW) {
			o = ucl_object_typed_new(UCL_OBJECT);
			ucl_object_insert_key(o,
			    ucl_object_fromstring((const char *)sqlite3_column_text(stmt2, 0)),
			    "name", 0, false);
			ucl_array_append(tags, o);
		}
		ucl_object_insert_key(post, tags, "tags", 0, false);
		ucl_array_append(posts, post);





















	}
	ucl_object_insert_key(out, posts, "Posts", 0, false);

	sqlite3_finalize(stmt2);



	return (ret);
}

void
set_tags(ucl_object_t *out, sqlite3 *sqlite)
{
................................................................................
bool
build_post(ucl_object_t *conf, ucl_object_t *out, const char *postname, sqlite3 *sqlite)
{
	sqlite3_stmt *stmt;
	bool ret = false;

	if (sqlite3_prepare_v2(sqlite,
		"SELECT link as filename, title, source, html, strftime(?2, datetime(date, 'unixepoch')) as date from posts "
		"WHERE link=?1 ;",
		-1, &stmt, NULL) != SQLITE_OK) {
		syslog(LOG_ERR, "%s", sqlite3_errmsg(sqlite));
		return (ret);
	}

	sqlite3_bind_text(stmt, 1, postname, -1, SQLITE_STATIC);
	sqlite3_bind_text(stmt, 2, ucl_get_string(conf, "dateformat"),
	    -1, SQLITE_STATIC);

	ret = add_posts(out, stmt, sqlite);
	sqlite3_finalize(stmt);
//	get_comments(hdf, postname, sqlite);

	return (ret);
}

void
build_index(struct kreq *r, ucl_object_t *conf, ucl_object_t *out,
    struct criteria *criteria, sqlite3 *sqlite)
................................................................................
	if (r->fieldmap[KEY_PAGE] != NULL)
		for (int i = 0; i < r->fieldmap[KEY_PAGE]->valsz; i++)
			page += page * 10 + (char)r->fieldmap[KEY_PAGE]->val[i] - '0';

	switch (criteria->type) {
	case CRITERIA_TAGNAME:
		baseurl =
	    "SELECT link as filename, title, source, html, strftime(?3, datetime(date, 'unixepoch')) as date, date as timestamp from posts, tags_posts, tags "
		"WHERE posts.id=post_id and tag_id=tags.id and tag=?4 "
		"ORDER BY timestamp DESC LIMIT ?1 OFFSET ?2 ";
		counturl =
	    "SELECT count(*) from posts, tags_posts, tags "
		"WHERE posts.id=post_id and tag_id=tags.id and tag=?1;";
		break;
	case CRITERIA_TIME_T:
		baseurl = "SELECT link as filename, title, source, html, strftime(?3, datetime(date, 'unixepoch')) as date, date as timestamp from posts "
			"WHERE date BETWEEN ?4 and ?5 "
			"ORDER BY timestamp DESC LIMIT ?1 OFFSET ?2;";
		counturl = "SELECT count(*) from posts WHERE date BETWEEN ?1 and ?2 ";
		break;
	default:
		baseurl = "SELECT link as filename, title, source, html, strftime(?3, datetime(date, 'unixepoch')) as date, date as timestamp, (select count(rowid) from comments where comments.post_id=posts.id) as nbcomments from posts ORDER BY timestamp DESC LIMIT ?1 OFFSET ?2;";
		counturl = "SELECT count(*) from posts;";
................................................................................
		break;
	}

	sqlite3_step(stmtcnt);
	total_posts = sqlite3_column_int64(stmtcnt, 0);
	sqlite3_finalize(stmtcnt);

	add_posts(out, stmt, sqlite);
	sqlite3_finalize(stmt);

	if (!criteria->feed)
		set_tags(out, sqlite);

	nb_pages = total_posts / max_post;
	if (total_posts % max_post > 0)
................................................................................
				ucl_object_insert_key(out, ucl_object_frombool(true), "viewsource", 0, false);
			snprintf(cspath, MAXPATHLEN, "%s/default.cs", tplpath);
			khttp_head(&r, kresps[KRESP_STATUS],
			    "%s", khttps[KHTTP_200]);
			break;
	}


	khttp_body(&r);
	bzero(&tpl, sizeof(tpl));
	tpl.udata = &r;
	tpl.data = out;
	tpl.addchar = &cblog_putc;
	tpl.addstr = &cblog_puts;
	ucltpl_process_file(&tpl, cspath);

	sqlite3_close(sqlite);
	khttp_free(&r);
	exit(0);
}







|



|
|








>
>
>
>
>
>
>





>
>
>
>







>
>







 







>
|





|

|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




>
>







 







|










|

<







 







|







|







 







|







 







>












58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

213
214
215
216
217
218
219
...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
...
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
	const char *timefmt;
	const char *tagname;
	int64_t start;
	int64_t end;
};

bool
add_posts(ucl_object_t *conf, ucl_object_t *out, sqlite3_stmt *stmt, sqlite3 *sqlite, bool comments)
{
	char *filename = NULL;
	int icol;
	sqlite3_stmt *stmt2, *stmt3;
	ucl_object_t *posts, *post, *array, *o;
	bool ret = false;

	if (sqlite3_prepare_v2(sqlite,
	    "SELECT tag FROM tags, tags_posts, posts WHERE link=?1 AND posts.id=post_id and tag_id=tags.id;",
	    -1, &stmt2, NULL) != SQLITE_OK) {
		syslog(LOG_ERR, "%s", sqlite3_errmsg(sqlite));
		return (ret);
	}

	if (comments && sqlite3_prepare_v2(sqlite,
	    "SELECT author as commentauthor, url as commenturl, strftime(?1, date(comments.date, 'unixepoch')) as commentdate, comments.date as timestamp, comment as content FROM comments, posts where post_id=posts.id and link=?2 ORDER by timestamp ASC;",
	    -1, &stmt3, NULL) != SQLITE_OK) {
		syslog(LOG_ERR, "%s", sqlite3_errmsg(sqlite));
		return (ret);
	}

	posts = ucl_object_typed_new(UCL_ARRAY);
	while (sqlite3_step(stmt) == SQLITE_ROW) {
		ret = true;
		sqlite3_reset(stmt2);
		if (comments) {
			sqlite3_reset(stmt3);
			sqlite3_bind_text(stmt3, 1, ucl_get_string(conf, "dateformat"), -1, SQLITE_STATIC);
		}
		post = ucl_object_typed_new(UCL_OBJECT);
		for (icol = 0; icol < sqlite3_column_count(stmt); icol++) {
			switch (sqlite3_column_type(stmt, icol)) {
			case SQLITE_TEXT:
				if (strcmp(sqlite3_column_name(stmt, icol), "filename") == 0) {
					filename = (char*)sqlite3_column_text(stmt, icol);
					sqlite3_bind_text(stmt2, 1, filename, -1, SQLITE_STATIC);
					if (comments)
						sqlite3_bind_text(stmt3, 2, filename, -1, SQLITE_STATIC);
				}
				ucl_object_insert_key(post,
				    ucl_object_fromstring_common((const char *)sqlite3_column_text(stmt, icol), 0, UCL_STRING_TRIM),
				    sqlite3_column_name(stmt, icol),
				    0, true);
				break;
			case SQLITE_INTEGER:
................................................................................
				    sqlite3_column_name(stmt, icol),
				    0, true);
				break;
			default:
				break;
			}
		}

		array = ucl_object_typed_new(UCL_ARRAY);
		while (sqlite3_step(stmt2) == SQLITE_ROW) {
			o = ucl_object_typed_new(UCL_OBJECT);
			ucl_object_insert_key(o,
			    ucl_object_fromstring((const char *)sqlite3_column_text(stmt2, 0)),
			    "name", 0, false);
			ucl_array_append(array, o);
		}
		ucl_object_insert_key(post, array, "tags", 0, false);
		ucl_array_append(posts, post);

		if (!comments)
			continue;

		array = ucl_object_typed_new(UCL_ARRAY);
		while (sqlite3_step(stmt3) == SQLITE_ROW) {
			o = ucl_object_typed_new(UCL_OBJECT);
			for (icol = 0; icol < sqlite3_column_count(stmt3); icol++) {
				switch (sqlite3_column_type(stmt3, icol)) {
				case SQLITE_TEXT:
					ucl_object_insert_key(o,
					    ucl_object_fromstring_common((const char *)sqlite3_column_text(stmt3, icol), 0, UCL_STRING_TRIM),
					    sqlite3_column_name(stmt3, icol), 0, true);
					break;
				default:
					break;
				}
			}
			ucl_array_append(array, o);
		}
		ucl_object_insert_key(post, array, "comments", 0, false);
	}
	ucl_object_insert_key(out, posts, "Posts", 0, false);

	sqlite3_finalize(stmt2);
	if (comments)
		sqlite3_finalize(stmt3);

	return (ret);
}

void
set_tags(ucl_object_t *out, sqlite3 *sqlite)
{
................................................................................
bool
build_post(ucl_object_t *conf, ucl_object_t *out, const char *postname, sqlite3 *sqlite)
{
	sqlite3_stmt *stmt;
	bool ret = false;

	if (sqlite3_prepare_v2(sqlite,
		"SELECT link as filename, title, source, html, strftime(?2, datetime(date, 'unixepoch')) as date, (select count(rowid) from comments where comments.post_id=posts.id) as nbcomments from posts "
		"WHERE link=?1 ;",
		-1, &stmt, NULL) != SQLITE_OK) {
		syslog(LOG_ERR, "%s", sqlite3_errmsg(sqlite));
		return (ret);
	}

	sqlite3_bind_text(stmt, 1, postname, -1, SQLITE_STATIC);
	sqlite3_bind_text(stmt, 2, ucl_get_string(conf, "dateformat"),
	    -1, SQLITE_STATIC);

	ret = add_posts(conf, out, stmt, sqlite, true);
	sqlite3_finalize(stmt);


	return (ret);
}

void
build_index(struct kreq *r, ucl_object_t *conf, ucl_object_t *out,
    struct criteria *criteria, sqlite3 *sqlite)
................................................................................
	if (r->fieldmap[KEY_PAGE] != NULL)
		for (int i = 0; i < r->fieldmap[KEY_PAGE]->valsz; i++)
			page += page * 10 + (char)r->fieldmap[KEY_PAGE]->val[i] - '0';

	switch (criteria->type) {
	case CRITERIA_TAGNAME:
		baseurl =
	    "SELECT link as filename, title, source, html, strftime(?3, datetime(date, 'unixepoch')) as date, date as timestamp, (select count(rowid) from comments where comments.post_id=posts.id) as nbcomments from posts, tags_posts, tags "
		"WHERE posts.id=post_id and tag_id=tags.id and tag=?4 "
		"ORDER BY timestamp DESC LIMIT ?1 OFFSET ?2 ";
		counturl =
	    "SELECT count(*) from posts, tags_posts, tags "
		"WHERE posts.id=post_id and tag_id=tags.id and tag=?1;";
		break;
	case CRITERIA_TIME_T:
		baseurl = "SELECT link as filename, title, source, html, strftime(?3, datetime(date, 'unixepoch')) as date, date as timestamp, (select count(rowid) from comments where comments.post_id=posts.id) as nbcomments from posts "
			"WHERE date BETWEEN ?4 and ?5 "
			"ORDER BY timestamp DESC LIMIT ?1 OFFSET ?2;";
		counturl = "SELECT count(*) from posts WHERE date BETWEEN ?1 and ?2 ";
		break;
	default:
		baseurl = "SELECT link as filename, title, source, html, strftime(?3, datetime(date, 'unixepoch')) as date, date as timestamp, (select count(rowid) from comments where comments.post_id=posts.id) as nbcomments from posts ORDER BY timestamp DESC LIMIT ?1 OFFSET ?2;";
		counturl = "SELECT count(*) from posts;";
................................................................................
		break;
	}

	sqlite3_step(stmtcnt);
	total_posts = sqlite3_column_int64(stmtcnt, 0);
	sqlite3_finalize(stmtcnt);

	add_posts(conf, out, stmt, sqlite, false);
	sqlite3_finalize(stmt);

	if (!criteria->feed)
		set_tags(out, sqlite);

	nb_pages = total_posts / max_post;
	if (total_posts % max_post > 0)
................................................................................
				ucl_object_insert_key(out, ucl_object_frombool(true), "viewsource", 0, false);
			snprintf(cspath, MAXPATHLEN, "%s/default.cs", tplpath);
			khttp_head(&r, kresps[KRESP_STATUS],
			    "%s", khttps[KHTTP_200]);
			break;
	}

	warnx("%s", ucl_object_emit(out, UCL_EMIT_JSON));
	khttp_body(&r);
	bzero(&tpl, sizeof(tpl));
	tpl.udata = &r;
	tpl.data = out;
	tpl.addchar = &cblog_putc;
	tpl.addstr = &cblog_puts;
	ucltpl_process_file(&tpl, cspath);

	sqlite3_close(sqlite);
	khttp_free(&r);
	exit(0);
}