2021-11-21 14:41:40 +01:00
|
|
|
#include "wp_application.h"
|
|
|
|
|
|
|
|
#include "core/http/request.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "core/file_cache.h"
|
|
|
|
|
|
|
|
#include "core/http/handler_instance.h"
|
|
|
|
|
|
|
|
#include "core/database/database_manager.h"
|
2021-11-21 16:15:29 +01:00
|
|
|
#include "core/database/query_builder.h"
|
|
|
|
#include "core/database/query_result.h"
|
2021-11-21 14:41:40 +01:00
|
|
|
|
|
|
|
#include "core/html/html_builder.h"
|
|
|
|
#include "core/http/http_session.h"
|
|
|
|
#include "core/http/session_manager.h"
|
|
|
|
|
2021-11-21 16:15:29 +01:00
|
|
|
#include "core/utils.h"
|
|
|
|
|
2021-11-21 15:18:42 +01:00
|
|
|
void WPApplication::index_fun(Object *instance, Request *request) {
|
|
|
|
WPApplication *app = Object::cast_to<WPApplication>(instance);
|
|
|
|
|
|
|
|
app->index(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::blog_fun(Object *instance, Request *request) {
|
|
|
|
WPApplication *app = Object::cast_to<WPApplication>(instance);
|
|
|
|
|
|
|
|
app->blog(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::index(Request *request) {
|
2021-11-21 16:28:13 +01:00
|
|
|
request->head += header;
|
|
|
|
|
2021-11-21 15:18:42 +01:00
|
|
|
HTMLBuilder b;
|
|
|
|
|
|
|
|
b.div("content");
|
|
|
|
|
|
|
|
b.div("content_head")->f()->w("Saved blogs:")->cdiv();
|
2021-11-21 14:41:40 +01:00
|
|
|
|
2021-11-21 15:18:42 +01:00
|
|
|
for (int i = 0; i < _blog_data.size(); ++i) {
|
|
|
|
BlogData &bd = _blog_data[i];
|
|
|
|
|
|
|
|
b.div("content_row")->f()->fa("/blog/" + bd.name + "/", bd.name, "blog_link")->cdiv();
|
|
|
|
}
|
|
|
|
|
|
|
|
b.cdiv();
|
|
|
|
|
|
|
|
request->body += b.result;
|
2021-11-21 16:28:13 +01:00
|
|
|
request->body += footer;
|
2021-11-21 14:41:40 +01:00
|
|
|
request->compile_and_send_body();
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:18:42 +01:00
|
|
|
void WPApplication::blog(Request *request) {
|
2021-11-21 16:28:13 +01:00
|
|
|
request->head += header;
|
|
|
|
|
2021-11-21 16:15:29 +01:00
|
|
|
String blog = request->get_current_path_segment();
|
|
|
|
|
|
|
|
Database *db = nullptr;
|
|
|
|
|
|
|
|
for (int i = 0; i < _blog_data.size(); ++i) {
|
|
|
|
BlogData &bd = _blog_data[i];
|
|
|
|
|
|
|
|
if (bd.name == blog) {
|
|
|
|
db = bd.db;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!db) {
|
|
|
|
request->send_error(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->push_path();
|
|
|
|
|
|
|
|
HTMLBuilder b;
|
|
|
|
|
|
|
|
int page = 1;
|
|
|
|
|
|
|
|
String action_segment = request->get_current_path_segment();
|
|
|
|
|
|
|
|
if (action_segment == "") {
|
|
|
|
// nothign to do
|
|
|
|
} else if (action_segment == "page") {
|
|
|
|
request->push_path();
|
|
|
|
|
|
|
|
page = request->get_current_path_segment().to_int();
|
|
|
|
} else if (action_segment == "post") {
|
2021-11-21 16:24:01 +01:00
|
|
|
request->push_path();
|
2021-11-21 16:28:13 +01:00
|
|
|
|
2021-11-21 16:24:01 +01:00
|
|
|
int post_id = request->get_current_path_segment().to_int();
|
|
|
|
|
|
|
|
PostData *p = get_post(db, post_id);
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
request->send_error(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
b.div("content");
|
|
|
|
b.div("blog_content")->f()->w(p->data)->cdiv();
|
|
|
|
b.cdiv();
|
|
|
|
|
|
|
|
request->body += b.result;
|
2021-11-21 16:28:13 +01:00
|
|
|
request->body += footer;
|
2021-11-21 16:15:29 +01:00
|
|
|
request->compile_and_send_body();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
request->send_error(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page <= 0) {
|
|
|
|
page = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
--page;
|
|
|
|
|
|
|
|
Vector<PostData *> posts = get_posts(db, page);
|
|
|
|
int post_count = (get_post_count(db) / 5.0) + 0.9;
|
|
|
|
|
|
|
|
b.div("content");
|
|
|
|
|
|
|
|
b.w(Utils::get_pagination("/blog/" + blog + "/page/", post_count, page));
|
|
|
|
|
|
|
|
if (posts.size() != 0) {
|
|
|
|
for (int i = 0; i < posts.size(); ++i) {
|
|
|
|
PostData *p = posts[i];
|
|
|
|
|
|
|
|
b.div("blog_content_row");
|
2021-11-21 16:24:01 +01:00
|
|
|
b.div("blog_entry_link")->f()->fa("/blog/" + blog + "/post/" + String::num(p->id), "Open")->cdiv();
|
|
|
|
b.div("blog_content")->f()->w(p->data)->cdiv();
|
2021-11-21 16:15:29 +01:00
|
|
|
b.cdiv();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.fdiv("No saved blog posts.", "blog_content_row");
|
|
|
|
}
|
|
|
|
|
|
|
|
b.w(Utils::get_pagination("/blog/" + blog + "/page/", post_count, page));
|
|
|
|
|
|
|
|
b.cdiv();
|
|
|
|
|
|
|
|
request->body += b.result;
|
2021-11-21 16:28:13 +01:00
|
|
|
request->body += footer;
|
2021-11-21 14:41:40 +01:00
|
|
|
request->compile_and_send_body();
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:15:29 +01:00
|
|
|
Vector<WPApplication::PostData *> WPApplication::get_posts(Database *db, const int page, const int num_per_page) {
|
|
|
|
Ref<QueryBuilder> qb = db->get_query_builder();
|
|
|
|
|
|
|
|
qb->select("id,url,extracted_data")->from("data")->order_by_desc("id")->corder_by()->limit(num_per_page)->offset(page * num_per_page)->end_command();
|
|
|
|
Ref<QueryResult> res = qb->run();
|
|
|
|
|
|
|
|
Vector<PostData *> r;
|
|
|
|
|
|
|
|
while (res->next_row()) {
|
|
|
|
PostData *p = new PostData();
|
|
|
|
|
|
|
|
p->id = res->get_cell_int(0);
|
|
|
|
p->url = res->get_cell(1);
|
|
|
|
p->data = res->get_cell(2);
|
|
|
|
|
|
|
|
r.push_back(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WPApplication::get_post_count(Database *db) {
|
|
|
|
Ref<QueryBuilder> qb = db->get_query_builder();
|
|
|
|
|
|
|
|
qb->select("COUNT(id)")->from("data")->end_command();
|
|
|
|
Ref<QueryResult> res = qb->run();
|
|
|
|
|
|
|
|
if (!res->next_row()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res->get_cell_int(0);
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:24:01 +01:00
|
|
|
WPApplication::PostData *WPApplication::get_post(Database *db, const int id) {
|
|
|
|
Ref<QueryBuilder> qb = db->get_query_builder();
|
|
|
|
|
|
|
|
qb->select("id,url,extracted_data")->from("data")->where()->wp("id", id)->end_command();
|
|
|
|
Ref<QueryResult> res = qb->run();
|
|
|
|
|
|
|
|
if (!res->next_row()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PostData *p = new PostData();
|
|
|
|
|
|
|
|
p->id = res->get_cell_int(0);
|
|
|
|
p->url = res->get_cell(1);
|
|
|
|
p->data = res->get_cell(2);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:41:40 +01:00
|
|
|
void WPApplication::routing_middleware(Object *instance, Request *request) {
|
2021-11-21 14:55:34 +01:00
|
|
|
String path = request->get_path_full();
|
2021-11-21 14:41:40 +01:00
|
|
|
|
|
|
|
WPApplication *app = Object::cast_to<WPApplication>(instance);
|
|
|
|
|
|
|
|
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
|
|
|
app->send_file(path, request);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:55:34 +01:00
|
|
|
bool handled = false;
|
|
|
|
|
2021-11-21 14:41:40 +01:00
|
|
|
if (request->get_path_segment_count() == 0) {
|
2021-11-21 14:55:34 +01:00
|
|
|
handled = true;
|
|
|
|
|
2021-11-21 14:50:03 +01:00
|
|
|
request->handler_instance = app->index_func;
|
2021-11-21 14:41:40 +01:00
|
|
|
} else {
|
2021-11-21 14:55:34 +01:00
|
|
|
const String main_route = request->get_current_path_segment();
|
2021-11-21 14:41:40 +01:00
|
|
|
|
|
|
|
request->push_path();
|
|
|
|
|
|
|
|
if (main_route == "blog") {
|
2021-11-21 14:55:34 +01:00
|
|
|
handled = true;
|
2021-11-21 14:50:03 +01:00
|
|
|
request->handler_instance = app->blog_func;
|
2021-11-21 14:41:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:55:34 +01:00
|
|
|
if (!handled) {
|
2021-11-21 14:41:40 +01:00
|
|
|
app->send_error(404, request);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->next_stage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::setup_routes() {
|
|
|
|
DWebApplication::setup_routes();
|
|
|
|
|
2021-11-21 15:18:42 +01:00
|
|
|
index_func = HandlerInstance(index_fun, this);
|
|
|
|
blog_func = HandlerInstance(blog_fun, this);
|
2021-11-21 14:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::setup_middleware() {
|
|
|
|
middlewares.push_back(HandlerInstance(routing_middleware, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WPApplication::migrate() {
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:18:42 +01:00
|
|
|
void WPApplication::add_blog(const String &name, Database *db) {
|
|
|
|
BlogData bd;
|
|
|
|
|
|
|
|
bd.name = name;
|
|
|
|
bd.db = db;
|
|
|
|
|
|
|
|
_blog_data.push_back(bd);
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:41:40 +01:00
|
|
|
void WPApplication::compile_menu() {
|
|
|
|
HTMLBuilder bh;
|
|
|
|
|
|
|
|
bh.meta()->charset_utf_8();
|
|
|
|
bh.title();
|
2021-11-21 14:50:03 +01:00
|
|
|
bh.w("WPSaver");
|
2021-11-21 14:41:40 +01:00
|
|
|
bh.ctitle();
|
|
|
|
|
2021-11-21 16:28:13 +01:00
|
|
|
bh.link()->rel_stylesheet()->href("/site.css");
|
2021-11-21 14:41:40 +01:00
|
|
|
bh.write_tag();
|
|
|
|
|
2021-11-21 14:50:03 +01:00
|
|
|
header = bh.result;
|
2021-11-21 14:41:40 +01:00
|
|
|
|
|
|
|
HTMLBuilder bf;
|
|
|
|
|
|
|
|
bf.cdiv();
|
|
|
|
bf.footer();
|
|
|
|
bf.cfooter();
|
|
|
|
|
|
|
|
footer = bf.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
WPApplication::WPApplication() :
|
|
|
|
DWebApplication() {
|
|
|
|
|
|
|
|
compile_menu();
|
|
|
|
}
|
|
|
|
|
|
|
|
WPApplication::~WPApplication() {
|
|
|
|
}
|